Lepsze wiadomości.
[redakcja.git] / project / static / js / models.js
index 9938eb0..3cd98f6 100644 (file)
@@ -1,7 +1,4 @@
 /*globals Editor fileId SplitView PanelContainerView EditorView FlashView messageCenter*/
-var documentsUrl = '/api/documents/';
-
-
 Editor.Model = Editor.Object.extend({
   synced: false,
   data: null
@@ -9,8 +6,7 @@ Editor.Model = Editor.Object.extend({
 
 
 Editor.ToolbarButtonsModel = Editor.Model.extend({
-  _className: 'Editor.ToolbarButtonsModel',
-  serverURL: '/api/toolbar/buttons',
+  className: 'Editor.ToolbarButtonsModel',  
   buttons: {},
   
   init: function() {
@@ -20,7 +16,7 @@ Editor.ToolbarButtonsModel = Editor.Model.extend({
   load: function() {
     if (!this.get('buttons').length) {
       $.ajax({
-        url: this.serverURL,
+        url: toolbarUrl,
         dataType: 'json',
         success: this.loadSucceeded.bind(this)
       });
@@ -35,6 +31,8 @@ Editor.ToolbarButtonsModel = Editor.Model.extend({
 
 // Stany modelu:
 //
+//                  -> error -> loading
+//                 /
 // empty -> loading -> synced -> unsynced -> loading
 //                           \
 //                            -> dirty -> updating -> updated -> synced
@@ -54,23 +52,44 @@ Editor.XMLModel = Editor.Model.extend({
     this.addObserver(this, 'data', this.dataChanged.bind(this));
   },
   
-  load: function() {
-    if (this.get('state') == 'empty') {
+  load: function(force) {
+    if (force || this.get('state') == 'empty') {
       this.set('state', 'loading');
+      messageCenter.addMessage('info', 'Wczytuję XML...');
       $.ajax({
         url: this.serverURL,
         dataType: 'text',
         data: {revision: this.get('revision')},
-        success: this.loadingSucceeded.bind(this)
+        success: this.loadingSucceeded.bind(this),
+        error: this.loadingFailed.bind(this)
       });
       return true;
     }
     return false;
   },
   
+  loadingSucceeded: function(data) {
+    if (this.get('state') != 'loading') {
+      alert('erroneous state:', this.get('state'));
+    }
+    this.set('data', data);
+    this.set('state', 'synced');
+    messageCenter.addMessage('success', 'Wczytałem XML :-)');
+  },
+  
+  loadingFailed: function() {
+    if (this.get('state') != 'loading') {
+      alert('erroneous state:', this.get('state'));
+    }
+    this.set('error', 'Nie udało się załadować panelu');
+    this.set('state', 'error');    
+    messageCenter.addMessage('error', 'Nie udało mi się wczytać XML. Spróbuj ponownie :-(');
+  },
+  
   update: function(message) {
     if (this.get('state') == 'dirty') {
       this.set('state', 'updating');
+      messageCenter.addMessage('info', 'Zapisuję XML...');
       
       var payload = {
         contents: this.get('data'),
@@ -93,17 +112,20 @@ Editor.XMLModel = Editor.Model.extend({
     return false;
   },
   
-  updatingSucceeded: function() {
+  updatingSucceeded: function(data) {
     if (this.get('state') != 'updating') {
       alert('erroneous state:', this.get('state'));
     }
+    this.set('revision', data.revision);
     this.set('state', 'updated');
+    messageCenter.addMessage('success', 'Zapisałem XML :-)');
   },
   
   updatingFailed: function() {
     if (this.get('state') != 'updating') {
       alert('erroneous state:', this.get('state'));
     }
+    messageCenter.addMessage('error', 'Nie udało mi się zapisać XML. Spróbuj ponownie :-(');
     this.set('state', 'dirty');
   },
   
@@ -121,14 +143,6 @@ Editor.XMLModel = Editor.Model.extend({
     }
   },
   
-  loadingSucceeded: function(data) {
-    if (this.get('state') != 'loading') {
-      alert('erroneous state:', this.get('state'));
-    }
-    this.set('data', data);
-    this.set('state', 'synced');
-  },
-  
   dispose: function() {
     this.removeObserver(this);
     this._super();
@@ -149,14 +163,16 @@ Editor.HTMLModel = Editor.Model.extend({
     this.serverURL = serverURL;
   },
   
-  load: function() {
-    if (this.get('state') == 'empty') {
+  load: function(force) {
+    if (force || this.get('state') == 'empty') {
       this.set('state', 'loading');
+      messageCenter.addMessage('info', 'Wczytuję HTML...');
       $.ajax({
         url: this.serverURL,
         dataType: 'text',
         data: {revision: this.get('revision')},
-        success: this.loadingSucceeded.bind(this)
+        success: this.loadingSucceeded.bind(this),
+        error: this.loadingFailed.bind(this)
       });
     }
   },
@@ -167,6 +183,16 @@ Editor.HTMLModel = Editor.Model.extend({
     }
     this.set('data', data);
     this.set('state', 'synced');
+    messageCenter.addMessage('success', 'Wczytałem HTML :-)');
+  },
+  
+  loadingFailed: function() {
+    if (this.get('state') != 'loading') {
+      alert('erroneous state:', this.get('state'));
+    }
+    this.set('error', 'Nie udało się załadować panelu');
+    this.set('state', 'error');    
+    messageCenter.addMessage('error', 'Nie udało mi się wczytać HTML. Spróbuj ponownie :-(');
   },
 
   // For debbuging
@@ -193,8 +219,8 @@ Editor.ImageGalleryModel = Editor.Model.extend({
     this.pages = [];
   },
 
-  load: function() {
-    if (this.get('state') == 'empty') {
+  load: function(force) {
+    if (force || this.get('state') == 'empty') {
       this.set('state', 'loading');
       $.ajax({
         url: this.serverURL,
@@ -245,6 +271,7 @@ Editor.DocumentModel = Editor.Model.extend({
   load: function() {
     if (this.get('state') == 'empty') {
       this.set('state', 'loading');
+      messageCenter.addMessage('info', 'Ładuję dane dokumentu...');
       $.ajax({
         cache: false,
         url: documentsUrl + fileId,
@@ -265,6 +292,7 @@ Editor.DocumentModel = Editor.Model.extend({
     for (var key in this.contentModels) {
       this.contentModels[key].addObserver(this, 'state', this.contentModelStateChanged.bind(this));
     }
+    messageCenter.addMessage('success', 'Dane dokumentu zostały załadowane :-)');
   },
   
   contentModelStateChanged: function(property, value, contentModel) {
@@ -280,7 +308,12 @@ Editor.DocumentModel = Editor.Model.extend({
       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.data.user_revision = this.contentModels[key].get('revision');
+        }
+      }
+      for (key in this.contentModels) {
+        if (this.contentModels[key].guid() != contentModel.guid()) {
+          this.contentModels[key].set('revision', this.data.user_revision);
           this.contentModels[key].set('state', 'empty');
         }
       }
@@ -298,6 +331,7 @@ Editor.DocumentModel = Editor.Model.extend({
   
   update: function() {
     this.set('state', 'loading');
+    messageCenter.addMessage('info', 'Uaktualniam dokument...');
     $.ajax({
       url: this.data.merge_url,
       dataType: 'json',
@@ -315,13 +349,20 @@ Editor.DocumentModel = Editor.Model.extend({
     console.log(xhr.status, textStatus);
     if (xhr.status == 200) { // Sukces
       this.data.user_revision = this.get('updateData').revision;
+      messageCenter.addMessage('info', 'Uaktualnienie dokumentu do wersji ' + this.get('updateData').revision,
+        'Uaktualnienie dokumentu do wersji ' + this.get('updateData').revision);
       for (var key in this.contentModels) {
         this.contentModels[key].set('revision', this.data.user_revision);
         this.contentModels[key].set('state', 'empty');
       }
+      messageCenter.addMessage('success', 'Uaktualniłem dokument do najnowszej wersji :-)');
     } else if (xhr.status == 202) { // Wygenerowano PullRequest (tutaj?)
     } else if (xhr.status == 204) { // Nic nie zmieniono
+      messageCenter.addMessage('info', 'Nic się nie zmieniło od ostatniej aktualizacji. Po co mam uaktualniać?');
     } else if (xhr.status == 409) { // Konflikt podczas operacji
+      messageCenter.addMessage('error', 'Wystąpił konflikt podczas aktualizacji. Pędź po programistów! :-(');
+    } else if (xhr.status == 500) {
+      messageCenter.addMessage('critical', 'Błąd serwera. Pędź po programistów! :-(');
     } 
     this.set('state', 'synced');
     this.set('updateData', null);
@@ -329,6 +370,7 @@ Editor.DocumentModel = Editor.Model.extend({
   
   merge: function(message) {
     this.set('state', 'loading');
+    messageCenter.addMessage('info', 'Scalam dokument z głównym repozytorium...');
     $.ajax({
       url: this.data.merge_url,
       type: 'post',
@@ -351,9 +393,15 @@ Editor.DocumentModel = Editor.Model.extend({
         this.contentModels[key].set('revision', this.data.user_revision);
         this.contentModels[key].set('state', 'empty');
       }
+      messageCenter.addMessage('success', 'Scaliłem dokument z głównym repozytorium :-)');
     } else if (xhr.status == 202) { // Wygenerowano PullRequest
+      messageCenter.addMessage('success', 'Wysłałem prośbę o scalenie dokumentu z głównym repozytorium.');
     } else if (xhr.status == 204) { // Nic nie zmieniono
+      messageCenter.addMessage('info', 'Nic się nie zmieniło od ostatniego scalenia. Po co mam scalać?');
     } else if (xhr.status == 409) { // Konflikt podczas operacji
+      messageCenter.addMessage('error', 'Wystąpił konflikt podczas scalania. Pędź po programistów! :-(');
+    } else if (xhr.status == 500) {
+      messageCenter.addMessage('critical', 'Błąd serwera. Pędź po programistów! :-(');
     }
     this.set('state', 'synced');
     this.set('mergeData', null);
@@ -371,12 +419,18 @@ Editor.DocumentModel = Editor.Model.extend({
 
 var leftPanelView, rightPanelContainer, doc;
 
-$(function() {
+$(function()
+{
+  documentsUrl = $('#api-base-url').text() + '/';
+  toolbarUrl = $('#api-toolbar-url').text();
+
   doc = new Editor.DocumentModel();
-  var editor = new EditorView('#body-wrap', doc);
+  var editor = new EditorView('#body-wrap', doc);  
   editor.freeze();
+
   var flashView = new FlashView('#flashview', messageCenter);
   var splitView = new SplitView('#splitview', doc);
+
   leftPanelView = new PanelContainerView('#left-panel-container', doc);
-  rightPanelContainer = new PanelContainerView('#right-panel-container', doc);
+  rightPanelContainer = new PanelContainerView('#right-panel-container', doc); 
 });