From e1bff45c85b21d5d87640273319c9a8fd2377f86 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Tue, 26 Nov 2013 16:52:49 +0100 Subject: [PATCH 1/1] canvas: wlxmlListener - handle setting root node --- .../modules/documentCanvas/canvas/canvas.js | 12 +++++++----- .../modules/documentCanvas/canvas/canvas.test.js | 16 +++++++++++++++- .../documentCanvas/canvas/wlxmlListener.js | 4 ++++ 3 files changed, 26 insertions(+), 6 deletions(-) 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; -- 2.20.1