1 define(function(require) {
5 var _ = require('libs/underscore'),
6 extension = {document: {transformations: {}, methods: {}}, wlxmlClass: {list: {methods: {}}}};
9 extension.wlxmlClass.list.methods = {
10 itemIndex: function(listItem) {
12 this.contents('.item').some(function(item, index) {
13 if(item.sameNode(listItem)) {
20 getItem: function(index) {
21 return this.contents('.item')[index];
25 extension.document.methods = {
26 areItemsOfSameList: function(params) {
27 return params.node1.parent().sameNode(params.node2.parent()) && params.node2.parent().is('list');
33 extension.document.transformations.createList = {
34 impl: function(params) {
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; }),
40 listNode = params.node1.document.createDocumentNode({tagName: 'div', attrs: {'class': 'list'}}),
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'}});
48 node.setClass('item');
50 nodesToWrap.push(node);
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'}});
59 //nodesToWrap[0].before(listNode);
63 params.node1.before(toInsert);
65 nodesToWrap.forEach(function(node) {
66 listNode.append(node);
69 getChangeRoot: function() {
70 return this.args[0].node1.parent();
72 isAllowed: function() {
73 return this.args[0].node1.parent().sameNode(this.args[0].node2.parent());
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(function(a,b) { return a-b;}),
85 items = list.contents(),
86 listIsNested = list.parent().is('item');
89 items.forEach(function(item, idx) {
90 if(idx < indexes[0]) {
91 precedingItems.push(item);
93 else if(idx >= indexes[0] && idx <= indexes[1]) {
94 extractedItems.push(item);
97 succeedingItems.push(item);
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);
111 if(precedingItems.length === 0) {
112 reference_orig.detach();
114 } else if(precedingItems.length === 0) {
115 extractedItems.forEach(function(item) {
116 reference.before(item);
122 extractedItems.forEach(function(item) {
123 reference.after(item);
129 var secondList = params.item1.document.createDocumentNode({tagName: 'div', attrs: {'class':'list'}}),
133 toAdd = secondList.wrapWith({tagName: 'div', attrs: {'class':'item'}});
135 succeedingItems.forEach(function(item) {
136 secondList.append(item);
139 reference.after(toAdd);
141 if(!params.merge && listIsNested) {
142 return this.extractItems({item1: extractedItems[0], item2: extractedItems[extractedItems.length-1]});
146 isAllowed: function() {
147 var parent = this.args[0].nodel1.parent();
148 return parent.is('list') && parent.sameNode(this.args[0].node2.parent());