canvas api wip: getting previous node
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 21 Jun 2013 12:24:55 +0000 (14:24 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 21 Jun 2013 12:24:55 +0000 (14:24 +0200)
modules/documentCanvas/canvas.js
modules/documentCanvas/tests/canvas.test.js

index 92c02c6..eb0fd0b 100644 (file)
@@ -36,6 +36,14 @@ Canvas.prototype.getNode = function(desc) {
     return toret;\r
 }\r
 \r
     return toret;\r
 }\r
 \r
+Canvas.prototype.getPreviousNode = function(options) {\r
+    var element = $(this.content.find('#' + options.node.id).get(0));\r
+    var prev = element.prev()\r
+    if(prev.length === 0)\r
+        prev = element.parent();\r
+    return new wlxmlNode.Node(prev);\r
+}\r
+\r
 Canvas.prototype._createNode = function(wlxmlTag, wlxmlClass) {\r
             var toBlock = ['div', 'document', 'section', 'header'];\r
             var htmlTag = _.contains(toBlock, wlxmlTag) ? 'div' : 'span';\r
 Canvas.prototype._createNode = function(wlxmlTag, wlxmlClass) {\r
             var toBlock = ['div', 'document', 'section', 'header'];\r
             var htmlTag = _.contains(toBlock, wlxmlTag) ? 'div' : 'span';\r
@@ -49,8 +57,11 @@ Canvas.prototype._createNode = function(wlxmlTag, wlxmlClass) {
 \r
 Canvas.prototype.insertNode = function(options) {\r
     var element = $(this.content.find('#' + options.context.id).get(0));\r
 \r
 Canvas.prototype.insertNode = function(options) {\r
     var element = $(this.content.find('#' + options.context.id).get(0));\r
-    if(options.place == 'after')\r
-        element[options.place](this._createNode(options.tag, options.klass));\r
+    if(options.place == 'after') {\r
+        var node = this._createNode(options.tag, options.klass);\r
+        element[options.place](node);\r
+        return node;\r
+    }\r
     else if(options.place == 'wrapText') {\r
         var elementContents = element.contents();\r
         if(elementContents.length !== 1 || elementContents.get(0).nodeType != 3)\r
     else if(options.place == 'wrapText') {\r
         var elementContents = element.contents();\r
         if(elementContents.length !== 1 || elementContents.get(0).nodeType != 3)\r
index 6ba3f42..57ad625 100644 (file)
@@ -54,6 +54,20 @@ define([
             assert.equal(header.tag, 'header');\r
             assert.equal(header.klass, 'some-class');\r
         });\r
             assert.equal(header.tag, 'header');\r
             assert.equal(header.klass, 'some-class');\r
         });\r
+        \r
+        test('getting previous node', function() {\r
+            var c = new canvas.Canvas('<section><div class="some.class">Div 1</div><div class="some.other.class">Div 2</div></section>');\r
+            var secondDiv = c.getNode({tag: 'div'})[1];\r
+            var firstDiv = c.getPreviousNode({node: secondDiv});\r
+            assert.equal(firstDiv.klass, 'some-class');\r
+        })\r
+        \r
+        test('pervious node of node without "previous siblings" is its parent', function() {\r
+            var c = new canvas.Canvas('<section><div class="some.class">Div 1</div></section>');\r
+            var div = c.getNode({tag: 'div'})[0];\r
+            var section = c.getPreviousNode({node: div});\r
+            assert.equal(section.tag, 'section');\r
+        })\r
     \r
         test('inserting after', function() {\r
             var c = new canvas.Canvas('<section><header>Header 1</header></section>');\r
     \r
         test('inserting after', function() {\r
             var c = new canvas.Canvas('<section><header>Header 1</header></section>');\r
@@ -119,7 +133,7 @@ define([
                         <div class="list.item">a cat</div>\\r
                     </div>\\r
                 </section>');\r
                         <div class="list.item">a cat</div>\\r
                     </div>\\r
                 </section>');\r
-            \r
+\r
         });\r
     });\r
 \r
         });\r
     });\r
 \r