X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/ad481d9335cb91f56adfffda3999284f37d37942..320fd24b68185b73f1180c425271e037331baa46:/project/static/js/models.js?ds=sidebyside diff --git a/project/static/js/models.js b/project/static/js/models.js index 4b29e67a..8ea488e1 100644 --- a/project/static/js/models.js +++ b/project/static/js/models.js @@ -37,7 +37,7 @@ Editor.ToolbarButtonsModel = Editor.Model.extend({ // // empty -> loading -> synced -> unsynced -> loading // \ -// -> dirty -> updating -> synced +// -> dirty -> updating -> updated -> synced // Editor.XMLModel = Editor.Model.extend({ _className: 'Editor.XMLModel', @@ -61,9 +61,50 @@ Editor.XMLModel = Editor.Model.extend({ dataType: 'text', success: this.loadingSucceeded.bind(this) }); + return true; } + return false; }, + update: function(message) { + if (this.get('state') == 'dirty') { + this.set('state', 'updating'); + + var payload = { + contents: this.get('data') + }; + if (message) { + payload.message = message; + } + + $.ajax({ + url: this.serverURL, + type: 'put', + dataType: 'json', + data: payload, + success: this.updatingSucceeded.bind(this), + error: this.updatingFailed.bind(this) + }); + return true; + } + return false; + }, + + updatingSucceeded: function() { + if (this.get('state') != 'updating') { + alert('erroneous state:', this.get('state')); + } + this.set('state', 'updated'); + }, + + updatingFailed: function() { + if (this.get('state') != 'updating') { + alert('erroneous state:', this.get('state')); + } + this.set('state', 'dirty'); + }, + + // For debbuging set: function(property, value) { if (property == 'state') { console.log(this.description(), ':', property, '=', value); @@ -122,7 +163,8 @@ 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); @@ -170,13 +212,39 @@ 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) { + for (var key in this.contentModels) { + if (this.contentModels[key].get('state') == 'dirty') { + this.contentModels[key].update(message); + break; + } + } + }, + + // For debbuging + set: function(property, value) { + if (property == 'state') { + console.log(this.description(), ':', property, '=', value); } + return this._super(property, value); } }); @@ -186,6 +254,7 @@ var leftPanelView, rightPanelContainer, doc; $(function() { doc = new Editor.DocumentModel(); var editor = new EditorView('#body-wrap', doc); + editor.freeze(); var splitView = new SplitView('#splitview', doc); leftPanelView = new PanelContainerView('#left-panel-container', doc); rightPanelContainer = new PanelContainerView('#right-panel-container', doc);