cefe7b40d56c5e4e8f35fa57d8821f137a248250
[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.nodes[node.id] = node;\r
35             var parents = node.parents();\r
36             parents.each(function() {\r
37                 var parent = this;\r
38                 nodes[parent.id] = parent;\r
39             });\r
40             this.dom.html(template({node: node, parents: parents}));\r
41         },\r
42         \r
43         highlightNode: function(node) {\r
44             this.dom.find('a[data-id="'+node.id+'"]').addClass('rng-common-hoveredNode');\r
45         },\r
46         dimNode: function(node) {\r
47             this.dom.find('a[data-id="'+node.id+'"]').removeClass('rng-common-hoveredNode');\r
48         }\r
49     }\r
50     \r
51     view.setup();\r
52     \r
53     return {\r
54         start: function() { sandbox.publish('ready'); },\r
55         getView: function() { return view.dom; },\r
56         setNode: function(node) { view.setNode(node); },\r
57         highlightNode: function(id) { view.highlightNode(id); },\r
58         dimNode: function(id) { view.dimNode(id); }\r
59     }\r
60 }\r
61 \r
62 });