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