Adding jshint grunt task
[fnpeditor.git] / modules / documentCanvas / canvasManager.js
index 6a0fc5c..201394e 100644 (file)
@@ -1,7 +1,7 @@
 define([\r
 'libs/jquery-1.9.1.min',\r
-'./wlxmlNode'\r
-], function($, wlxmlNode) {\r
+'./canvasNode'\r
+], function($, canvasNode) {\r
 \r
 'use strict';\r
 \r
@@ -14,6 +14,7 @@ var getCursorPosition = function() {
         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
@@ -33,16 +34,16 @@ var Manager = function(canvas, sandbox) {
 \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
-        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
-        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
@@ -52,7 +53,7 @@ var Manager = function(canvas, sandbox) {
             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
@@ -78,11 +79,11 @@ var Manager = function(canvas, sandbox) {
     }\r
 };\r
     \r
-Manager.prototype.selectNode = function(wlxmlNode, options) {\r
+Manager.prototype.selectNode = function(cnode, options) {\r
     options = options || {};\r
-    var nodeElement = this.getNodeElement(wlxmlNode)\r
+    var nodeElement = this.getNodeElement(cnode)\r
     \r
-    this.dimNode(wlxmlNode);\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
@@ -91,16 +92,57 @@ Manager.prototype.selectNode = function(wlxmlNode, options) {
         this.movecaretToNode(nodeElement, options.movecaret);\r
     }\r
     \r
-    this.currentNode = wlxmlNode;\r
-    this.sandbox.publish('nodeSelected', wlxmlNode);\r
+    this.currentNode = cnode;\r
+    this.sandbox.publish('nodeSelected', cnode);\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
+        $anchorNode = $(selection.anchorNode),\r
+        $focusNode = $(selection.focusNode);\r
+        \r
+        \r
+    if(!selection.isCollapsed && $anchorNode.parent()[0] === $focusNode.parent()[0]) {\r
+        var textNodeIdx,\r
+            parent = $anchorNode.parent(),\r
+            parentContents = parent.contents(),\r
+            offsetStart = selection.anchorOffset,\r
+            offsetEnd = selection.focusOffset;\r
+        \r
+        if(selection.anchorNode === selection.focusNode) {\r
+            if(offsetStart > offsetEnd) {\r
+                var tmp = offsetStart;\r
+                offsetStart = offsetEnd;\r
+                offsetEnd = tmp;\r
+            }\r
+            textNodeIdx = parentContents.index($anchorNode);\r
+        } else {\r
+            if(parentContents.index($anchorNode) > parentContents.index($focusNode)) {\r
+                offsetStart = selection.focusOffset;\r
+                offsetEnd = selection.anchorOffset;\r
+            }\r
+            textNodeIdx = [parentContents.index($anchorNode), parentContents.index($focusNode)];\r
+        }\r
+        \r
+        var wrapper = canvasNode.create({tag: wlxmlTag, klass: wlxmlClass});\r
+        this.canvas.nodeWrap({inside: canvasNode.create(parent),\r
+                              _with: wrapper,\r
+                              offsetStart: offsetStart,\r
+                              offsetEnd: offsetEnd,\r
+                              textNodeIdx: textNodeIdx\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
-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
@@ -111,8 +153,8 @@ Manager.prototype.highlightNode = function(wlxmlNode) {
     }\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
@@ -129,10 +171,12 @@ Manager.prototype.selectFirstNode = function() {
     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
 Manager.prototype.movecaretToNode = function(nodeElement, where) {\r
+    if(!nodeElement.length)\r
+        return;\r
     var range = document.createRange();\r
     range.selectNodeContents(nodeElement[0]);\r
     \r
@@ -153,14 +197,17 @@ Manager.prototype.toggleGrid =  function(toggle) {
 Manager.prototype.onEnterKey = function(e) {\r
     e.preventDefault();\r
     var pos = getCursorPosition();\r
-    var insertedNode;\r
+    var contextNode = this.canvas.getNodeById(pos.parentNode.attr('id'));\r
+    var newNode;\r
+\r
     if(pos.isAtEnd) {\r
-        insertedNode = this.canvas.insertNode({place: 'after', context: {id: pos.parentNode.attr('id')}, tag: pos.parentNode.attr('wlxml-tag'), klass: pos.parentNode.attr('wlxml-class')});\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
-        insertedNode = this.canvas.splitNode({node: {id: pos.parentNode.attr('id')}, textNodeIdx: pos.textNodeIndex, offset: pos.textNodeOffset});\r
+        newNode = this.canvas.nodeSplit({node: contextNode, textNodeIdx: pos.textNodeIndex, offset: pos.textNodeOffset});\r
     }\r
-    if(insertedNode.length)\r
-        this.selectNode(new wlxmlNode.Node(insertedNode), {movecaret: true});\r
+    if(newNode)\r
+        this.selectNode(newNode, {movecaret: true});\r
     this.sandbox.publish('contentChanged');\r
 };\r
 \r
@@ -174,13 +221,33 @@ Manager.prototype.onBackspaceKey = function(e) {
     }\r
     if(len === 0) {\r
         e.preventDefault();\r
-        var toRemove = new wlxmlNode.Node(pos.textNode);\r
-        var prevNode = this.canvas.getPreviousNode({node:toRemove});\r
-        this.canvas.removeNode({node: toRemove}); // jesli nie ma tekstu, to anchor nie jest tex nodem\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(0).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