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'}}),
41 listPlacePtr = params.node1,
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]) {
52 node.setClass('item');
54 nodesToWrap.push(node);
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'}});
63 //nodesToWrap[0].before(listNode);
67 listPlacePtr.before(toInsert);
69 nodesToWrap.forEach(function(node) {
70 listNode.append(node);
73 getChangeRoot: function() {
74 return this.args[0].node1.parent();
76 isAllowed: function() {
77 return this.args[0].node1.parent().sameNode(this.args[0].node2.parent());
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;}),
89 items = list.contents(),
90 listIsNested = list.parent().is('item');
93 items.forEach(function(item, idx) {
94 if(idx < indexes[0]) {
95 precedingItems.push(item);
97 else if(idx >= indexes[0] && idx <= indexes[1]) {
98 extractedItems.push(item);
101 succeedingItems.push(item);
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);
115 if(precedingItems.length === 0) {
116 reference_orig.detach();
118 } else if(precedingItems.length === 0) {
119 extractedItems.forEach(function(item) {
120 reference.before(item);
126 extractedItems.forEach(function(item) {
127 reference.after(item);
133 var secondList = params.item1.document.createDocumentNode({tagName: 'div', attrs: {'class':'list'}}),
137 toAdd = secondList.wrapWith({tagName: 'div', attrs: {'class':'item'}});
139 succeedingItems.forEach(function(item) {
140 secondList.append(item);
143 reference.after(toAdd);
145 if(!params.merge && listIsNested) {
146 return this.extractItems({item1: extractedItems[0], item2: extractedItems[extractedItems.length-1]});
150 isAllowed: function() {
151 var parent = this.args[0].nodel1.parent();
152 return parent.is('list') && parent.sameNode(this.args[0].node2.parent());