refactoring: removing DocumentElement.bound
[fnpeditor.git] / src / wlxml / extensions / list / list.js
index 75b5336..29834ae 100644 (file)
@@ -1,8 +1,9 @@
-define(function() {
+define(function(require) {
 
 'use strict';
 
-var extension = {document: {transformations: {}}, wlxmlClass: {list: {methods: {}}}};
+var _ = require('libs/underscore'),
+    extension = {document: {transformations: {}, methods: {}}, wlxmlClass: {list: {methods: {}}}};
 
 
 extension.wlxmlClass.list.methods = {
@@ -19,31 +20,59 @@ extension.wlxmlClass.list.methods = {
     getItem: function(index) {
         return this.contents('.item')[index];
     }
-}
+};
 
-extension.documentMethods = {
-    areItemsOfSameList: function(params) {
-        return params.node1.parent().sameNode(params.node2.parent()) && params.node2.parent().is('list');
+extension.wlxmlClass.list.transformations = {
+    extractAllItems: function() {
+        var contents = this.contents();
+        return this.document.extractItems({item1: contents[0], item2: _.last(contents)});
     }
-}
+};
 
+extension.wlxmlClass.list.transformations = {
+    extractListItems: function() {
+        var contents = this.contents(),
+            first = contents[0],
+            last;
+        if(contents.length) {
+            last = contents[contents.length-1];
+            this.document.extractItems({
+                item1: first,
+                item2: last
+            });
+        } else {
+            this.detach();
+        }
+    }
+};
 
+extension.document.methods = {
+    areItemsOfSameList: function(params) {
+        return params.node1.parent().sameNode(params.node2.parent()) && params.node2.parent().is('list');
+    }
+};
 
 extension.document.transformations.createList = {
-    impl: function(params) {          
+    impl: function(params) {
+        /* globals Node */
         var parent = params.node1.parent(),
             parentContents = parent.contents(),
-            nodeIndexes = [params.node1.getIndex(), params.node2.getIndex()].sort(),
+            nodeIndexes = [params.node1.getIndex(), params.node2.getIndex()].sort(function(a,b) { return a-b; }),
             nodesToWrap = [],
-            listNode = params.node1.document.createDocumentNode({tagName: 'div', attrs: {'class': 'list'}}),
+            listClass = params.klass || 'list',
+            listNode = params.node1.document.createDocumentNode({tagName: 'div', attrs: {'class': listClass}}),
+            listPlacePtr = params.node1,
             node, i;
-
+        
         for(i = nodeIndexes[0]; i <= nodeIndexes[1]; i++) {
             node = parentContents[i];
             if(node.nodeType === Node.TEXT_NODE) {
-                node = node.wrapWith({tagName: 'div', attrs: {'class': 'item'}}); //t
+                node = node.wrapWith({tagName: 'div', attrs: {'class': 'item'}});
+                if(i === nodeIndexes[0]) {
+                    listPlacePtr = node;
+                }
             } else {
-                node.setClass('item'); //t
+                node.setClass('item');
             }
             nodesToWrap.push(node);
         }
@@ -51,24 +80,25 @@ extension.document.transformations.createList = {
         var toInsert;
         if(parent.is('list') && parent.object.itemIndex(nodesToWrap[0]) > 0) { // object api
             // var prevItem = parent.object.getItem(parent.object.itemIndex(nodesToWrap[0])-1); // object api
-            // prevItem.append(listNode); //t
+            // prevItem.append(listNode);
             toInsert = listNode.wrapWith({tagName: 'div', attrs: {'class': 'item'}});
         } else {
-            //nodesToWrap[0].before(listNode); //t
+            //nodesToWrap[0].before(listNode);
             toInsert = listNode;
-        }  
+        }
 
-        params.node1.before(toInsert);
+        listPlacePtr.before(toInsert);
 
         nodesToWrap.forEach(function(node) {
-            listNode.append(node); //t
+            listNode.append(node);
         });
+        return listNode;
     },
     getChangeRoot: function() {
-        return this.args.node1.parent();
+        return this.args[0].node1.parent();
     },
     isAllowed: function() {
-        return this.args.node1.parent().sameNode(this.args.node2.parent());
+        return this.args[0].node1.parent().sameNode(this.args[0].node2.parent());
     }
 };
 
@@ -76,13 +106,13 @@ extension.document.transformations.extractItems = {
     impl: function(params) {
         params = _.extend({}, {merge: true}, params);
         var list = params.item1.parent(),
-            indexes = [params.item1.getIndex(), params.item2.getIndex()].sort(),
+            indexes = [params.item1.getIndex(), params.item2.getIndex()].sort(function(a,b) { return a-b;}),
             precedingItems = [],
             extractedItems = [],
             succeedingItems = [],
-            items = list.contents(), // lub list.object.items()
-            listIsNested = list.parent().is('item'),
-            i;
+            items = list.contents(),
+            listIsNested = list.parent().is('item');
+
         
         items.forEach(function(item, idx) {
             if(idx < indexes[0]) {
@@ -100,29 +130,31 @@ extension.document.transformations.extractItems = {
         if(succeedingItems.length === 0) {
             var reference_orig = reference;
             extractedItems.forEach(function(item) {
-                reference.after(item); //t
+                reference.after(item);
                 reference = item;
                 if(!listIsNested) {
-                    item.setClass(null); //t
+                    item.setClass('');
                 }
             });
-            if(precedingItems.length === 0)
-                reference_orig.detach(); //t
+            if(precedingItems.length === 0) {
+                reference_orig.detach();
+            }
         } else if(precedingItems.length === 0) {
             extractedItems.forEach(function(item) {
-                reference.before(item); //t
+                reference.before(item);
                 if(!listIsNested) {
-                    item.setClass(null); //t
+                    item.setClass('');
                 }
             });
         } else {
             extractedItems.forEach(function(item) {
-                reference.after(item); //t
-                if(!listIsNested)
-                    item.setClass(null); //t
+                reference.after(item);
+                if(!listIsNested) {
+                    item.setClass('');
+                }
                 reference = item;
             });
-            var secondList = params.item1.document.createDocumentNode({tagName: 'div', attrs: {'class':'list'}}),
+            var secondList = params.item1.document.createDocumentNode({tagName: 'div', attrs: {'class': list.getClass() || 'list'}}),
                 toAdd = secondList;
             
             if(listIsNested) {
@@ -135,13 +167,13 @@ extension.document.transformations.extractItems = {
             reference.after(toAdd);
         }
         if(!params.merge && listIsNested) {
-            return this.transform('extractItems', {item1: extractedItems[0], item2: extractedItems[extractedItems.length-1]});
+            return this.extractItems({item1: extractedItems[0], item2: extractedItems[extractedItems.length-1]});
         }
         return true;
     },
     isAllowed: function() {
-        var parent = this.args.nodel1.parent();
-        return parent.is('list') && parent.sameNode(this.args.node2.parent());
+        var parent = this.args[0].nodel1.parent();
+        return parent.is('list') && parent.sameNode(this.args[0].node2.parent());
     }
 };