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