X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/5be499ab63f91b65065fe34f3a6d645efc9ae7b6..412e60ded1457ec0f408e2234c9dd60122929bac:/modules/nodeFamilyTree/nodeFamilyTree.js diff --git a/modules/nodeFamilyTree/nodeFamilyTree.js b/modules/nodeFamilyTree/nodeFamilyTree.js index 3c78bd1..45f47c0 100644 --- a/modules/nodeFamilyTree/nodeFamilyTree.js +++ b/modules/nodeFamilyTree/nodeFamilyTree.js @@ -1,83 +1,105 @@ -define([ -'libs/jquery-1.9.1.min', -'libs/underscore-min', -'libs/text!./template.html' -], function($, _, templateSrc) { - -'use strict'; - -return function(sandbox) { - - var template = _.template(templateSrc); - - var view = { - dom: $('
' + template({children: null, parent: null}) + '
'), - setup: function() { - var view = this; - this.dom.on('click', 'a', function(e) { - var target = $(e.target); - sandbox.publish('nodeSelected', view.nodes[target.attr('data-id')]); - }); - - this.dom.on('mouseenter', 'a', function(e) { - var target = $(e.target); - sandbox.publish('nodeEntered', view.nodes[target.attr('data-id')]) - }); - this.dom.on('mouseleave', 'a', function(e) { - var target = $(e.target); - sandbox.publish('nodeLeft', view.nodes[target.attr('data-id')]) - }); - }, - setNode: function(node) { - console.log('familyTree sets node'); - var nodes = this.nodes = {}; - var parentNode = node.parent(); - var parent = undefined; - - if(parentNode) { - parent = { - repr: parentNode.tag + (parentNode.klass ? ' / ' + parentNode.klass : ''), - id: parentNode.id - }; - this.nodes[parentNode.id] = parentNode; - } - - var children = []; - node.children().each(function() { - var child = this; - children.push({repr: child.tag + (child.klass ? ' / ' + child.klass : ''), id: child.id}); - nodes[child.id] = child; - }); - this.dom.empty(); - this.dom.append($(template({parent: parent, children: children}))); - }, - highlightNode: function(wlxmlNode) { - this.dom.find('a[data-id="'+wlxmlNode.id+'"]').addClass('rng-common-hoveredNode'); - }, - dimNode: function(wlxmlNode) { - this.dom.find('a[data-id="'+wlxmlNode.id+'"]').removeClass('rng-common-hoveredNode'); - } - } - - view.setup(); - - return { - start: function() { - sandbox.publish('ready'); - }, - setNode: function(node) { - view.setNode(node); - }, - getView: function() { - return view.dom; - }, - highlightNode: function(wlxmlNode) { - view.highlightNode(wlxmlNode); - }, - dimNode: function(wlxmlNode) { - view.dimNode(wlxmlNode); - } - }; -}; - +define([ +'libs/jquery', +'libs/underscore', +'utils/wlxml', +'libs/text!./template.html' +], function($, _, wlxmlUtils, templateSrc) { + +'use strict'; + +return function(sandbox) { + + var template = _.template(templateSrc); + + var view = { + dom: $('
' + template({children: null, parent: null}) + '
'), + setup: function() { + var view = this; + this.dom.on('click', 'a', function(e) { + var target = $(e.target); + sandbox.publish('elementClicked', target.data('element')); + }); + + this.dom.on('mouseenter', 'a', function(e) { + var target = $(e.target); + sandbox.publish('elementEntered', target.data('element')); + }); + this.dom.on('mouseleave', 'a', function(e) { + var target = $(e.target); + sandbox.publish('elementLeft', target.data('element')); + }); + }, + setElement: function(element) { + console.log('familyTree sets node'); + var textElement = element.getText ? element : null, + nodeElement = element.getText ? element.parent() : element, // TODO: better type detection + nodeElementParent = nodeElement.parent(), + parent; + + this.currentNodeElement = nodeElement; + + if(nodeElementParent) { + parent = { + repr: wlxmlUtils.wlxmlTagNames[nodeElementParent.getWlxmlTag()] + (nodeElementParent.getWlxmlClass() ? ' / ' + wlxmlUtils.wlxmlClassNames[nodeElementParent.getWlxmlClass()] : '') + }; + } + + var nodeChildren = nodeElement.children(), + children = []; + nodeChildren.forEach(function(child) { + if(child.getText) { + var text = child.getText(); + if(!text) + text = ''; + else { + if(text.length > 13) { + text = text.substr(0,13) + '...'; + } + text = '"' + text + '"'; + } + children.push({repr: _.escape(text), bold: child.sameNode(textElement)}); + } else { + children.push({repr: wlxmlUtils.wlxmlTagNames[child.getWlxmlTag()] + (child.getWlxmlClass() ? ' / ' + wlxmlUtils.wlxmlClassNames[child.getWlxmlClass()] : '')}); + } + }); + this.dom.empty(); + this.dom.append($(template({parent: parent, children: children}))); + + if(parent) { + this.dom.find('.rng-module-nodeFamilyTree-parent').data('element', nodeElementParent) + } + this.dom.find('li a').each(function(idx, a) { + $(a).data('element', nodeChildren[idx]); + }); + }, + highlightNode: function(canvasNode) { + this.dom.find('a[data-id="'+canvasNode.getId()+'"]').addClass('rng-common-hoveredNode'); + }, + dimNode: function(canvasNode) { + this.dom.find('a[data-id="'+canvasNode.getId()+'"]').removeClass('rng-common-hoveredNode'); + } + }; + + view.setup(); + + return { + start: function() { + sandbox.publish('ready'); + }, + setElement: function(element) { + if(!(element.sameNode(view.currentNodeElement))) + view.setElement(element); + }, + getView: function() { + return view.dom; + }, + highlightNode: function(canvasNode) { + view.highlightNode(canvasNode); + }, + dimNode: function(canvasNode) { + view.dimNode(canvasNode); + } + }; +}; + }); \ No newline at end of file