X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/020ad779bc86249cd11cb8aa4970420b16789f55..4c49e768f97e6e158b70abd3c241387fa1148d6d:/platforma/static/js/models.js?ds=inline diff --git a/platforma/static/js/models.js b/platforma/static/js/models.js old mode 100644 new mode 100755 index fe42e895..ba2f3ce3 --- a/platforma/static/js/models.js +++ b/platforma/static/js/models.js @@ -88,10 +88,12 @@ Editor.XMLModel = Editor.Model.extend({ messageCenter.addMessage('success', 'xmlload', 'WczytaÅem XML :-)'); }, - loadingFailed: function() { + loadingFailed: function(response) + { if (this.get('state') != 'loading') { alert('erroneous state:', this.get('state')); } + var message = parseXHRError(response); this.set('error', '
'+message+'
'); @@ -218,15 +220,15 @@ Editor.HTMLModel = Editor.Model.extend({ alert('erroneous state:', this.get('state')); } - var message = parseXHRError(response); + var err = parseXHRError(response); - this.set('error', 'Nie udaÅo siÄ wczytaÄ widoku HTML:
' + message); + this.set('error', 'Nie udaÅo siÄ wczytaÄ widoku HTML:
' + err.error_message); this.set('state', 'error'); }, getXMLPart: function(elem, callback) { - var path = elem.attr('wl2o:path'); + var path = elem.attr('x-pointer'); if(!this.xmlParts[path]) this.loadXMLPart(elem, callback); else @@ -235,7 +237,7 @@ Editor.HTMLModel = Editor.Model.extend({ loadXMLPart: function(elem, callback) { - var path = elem.attr('wl2o:path'); + var path = elem.attr('x-pointer'); var self = this; $.ajax({ @@ -244,10 +246,12 @@ Editor.HTMLModel = Editor.Model.extend({ data: { revision: this.get('revision'), user: this.document.get('user'), - part: path + chunk: path + // format: 'nl' }, success: function(data) { self.xmlParts[path] = data; + console.log(data); callback(path, data); }, // TODO: error handling @@ -258,13 +262,13 @@ Editor.HTMLModel = Editor.Model.extend({ }); }, - putXMLPart: function(elem, data) { + putXMLPart: function(elem, data, callback) { var self = this; - var path = elem.attr('wl2o:path'); + var path = elem.attr('x-pointer'); this.xmlParts[path] = data; - this.set('state', 'unsynced'); + this.set('state', 'dirty'); /* re-render the changed fragment */ $.ajax({ @@ -273,10 +277,11 @@ Editor.HTMLModel = Editor.Model.extend({ dataType: 'text; charset=utf-8', data: { fragment: data, - part: path + chunk: path + // format: 'nl' }, success: function(htmldata) { - elem.replaceWith(htmldata); + callback(elem, htmldata); self.set('state', 'dirty'); } }); @@ -347,7 +352,7 @@ Editor.ImageGalleryModel = Editor.Model.extend({ data: [], state: 'empty', - init: function(serverURL) { + init: function(document, serverURL) { this._super(); this.set('state', 'empty'); this.serverURL = serverURL; @@ -357,16 +362,22 @@ Editor.ImageGalleryModel = Editor.Model.extend({ load: function(force) { if (force || this.get('state') == 'empty') { + console.log("setting state"); this.set('state', 'loading'); + console.log("going ajax"); $.ajax({ url: this.serverURL, dataType: 'json', - success: this.loadingSucceeded.bind(this) + success: this.loadingSucceeded.bind(this), + error: this.loadingFailed.bind(this) }); } }, - loadingSucceeded: function(data) { + loadingSucceeded: function(data) + { + console.log("success"); + if (this.get('state') != 'loading') { alert('erroneous state:', this.get('state')); } @@ -382,6 +393,16 @@ Editor.ImageGalleryModel = Editor.Model.extend({ this.set('state', 'synced'); }, + loadingFailed: function(data) { + console.log("failed"); + + if (this.get('state') != 'loading') { + alert('erroneous state:', this.get('state')); + } + + this.set('state', 'error'); + }, + set: function(property, value) { if (property == 'state') { console.log(this.description(), ':', property, '=', value); @@ -446,8 +467,8 @@ Editor.DocumentModel = Editor.Model.extend({ alert('erroneous state:', this.get('state')); } - var message = parseXHRError(response); - this.set('error', ''+message+"
"); + var err = parseXHRError(response); + this.set('error', ''+err.error_message+"
"); this.set('state', 'error'); }, @@ -501,53 +522,58 @@ Editor.DocumentModel = Editor.Model.extend({ revision: this.get('revision'), user: this.get('user') }, - complete: this.updateCompleted.bind(this), - success: function(data) { - this.set('updateData', data); - console.log("new data:", data) - }.bind(this) + complete: this.updateCompleted.bind(this), }); }, - updateCompleted: function(xhr, textStatus) { - console.log(xhr.status, textStatus); - - if (xhr.status == 200) + updateCompleted: function(xhr, textStatus) + { + console.log(xhr.status, xhr.responseText); + var response = parseXHRResponse(xhr); + if(response.success) { - var udata = this.get('updateData'); - if(udata.timestamp == udata.parent_timestamp) + if( (response.data.result == 'no-op') + || (response.data.timestamp == response.data.parent_timestamp)) { - // no change + if( (response.data.revision) && (response.data.revision != this.get('revision')) ) + { + // we're out of sync + this.set('state', 'unsynced'); + return; + } + messageCenter.addMessage('info', 'doc_update', - 'Nic siÄ nie zmieniÅo od ostatniej aktualizacji. Po co mam uaktualniaÄ?'); + 'Już posiadasz najbardziej aktualnÄ wersjÄ.'); + this.set('state', 'synced'); + return; + } + // result: success + this.set('revision', response.data.revision); + this.set('user', response.data.user); + + messageCenter.addMessage('info', 'doc_update', + 'Uaktualnienie dokumentu do wersji ' + response.data.revision); + + for (var key in this.contentModels) { + this.contentModels[key].set('revision', this.get('revision') ); + this.contentModels[key].set('state', 'empty'); } - else { - this.set('revision', udata.revision); - this.set('user', udata.user); - messageCenter.addMessage('info', 'doc_update', - 'Uaktualnienie dokumentu do wersji ' + udata.revision); - - for (var key in this.contentModels) { - this.contentModels[key].set('revision', this.get('revision') ); - this.contentModels[key].set('state', 'empty'); - } - } - } else if (xhr.status == 409) { // Konflikt podczas operacji - messageCenter.addMessage('error', 'doc_update', - 'WystÄ piÅ konflikt podczas aktualizacji. PÄdź po programistów! :-('); - } else { - messageCenter.addMessage('critical', 'doc_update', - 'Nieoczekiwany bÅÄ d. PÄdź po programistów! :-('); + + this.set('state', 'synced'); + return; } + + // no success means trouble + messageCenter.addMessage(response.error_level, 'doc_update', + response.error_message); - this.set('state', 'synced'); - this.set('updateData', null); + this.set('state', 'unsynced'); }, merge: function(message) { this.set('state', 'loading'); - messageCenter.addMessage('info', null, + messageCenter.addMessage('info', 'doc_merge', 'Scalam dokument z gÅównym repozytorium...'); $.ajax({ @@ -568,28 +594,53 @@ Editor.DocumentModel = Editor.Model.extend({ }, mergeCompleted: function(xhr, textStatus) { - console.log(xhr.status, textStatus); - if (xhr.status == 200) { // Sukces - this.set('revision', this.get('updateData').revision); - this.set('user', this.get('updateData').user); - - for (var key in this.contentModels) { - this.contentModels[key].set('revision', this.get('revision')); - this.contentModels[key].set('state', 'empty'); + console.log(xhr.status, xhr.responseText); + var response = parseXHRResponse(xhr); + + if(response.success) { + + if( (response.data.result == 'no-op') || + ( response.data.shared_parent_timestamp + && response.data.shared_timestamp + && (response.data.shared_timestamp == response.data.shared_parent_timestamp)) ) + { + if( (response.data.revision) && (response.data.revision != this.get('revision')) ) + { + // we're out of sync + this.set('state', 'unsynced'); + return; + } + + messageCenter.addMessage('info', 'doc_merge', + 'Twoja aktualna wersja nie różni siÄ od ostatnio zatwierdzonej.'); + this.set('state', 'synced'); + return; } - messageCenter.addMessage('success', null, 'ScaliÅem dokument z gÅównym repozytorium :-)'); - } else if (xhr.status == 202) { // Wygenerowano PullRequest - messageCenter.addMessage('success', null, 'WysÅaÅem proÅbÄ o scalenie dokumentu z gÅównym repozytorium.'); - } else if (xhr.status == 204) { // Nic nie zmieniono - messageCenter.addMessage('info', null, 'Nic siÄ nie zmieniÅo od ostatniego scalenia. Po co mam scalaÄ?'); - } else if (xhr.status == 409) { // Konflikt podczas operacji - messageCenter.addMessage('error', null, 'WystÄ piÅ konflikt podczas scalania. PÄdź po programistów! :-('); - } else if (xhr.status == 500) { - messageCenter.addMessage('critical', null, 'BÅÄ d serwera. PÄdź po programistów! :-('); + if( response.data.result == 'accepted') + { + messageCenter.addMessage('info', 'doc_merge', + 'ProÅba o zatwierdzenie zostaÅa przyjÄta i oczekujÄ na przyjÄcie.'); + this.set('state', 'synced'); + return; + } + + // result: success + this.set('revision', response.data.revision); + this.set('user', response.data.user); + + messageCenter.addMessage('info', 'doc_merge', + 'Twoja wersja dokumentu zostaÅa zatwierdzona.'); + + this.set('state', 'synced'); + return; } - this.set('state', 'synced'); - this.set('mergeData', null); + + // no success means trouble + messageCenter.addMessage(response.error_level, 'doc_merge', + response.error_message); + + this.set('state', 'unsynced'); }, // For debbuging