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.wlxmlClass.list.transformations = {
26 extractAllItems: function() {
27 var contents = this.contents();
28 return this.document.extractItems({item1: contents[0], item2: _.last(contents)});
32 extension.wlxmlClass.list.transformations = {
33 extractListItems: function() {
34 var contents = this.contents(),
38 last = contents[contents.length-1];
39 return this.document.extractItems({
49 extension.document.methods = {
50 areItemsOfSameList: function(params) {
51 return params.node1.parent().sameNode(params.node2.parent()) && params.node2.parent().is('list');
55 extension.document.transformations.createList = {
56 impl: function(params) {
58 var parent = params.node1.parent(),
59 parentContents = parent.contents(),
60 nodeIndexes = [params.node1.getIndex(), params.node2.getIndex()].sort(function(a,b) { return a-b; }),
62 listClass = params.klass || 'list',
63 listNode = params.node1.document.createDocumentNode({tagName: 'div', attrs: {'class': listClass}}),
64 listPlacePtr = params.node1,
67 for(i = nodeIndexes[0]; i <= nodeIndexes[1]; i++) {
68 node = parentContents[i];
69 if(node.nodeType === Node.TEXT_NODE) {
70 node = node.wrapWith({tagName: 'div', attrs: {'class': 'item'}});
71 if(i === nodeIndexes[0]) {
75 node.setClass('item');
77 nodesToWrap.push(node);
81 if(parent.is('list') && parent.object.itemIndex(nodesToWrap[0]) > 0) { // object api
82 // var prevItem = parent.object.getItem(parent.object.itemIndex(nodesToWrap[0])-1); // object api
83 // prevItem.append(listNode);
84 toInsert = listNode.wrapWith({tagName: 'div', attrs: {'class': 'item'}});
86 //nodesToWrap[0].before(listNode);
90 listPlacePtr.before(toInsert);
92 nodesToWrap.forEach(function(node) {
93 listNode.append(node);
97 getChangeRoot: function() {
98 return this.args[0].node1.parent();
100 isAllowed: function() {
101 return this.args[0].node1.parent().sameNode(this.args[0].node2.parent());
105 extension.document.transformations.extractItems = {
106 impl: function(params) {
107 params = _.extend({}, {merge: true}, params);
108 var list = params.item1.parent(),
109 indexes = [params.item1.getIndex(), params.item2.getIndex()].sort(function(a,b) { return a-b;}),
112 succeedingItems = [],
113 items = list.contents(),
114 listIsNested = list.parent().is('item');
117 items.forEach(function(item, idx) {
118 if(idx < indexes[0]) {
119 precedingItems.push(item);
121 else if(idx >= indexes[0] && idx <= indexes[1]) {
122 extractedItems.push(item);
125 succeedingItems.push(item);
129 var reference = listIsNested ? list.parent() : list;
130 if(succeedingItems.length === 0) {
131 var reference_orig = reference;
132 extractedItems.forEach(function(item) {
133 reference.after(item);
139 if(precedingItems.length === 0) {
140 reference_orig.detach();
142 } else if(precedingItems.length === 0) {
143 extractedItems.forEach(function(item) {
144 reference.before(item);
150 extractedItems.forEach(function(item) {
151 reference.after(item);
157 var secondList = params.item1.document.createDocumentNode({tagName: 'div', attrs: {'class': list.getClass() || 'list'}}),
161 toAdd = secondList.wrapWith({tagName: 'div', attrs: {'class':'item'}});
163 succeedingItems.forEach(function(item) {
164 secondList.append(item);
167 reference.after(toAdd);
169 if(!params.merge && listIsNested) {
170 return this.extractItems({item1: extractedItems[0], item2: extractedItems[extractedItems.length-1]});
172 return extractedItems[0];
174 isAllowed: function() {
175 var parent = this.args[0].nodel1.parent();
176 return parent.is('list') && parent.sameNode(this.args[0].node2.parent());