2a996ac5f01d045549fbea1c075090741b31f0e1
[fnpeditor.git] / src / wlxml / extensions / list / list.js
1 define(function(require) {
2
3 'use strict';
4
5 var _ = require('libs/underscore'),
6     extension = {document: {transformations: {}, methods: {}}, wlxmlClass: {list: {methods: {}}}};
7
8
9 extension.wlxmlClass.list.methods = {
10     itemIndex: function(listItem) {
11         var toret = -1;
12         this.contents('.item').some(function(item, index) {
13             if(item.sameNode(listItem)) {
14                 toret = index;
15                 return true; // break
16             }
17         });
18         return toret;
19     },
20     getItem: function(index) {
21         return this.contents('.item')[index];
22     }
23 };
24
25 extension.wlxmlClass.list.transformations = {
26     extractAllItems: function() {
27         var contents = this.contents();
28         return this.document.extractItems({item1: contents[0], item2: _.last(contents)});
29     }
30 };
31
32 extension.document.methods = {
33     areItemsOfSameList: function(params) {
34         return params.node1.parent().sameNode(params.node2.parent()) && params.node2.parent().is('list');
35     }
36 };
37
38
39
40 extension.document.transformations.createList = {
41     impl: function(params) {
42         /* globals Node */
43         var parent = params.node1.parent(),
44             parentContents = parent.contents(),
45             nodeIndexes = [params.node1.getIndex(), params.node2.getIndex()].sort(function(a,b) { return a-b; }),
46             nodesToWrap = [],
47             listClass = params.klass || 'list',
48             listNode = params.node1.document.createDocumentNode({tagName: 'div', attrs: {'class': listClass}}),
49             listPlacePtr = params.node1,
50             node, i;
51         
52         for(i = nodeIndexes[0]; i <= nodeIndexes[1]; i++) {
53             node = parentContents[i];
54             if(node.nodeType === Node.TEXT_NODE) {
55                 node = node.wrapWith({tagName: 'div', attrs: {'class': 'item'}});
56                 if(i === nodeIndexes[0]) {
57                     listPlacePtr = node;
58                 }
59             } else {
60                 node.setClass('item');
61             }
62             nodesToWrap.push(node);
63         }
64
65         var toInsert;
66         if(parent.is('list') && parent.object.itemIndex(nodesToWrap[0]) > 0) { // object api
67             // var prevItem = parent.object.getItem(parent.object.itemIndex(nodesToWrap[0])-1); // object api
68             // prevItem.append(listNode);
69             toInsert = listNode.wrapWith({tagName: 'div', attrs: {'class': 'item'}});
70         } else {
71             //nodesToWrap[0].before(listNode);
72             toInsert = listNode;
73         }
74
75         listPlacePtr.before(toInsert);
76
77         nodesToWrap.forEach(function(node) {
78             listNode.append(node);
79         });
80     },
81     getChangeRoot: function() {
82         return this.args[0].node1.parent();
83     },
84     isAllowed: function() {
85         return this.args[0].node1.parent().sameNode(this.args[0].node2.parent());
86     }
87 };
88
89 extension.document.transformations.extractItems = {
90     impl: function(params) {
91         params = _.extend({}, {merge: true}, params);
92         var list = params.item1.parent(),
93             indexes = [params.item1.getIndex(), params.item2.getIndex()].sort(function(a,b) { return a-b;}),
94             precedingItems = [],
95             extractedItems = [],
96             succeedingItems = [],
97             items = list.contents(),
98             listIsNested = list.parent().is('item');
99
100         
101         items.forEach(function(item, idx) {
102             if(idx < indexes[0]) {
103                 precedingItems.push(item);
104             }
105             else if(idx >= indexes[0] && idx <= indexes[1]) {
106                 extractedItems.push(item);
107             }
108             else {
109                 succeedingItems.push(item);
110             }
111         });
112
113         var reference = listIsNested ? list.parent() : list;
114         if(succeedingItems.length === 0) {
115             var reference_orig = reference;
116             extractedItems.forEach(function(item) {
117                 reference.after(item);
118                 reference = item;
119                 if(!listIsNested) {
120                     item.setClass('');
121                 }
122             });
123             if(precedingItems.length === 0) {
124                 reference_orig.detach();
125             }
126         } else if(precedingItems.length === 0) {
127             extractedItems.forEach(function(item) {
128                 reference.before(item);
129                 if(!listIsNested) {
130                     item.setClass('');
131                 }
132             });
133         } else {
134             extractedItems.forEach(function(item) {
135                 reference.after(item);
136                 if(!listIsNested) {
137                     item.setClass('');
138                 }
139                 reference = item;
140             });
141             var secondList = params.item1.document.createDocumentNode({tagName: 'div', attrs: {'class': list.getClass() || 'list'}}),
142                 toAdd = secondList;
143             
144             if(listIsNested) {
145                 toAdd = secondList.wrapWith({tagName: 'div', attrs: {'class':'item'}});
146             }
147             succeedingItems.forEach(function(item) {
148                 secondList.append(item);
149             });
150
151             reference.after(toAdd);
152         }
153         if(!params.merge && listIsNested) {
154             return this.extractItems({item1: extractedItems[0], item2: extractedItems[extractedItems.length-1]});
155         }
156         return true;
157     },
158     isAllowed: function() {
159         var parent = this.args[0].nodel1.parent();
160         return parent.is('list') && parent.sameNode(this.args[0].node2.parent());
161     }
162 };
163
164 return extension;
165
166
167 });