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