X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/5c8c1676ef0036d6feceee576423ed0f1d28ae7c..32d1a0824b971c724ded6b11c85f6aaf371ab539:/modules/documentCanvas/documentCanvas.js diff --git a/modules/documentCanvas/documentCanvas.js b/modules/documentCanvas/documentCanvas.js index 031ca1f..9d2e725 100644 --- a/modules/documentCanvas/documentCanvas.js +++ b/modules/documentCanvas/documentCanvas.js @@ -3,9 +3,10 @@ define([ 'libs/underscore-min', './transformations', -'libs/text!./template.html'], function(_, transformations, template) { - +'./wlxmlNode', +'libs/text!./template.html'], function(_, transformations, wlxmlNode, template) { +'use strict'; return function(sandbox) { @@ -20,11 +21,18 @@ return function(sandbox) { sandbox.publish('contentChanged'); }); - this.node.on('mouseover', '[wlxml-tag]', function(e) { sandbox.publish('nodeHovered', $(e.target)); }); - this.node.on('mouseout', '[wlxml-tag]', function(e) { sandbox.publish('nodeBlured', $(e.target)); }); + 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._markSelected($(e.target)); + view.selectNode(new wlxmlNode.Node($(e.target))); }); this.node.on('keyup', '#rng-module-documentCanvas-contentWrapper', function(e) { @@ -33,7 +41,7 @@ return function(sandbox) { anchor = anchor.parent(); if(!anchor.is('[wlxml-tag]')) return; - view._markSelected(anchor); + view.selectNode(new wlxmlNode.Node(anchor)); }); this.node.on('keydown', '#rng-module-documentCanvas-contentWrapper', function(e) { @@ -49,7 +57,7 @@ return function(sandbox) { _.each(mutation.addedNodes, function(node) { node = $(node); node.parent().find('[wlxml-tag]').each(function() { - tag = $(this); + var tag = $(this); if(!tag.attr('id')) tag.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);})); }); @@ -154,58 +162,45 @@ return function(sandbox) { getBody: function() { return this.node.find('#rng-module-documentCanvas-content').html(); }, - _markSelected: function(node) { - this.dimNode(node); + selectNode: function(wlxmlNode, options) { + options = options || {}; + var nodeElement = this.getNodeElement(wlxmlNode) - this.node.find('.rng-module-documentCanvas-currentNode').removeClass('rng-module-documentCanvas-currentNode'); + this.dimNode(wlxmlNode); - node.addClass('rng-module-documentCanvas-currentNode'); - - this.currentNode = node; - sandbox.publish('nodeSelected', node); + this.node.find('.rng-module-documentCanvas-currentNode').removeClass('rng-module-documentCanvas-currentNode'); + nodeElement.addClass('rng-module-documentCanvas-currentNode'); + + if(options.moveCarret) { + var range = document.createRange(); + range.selectNodeContents(nodeElement[0]); + range.collapse(false); + var selection = document.getSelection(); + selection.removeAllRanges() + selection.addRange(range); + } + this.currentNode = wlxmlNode; + sandbox.publish('nodeSelected', wlxmlNode); }, - selectNode: function(node) { - view._markSelected(node); - var range = document.createRange(); - range.selectNodeContents(node[0]); - range.collapse(false); - - var selection = document.getSelection(); - selection.removeAllRanges() - selection.addRange(range); - }, - selectNodeById: function(id) { - var node = this.node.find('#'+id); - if(node) - this.selectNode(node); - }, - highlightNode: function(node) { + highlightNode: function(wlxmlNode) { + var nodeElement = this.getNodeElement(wlxmlNode); if(!this.gridToggled) { - node.addClass('rng-hover'); - var label = node.attr('wlxml-tag'); - if(node.attr('wlxml-class')) - label += ' / ' + node.attr('wlxml-class'); + 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); - node.append(tag); + nodeElement.append(tag); } }, - dimNode: function(node) { + dimNode: function(wlxmlNode) { + var nodeElement = this.getNodeElement(wlxmlNode); if(!this.gridToggled) { - node.removeClass('rng-hover'); - node.find('.rng-module-documentCanvas-hoveredNodeTag').remove(); + nodeElement.removeClass('rng-common-hoveredNode'); + nodeElement.find('.rng-module-documentCanvas-hoveredNodeTag').remove(); } }, - highlightNodeById: function(id) { - var node = this.node.find('#'+id); - if(node) - this.highlightNode(node); - }, - dimNodeById: function(id) { - var node = this.node.find('#'+id); - if(node) - this.dimNode(node); - }, selectFirstNode: function() { var firstNodeWithText = this.node.find('[wlxml-tag]').filter(function() { return $(this).clone().children().remove().end().text().trim() !== ''; @@ -216,11 +211,14 @@ return function(sandbox) { else { node = this.node.find('[wlxml-class|="p"]') } - this.selectNode(node); + this.selectNode(new wlxmlNode.Node(node)); }, toggleGrid: function(toggle) { - this.node.find('[wlxml-tag]').toggleClass('rng-hover', toggle); + this.node.find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', toggle); this.gridToggled = toggle; + }, + getNodeElement: function(wlxmlNode) { + return this.node.find('#'+wlxmlNode.id); } }; @@ -234,20 +232,26 @@ return function(sandbox) { var transformed = transformations.fromXML.getDocumentDescription(xml); view.setBody(transformed.HTMLTree); view.selectFirstNode(); - //isDirty = false; + sandbox.publish('documentSet'); + }, + getDocument: function() { + return transformations.toXML.getXML(view.getBody()); }, modifyCurrentNode: function(attr, value) { - if(view.currentNode) - view.currentNode.attr('wlxml-'+attr, value); + if(view.currentNode) { + view.getNodeElement(view.currentNode).attr('wlxml-'+attr, value); + sandbox.publish('contentChanged'); + } }, - highlightNode: function(id) { - view.highlightNodeById(id); + highlightNode: function(wlxmlNode) { + view.highlightNode(wlxmlNode); }, - dimNode: function(id) { - view.dimNodeById(id); + dimNode: function(wlxmlNode) { + view.dimNode(wlxmlNode); }, - selectNode: function(id) { - view.selectNodeById(id); + selectNode: function(wlxmlNode) { + if(!wlxmlNode.is(view.currentNode)) + view.selectNode(wlxmlNode, {moveCarret: true}); }, toggleGrid: function(toggle) { view.toggleGrid(toggle);