smartxml: signal that operation ended
[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         return listNode;
81     },
82     getChangeRoot: function() {
83         return this.args[0].node1.parent();
84     },
85     isAllowed: function() {
86         return this.args[0].node1.parent().sameNode(this.args[0].node2.parent());
87     }
88 };
89
90 extension.document.transformations.extractItems = {
91     impl: function(params) {
92         params = _.extend({}, {merge: true}, params);
93         var list = params.item1.parent(),
94             indexes = [params.item1.getIndex(), params.item2.getIndex()].sort(function(a,b) { return a-b;}),
95             precedingItems = [],
96             extractedItems = [],
97             succeedingItems = [],
98             items = list.contents(),
99             listIsNested = list.parent().is('item');
100
101         
102         items.forEach(function(item, idx) {
103             if(idx < indexes[0]) {
104                 precedingItems.push(item);
105             }
106             else if(idx >= indexes[0] && idx <= indexes[1]) {
107                 extractedItems.push(item);
108             }
109             else {
110                 succeedingItems.push(item);
111             }
112         });
113
114         var reference = listIsNested ? list.parent() : list;
115         if(succeedingItems.length === 0) {
116             var reference_orig = reference;
117             extractedItems.forEach(function(item) {
118                 reference.after(item);
119                 reference = item;
120                 if(!listIsNested) {
121                     item.setClass('');
122                 }
123             });
124             if(precedingItems.length === 0) {
125                 reference_orig.detach();
126             }
127         } else if(precedingItems.length === 0) {
128             extractedItems.forEach(function(item) {
129                 reference.before(item);
130                 if(!listIsNested) {
131                     item.setClass('');
132                 }
133             });
134         } else {
135             extractedItems.forEach(function(item) {
136                 reference.after(item);
137                 if(!listIsNested) {
138                     item.setClass('');
139                 }
140                 reference = item;
141             });
142             var secondList = params.item1.document.createDocumentNode({tagName: 'div', attrs: {'class': list.getClass() || 'list'}}),
143                 toAdd = secondList;
144             
145             if(listIsNested) {
146                 toAdd = secondList.wrapWith({tagName: 'div', attrs: {'class':'item'}});
147             }
148             succeedingItems.forEach(function(item) {
149                 secondList.append(item);
150             });
151
152             reference.after(toAdd);
153         }
154         if(!params.merge && listIsNested) {
155             return this.extractItems({item1: extractedItems[0], item2: extractedItems[extractedItems.length-1]});
156         }
157         return true;
158     },
159     isAllowed: function() {
160         var parent = this.args[0].nodel1.parent();
161         return parent.is('list') && parent.sameNode(this.args[0].node2.parent());
162     }
163 };
164
165 return extension;
166
167
168 });