Visual editor - inserting node before the current one if anchorOffset is 0
[fnpeditor.git] / modules / visualEditor.js
index dfa8335..012f7a9 100644 (file)
@@ -98,6 +98,7 @@ rng.modules.visualEditor = function(sandbox) {
         insertNewNode: function(wlxmlTag, wlxmlClass) {\r
             //TODO: Insert inline\r
             var anchor = $(window.getSelection().anchorNode);\r
+            var anchorOffset = window.getSelection().anchorOffset;\r
             if(anchor[0].nodeType === Node.TEXT_NODE)\r
                 anchor = anchor.parent();\r
             if(anchor.text() === '') {\r
@@ -106,7 +107,10 @@ rng.modules.visualEditor = function(sandbox) {
                 todel.remove();\r
             }\r
             var newNode = this._createNode(wlxmlTag || anchor.attr('wlxml-tag'), wlxmlClass || anchor.attr('wlxml-class'));\r
-            anchor.after(newNode);\r
+            if(anchorOffset === 0)\r
+                anchor.before(newNode)\r
+            else\r
+                anchor.after(newNode);\r
             mediator.nodeCreated(newNode);\r
             isDirty = true;\r
         },\r
@@ -283,10 +287,10 @@ rng.modules.visualEditor = function(sandbox) {
             var pane = this.node.find('#rng-visualEditor-edit');\r
             pane.html( $(sandbox.getTemplate('editPane')({tag: node.attr('wlxml-tag'), klass: node.attr('wlxml-class')})));\r
             \r
-            var parent = {\r
+            var parent = node.parent('[wlxml-tag]').length ? {\r
                 repr: node.parent().attr('wlxml-tag') + ' / ' + (node.parent().attr('wlxml-class') || '[[no class]]'),\r
                 id: node.parent().attr('id')\r
-            }\r
+            } : undefined;\r
             var children = [];\r
             node.children('[wlxml-tag]').each(function() {\r
                 var child = $(this);\r
@@ -326,9 +330,42 @@ rng.modules.visualEditor = function(sandbox) {
         }\r
     }\r
     \r
+    var statusBarView = {\r
+        node: view.node.find('#rng-visualEditor-statusbar'),\r
+        setup: function() {\r
+            var view = this;\r
+            view.node.on('mouseenter', 'a', function(e) {\r
+                var target = $(e.target);\r
+                mediator.nodeHighlightedById(target.attr('data-id')); \r
+            });\r
+            view.node.on('mouseleave', 'a', function(e) {\r
+                var target = $(e.target);\r
+                mediator.nodeDimmedById(target.attr('data-id')); \r
+            });\r
+            view.node.on('click', 'a', function(e) {\r
+                e.preventDefault();\r
+                mediator.nodeSelectedById($(e.target).attr('data-id'));\r
+            });\r
+        },\r
+        \r
+        showNode: function(node) {\r
+            this.node.empty();\r
+            this.node.html(sandbox.getTemplate('statusBarNodeDisplay')({node: node, parents: node.parents('[wlxml-tag]')}));\r
+            //node.parents('[wlxml-tag]')\r
+        },\r
+        \r
+        highlightNode: function(id) {\r
+            this.node.find('a[data-id="'+id+'"]').addClass('rng-hover');\r
+        },\r
+        dimNode: function(id) {\r
+            this.node.find('a[data-id="' +id+'"]').removeClass('rng-hover');\r
+        }\r
+    }\r
+    \r
     view.setup();\r
     sideBarView.setup();\r
     toolbarView.setup();\r
+    statusBarView.setup();\r
     \r
     var mediator = {\r
         getCurrentNode: function() {\r
@@ -339,6 +376,7 @@ rng.modules.visualEditor = function(sandbox) {
         },\r
         nodeSelected: function(node) {\r
             sideBarView.updateEditPane(node);\r
+            statusBarView.showNode(node);\r
         },\r
         nodeSelectedById: function(id) {\r
             view.selectNodeById(id);\r
@@ -369,10 +407,12 @@ rng.modules.visualEditor = function(sandbox) {
         nodeHovered: function(node) {\r
             view.highlightNode(node);\r
             sideBarView.highlightNode(node.attr('id'));\r
+            statusBarView.highlightNode(node.attr('id'));\r
         },\r
         nodeBlured: function(node) {\r
             view.dimNode(node);\r
             sideBarView.dimNode(node.attr('id'));\r
+            statusBarView.dimNode(node.attr('id'));\r
         },\r
         wrapWithNodeRequest: function(wlxmlTag, wlxmlClass) {\r
             view.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);\r