From: Aleksander Ɓukasz Date: Wed, 10 Jul 2013 14:53:21 +0000 (+0200) Subject: Extracting items from nested lists X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/6651adb0f9bd9b0c5c83047a3b66f66e1cc7fc57?hp=aa19ea0b08ad083be37ebc43aa9ba3f2bf63a043 Extracting items from nested lists --- diff --git a/modules/documentCanvas/canvas/canvas.js b/modules/documentCanvas/canvas/canvas.js index d883835..eb4e152 100644 --- a/modules/documentCanvas/canvas/canvas.js +++ b/modules/documentCanvas/canvas/canvas.js @@ -175,37 +175,67 @@ $.extend(Canvas.prototype.list, { var idx1 = list.childIndex(params.element1), idx2 = list.childIndex(params.element2), + precedingItems = [], extractedItems = [], succeedingItems = [], items = list.children(), + listIsNested = list.parent().getWlxmlClass() === 'item', i; - for(i = Math.min(idx1,idx2); i <= Math.max(idx1, idx2); i++) { - extractedItems.push(items[i]); - items[i].detach(); - } - for(i = i; i < items.length; i++) { - succeedingItems.push(items[i]); - items[i].detach(); + if(idx1 > idx2) { + var tmp = idx1; idx1 = idx2; idx2 = tmp; } - var last = list; - extractedItems.forEach(function(item) { - item.setWlxmlClass(null); // - last.after(item); - last = item; + items.forEach(function(item, idx) { + if(idx < idx1) + precedingItems.push(item); + else if(idx >= idx1 && idx <= idx2) { + extractedItems.push(item); + } + else { + succeedingItems.push(item); + } }); - if(list.children().length === 0) - list.detach(); - - if(succeedingItems.length > 0) { - var secondList = documentElement.DocumentNodeElement.create({tag: 'div', klass:'list-items'}, this); - last.after(secondList); - + var reference = listIsNested ? list.parent() : list; + if(precedingItems.length === 0 && succeedingItems.length === 0) { + extractedItems.forEach(function(item) { + reference.after(item); + if(!listIsNested) + item.setWlxmlClass(null); + }); + reference.detach(); + } else if(precedingItems.length > 0 && succeedingItems.length === 0) { + extractedItems.forEach(function(item) { + reference.after(item); + if(!listIsNested) + item.setWlxmlClass(null); + }); + } else if(precedingItems.length === 0 && succeedingItems.length > 0) { + extractedItems.forEach(function(item) { + reference.before(item); + if(!listIsNested) + item.setWlxmlClass(null); + }); + } else { + var ptr = reference; + extractedItems.forEach(function(item) { + ptr.after(item); + if(!listIsNested) + item.setWlxmlClass(null); + ptr = item; + }); + var secondList = documentElement.DocumentNodeElement.create({tag: 'div', klass:'list-items'}, this), + toAdd = secondList; + + if(listIsNested) { + toAdd = secondList.wrapWithNodeElement({tag: 'div', klass:'item'}); + } succeedingItems.forEach(function(item) { secondList.append(item); }); + + ptr.after(toAdd); } } }); diff --git a/modules/documentCanvas/canvas/canvas.test3.js b/modules/documentCanvas/canvas/canvas.test3.js index 32e6190..135b0b4 100644 --- a/modules/documentCanvas/canvas/canvas.test3.js +++ b/modules/documentCanvas/canvas/canvas.test3.js @@ -561,6 +561,159 @@ describe('Canvas', function() { expect(section.children().length).to.equal(1, 'section contains one child'); }); + + it('creates two lists with extracted items in the middle if extracting from the middle of the list - nested case' , function() { + var c = canvas.fromXML('\ +
\ +
\ +
0
\ +
\ +
\ +
1.1
\ +
1.2
\ +
1.3
\ +
\ +
\ +
2
\ +
\ +
'), + list = c.doc().children()[0], + nestedList = list.children()[1].children()[0], + nestedListItem = nestedList.children()[1]; + + c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: true}); + + var section = c.doc(), + list = section.children()[0], + item1 = list.children()[0], + item2 = list.children()[1], // + item3 = list.children()[2], + item4 = list.children()[3], // + item5 = list.children()[4], + nestedList1 = item2.children()[0], + nestedList2 = item4.children()[0]; + + expect(list.children().length).to.equal(5, 'top list has five items'); + + expect(item1.children()[0].getText()).to.equal('0', 'first item ok'); + + expect(item2.getWlxmlClass()).to.equal('item', 'first nested list is still wrapped in item element'); + expect(nestedList1.children().length).to.equal(1, 'first nested list is left with one child'); + expect(nestedList1.children()[0].children()[0].getText()).to.equal('1.1', 'first nested list item left alone'); + + expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok'); + + expect(item4.getWlxmlClass()).to.equal('item', 'second nested list is still wrapped in item element'); + expect(nestedList2.children().length).to.equal(1, 'second nested list is left with one child'); + expect(nestedList2.children()[0].children()[0].getText()).to.equal('1.3', 'second nested list item left alone'); + + expect(item5.children()[0].getText()).to.equal('2', 'last item ok'); + }); + + it('puts extracted items below the list if ending item is the last one - nested case' , function() { + var c = canvas.fromXML('\ +
\ +
\ +
0
\ +
\ +
\ +
1.1
\ +
1.2
\ +
\ +
\ +
2
\ +
\ +
'), + list = c.doc().children()[0], + nestedList = list.children()[1].children()[0], + nestedListItem = nestedList.children()[1]; + + c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: true}); + + var section = c.doc(), + list = section.children()[0], + item1 = list.children()[0], + item2 = list.children()[1], + item3 = list.children()[2], + item4 = list.children()[3]; + nestedList = item2.children()[0]; + + expect(list.children().length).to.equal(4, 'top list has four items'); + expect(item1.children()[0].getText()).to.equal('0', 'first item ok'); + expect(item2.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element'); + expect(nestedList.children().length).to.equal(1, 'nested list is left with one child'); + expect(nestedList.children()[0].children()[0].getText()).to.equal('1.1', 'nested list item left alone'); + expect(item3.children()[0].getText()).to.equal('1.2', 'second item ok'); + expect(item4.children()[0].getText()).to.equal('2', 'fourth item ok'); + }); + + it('puts extracted items above the list if starting item is the first one - nested case' , function() { + var c = canvas.fromXML('\ +
\ +
\ +
0
\ +
\ +
\ +
1.1
\ +
1.2
\ +
\ +
\ +
2
\ +
\ +
'), + list = c.doc().children()[0], + nestedList = list.children()[1].children()[0], + nestedListItem = nestedList.children()[0]; + + c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: true}); + + var section = c.doc(), + list = section.children()[0], + item1 = list.children()[0], + item2 = list.children()[1], + item3 = list.children()[2], + item4 = list.children()[3]; + nestedList = item3.children()[0]; + + expect(list.children().length).to.equal(4, 'top list has four items'); + expect(item1.children()[0].getText()).to.equal('0', 'first item ok'); + expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok'); + expect(item3.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element'); + expect(nestedList.children().length).to.equal(1, 'nested list is left with one child'); + expect(nestedList.children()[0].children()[0].getText()).to.equal('1.2', 'nested list item left alone'); + expect(item4.children()[0].getText()).to.equal('2', 'fourth item ok'); + }); + + it('removes list if all its items are extracted - nested case', function() { + var c = canvas.fromXML('\ +
\ +
\ +
0
\ +
\ +
\ +
1.1
\ +
\ +
\ +
2
\ +
\ +
'), + list = c.doc().children()[0], + nestedList = list.children()[1].children()[0], + nestedListItem = nestedList.children()[0]; + + c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: true}); + + var section = c.doc(), + list = section.children()[0], + item1 = list.children()[0], + item2 = list.children()[1], + item3 = list.children()[2]; + + expect(list.children().length).to.equal(3, 'top list has three items'); + expect(item1.children()[0].getText()).to.equal('0', 'first item ok'); + expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok'); + expect(item3.children()[0].getText()).to.equal('2', 'third item ok'); + }); }); });