From: Aleksander Ɓukasz Date: Tue, 26 Nov 2013 15:52:49 +0000 (+0100) Subject: canvas: wlxmlListener - handle setting root node X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/e1bff45c85b21d5d87640273319c9a8fd2377f86?ds=inline canvas: wlxmlListener - handle setting root node --- diff --git a/src/editor/modules/documentCanvas/canvas/canvas.js b/src/editor/modules/documentCanvas/canvas/canvas.js index 9a01457..cf63c47 100644 --- a/src/editor/modules/documentCanvas/canvas/canvas.js +++ b/src/editor/modules/documentCanvas/canvas/canvas.js @@ -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) { diff --git a/src/editor/modules/documentCanvas/canvas/canvas.test.js b/src/editor/modules/documentCanvas/canvas/canvas.test.js index 35ad728..98b7ffe 100644 --- a/src/editor/modules/documentCanvas/canvas/canvas.test.js +++ b/src/editor/modules/documentCanvas/canvas/canvas.test.js @@ -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('
'), + 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; diff --git a/src/editor/modules/documentCanvas/canvas/wlxmlListener.js b/src/editor/modules/documentCanvas/canvas/wlxmlListener.js index 9269ce1..2dd61ff 100644 --- a/src/editor/modules/documentCanvas/canvas/wlxmlListener.js +++ b/src/editor/modules/documentCanvas/canvas/wlxmlListener.js @@ -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;