From e573418502325e80c42523264424fa547a435de5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Thu, 20 Jun 2013 12:33:14 +0200 Subject: [PATCH] canvas manager wip --- modules/documentCanvas/canvas.js | 13 +- modules/documentCanvas/canvasManager.js | 144 ++++++++++++++++++ modules/documentCanvas/documentCanvas.js | 178 +++-------------------- 3 files changed, 172 insertions(+), 163 deletions(-) create mode 100644 modules/documentCanvas/canvasManager.js diff --git a/modules/documentCanvas/canvas.js b/modules/documentCanvas/canvas.js index dc64cac..a15bb2c 100644 --- a/modules/documentCanvas/canvas.js +++ b/modules/documentCanvas/canvas.js @@ -9,13 +9,14 @@ define([ 'use strict'; var Canvas = function(xml) { - this.xml = xml; this.dom = $(template); - - this.content = this.dom.find('#rng-module-documentCanvas-content') - - - this.content.html(transformations.fromXML.getHTMLTree(xml)); + this.content = this.dom.find('#rng-module-documentCanvas-content'); + this.setXML(xml); +} + +Canvas.prototype.setXML = function(xml) { + this.xml = xml; + this.content.html(transformations.fromXML.getHTMLTree(xml)); } Canvas.prototype.toXML = function() { diff --git a/modules/documentCanvas/canvasManager.js b/modules/documentCanvas/canvasManager.js new file mode 100644 index 0000000..fcf2659 --- /dev/null +++ b/modules/documentCanvas/canvasManager.js @@ -0,0 +1,144 @@ +define([ +'libs/jquery-1.9.1.min', +'./wlxmlNode' +], function($, wlxmlNode) { + +'use strict'; + +var Manager = function(canvas, sandbox) { + this.canvas = canvas; + this.sandbox = sandbox; + this.shownAlready = false; + this.gridToggled = false; + this.scrollbarPosition = 0; + this.currentNode = null; + var manager = this; + + canvas.dom.find('#rng-module-documentCanvas-content').on('keyup', function() { + manager.sandbox.publish('contentChanged'); + }); + + canvas.dom.on('mouseover', '[wlxml-tag]', function(e) { + e.stopPropagation(); + manager.sandbox.publish('nodeHovered', new wlxmlNode.Node($(e.target))); + }); + canvas.dom.on('mouseout', '[wlxml-tag]', function(e) { + e.stopPropagation(); + manager.sandbox.publish('nodeBlured', new wlxmlNode.Node($(e.target))); + }); + canvas.dom.on('click', '[wlxml-tag]', function(e) { + e.stopPropagation(); + console.log('clicked node type: '+e.target.nodeType); + manager.selectNode(new wlxmlNode.Node($(e.target))); + }); + + canvas.dom.on('keyup', '#rng-module-documentCanvas-contentWrapper', function(e) { + var anchor = $(window.getSelection().anchorNode); + if(anchor[0].nodeType === Node.TEXT_NODE) + anchor = anchor.parent(); + if(!anchor.is('[wlxml-tag]')) + return; + manager.selectNode(new wlxmlNode.Node(anchor)); + }); + + canvas.dom.on('keydown', '#rng-module-documentCanvas-contentWrapper', function(e) { + if(e.which === 13) { + e.preventDefault(); + //view.insertNewNode(null, null); + } + if(e.which === 8) { + var anchor = window.getSelection().anchorNode; + var len = anchor.length; + console.log(len); + if(len === 1) { + e.preventDefault(); + $(anchor).parent().text(''); + } + } + }); + + canvas.dom.onShow = function() { + if(!manager.shownAlready) { + manager.shownAlready = true; + manager.selectFirstNode(); + } else if(manager.currentNode) { + manager.movecaretToNode(manager.getNodeElement(manager.currentNode)); + canvas.dom.find('#rng-module-documentCanvas-contentWrapper').scrollTop(manager.scrollbarPosition); + } + }; + canvas.dom.onHide = function() { + manager.scrollbarPosition = canvas.dom.find('#rng-module-documentCanvas-contentWrapper').scrollTop(); + } +}; + +Manager.prototype.selectNode = function(wlxmlNode, options) { + options = options || {}; + var nodeElement = this.getNodeElement(wlxmlNode) + + this.dimNode(wlxmlNode); + + this.canvas.dom.find('.rng-module-documentCanvas-currentNode').removeClass('rng-module-documentCanvas-currentNode'); + nodeElement.addClass('rng-module-documentCanvas-currentNode'); + + if(options.movecaret) { + this.movecaretToNode(nodeElement); + } + + this.currentNode = wlxmlNode; + this.sandbox.publish('nodeSelected', wlxmlNode); +}; + +Manager.prototype.getNodeElement = function(wlxmlNode) { + return this.canvas.dom.find('#'+wlxmlNode.id); +}; + +Manager.prototype.highlightNode = function(wlxmlNode) { + var nodeElement = this.getNodeElement(wlxmlNode); + if(!this.gridToggled) { + nodeElement.addClass('rng-common-hoveredNode'); + var label = nodeElement.attr('wlxml-tag'); + if(nodeElement.attr('wlxml-class')) + label += ' / ' + nodeElement.attr('wlxml-class'); + var tag = $('
').addClass('rng-module-documentCanvas-hoveredNodeTag').text(label); + nodeElement.append(tag); + } +}; + +Manager.prototype.dimNode = function(wlxmlNode) { + var nodeElement = this.getNodeElement(wlxmlNode); + if(!this.gridToggled) { + nodeElement.removeClass('rng-common-hoveredNode'); + nodeElement.find('.rng-module-documentCanvas-hoveredNodeTag').remove(); + } +}; + +Manager.prototype.selectFirstNode = function() { + var firstNodeWithText = this.canvas.dom.find('[wlxml-tag]').filter(function() { + return $(this).clone().children().remove().end().text().trim() !== ''; + }).first(); + var node; + if(firstNodeWithText.length) + node = $(firstNodeWithText[0]) + else { + node = this.canvas.dom.find('[wlxml-class|="p"]') + } + this.selectNode(new wlxmlNode.Node(node), {movecaret: true}); +}; + +Manager.prototype.movecaretToNode = function(nodeElement) { + var range = document.createRange(); + range.selectNodeContents(nodeElement[0]); + range.collapse(false); + var selection = document.getSelection(); + selection.removeAllRanges() + selection.addRange(range); +}; + +Manager.prototype.toggleGrid = function(toggle) { + this.canvas.dom.find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', toggle); + this.gridToggled = toggle; +}; + +return Manager; + +}); \ No newline at end of file diff --git a/modules/documentCanvas/documentCanvas.js b/modules/documentCanvas/documentCanvas.js index 1577be0..6030703 100644 --- a/modules/documentCanvas/documentCanvas.js +++ b/modules/documentCanvas/documentCanvas.js @@ -4,89 +4,21 @@ define([ 'libs/underscore-min', './transformations', './wlxmlNode', -'libs/text!./template.html'], function(_, transformations, wlxmlNode, template) { +'./canvas', +'./canvasManager', +'libs/text!./template.html'], function(_, transformations, wlxmlNode, Canvas, CanvasManager, template) { 'use strict'; return function(sandbox) { - var view = { - node: $(_.template(template)()), - currentNode: null, - shownAlready: false, - scrollbarPosition: 0, - setup: function() { - var view = this; - - this.node.find('#rng-module-documentCanvas-content').on('keyup', function() { - //isDirty = true; - sandbox.publish('contentChanged'); - }); + var canvas = new Canvas.Canvas(); + + var manager = new CanvasManager(canvas, sandbox); + - this.node.on('mouseover', '[wlxml-tag]', function(e) { - e.stopPropagation(); - sandbox.publish('nodeHovered', new wlxmlNode.Node($(e.target))); - }); - this.node.on('mouseout', '[wlxml-tag]', function(e) { - e.stopPropagation(); - sandbox.publish('nodeBlured', new wlxmlNode.Node($(e.target))); - }); - this.node.on('click', '[wlxml-tag]', function(e) { - e.stopPropagation(); - console.log('clicked node type: '+e.target.nodeType); - view.selectNode(new wlxmlNode.Node($(e.target))); - }); + var view = { - this.node.on('keyup', '#rng-module-documentCanvas-contentWrapper', function(e) { - var anchor = $(window.getSelection().anchorNode); - if(anchor[0].nodeType === Node.TEXT_NODE) - anchor = anchor.parent(); - if(!anchor.is('[wlxml-tag]')) - return; - view.selectNode(new wlxmlNode.Node(anchor)); - }); - - this.node.on('keydown', '#rng-module-documentCanvas-contentWrapper', function(e) { - if(e.which === 13) { - e.preventDefault(); - view.insertNewNode(null, null); - } - if(e.which === 8) { - var anchor = window.getSelection().anchorNode; - var len = anchor.length; - console.log(len); - if(len === 1) { - e.preventDefault(); - $(anchor).parent().text(''); - } - } - }); - - this.node.onShow = function() { - if(!view.shownAlready) { - view.shownAlready = true; - view.selectFirstNode(); - } else if(view.currentNode) { - view.movecaretToNode(view.getNodeElement(view.currentNode)); - view.node.find('#rng-module-documentCanvas-contentWrapper').scrollTop(view.scrollbarPosition); - } - }; - this.node.onHide = function() { - view.scrollbarPosition = view.node.find('#rng-module-documentCanvas-contentWrapper').scrollTop(); - } - - this.gridToggled = false; - }, - _createNode: function(wlxmlTag, wlxmlClass) { - var toBlock = ['div', 'document', 'section', 'header']; - var htmlTag = _.contains(toBlock, wlxmlTag) ? 'div' : 'span'; - var toret = $('<' + htmlTag + '>'); - toret.attr('wlxml-tag', wlxmlTag); - if(wlxmlClass) - toret.attr('wlxml-class', wlxmlClass); - toret.attr('id', 'xxxxxxxx-xxxx-xxxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);})); - return toret; - }, insertNewNode: function(wlxmlTag, wlxmlClass) { //TODO: Insert inline var anchor = $(window.getSelection().anchorNode); @@ -167,116 +99,48 @@ return function(sandbox) { //isDirty = true; sandbox.publish('contentChanged'); } - }, - setBody: function(HTMLTree) { - this.node.find('#rng-module-documentCanvas-content').html(HTMLTree); - }, - getBody: function() { - return this.node.find('#rng-module-documentCanvas-content').html(); - }, - selectNode: function(wlxmlNode, options) { - options = options || {}; - var nodeElement = this.getNodeElement(wlxmlNode) - - this.dimNode(wlxmlNode); - - this.node.find('.rng-module-documentCanvas-currentNode').removeClass('rng-module-documentCanvas-currentNode'); - nodeElement.addClass('rng-module-documentCanvas-currentNode'); - - if(options.movecaret) { - this.movecaretToNode(nodeElement); - } - - this.currentNode = wlxmlNode; - sandbox.publish('nodeSelected', wlxmlNode); - }, - highlightNode: function(wlxmlNode) { - var nodeElement = this.getNodeElement(wlxmlNode); - if(!this.gridToggled) { - nodeElement.addClass('rng-common-hoveredNode'); - var label = nodeElement.attr('wlxml-tag'); - if(nodeElement.attr('wlxml-class')) - label += ' / ' + nodeElement.attr('wlxml-class'); - var tag = $('
').addClass('rng-module-documentCanvas-hoveredNodeTag').text(label); - nodeElement.append(tag); - } - }, - dimNode: function(wlxmlNode) { - var nodeElement = this.getNodeElement(wlxmlNode); - if(!this.gridToggled) { - nodeElement.removeClass('rng-common-hoveredNode'); - nodeElement.find('.rng-module-documentCanvas-hoveredNodeTag').remove(); - } - }, - selectFirstNode: function() { - var firstNodeWithText = this.node.find('[wlxml-tag]').filter(function() { - return $(this).clone().children().remove().end().text().trim() !== ''; - }).first(); - var node; - if(firstNodeWithText.length) - node = $(firstNodeWithText[0]) - else { - node = this.node.find('[wlxml-class|="p"]') - } - this.selectNode(new wlxmlNode.Node(node), {movecaret: true}); - }, - toggleGrid: function(toggle) { - this.node.find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', toggle); - this.gridToggled = toggle; - }, - getNodeElement: function(wlxmlNode) { - return this.node.find('#'+wlxmlNode.id); - }, - movecaretToNode: function(nodeElement) { - var range = document.createRange(); - range.selectNodeContents(nodeElement[0]); - range.collapse(false); - var selection = document.getSelection(); - selection.removeAllRanges() - selection.addRange(range); } + }; - view.setup(); /* public api */ return { start: function() { sandbox.publish('ready'); }, getView: function() { - return view.node; + return canvas.dom; }, setDocument: function(xml) { - var transformed = transformations.fromXML.getDocumentDescription(xml); - view.setBody(transformed.HTMLTree); + canvas.setXML(xml); sandbox.publish('documentSet'); }, getDocument: function() { - return transformations.toXML.getXML(view.getBody()); + return canvas.toXML(); }, modifyCurrentNode: function(attr, value) { - if(view.currentNode) { - view.getNodeElement(view.currentNode).attr('wlxml-'+attr, value); + if(manager.currentNode) { + manager.getNodeElement(manager.currentNode).attr('wlxml-'+attr, value); sandbox.publish('contentChanged'); } }, highlightNode: function(wlxmlNode) { - view.highlightNode(wlxmlNode); + manager.highlightNode(wlxmlNode); }, dimNode: function(wlxmlNode) { - view.dimNode(wlxmlNode); + manager.dimNode(wlxmlNode); }, selectNode: function(wlxmlNode) { - if(!wlxmlNode.is(view.currentNode)) - view.selectNode(wlxmlNode, {movecaret: true}); + if(!wlxmlNode.is(manager.currentNode)) + manager.selectNode(wlxmlNode, {movecaret: true}); }, toggleGrid: function(toggle) { - view.toggleGrid(toggle); + manager.toggleGrid(toggle); }, insertNewNode: function(wlxmlTag, wlxmlClass) { - view.insertNewNode(wlxmlTag, wlxmlClass); + manager.insertNewNode(wlxmlTag, wlxmlClass); }, wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) { - view.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass); + manager.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass); } } -- 2.20.1