X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/75c342df9bdad4455cd555ebb449cb82d3dc6fae..e823ad27e90d5cba05fb2ea8ac2a0d258be2f707:/project/static/js/app.js?ds=sidebyside diff --git a/project/static/js/app.js b/project/static/js/app.js index 2530ee1a..6e6f0f80 100644 --- a/project/static/js/app.js +++ b/project/static/js/app.js @@ -111,18 +111,18 @@ var Model = Class.extend({ signal: function(event, data) { console.log('signal', this, event, data); if (this.observers[event]) { - for (var i=0; i < this.observers[event].length; i++) { - this.observers[event][i].handle(event, data); - }; + for (key in this.observers[event]) { + this.observers[event][key](event, data); + } }; return this; }, - addObserver: function(observer, event) { + addObserver: function(observer, event, callback) { if (!this.observers[event]) { - this.observers[event] = []; + this.observers[event] = {}; } - this.observers[event].push(observer); + this.observers[event][observer.id] = callback; return this; }, @@ -132,63 +132,146 @@ var Model = Class.extend({ this.removeObserver(observer, e); } } else { - for (var i=0; i < this.observers[event].length; i++) { - if (this.observers[event][i] === observer) { - this.observers[event].splice(i, 1); - } - } + delete this.observers[event][observer.id]; } return this; } +}); + + +var XMLModel = Model.extend({ + parent: null, + data: '', + serverURL: null, + needsReload: false, + + init: function(parent, serverURL) { + this.parent = parent; + this.serverURL = serverURL; + }, + + getData: function() { + if (!this.data) { + this.reload(); + } + return this.data; + }, + + setData: function(data) { + this.data = data; + this.dataChanged(); + }, + + reload: function() { + $.ajax({ + url: this.serverURL, + dataType: 'text', + success: this.reloadSucceeded.bind(this) + }); + }, + + reloadSucceeded: function(data) { + this.data = data; + this.signal('reloaded'); + }, + + dataChanged: function() { + this.parent.modelChanged('xml'); + this.signal('dataChanged'); + }, + + needsReload: function() { + this.needsReload = true; + this.signal('needsReload'); + } +}) + + + +var HTMLModel = Model.extend({ + parent: null, + data: '', + serverURL: null, + needsReload: false, + + init: function(parent, serverURL) { + this.parent = parent; + this.serverURL = serverURL; + }, + + getData: function() { + if (!this.data) { + this.reload(); + } + return this.data; + }, + + setData: function(data) { + console.log('setData'); + if (this.data != data) { + this.data = data; + this.dataChanged(); + } + }, + + reload: function() { + $.ajax({ + url: this.serverURL, + dataType: 'text', + success: this.reloadSucceeded.bind(this) + }); + }, + + reloadSucceeded: function(data) { + this.data = data; + this.signal('reloaded'); + }, + + dataChanged: function() { + this.parent.modelChanged('html'); + }, + + needsReload: function() { + this.needsReload = true; + this.signal('needsReload'); + } }) + var DocumentModel = Model.extend({ data: null, // name, text_url, latest_rev, latest_shared_rev, parts_url, dc_url, size - xml: '', - html: '', + xml: null, + html: null, + contentModels: {}, - init: function() {}, + init: function() { + this.getData(); + }, - getData: function(callback) { - console.log('get:', documentsUrl + fileId); + getData: function() { + console.log('DocumentModel#getData'); $.ajax({ cache: false, url: documentsUrl + fileId, dataType: 'json', - success: this.successfulGetData.bind(this, callback) + success: this.successfulGetData.bind(this) }); }, - successfulGetData: function(callback, data) { + successfulGetData: function(data) { + console.log('DocumentModel#successfulGetData:', data); this.data = data; - this.signal('changed'); - if (callback) { - (callback.bind(this))(data); - } - }, - - getXML: function(callback) { - this.signal('xml-freeze'); - if (!this.data) { - this.getData(this.getXML); - } else { - console.log('getXML:', this.data.text_url); - $.ajax({ - cache: false, - url: this.data.text_url, - dataType: 'text', - success: this.successfulGetXML.bind(this, callback) - }); + this.contentModels = { + 'xml': new XMLModel(this, data.text_url) }; }, - successfulGetXML: function(callback, data) { - if (data != this.xml) { - this.xml = data; - this.signal('changed'); - this.signal('xml-changed'); + modelChanged: function(contentModelName) { + for (modelName in this.contentModels) { + if (!(modelName == contentModelName)) { + this.contentModels[modelName].needsReload(); + } } - this.signal('xml-unfreeze'); } });