canvas api wip: splitting nodes with subnodes
[fnpeditor.git] / modules / nodeFamilyTree / nodeFamilyTree.js
1 define([\r
2 'libs/jquery-1.9.1.min',\r
3 'libs/underscore-min',\r
4 'libs/text!./template.html'\r
5 ], function($, _, templateSrc) {\r
6 \r
7 'use strict';\r
8 \r
9 return function(sandbox) {\r
10     \r
11     var template = _.template(templateSrc);\r
12     \r
13     var view = {\r
14         dom: $('<div>' + template({children: null, parent: null}) + '</div>'),\r
15         setup: function() {\r
16             var view = this;\r
17             this.dom.on('click', 'a', function(e) {\r
18                 var target = $(e.target);\r
19                 sandbox.publish('nodeSelected', view.nodes[target.attr('data-id')]);\r
20             });\r
21             \r
22             this.dom.on('mouseenter', 'a', function(e) {\r
23                 var target = $(e.target);\r
24                 sandbox.publish('nodeEntered', view.nodes[target.attr('data-id')])\r
25             });\r
26             this.dom.on('mouseleave', 'a', function(e) {\r
27                 var target = $(e.target);\r
28                 sandbox.publish('nodeLeft', view.nodes[target.attr('data-id')])\r
29             });\r
30         },\r
31         setNode: function(node) {\r
32             console.log('familyTree sets node');\r
33             var nodes = this.nodes = {};\r
34             this.currentNode = node;\r
35             var parentNode = node.parent();\r
36             var parent = undefined;\r
37             \r
38             if(parentNode) {\r
39                 parent = {\r
40                     repr: parentNode.tag + (parentNode.klass ? ' / ' + parentNode.klass : ''),\r
41                     id: parentNode.id\r
42                 };\r
43                 this.nodes[parentNode.id] = parentNode;\r
44             }\r
45         \r
46             var children = [];\r
47             node.children().each(function() {\r
48                 var child = this;\r
49                 children.push({repr: child.tag + (child.klass ? ' / ' + child.klass : ''), id: child.id});\r
50                 nodes[child.id] = child;\r
51             });\r
52             this.dom.empty();\r
53             this.dom.append($(template({parent: parent, children: children})));\r
54         },\r
55         highlightNode: function(wlxmlNode) {\r
56             this.dom.find('a[data-id="'+wlxmlNode.id+'"]').addClass('rng-common-hoveredNode');\r
57         },\r
58         dimNode: function(wlxmlNode) {\r
59             this.dom.find('a[data-id="'+wlxmlNode.id+'"]').removeClass('rng-common-hoveredNode');\r
60         }\r
61     }\r
62     \r
63     view.setup();\r
64     \r
65     return {\r
66         start: function() {\r
67             sandbox.publish('ready');\r
68         },\r
69         setNode: function(wlxmlNode) {\r
70             if(!wlxmlNode.is(view.currentNode))\r
71                 view.setNode(wlxmlNode);\r
72         },\r
73         getView: function() {\r
74             return view.dom;\r
75         },\r
76         highlightNode: function(wlxmlNode) {\r
77             view.highlightNode(wlxmlNode);\r
78         },\r
79         dimNode: function(wlxmlNode) {\r
80             view.dimNode(wlxmlNode);\r
81         }\r
82     };\r
83 };\r
84 \r
85 });