3c78bd17f3e23cfad7bc26993b609ede5801ba8f
[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             var parentNode = node.parent();\r
35             var parent = undefined;\r
36             \r
37             if(parentNode) {\r
38                 parent = {\r
39                     repr: parentNode.tag + (parentNode.klass ? ' / ' + parentNode.klass : ''),\r
40                     id: parentNode.id\r
41                 };\r
42                 this.nodes[parentNode.id] = parentNode;\r
43             }\r
44         \r
45             var children = [];\r
46             node.children().each(function() {\r
47                 var child = this;\r
48                 children.push({repr: child.tag + (child.klass ? ' / ' + child.klass : ''), id: child.id});\r
49                 nodes[child.id] = child;\r
50             });\r
51             this.dom.empty();\r
52             this.dom.append($(template({parent: parent, children: children})));\r
53         },\r
54         highlightNode: function(wlxmlNode) {\r
55             this.dom.find('a[data-id="'+wlxmlNode.id+'"]').addClass('rng-common-hoveredNode');\r
56         },\r
57         dimNode: function(wlxmlNode) {\r
58             this.dom.find('a[data-id="'+wlxmlNode.id+'"]').removeClass('rng-common-hoveredNode');\r
59         }\r
60     }\r
61     \r
62     view.setup();\r
63     \r
64     return {\r
65         start: function() {\r
66             sandbox.publish('ready');\r
67         },\r
68         setNode: function(node) {\r
69             view.setNode(node);\r
70         },\r
71         getView: function() {\r
72             return view.dom;\r
73         },\r
74         highlightNode: function(wlxmlNode) {\r
75             view.highlightNode(wlxmlNode);\r
76         },\r
77         dimNode: function(wlxmlNode) {\r
78             view.dimNode(wlxmlNode);\r
79         }\r
80     };\r
81 };\r
82 \r
83 });