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