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.document.methods = {
33 areItemsOfSameList: function(params) {
34 return params.node1.parent().sameNode(params.node2.parent()) && params.node2.parent().is('list');
40 extension.document.transformations.createList = {
41 impl: function(params) {
43 var parent = params.node1.parent(),
44 parentContents = parent.contents(),
45 nodeIndexes = [params.node1.getIndex(), params.node2.getIndex()].sort(function(a,b) { return a-b; }),
47 listClass = params.klass || 'list',
48 listNode = params.node1.document.createDocumentNode({tagName: 'div', attrs: {'class': listClass}}),
49 listPlacePtr = params.node1,
52 for(i = nodeIndexes[0]; i <= nodeIndexes[1]; i++) {
53 node = parentContents[i];
54 if(node.nodeType === Node.TEXT_NODE) {
55 node = node.wrapWith({tagName: 'div', attrs: {'class': 'item'}});
56 if(i === nodeIndexes[0]) {
60 node.setClass('item');
62 nodesToWrap.push(node);
66 if(parent.is('list') && parent.object.itemIndex(nodesToWrap[0]) > 0) { // object api
67 // var prevItem = parent.object.getItem(parent.object.itemIndex(nodesToWrap[0])-1); // object api
68 // prevItem.append(listNode);
69 toInsert = listNode.wrapWith({tagName: 'div', attrs: {'class': 'item'}});
71 //nodesToWrap[0].before(listNode);
75 listPlacePtr.before(toInsert);
77 nodesToWrap.forEach(function(node) {
78 listNode.append(node);
82 getChangeRoot: function() {
83 return this.args[0].node1.parent();
85 isAllowed: function() {
86 return this.args[0].node1.parent().sameNode(this.args[0].node2.parent());
90 extension.document.transformations.extractItems = {
91 impl: function(params) {
92 params = _.extend({}, {merge: true}, params);
93 var list = params.item1.parent(),
94 indexes = [params.item1.getIndex(), params.item2.getIndex()].sort(function(a,b) { return a-b;}),
98 items = list.contents(),
99 listIsNested = list.parent().is('item');
102 items.forEach(function(item, idx) {
103 if(idx < indexes[0]) {
104 precedingItems.push(item);
106 else if(idx >= indexes[0] && idx <= indexes[1]) {
107 extractedItems.push(item);
110 succeedingItems.push(item);
114 var reference = listIsNested ? list.parent() : list;
115 if(succeedingItems.length === 0) {
116 var reference_orig = reference;
117 extractedItems.forEach(function(item) {
118 reference.after(item);
124 if(precedingItems.length === 0) {
125 reference_orig.detach();
127 } else if(precedingItems.length === 0) {
128 extractedItems.forEach(function(item) {
129 reference.before(item);
135 extractedItems.forEach(function(item) {
136 reference.after(item);
142 var secondList = params.item1.document.createDocumentNode({tagName: 'div', attrs: {'class': list.getClass() || 'list'}}),
146 toAdd = secondList.wrapWith({tagName: 'div', attrs: {'class':'item'}});
148 succeedingItems.forEach(function(item) {
149 secondList.append(item);
152 reference.after(toAdd);
154 if(!params.merge && listIsNested) {
155 return this.extractItems({item1: extractedItems[0], item2: extractedItems[extractedItems.length-1]});
159 isAllowed: function() {
160 var parent = this.args[0].nodel1.parent();
161 return parent.is('list') && parent.sameNode(this.args[0].node2.parent());