wlxml: ElementNode.getClassHierarchy
[fnpeditor.git] / src / editor / modules / nodeFamilyTree / nodeFamilyTree.js
index 45f47c0..bda562f 100644 (file)
@@ -9,74 +9,114 @@ define([
 
 return function(sandbox) {
     
 
 return function(sandbox) {
     
-    var template = _.template(templateSrc);
+    var template = _.template(templateSrc),
+        listens = false,
+        items = [];
+
+    var getItemId = function(item) {
+        var idx = -1;
+        var found = _.find(items, function(i) {
+            idx += 1;
+            return item.sameNode(i);
+        });
+        if(found) {
+            return idx;
+        }
+        return -1;
+    };
     
     
+    var startListening = function(document) {
+        listens = true;
+        document.on('change', function(event) {
+            if(event.type === 'nodeTextChange' && event.meta.node.parent().sameNode(view.currentNodeElement)) {
+                view.setElement(view.currentNodeElement);
+            }
+        }, this);
+    };
+
     var view = {
     var view = {
-        dom: $('<div>' + template({children: null, parent: null}) + '</div>'),
+        dom: $('<div>' + template({contents: null, parent: null}) + '</div>'),
         setup: function() {
         setup: function() {
-            var view = this;
             this.dom.on('click', 'a', function(e) {
                 var target = $(e.target);
             this.dom.on('click', 'a', function(e) {
                 var target = $(e.target);
-                sandbox.publish('elementClicked', target.data('element'));
+                sandbox.publish('nodeClicked', target.data('element'));
             });
             
             this.dom.on('mouseenter', 'a', function(e) {
                 var target = $(e.target);
             });
             
             this.dom.on('mouseenter', 'a', function(e) {
                 var target = $(e.target);
-                sandbox.publish('elementEntered', target.data('element'));
+                sandbox.publish('nodeEntered', target.data('element'));
             });
             this.dom.on('mouseleave', 'a', function(e) {
                 var target = $(e.target);
             });
             this.dom.on('mouseleave', 'a', function(e) {
                 var target = $(e.target);
-                sandbox.publish('elementLeft', target.data('element'));
+                sandbox.publish('nodeLeft', target.data('element'));
             });
         },
         setElement: function(element) {
             });
         },
         setElement: function(element) {
-            console.log('familyTree sets node');
-            var textElement = element.getText ? element : null,
-                nodeElement = element.getText ? element.parent() : element, // TODO: better type detection
-                nodeElementParent = nodeElement.parent(),
-                parent;
-            
-            this.currentNodeElement = nodeElement;
+            var contents = [],
+                parent, nodeElementParent;
 
 
-            if(nodeElementParent) {
-                parent = {
-                    repr: wlxmlUtils.wlxmlTagNames[nodeElementParent.getWlxmlTag()] + (nodeElementParent.getWlxmlClass() ? ' / ' + wlxmlUtils.wlxmlClassNames[nodeElementParent.getWlxmlClass()] : '')
-                };
-            }
-        
-            var nodeChildren = nodeElement.children(),
-                children = [];
-            nodeChildren.forEach(function(child) {
-                if(child.getText) {
-                    var text = child.getText();
-                    if(!text)
-                        text = '<pusty tekst>';
-                    else {
-                        if(text.length > 13) {
-                            text = text.substr(0,13) + '...';
+            if(element) {
+                element = element || this.currentNodeElement;
+                var textElement = element.getText ? element : null,
+                    nodeElement = element.getText ? element.parent() : element, // TODO: better type detection
+                    items;
+                
+                this.currentNodeElement = nodeElement;
+                items = [];
+                nodeElementParent = nodeElement.parent();
+
+                if(nodeElementParent) {
+                    items.push(nodeElementParent);
+                    parent = {
+                        id: items.length - 1,
+                        repr: wlxmlUtils.getTagLabel(nodeElementParent.getTagName()) + (nodeElementParent.getClass() ? ' / ' + wlxmlUtils.getClassLabel(nodeElementParent.getClass()) : '')
+                    };
+                    
+                }
+            
+                var nodeContents = nodeElement.contents();
+                nodeContents.forEach(function(child) {
+                    if(child.getText) {
+                        var text = child.getText();
+                        if(!text) {
+                            text = '<pusty tekst>';
+                        }
+                        else {
+                            if(text.length > 13) {
+                                text = text.substr(0,13) + '...';
+                            }
+                            text = '"' + text + '"';
                         }
                         }
-                        text = '"' + text + '"';
+                        contents.push({
+                            id: items.length,
+                            repr: _.escape(text), bold: child.sameNode(textElement)
+                        });
+                    } else {
+                        contents.push({
+                            id: items.length,
+                            repr: wlxmlUtils.getTagLabel(child.getTagName()) + (child.getClass() ? ' / ' + wlxmlUtils.getClassLabel(child.getClass()) : '')
+                        });
                     }
                     }
-                    children.push({repr: _.escape(text), bold: child.sameNode(textElement)});
-                } else {
-                    children.push({repr: wlxmlUtils.wlxmlTagNames[child.getWlxmlTag()] + (child.getWlxmlClass() ? ' / ' + wlxmlUtils.wlxmlClassNames[child.getWlxmlClass()] : '')});
-                }
-            });
+                    items.push(child);
+                });
+            }
             this.dom.empty();
             this.dom.empty();
-            this.dom.append($(template({parent: parent, children: children})));
+            this.dom.append($(template({parent: parent, contents: contents})));
 
             if(parent) {
 
             if(parent) {
-                this.dom.find('.rng-module-nodeFamilyTree-parent').data('element', nodeElementParent)
+                this.dom.find('.rng-module-nodeFamilyTree-parent').data('element', nodeElementParent);
             }
             this.dom.find('li a').each(function(idx, a) {
             }
             this.dom.find('li a').each(function(idx, a) {
-                $(a).data('element', nodeChildren[idx]);
+                $(a).data('element', nodeContents[idx]);
             });
         },
         highlightNode: function(canvasNode) {
             });
         },
         highlightNode: function(canvasNode) {
-            this.dom.find('a[data-id="'+canvasNode.getId()+'"]').addClass('rng-common-hoveredNode');
+            var id = getItemId(canvasNode);
+            this.dom.find('a[rng-module-nodeFamilyTree-item-id="'+id+'"]').addClass('rng-common-hoveredNode');
         },
         dimNode: function(canvasNode) {
         },
         dimNode: function(canvasNode) {
-            this.dom.find('a[data-id="'+canvasNode.getId()+'"]').removeClass('rng-common-hoveredNode');
+            var id = getItemId(canvasNode);
+            this.dom.find('a[rng-module-nodeFamilyTree-item-id="'+id+'"]').removeClass('rng-common-hoveredNode');
         }
     };
     
         }
     };
     
@@ -87,16 +127,20 @@ return function(sandbox) {
             sandbox.publish('ready');
         },
         setElement: function(element) {
             sandbox.publish('ready');
         },
         setElement: function(element) {
-            if(!(element.sameNode(view.currentNodeElement)))
+            if(!listens && element) {
+                startListening(element.document);
+            }
+            if(!element || !(element.sameNode(view.currentNodeElement))) {
                 view.setElement(element);
                 view.setElement(element);
+            }
         },
         getView: function() {
             return view.dom;
         },
         },
         getView: function() {
             return view.dom;
         },
-        highlightNode: function(canvasNode) {
+        highlightElement: function(canvasNode) {
             view.highlightNode(canvasNode);
         },
             view.highlightNode(canvasNode);
         },
-        dimNode: function(canvasNode) {
+        dimElement: function(canvasNode) {
             view.dimNode(canvasNode);
         }
     };
             view.dimNode(canvasNode);
         }
     };