creating a list
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 9 Jul 2013 07:32:57 +0000 (09:32 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 9 Jul 2013 07:32:57 +0000 (09:32 +0200)
modules/documentCanvas/canvas/canvas.js
modules/documentCanvas/canvas/canvas.test3.js
modules/documentCanvas/canvas/documentElement.js

index 4326655..3ae6a3e 100644 (file)
@@ -86,8 +86,60 @@ $.extend(Canvas.prototype, {
         if(suffixOutside.length > 0)
             wrapperElement.after({text: suffixOutside});
         return wrapperElement;
-    }
+    },
+    list: {}
+});
 
+$.extend(Canvas.prototype.list, {
+    create: function(params) {
+        if(!(params.element1.parent().sameNode(params.element2.parent())))
+            return false;
+            
+        var parent = params.element1.parent();
+        
+        if(parent.childIndex(params.element1) > parent.childIndex(params.element2)) {
+            var tmp = params.element1;
+            params.element1 = params.element2;
+            params.element2 = tmp;
+        }
+        
+        var elementsToWrap = [];
+        
+        var place = 'before';
+        var canvas = this;
+        parent.children().forEach(function(element) {
+            if(element.sameNode(params.element1))
+                place = 'inside';
+            if(place === 'inside') {
+                if(element instanceof documentElement.DocumentTextElement) {
+                    element = element.wrapWithNodeElement({tag: 'div', klass: 'list.item'});
+                    if(element.children()[0].sameNode(params.element1))
+                        params.element1 = element;
+                }
+                element.setWlxmlClass('item');
+                elementsToWrap.push(element);
+            }
+            if(element.sameNode(params.element2))
+                return false;
+        });
+        
+        var listElement = documentElement.DocumentNodeElement.create({tag: 'div', klass: 'list-items' + (params.type === 'enum' ? '-enum' : '')});
+        
+        var toret;
+        if(parent.is('list')) {
+            listElement.wrap({tag: 'div', klass: 'item'});
+            toret = listElement.parent();
+        } else {
+            toret = listElement;
+        }  
+        
+        params.element1.before(toret);
+        
+        elementsToWrap.forEach(function(element) {
+            element.detach();
+            listElement.append(element);
+        });
+    }
 });
 
 return {
index dc29f5d..56f8ee6 100644 (file)
@@ -350,6 +350,29 @@ describe('Canvas', function() {
             });
         });
 
+        describe('Lists api', function() {
+            it('allows creation of a list from existing sibling DocumentElements', function() {
+                var c = canvas.fromXML('\
+                    <section>\
+                        Alice\
+                        <div>has</div>\
+                        a\
+                        <div>cat</div>\
+                    </section>'),
+                    section = c.doc(),
+                    textAlice = section.children()[0],
+                    divCat = section.children()[3]
+                
+                c.list.create({element1: textAlice, element2: divCat});
+
+                expect(section.children().length).to.equal(1, 'section has one child element');
+
+                var list = section.children()[0];
+                expect(list.is('list')).to.equal(true, 'section\'s only child is a list');
+                expect(list.children().length).to.equal(4, 'list contains four elements');
+            });
+        });
+
     });
 });
 
index d82f8e4..da9b148 100644 (file)
@@ -1,6 +1,7 @@
 define([
-'libs/jquery-1.9.1.min'
-], function($) {
+'libs/jquery-1.9.1.min',
+'libs/underscore-min'
+], function($, _) {
     
 'use strict';
 
@@ -49,7 +50,7 @@ $.extend(DocumentElement.prototype, {
     },
 
     wrapWithNodeElement: function(wlxmlNode) {
-        this.$element.wrap($('<' + wlxmlNode.tag + ' class="' + wlxmlNode.klass + '"">')[0]);
+        this.$element.wrap($('<' + wlxmlNode.tag + ' class="' + wlxmlNode.klass.replace('.', '-') + '">')[0]);
         return documentElementFromHTMLElement(this.$element.parent().get(0), this.canvas);
     },
 
@@ -104,6 +105,14 @@ $.extend(DocumentNodeElement.prototype, {
     },
     after: function(params) {
         manipulate(this, params, 'after');
+    },
+    setWlxmlClass: function(klass) {
+        this.$element.attr('class', klass);
+    },
+    is: function(what) {
+        if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.$element.attr('class')))
+            return true;
+        return false;
     }
 });