5 var extension = {document: {transformations: {}}, wlxmlClass: {list: {methods: {}}}};
8 extension.wlxmlClass.list.methods = {
9 itemIndex: function(listItem) {
11 this.contents('.item').some(function(item, index) {
12 if(item.sameNode(listItem)) {
19 getItem: function(index) {
20 return this.contents('.item')[index];
24 extension.documentMethods = {
25 areItemsOfSameList: function(params) {
26 return params.node1.parent().sameNode(params.node2.parent()) && params.node2.parent().is('list');
32 extension.document.transformations.createList = {
33 impl: function(params) {
34 var parent = params.node1.parent(),
35 parentContents = parent.contents(),
36 nodeIndexes = [params.node1.getIndex(), params.node2.getIndex()].sort(),
38 listNode = params.node1.document.createDocumentNode({tagName: 'div', attrs: {'class': 'list'}}),
41 for(i = nodeIndexes[0]; i <= nodeIndexes[1]; i++) {
42 node = parentContents[i];
43 if(node.nodeType === Node.TEXT_NODE) {
44 node = node.wrapWith({tagName: 'div', attrs: {'class': 'item'}}); //t
46 node.setClass('item'); //t
48 nodesToWrap.push(node);
52 if(parent.is('list') && parent.object.itemIndex(nodesToWrap[0]) > 0) { // object api
53 // var prevItem = parent.object.getItem(parent.object.itemIndex(nodesToWrap[0])-1); // object api
54 // prevItem.append(listNode); //t
55 toInsert = listNode.wrapWith({tagName: 'div', attrs: {'class': 'item'}});
57 //nodesToWrap[0].before(listNode); //t
61 params.node1.before(toInsert);
63 nodesToWrap.forEach(function(node) {
64 listNode.append(node); //t
67 getChangeRoot: function() {
68 return this.args[0].node1.parent();
70 isAllowed: function() {
71 return this.args[0].node1.parent().sameNode(this.args[0].node2.parent());
75 extension.document.transformations.extractItems = {
76 impl: function(params) {
77 params = _.extend({}, {merge: true}, params);
78 var list = params.item1.parent(),
79 indexes = [params.item1.getIndex(), params.item2.getIndex()].sort(),
83 items = list.contents(), // lub list.object.items()
84 listIsNested = list.parent().is('item'),
87 items.forEach(function(item, idx) {
88 if(idx < indexes[0]) {
89 precedingItems.push(item);
91 else if(idx >= indexes[0] && idx <= indexes[1]) {
92 extractedItems.push(item);
95 succeedingItems.push(item);
99 var reference = listIsNested ? list.parent() : list;
100 if(succeedingItems.length === 0) {
101 var reference_orig = reference;
102 extractedItems.forEach(function(item) {
103 reference.after(item); //t
106 item.setClass(null); //t
109 if(precedingItems.length === 0)
110 reference_orig.detach(); //t
111 } else if(precedingItems.length === 0) {
112 extractedItems.forEach(function(item) {
113 reference.before(item); //t
115 item.setClass(null); //t
119 extractedItems.forEach(function(item) {
120 reference.after(item); //t
122 item.setClass(null); //t
125 var secondList = params.item1.document.createDocumentNode({tagName: 'div', attrs: {'class':'list'}}),
129 toAdd = secondList.wrapWith({tagName: 'div', attrs: {'class':'item'}});
131 succeedingItems.forEach(function(item) {
132 secondList.append(item);
135 reference.after(toAdd);
137 if(!params.merge && listIsNested) {
138 return this.extractItems({item1: extractedItems[0], item2: extractedItems[extractedItems.length-1]});
142 isAllowed: function() {
143 var parent = this.args[0].nodel1.parent();
144 return parent.is('list') && parent.sameNode(this.args[0].node2.parent());