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 listClass = params.klass || 'list',
41 listNode = params.node1.document.createDocumentNode({tagName: 'div', attrs: {'class': listClass}}),
42 listPlacePtr = params.node1,
45 for(i = nodeIndexes[0]; i <= nodeIndexes[1]; i++) {
46 node = parentContents[i];
47 if(node.nodeType === Node.TEXT_NODE) {
48 node = node.wrapWith({tagName: 'div', attrs: {'class': 'item'}});
49 if(i === nodeIndexes[0]) {
53 node.setClass('item');
55 nodesToWrap.push(node);
59 if(parent.is('list') && parent.object.itemIndex(nodesToWrap[0]) > 0) { // object api
60 // var prevItem = parent.object.getItem(parent.object.itemIndex(nodesToWrap[0])-1); // object api
61 // prevItem.append(listNode);
62 toInsert = listNode.wrapWith({tagName: 'div', attrs: {'class': 'item'}});
64 //nodesToWrap[0].before(listNode);
68 listPlacePtr.before(toInsert);
70 nodesToWrap.forEach(function(node) {
71 listNode.append(node);
74 getChangeRoot: function() {
75 return this.args[0].node1.parent();
77 isAllowed: function() {
78 return this.args[0].node1.parent().sameNode(this.args[0].node2.parent());
82 extension.document.transformations.extractItems = {
83 impl: function(params) {
84 params = _.extend({}, {merge: true}, params);
85 var list = params.item1.parent(),
86 indexes = [params.item1.getIndex(), params.item2.getIndex()].sort(function(a,b) { return a-b;}),
90 items = list.contents(),
91 listIsNested = list.parent().is('item');
94 items.forEach(function(item, idx) {
95 if(idx < indexes[0]) {
96 precedingItems.push(item);
98 else if(idx >= indexes[0] && idx <= indexes[1]) {
99 extractedItems.push(item);
102 succeedingItems.push(item);
106 var reference = listIsNested ? list.parent() : list;
107 if(succeedingItems.length === 0) {
108 var reference_orig = reference;
109 extractedItems.forEach(function(item) {
110 reference.after(item);
116 if(precedingItems.length === 0) {
117 reference_orig.detach();
119 } else if(precedingItems.length === 0) {
120 extractedItems.forEach(function(item) {
121 reference.before(item);
127 extractedItems.forEach(function(item) {
128 reference.after(item);
134 var secondList = params.item1.document.createDocumentNode({tagName: 'div', attrs: {'class':'list'}}),
138 toAdd = secondList.wrapWith({tagName: 'div', attrs: {'class':'item'}});
140 succeedingItems.forEach(function(item) {
141 secondList.append(item);
144 reference.after(toAdd);
146 if(!params.merge && listIsNested) {
147 return this.extractItems({item1: extractedItems[0], item2: extractedItems[extractedItems.length-1]});
151 isAllowed: function() {
152 var parent = this.args[0].nodel1.parent();
153 return parent.is('list') && parent.sameNode(this.args[0].node2.parent());