editor: inform user about errors that occured during document transaction
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 24 Mar 2014 14:52:18 +0000 (15:52 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 1 Apr 2014 19:37:10 +0000 (21:37 +0200)
src/editor/modules/data/data.js
src/editor/modules/data/document.js [new file with mode: 0644]
src/editor/modules/documentCanvas/commands.js

index 540f896..38a80a9 100644 (file)
@@ -4,8 +4,9 @@ define([
     'wlxml/wlxml',
     'wlxml/extensions/list/list',
     'fnpjs/logging/logging',
-    'fnpjs/datetime'
-], function($, Dialog, wlxml, listExtension, logging, datetime) {
+    'fnpjs/datetime',
+    './document'
+], function($, Dialog, wlxml, listExtension, logging, datetime, Document) {
 
 'use strict';
 /* global gettext, alert, window */
@@ -36,11 +37,11 @@ return function(sandbox) {
     var loadDocument = function(text, isDraft, draftTimestamp) {
         logger.debug('loading document');
         try {
-            wlxmlDocument = wlxml.WLXMLDocumentFromXML(text);
+            wlxmlDocument = wlxml.WLXMLDocumentFromXML(text, {}, Document);
         } catch(e) {
             logger.exception(e);
             alert(gettext('This document contains errors and can\'t be loaded. :(')); // TODO
-            wlxmlDocument = wlxml.WLXMLDocumentFromXML(stubDocument);
+            wlxmlDocument = wlxml.WLXMLDocumentFromXML(stubDocument, {}, Document);
         }
 
         wlxmlDocument.registerExtension(listExtension);
diff --git a/src/editor/modules/data/document.js b/src/editor/modules/data/document.js
new file mode 100644 (file)
index 0000000..0dbff14
--- /dev/null
@@ -0,0 +1,46 @@
+define(function(require) {
+    
+'use strict';
+
+/* globals gettext */
+
+var _ = require('libs/underscore'),
+    Dialog = require('views/dialog/dialog'),
+    wlxml = require('wlxml/wlxml'),
+    logging = require('fnpjs/logging/logging');
+
+
+var logger = logging.getLogger('document');
+
+var Document = function() {
+    wlxml.WLXMLDocument.apply(this, Array.prototype.slice.call(arguments, 0));
+};
+Document.prototype = Object.create(wlxml.WLXMLDocument.prototype);
+
+_.extend(Document.prototype, {
+    transaction: function(body, params) {
+        params = params || {};
+        var error = params.error;
+        params.error = function(e) {
+            logger.exception(e);
+
+            var dialog = Dialog.create({
+                title: gettext('Error'),
+                text: gettext('Something wrong happend when applying this change so it was undone.'),
+                executeButtonText: gettext('Close')
+            });
+            dialog.show();
+            if(error) {
+                error(e);
+            }
+            dialog.on('execute', function(e) {
+                e.success();
+            });
+        }.bind(this);
+        return wlxml.WLXMLDocument.prototype.transaction.call(this, body, params);
+    }
+});
+
+return Document;
+
+});
\ No newline at end of file
index 5f3583e..a411a37 100644 (file)
@@ -111,7 +111,7 @@ commands.register('newNodeRequested', function(canvas, params, user) {
 
     var insertNode = function(insertion, callback) {
         var doc = canvas.wlxmlDocument,
-            node, metadata, creator, dialog;
+            metadata, creator, dialog;
 
         var execCallback = function(node) {
             if(callback) {
@@ -121,7 +121,7 @@ commands.register('newNodeRequested', function(canvas, params, user) {
 
         if(params.wlxmlTag === 'aside' && params.wlxmlClass === 'comment') {
             doc.transaction(function() {
-                node = insertion();
+                var node = insertion();
                 if(user) {
                     creator = user.name;
                     if(user.email) {
@@ -134,8 +134,10 @@ commands.register('newNodeRequested', function(canvas, params, user) {
                 metadata = node.getMetadata();
                 metadata.add({key: 'creator', value: creator});
                 metadata.add({key: 'date', value: datetime.currentStrfmt()});
+                return node;
+            }, {
+                success: execCallback
             });
-            execCallback(node);
         } else if(params.wlxmlClass === 'link') {
             dialog = Dialog.create({
                 title: gettext('Create link'),
@@ -147,18 +149,19 @@ commands.register('newNodeRequested', function(canvas, params, user) {
             });
             dialog.on('execute', function(event) {
                 doc.transaction(function() {
-                    node = insertion();
+                    var node = insertion();
                     node.setAttr('href', event.formData.href);
                     event.success();
+                    return node;
+                }, {
+                    success: execCallback
                 });
-                execCallback(node);
             });
             dialog.show();
         } else {
             doc.transaction(function() {
-                node = insertion();
-            });
-            execCallback(node);
+                return insertion();
+            }, {success: execCallback});
         }
     };