refactoring
[fnpeditor.git] / modules / nodeBreadCrumbs / nodeBreadCrumbs.js
index 4a787f4..e9fc107 100644 (file)
@@ -1,54 +1,67 @@
-define([\r
-'libs/jquery-1.9.1.min',\r
-'libs/underscore-min',\r
-'libs/text!./template.html'], function($, _, templateSrc) {\r
-\r
-'use strict';\r
-\r
-return function(sandbox) {\r
-    \r
-    var template = _.template(templateSrc);\r
-    \r
-    var view = {\r
-        dom: $('<div>' + template({node:null, parents: null}) + '</div>'),\r
-        setup: function() {\r
-            this.dom.on('mouseenter', 'a', function(e) {\r
-                var target = $(e.target);\r
-                sandbox.publish('nodeHighlighted', target.attr('data-id'));\r
-            });\r
-            this.dom.on('mouseleave', 'a', function(e) {\r
-                var target = $(e.target);\r
-                sandbox.publish('nodeDimmed', target.attr('data-id'));\r
-            });\r
-            this.dom.on('click', 'a', function(e) {\r
-                e.preventDefault();\r
-                var target = $(e.target);\r
-                sandbox.publish('nodeSelected', target.attr('data-id'));\r
-            });\r
-        },\r
-        \r
-        setNode: function(node) {\r
-            this.dom.empty();\r
-            this.dom.html(template({node: node, parents: node.parents('[wlxml-tag]')}));\r
-        },\r
-        \r
-        highlightNode: function(id) {\r
-            this.dom.find('a[data-id="'+id+'"]').addClass('rng-common-hoveredNode');\r
-        },\r
-        dimNode: function(id) {\r
-            this.dom.find('a[data-id="' +id+'"]').removeClass('rng-common-hoveredNode');\r
-        }\r
-    }\r
-    \r
-    view.setup();\r
-    \r
-    return {\r
-        start: function() { sandbox.publish('ready'); },\r
-        getView: function() { return view.dom; },\r
-        setNode: function(node) { view.setNode(node); },\r
-        highlightNode: function(id) { view.highlightNode(id); },\r
-        dimNode: function(id) { view.dimNode(id); }\r
-    }\r
-}\r
-\r
+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: $('<div>' + template({node:null, parents: null}) + '</div>'),
+        setup: function() {
+            var view = this;
+            this.dom.on('mouseenter', 'a', function(e) {
+                var target = $(e.target);
+                sandbox.publish('nodeHighlighted', view.nodes[target.attr('data-id')]);
+            });
+            this.dom.on('mouseleave', 'a', function(e) {
+                var target = $(e.target);
+                sandbox.publish('nodeDimmed', view.nodes[target.attr('data-id')]);
+            });
+            this.dom.on('click', 'a', function(e) {
+                e.preventDefault();
+                var target = $(e.target);
+                sandbox.publish('nodeSelected', view.nodes[target.attr('data-id')]);
+            });
+        },
+        
+        setNode: function(node) {
+            this.dom.empty();
+            var nodes = this.nodes = {};
+            this.currentNode = node;
+            this.nodes[node.getId()] = node;
+            var parents = node.parents();
+            parents.each(function() {
+                var parent = this;
+                nodes[parent.getId()] = parent;
+            });
+            this.dom.html(template({node: node, parents: parents}));
+        },
+        
+        highlightNode: function(node) {
+            this.dom.find('a[data-id="'+node.id+'"]').addClass('rng-common-hoveredNode');
+        },
+        dimNode: function(node) {
+            this.dom.find('a[data-id="'+node.id+'"]').removeClass('rng-common-hoveredNode');
+        }
+    };
+    
+    view.setup();
+    
+    return {
+        start: function() { sandbox.publish('ready'); },
+        getView: function() { return view.dom; },
+        setNode: function(canvasNode) {
+            if(!canvasNode.isSame(view.currentNode)) {
+                view.setNode(canvasNode);
+            }
+        },
+        highlightNode: function(id) { view.highlightNode(id); },
+        dimNode: function(id) { view.dimNode(id); }
+    };
+};
+
 });
\ No newline at end of file