7331def5f4cb2b884e2911f0110b48c92717d79f
[fnpeditor.git] / modules / nodeBreadCrumbs / nodeBreadCrumbs.js
1 define([\r
2 'libs/jquery-1.9.1.min',\r
3 'libs/underscore-min',\r
4 'libs/text!./template.html'], function($, _, templateSrc) {\r
5 \r
6 'use strict';\r
7 \r
8 return function(sandbox) {\r
9     \r
10     var template = _.template(templateSrc);\r
11     \r
12     var view = {\r
13         dom: $('<div>' + template({node:null, parents: null}) + '</div>'),\r
14         setup: function() {\r
15             var view = this;\r
16             this.dom.on('mouseenter', 'a', function(e) {\r
17                 var target = $(e.target);\r
18                 sandbox.publish('nodeHighlighted', view.nodes[target.attr('data-id')]);\r
19             });\r
20             this.dom.on('mouseleave', 'a', function(e) {\r
21                 var target = $(e.target);\r
22                 sandbox.publish('nodeDimmed', view.nodes[target.attr('data-id')]);\r
23             });\r
24             this.dom.on('click', 'a', function(e) {\r
25                 e.preventDefault();\r
26                 var target = $(e.target);\r
27                 sandbox.publish('nodeSelected', view.nodes[target.attr('data-id')]);\r
28             });\r
29         },\r
30         \r
31         setNode: function(node) {\r
32             this.dom.empty();\r
33             var nodes = this.nodes = {};\r
34             this.currentNode = node;\r
35             this.nodes[node.getId()] = node;\r
36             var parents = node.parents();\r
37             parents.each(function() {\r
38                 var parent = this;\r
39                 nodes[parent.getId()] = parent;\r
40             });\r
41             this.dom.html(template({node: node, parents: parents}));\r
42         },\r
43         \r
44         highlightNode: function(node) {\r
45             this.dom.find('a[data-id="'+node.id+'"]').addClass('rng-common-hoveredNode');\r
46         },\r
47         dimNode: function(node) {\r
48             this.dom.find('a[data-id="'+node.id+'"]').removeClass('rng-common-hoveredNode');\r
49         }\r
50     };\r
51     \r
52     view.setup();\r
53     \r
54     return {\r
55         start: function() { sandbox.publish('ready'); },\r
56         getView: function() { return view.dom; },\r
57         setNode: function(canvasNode) {\r
58             if(!canvasNode.isSame(view.currentNode)) {\r
59                 view.setNode(canvasNode);\r
60             }\r
61         },\r
62         highlightNode: function(id) { view.highlightNode(id); },\r
63         dimNode: function(id) { view.dimNode(id); }\r
64     };\r
65 };\r
66 \r
67 });