X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/4ef9f94e2465f7af40708a1cf3ee348bb7924473..ad481d9335cb91f56adfffda3999284f37d37942:/project/static/js/models.js diff --git a/project/static/js/models.js b/project/static/js/models.js index 5c5e6fb1..4b29e67a 100644 --- a/project/static/js/models.js +++ b/project/static/js/models.js @@ -1,4 +1,4 @@ -/*globals Editor fileId SplitView PanelContainerView*/ +/*globals Editor fileId SplitView PanelContainerView EditorView*/ var documentsUrl = '/api/documents/'; @@ -8,36 +8,86 @@ Editor.Model = Editor.Object.extend({ }); +Editor.ToolbarButtonsModel = Editor.Model.extend({ + _className: 'Editor.ToolbarButtonsModel', + serverURL: '/api/toolbar/buttons', + buttons: {}, + + init: function() { + this._super(); + }, + + load: function() { + if (!this.get('buttons').length) { + $.ajax({ + url: this.serverURL, + dataType: 'json', + success: this.loadSucceeded.bind(this) + }); + } + }, + + loadSucceeded: function(data) { + this.set('buttons', data); + } +}); + + +// Stany modelu: +// +// empty -> loading -> synced -> unsynced -> loading +// \ +// -> dirty -> updating -> synced +// Editor.XMLModel = Editor.Model.extend({ _className: 'Editor.XMLModel', serverURL: null, data: '', + state: 'empty', init: function(serverURL) { this._super(); + this.set('state', 'empty'); this.serverURL = serverURL; - }, - - getData: function() { - if (!this.data) { - this.reload(); - } - return this.data; + this.toolbarButtonsModel = new Editor.ToolbarButtonsModel(); + this.addObserver(this, 'data', this.dataChanged.bind(this)); }, load: function() { - if (!this.get('synced')) { + if (this.get('state') == 'empty') { + this.set('state', 'loading'); $.ajax({ url: this.serverURL, dataType: 'text', - success: this.reloadSucceeded.bind(this) + success: this.loadingSucceeded.bind(this) }); } }, - reloadSucceeded: function(data) { + set: function(property, value) { + if (property == 'state') { + console.log(this.description(), ':', property, '=', value); + } + return this._super(property, value); + }, + + dataChanged: function(property, value) { + if (this.get('state') == 'synced') { + this.set('state', 'dirty'); + } + }, + + loadingSucceeded: function(data) { + if (this.get('state') != 'loading') { + alert('erroneous state:', this.get('state')); + } this.set('data', data); - this.set('synced', true); + this.set('state', 'synced'); + }, + + dispose: function() { + this.removeObserver(this); + this._super(); } }); @@ -46,25 +96,38 @@ Editor.HTMLModel = Editor.Model.extend({ _className: 'Editor.HTMLModel', serverURL: null, data: '', + state: 'empty', init: function(serverURL) { this._super(); + this.set('state', 'empty'); this.serverURL = serverURL; }, load: function() { - if (!this.get('synced')) { + if (this.get('state') == 'empty') { + this.set('state', 'loading'); $.ajax({ url: this.serverURL, dataType: 'text', - success: this.reloadSucceeded.bind(this) + success: this.loadingSucceeded.bind(this) }); } }, - reloadSucceeded: function(data) { + loadingSucceeded: function(data) { + if (this.get('state') != 'loading') { + alert('erroneous state:', this.get('state')); + } this.set('data', data); - this.set('synced', true); + this.set('state', 'synced'); + }, + + set: function(property, value) { + if (property == 'state') { + console.log(this.description(), ':', property, '=', value); + } + return this._super(property, value); } }); @@ -73,40 +136,45 @@ Editor.DocumentModel = Editor.Model.extend({ _className: 'Editor.DocumentModel', data: null, // name, text_url, latest_rev, latest_shared_rev, parts_url, dc_url, size contentModels: {}, + state: 'empty', init: function() { this._super(); + this.set('state', 'empty'); this.load(); }, load: function() { - console.log('DocumentModel#load'); - $.ajax({ - cache: false, - url: documentsUrl + fileId, - dataType: 'json', - success: this.successfulLoad.bind(this) - }); + if (this.get('state') == 'empty') { + this.set('state', 'loading'); + $.ajax({ + cache: false, + url: documentsUrl + fileId, + dataType: 'json', + success: this.successfulLoad.bind(this) + }); + } }, successfulLoad: function(data) { - console.log('DocumentModel#successfulLoad:', data); this.set('data', data); + this.set('state', 'synced'); this.contentModels = { 'xml': new Editor.XMLModel(data.text_url), 'html': new Editor.HTMLModel(data.html_url) }; for (var key in this.contentModels) { - this.contentModels[key].addObserver(this, 'data', this.contentModelDataChanged.bind(this)); + this.contentModels[key].addObserver(this, 'state', this.contentModelStateChanged.bind(this)); } }, - contentModelDataChanged: function(property, value, contentModel) { - console.log('data of', contentModel.description(), 'changed!'); - for (var key in this.contentModels) { - if (this.contentModels[key].guid() != contentModel.guid()) { - console.log(this.contentModels[key].description(), 'frozen'); - this.contentModels[key].set('synced', false); + contentModelStateChanged: function(property, value, contentModel) { + if (value == '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'); + } } } } @@ -117,6 +185,7 @@ var leftPanelView, rightPanelContainer, doc; $(function() { doc = new Editor.DocumentModel(); + var editor = new EditorView('#body-wrap', doc); var splitView = new SplitView('#splitview', doc); leftPanelView = new PanelContainerView('#left-panel-container', doc); rightPanelContainer = new PanelContainerView('#right-panel-container', doc);