wip: Experimenting with new approach - dividing visualEditor module
[fnpeditor.git] / modules / nodeFamilyTree / nodeFamilyTree.js
diff --git a/modules/nodeFamilyTree/nodeFamilyTree.js b/modules/nodeFamilyTree/nodeFamilyTree.js
new file mode 100644 (file)
index 0000000..68ab8f0
--- /dev/null
@@ -0,0 +1,62 @@
+define([\r
+'libs/jquery-1.9.1.min',\r
+'libs/underscore-min',\r
+'libs/text!./template.html'\r
+], 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({children: null, parent: null}) + '</div>'),\r
+        setup: function() {\r
+            this.dom.on('click', 'a', function(e) {\r
+                var target = $(e.target);\r
+                sandbox.publish('nodeSelected', target.attr('data-id'));\r
+            });\r
+            \r
+            this.dom.on('mouseenter', 'a', function(e) {\r
+                var target = $(e.target);\r
+                sandbox.publish('nodeEntered', target.attr('data-id'));\r
+            });\r
+            this.dom.on('mouseleave', 'a', function(e) {\r
+                var target = $(e.target);\r
+                sandbox.publish('nodeLeft', target.attr('data-id'));\r
+            });\r
+        },\r
+        setNode: function(node) {\r
+            var parentClass = node.parent().attr('wlxml-class');\r
+            var parent = node.parent('[wlxml-tag]').length ? {\r
+                repr: node.parent().attr('wlxml-tag') + (parentClass ? ' / ' + parentClass : ''),\r
+                id: node.parent().attr('id')\r
+            } : undefined;\r
+            var children = [];\r
+            node.children('[wlxml-tag]').each(function() {\r
+                var child = $(this);\r
+                var childClass = child.attr('wlxml-class');\r
+                children.push({repr: child.attr('wlxml-tag') + (childClass ? ' / ' + childClass : ''), id: child.attr('id')});\r
+            });\r
+            this.dom.empty();\r
+            this.dom.append($(template({parent: parent, children: children})));\r
+        }\r
+    }\r
+    \r
+    view.setup();\r
+    \r
+    return {\r
+        start: function() {\r
+            sandbox.publish('ready');\r
+        },\r
+        setNode: function(node) {\r
+            view.setNode(node);\r
+        },\r
+        getView: function() {\r
+            return view.dom;\r
+        }\r
+    };\r
+};\r
+\r
+});
\ No newline at end of file