Using === instead of isSameNode (not supported in FF)
[fnpeditor.git] / modules / documentCanvas / canvasManager.js
index fcf2659..a42eee6 100644 (file)
@@ -1,10 +1,24 @@
 define([\r
 'libs/jquery-1.9.1.min',\r
 define([\r
 'libs/jquery-1.9.1.min',\r
-'./wlxmlNode'\r
-], function($, wlxmlNode) {\r
+'./canvasNode'\r
+], function($, canvasNode) {\r
 \r
 'use strict';\r
 \r
 \r
 'use strict';\r
 \r
+var getCursorPosition = function() {\r
+    var selection = window.getSelection();\r
+    var anchorNode = $(selection.anchorNode);\r
+    var parent = anchorNode.parent();\r
+    return {\r
+        textNode: anchorNode,\r
+        textNodeOffset: selection.anchorOffset,\r
+        textNodeIndex: parent.contents().index(anchorNode),\r
+        parentNode: parent,\r
+        focusNode: $(selection.focusNode).parent(),\r
+        isAtEnd: selection.anchorOffset === anchorNode.text().length\r
+    }\r
+};\r
+\r
 var Manager = function(canvas, sandbox) {\r
     this.canvas = canvas;\r
     this.sandbox = sandbox;\r
 var Manager = function(canvas, sandbox) {\r
     this.canvas = canvas;\r
     this.sandbox = sandbox;\r
@@ -20,40 +34,34 @@ var Manager = function(canvas, sandbox) {
 \r
     canvas.dom.on('mouseover', '[wlxml-tag]', function(e) {\r
         e.stopPropagation();\r
 \r
     canvas.dom.on('mouseover', '[wlxml-tag]', function(e) {\r
         e.stopPropagation();\r
-        manager.sandbox.publish('nodeHovered', new wlxmlNode.Node($(e.target)));\r
+        manager.sandbox.publish('nodeHovered', canvasNode.create($(e.target)));\r
     });\r
     canvas.dom.on('mouseout', '[wlxml-tag]', function(e) {\r
         e.stopPropagation();\r
     });\r
     canvas.dom.on('mouseout', '[wlxml-tag]', function(e) {\r
         e.stopPropagation();\r
-        manager.sandbox.publish('nodeBlured', new wlxmlNode.Node($(e.target)));\r
+        manager.sandbox.publish('nodeBlured', canvasNode.create($(e.target)));\r
     });\r
     canvas.dom.on('click', '[wlxml-tag]', function(e) {\r
         e.stopPropagation();\r
         console.log('clicked node type: '+e.target.nodeType);\r
     });\r
     canvas.dom.on('click', '[wlxml-tag]', function(e) {\r
         e.stopPropagation();\r
         console.log('clicked node type: '+e.target.nodeType);\r
-        manager.selectNode(new wlxmlNode.Node($(e.target)));\r
+        manager.selectNode(canvasNode.create($(e.target)));\r
     });\r
 \r
     canvas.dom.on('keyup', '#rng-module-documentCanvas-contentWrapper', function(e) {\r
         var anchor = $(window.getSelection().anchorNode);\r
     });\r
 \r
     canvas.dom.on('keyup', '#rng-module-documentCanvas-contentWrapper', function(e) {\r
         var anchor = $(window.getSelection().anchorNode);\r
+        \r
         if(anchor[0].nodeType === Node.TEXT_NODE)\r
             anchor = anchor.parent();\r
         if(!anchor.is('[wlxml-tag]'))\r
             return;\r
         if(anchor[0].nodeType === Node.TEXT_NODE)\r
             anchor = anchor.parent();\r
         if(!anchor.is('[wlxml-tag]'))\r
             return;\r
-        manager.selectNode(new wlxmlNode.Node(anchor));\r
+        manager.selectNode(canvasNode.create(anchor));\r
     });\r
     \r
     canvas.dom.on('keydown', '#rng-module-documentCanvas-contentWrapper', function(e) {\r
         if(e.which === 13) { \r
     });\r
     \r
     canvas.dom.on('keydown', '#rng-module-documentCanvas-contentWrapper', function(e) {\r
         if(e.which === 13) { \r
-            e.preventDefault();\r
-            //view.insertNewNode(null, null);\r
+            manager.onEnterKey(e);\r
         }\r
         if(e.which === 8) {\r
         }\r
         if(e.which === 8) {\r
-            var anchor = window.getSelection().anchorNode;\r
-            var len = anchor.length;\r
-            console.log(len);\r
-            if(len === 1) {\r
-                e.preventDefault();\r
-                $(anchor).parent().text('');\r
-            }\r
+            manager.onBackspaceKey(e);\r
         }\r
     });\r
               \r
         }\r
     });\r
               \r
@@ -71,29 +79,54 @@ var Manager = function(canvas, sandbox) {
     }\r
 };\r
     \r
     }\r
 };\r
     \r
-Manager.prototype.selectNode = function(wlxmlNode, options) {\r
-        options = options || {};\r
-        var nodeElement = this.getNodeElement(wlxmlNode)\r
-        \r
-        this.dimNode(wlxmlNode);\r
-        \r
-        this.canvas.dom.find('.rng-module-documentCanvas-currentNode').removeClass('rng-module-documentCanvas-currentNode');\r
-        nodeElement.addClass('rng-module-documentCanvas-currentNode');\r
-         \r
-        if(options.movecaret) {\r
-            this.movecaretToNode(nodeElement);\r
-        }\r
-        \r
-        this.currentNode = wlxmlNode;\r
-        this.sandbox.publish('nodeSelected', wlxmlNode);\r
+Manager.prototype.selectNode = function(cnode, options) {\r
+    options = options || {};\r
+    var nodeElement = this.getNodeElement(cnode)\r
+    \r
+    this.dimNode(cnode);\r
+    \r
+    this.canvas.dom.find('.rng-module-documentCanvas-currentNode').removeClass('rng-module-documentCanvas-currentNode');\r
+    nodeElement.addClass('rng-module-documentCanvas-currentNode');\r
+    \r
+    if(options.movecaret) {\r
+        this.movecaretToNode(nodeElement, options.movecaret);\r
+    }\r
+    \r
+    this.currentNode = cnode;\r
+    this.sandbox.publish('nodeSelected', cnode);\r
 };\r
 \r
 };\r
 \r
-Manager.prototype.getNodeElement = function(wlxmlNode) {\r
-    return this.canvas.dom.find('#'+wlxmlNode.id);\r
+Manager.prototype.insertNewNode = function(wlxmlTag, wlxmlClass) {\r
+    var selection = window.getSelection();\r
+\r
+    if(selection.getRangeAt(0).collapsed) {\r
+    \r
+    } else {\r
+        var offsetStart = selection.anchorOffset;\r
+        var offsetEnd = selection.focusOffset;\r
+        if(offsetStart > offsetEnd) {\r
+            var tmp = offsetStart;\r
+            offsetStart = offsetEnd;\r
+            offsetEnd = tmp;\r
+        }\r
+        var wrapper = canvasNode.create({tag: wlxmlTag, klass: wlxmlClass});\r
+        this.canvas.nodeWrap({inside: canvasNode.create($(selection.anchorNode).parent()),\r
+                              _with: wrapper,\r
+                              offsetStart: offsetStart,\r
+                              offsetEnd: offsetEnd\r
+                            });\r
+        this.selectNode(wrapper, {movecaret: 'end'});\r
+    }\r
+    \r
+    \r
+}\r
+\r
+Manager.prototype.getNodeElement = function(cnode) {\r
+    return this.canvas.dom.find('#'+cnode.getId());\r
 };\r
 \r
 };\r
 \r
-Manager.prototype.highlightNode = function(wlxmlNode) {\r
-    var nodeElement = this.getNodeElement(wlxmlNode);\r
+Manager.prototype.highlightNode = function(cnode) {\r
+    var nodeElement = this.getNodeElement(cnode);\r
     if(!this.gridToggled) {\r
         nodeElement.addClass('rng-common-hoveredNode');\r
         var label = nodeElement.attr('wlxml-tag');\r
     if(!this.gridToggled) {\r
         nodeElement.addClass('rng-common-hoveredNode');\r
         var label = nodeElement.attr('wlxml-tag');\r
@@ -104,8 +137,8 @@ Manager.prototype.highlightNode = function(wlxmlNode) {
     }\r
 };\r
 \r
     }\r
 };\r
 \r
-Manager.prototype.dimNode = function(wlxmlNode) {\r
-    var nodeElement = this.getNodeElement(wlxmlNode);\r
+Manager.prototype.dimNode = function(cnode) {\r
+    var nodeElement = this.getNodeElement(cnode);\r
     if(!this.gridToggled) {\r
         nodeElement.removeClass('rng-common-hoveredNode');\r
         nodeElement.find('.rng-module-documentCanvas-hoveredNodeTag').remove();\r
     if(!this.gridToggled) {\r
         nodeElement.removeClass('rng-common-hoveredNode');\r
         nodeElement.find('.rng-module-documentCanvas-hoveredNodeTag').remove();\r
@@ -122,13 +155,19 @@ Manager.prototype.selectFirstNode = function() {
     else {\r
         node = this.canvas.dom.find('[wlxml-class|="p"]')\r
     }\r
     else {\r
         node = this.canvas.dom.find('[wlxml-class|="p"]')\r
     }\r
-    this.selectNode(new wlxmlNode.Node(node), {movecaret: true});\r
+    this.selectNode(canvasNode.create(node), {movecaret: true});\r
 };\r
 \r
 };\r
 \r
-Manager.prototype.movecaretToNode = function(nodeElement) {\r
+Manager.prototype.movecaretToNode = function(nodeElement, where) {\r
+    if(!nodeElement.length)\r
+        return;\r
     var range = document.createRange();\r
     range.selectNodeContents(nodeElement[0]);\r
     var range = document.createRange();\r
     range.selectNodeContents(nodeElement[0]);\r
-    range.collapse(false);\r
+    \r
+    var collapseArg = true;\r
+    if(where === 'end')\r
+        collapseArg = false;\r
+    range.collapse(collapseArg);\r
     var selection = document.getSelection();\r
     selection.removeAllRanges()\r
     selection.addRange(range);\r
     var selection = document.getSelection();\r
     selection.removeAllRanges()\r
     selection.addRange(range);\r
@@ -139,6 +178,62 @@ Manager.prototype.toggleGrid =  function(toggle) {
     this.gridToggled = toggle;\r
 };\r
 \r
     this.gridToggled = toggle;\r
 };\r
 \r
+Manager.prototype.onEnterKey = function(e) {\r
+    e.preventDefault();\r
+    var pos = getCursorPosition();\r
+    var contextNode = this.canvas.getNodeById(pos.parentNode.attr('id'));\r
+    var newNode;\r
+\r
+    if(pos.isAtEnd) {\r
+        newNode = canvasNode.create({tag: pos.parentNode.attr('wlxml-tag'), klass: pos.parentNode.attr('wlxml-class')});\r
+        this.canvas.nodeInsertAfter({node: newNode, after: this.canvas.getNodeById(pos.parentNode.attr('id'))});\r
+    } else {\r
+        newNode = this.canvas.nodeSplit({node: contextNode, textNodeIdx: pos.textNodeIndex, offset: pos.textNodeOffset});\r
+    }\r
+    if(newNode)\r
+        this.selectNode(newNode, {movecaret: true});\r
+    this.sandbox.publish('contentChanged');\r
+};\r
+\r
+Manager.prototype.onBackspaceKey = function(e) {\r
+    var pos = getCursorPosition();\r
+    var len = pos.textNode.text().length;\r
+    if(len === 1) {\r
+        // Prevent deleting node by browser after last character removed;\r
+        e.preventDefault();\r
+        pos.parentNode.text('');\r
+    }\r
+    if(len === 0) {\r
+        e.preventDefault();\r
+        var toRemove = canvasNode.create(pos.textNode);\r
+        var prevNode = this.canvas.getPrecedingNode({node:toRemove});\r
+        this.canvas.nodeRemove({node: toRemove}); // jesli nie ma tekstu, to anchor nie jest tex nodem\r
+        this.selectNode(prevNode, {movecaret: 'end'});\r
+    }\r
+}\r
+\r
+Manager.prototype.command = function(command, meta) {\r
+    var pos = getCursorPosition();\r
+    \r
+    if(command === 'createList') {\r
+        var node = canvasNode.create(pos.parentNode);\r
+        if(window.getSelection().getRangeAt().collapsed && this.canvas.nodeInsideList({node: node})) {\r
+            this.canvas.listRemove({pointer: node});\r
+            this.selectNode(node, {movecaret: 'end'});\r
+            this.sandbox.publish('contentChanged');\r
+        }\r
+        else {\r
+            //if(!this.canvas.nodeInsideList({node: node})) {\r
+                this.canvas.listCreate({start: node, end: canvasNode.create(pos.focusNode), type: meta});\r
+                this.selectNode(node, {movecaret: 'end'});\r
+                this.sandbox.publish('contentChanged');\r
+            //}\r
+        }\r
+    }\r
+\r
+}\r
+\r
+\r
 return Manager;\r
     \r
 });
\ No newline at end of file
 return Manager;\r
     \r
 });
\ No newline at end of file