Merge branch 'master' of stigma:platforma
[redakcja.git] / project / static / js / models.js
index 9542eac..62d1c78 100644 (file)
@@ -104,6 +104,7 @@ Editor.XMLModel = Editor.Model.extend({
     this.set('state', 'dirty');
   },
   
+  // For debbuging
   set: function(property, value) {
     if (property == 'state') {
       console.log(this.description(), ':', property, '=', value);
@@ -162,7 +163,50 @@ Editor.HTMLModel = Editor.Model.extend({
     this.set('data', data);
     this.set('state', 'synced');
   },
-  
+
+  // For debbuging
+  set: function(property, value) {
+    if (property == 'state') {
+      console.log(this.description(), ':', property, '=', value);
+    }
+    return this._super(property, value);
+  }
+});
+
+
+Editor.ImageGalleryModel = Editor.Model.extend({
+  _className: 'Editor.ImageGalleryModel',
+  serverURL: null,  
+  state: 'empty',
+
+  init: function(serverURL) {
+    this._super();
+    this.set('state', 'empty');
+    this.serverURL = serverURL;
+    // olewać data    
+    this.pages = []
+  },
+
+  load: function() {
+    if (this.get('state') == 'empty') {
+      this.set('state', 'loading');
+      $.ajax({
+        url: this.serverURL,
+        dataType: 'json',
+        success: this.loadingSucceeded.bind(this)
+      });
+    }
+  },  
+
+  loadingSucceeded: function(data) {
+    if (this.get('state') != 'loading') {
+      alert('erroneous state:', this.get('state'));
+    }
+
+    this.set('pages', data.pages)      
+    this.set('state', 'synced');
+  },
+
   set: function(property, value) {
     if (property == 'state') {
       console.log(this.description(), ':', property, '=', value);
@@ -201,7 +245,8 @@ Editor.DocumentModel = Editor.Model.extend({
     this.set('state', 'synced');
     this.contentModels = {
       'xml': new Editor.XMLModel(data.text_url),
-      'html': new Editor.HTMLModel(data.html_url)
+      'html': new Editor.HTMLModel(data.html_url),
+      'gallery': new Editor.ImageGalleryModel(data.gallery_url)
     };
     for (var key in this.contentModels) {
       this.contentModels[key].addObserver(this, 'state', this.contentModelStateChanged.bind(this));
@@ -210,22 +255,47 @@ Editor.DocumentModel = Editor.Model.extend({
   
   contentModelStateChanged: function(property, value, contentModel) {
     if (value == 'dirty') {
+      this.set('state', 'dirty');
       for (var key in this.contentModels) {
         if (this.contentModels[key].guid() != contentModel.guid()) {
-          // console.log(this.contentModels[key].description(), 'frozen');
           this.contentModels[key].set('state', 'unsynced');
         }
       }
+    } else if (value == 'updated') {
+      this.set('state', 'synced');
+      for (key in this.contentModels) {
+        if (this.contentModels[key].guid() == contentModel.guid()) {
+          this.contentModels[key].set('state', 'synced');
+        } else if (this.contentModels[key].get('state') == 'unsynced') {
+          this.contentModels[key].set('state', 'empty');
+        }
+      }
     }
   },
   
-  quickSave: function(message) {
+  saveDirtyContentModel: function(message) {
     for (var key in this.contentModels) {
       if (this.contentModels[key].get('state') == 'dirty') {
         this.contentModels[key].update(message);
         break;
       }
     }
+  },
+  
+  update: function() {
+    
+  },
+  
+  merge: function() {
+    
+  },
+  
+  // For debbuging
+  set: function(property, value) {
+    if (property == 'state') {
+      console.log(this.description(), ':', property, '=', value);
+    }
+    return this._super(property, value);
   }
 });