canvas: wlxmlListener - handle setting root node
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 26 Nov 2013 15:52:49 +0000 (16:52 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 2 Dec 2013 13:50:54 +0000 (14:50 +0100)
src/editor/modules/documentCanvas/canvas/canvas.js
src/editor/modules/documentCanvas/canvas/canvas.test.js
src/editor/modules/documentCanvas/canvas/wlxmlListener.js

index 9a01457..cf63c47 100644 (file)
@@ -25,15 +25,17 @@ $.extend(Canvas.prototype, {
             return false;
         }
 
-        var canvasDOM = this.generateCanvasDOM(wlxmlDocument.root);
+        this.wlxmlListener.listenTo(wlxmlDocument);
+        this.wlxmlDocument = wlxmlDocument;
+        this.reloadRoot();
+        this.setupEventHandling();
+    },
 
+    reloadRoot: function() {
+        var canvasDOM = this.generateCanvasDOM(this.wlxmlDocument.root);
         this.wrapper.empty();
         this.wrapper.append(canvasDOM);
         this.d = this.wrapper.children(0);
-        this.setupEventHandling();
-
-        this.wlxmlListener.listenTo(wlxmlDocument);
-        this.wlxmlDocument = wlxmlDocument;
     },
 
     generateCanvasDOM: function(wlxmlNode) {
index 35ad728..98b7ffe 100644 (file)
@@ -14,9 +14,13 @@ define([
 var expect = chai.expect;
 
 var getCanvasFromXML = function(xml) {
-    return canvas.fromXMLDocument(wlxml.WLXMLDocumentFromXML(xml));
+    return canvas.fromXMLDocument(getDocumentFromXML(xml));
 };
 
+var getDocumentFromXML = function(xml) {
+    return wlxml.WLXMLDocumentFromXML(xml);
+}
+
 var wait = function(callback, timeout) {
     return window.setTimeout(callback, timeout || 0.5);
 };
@@ -46,6 +50,16 @@ describe('Handling empty text nodes', function() {
     });
 });
 
+describe('Handling changes to the document', function() {
+    it('replaces the whole canvas content when document root node replaced', function() {
+        var doc = getDocumentFromXML('<section></section>'),
+            c = canvas.fromXMLDocument(doc);
+
+        var header = doc.root.replaceWith({tagName: 'header'});
+        expect(c.doc().data('wlxmlNode').sameNode(header)).to.be.true;
+    });
+});
+
 describe('Cursor', function() {
 
     var getSelection;
index 9269ce1..2dd61ff 100644 (file)
@@ -41,6 +41,10 @@ var handlers = {
         canvasNode.setWlxmlTag(event.meta.newTagName);
     },
     nodeAdded: function(event) {
+        if(event.meta.node.isRoot()) {
+            this.canvas.reloadRoot();
+            return;
+        }
         var parentElement = utils.findCanvasElement(event.meta.node.parent()),
             nodeIndex = event.meta.node.getIndex(),
             referenceElement, referenceAction;