editor: plugins/core - restore selection after editing/deleting link via a link tool box
[fnpeditor.git] / src / editor / modules / nodeFamilyTree / nodeFamilyTree.js
index cb7b57a..bda562f 100644 (file)
@@ -23,21 +23,20 @@ return function(sandbox) {
             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.setElement(view.currentNodeElement);
             }
         }, this);
-    }
+    };
 
     var view = {
         dom: $('<div>' + template({contents: null, parent: null}) + '</div>'),
         setup: function() {
-            var view = this;
             this.dom.on('click', 'a', function(e) {
                 var target = $(e.target);
                 sandbox.publish('nodeClicked', target.data('element'));
@@ -53,55 +52,59 @@ return function(sandbox) {
             });
         },
         setElement: function(element) {
-            element = element || this.currentNodeElement;
-            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;
-            items = [];
+            var contents = [],
+                parent, nodeElementParent;
 
-            if(nodeElementParent) {
-                items.push(nodeElementParent);
-                parent = {
-                    id: items.length - 1,
-                    repr: wlxmlUtils.wlxmlTagNames[nodeElementParent.getTagName()] + (nodeElementParent.getClass() ? ' / ' + wlxmlUtils.wlxmlClassNames[nodeElementParent.getClass()] : '')
-                };
+            if(element) {
+                element = element || this.currentNodeElement;
+                var textElement = element.getText ? element : null,
+                    nodeElement = element.getText ? element.parent() : element, // TODO: better type detection
+                    items;
                 
-            }
-        
-            var nodeContents = nodeElement.contents(),
-                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) + '...';
+                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()) : '')
+                        });
                     }
-                    contents.push({
-                        id: items.length,
-                        repr: _.escape(text), bold: child.sameNode(textElement)
-                    });
-                } else {
-                    contents.push({
-                        id: items.length,
-                        repr: wlxmlUtils.wlxmlTagNames[child.getTagName()] + (child.getClass() ? ' / ' + wlxmlUtils.wlxmlClassNames[child.getClass()] : '')
-                    });
-                }
-                items.push(child);
-            });
+                    items.push(child);
+                });
+            }
             this.dom.empty();
             this.dom.append($(template({parent: parent, contents: contents})));
 
             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) {
                 $(a).data('element', nodeContents[idx]);
@@ -124,11 +127,12 @@ return function(sandbox) {
             sandbox.publish('ready');
         },
         setElement: function(element) {
-            if(!listens) {
+            if(!listens && element) {
                 startListening(element.document);
             }
-            if(!(element.sameNode(view.currentNodeElement)))
+            if(!element || !(element.sameNode(view.currentNodeElement))) {
                 view.setElement(element);
+            }
         },
         getView: function() {
             return view.dom;