From: Aleksander Ɓukasz Date: Tue, 9 Jul 2013 07:32:57 +0000 (+0200) Subject: creating a list X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/72006ca5628befdc32bb1cd31002f22c4a7c8bca creating a list --- diff --git a/modules/documentCanvas/canvas/canvas.js b/modules/documentCanvas/canvas/canvas.js index 4326655..3ae6a3e 100644 --- a/modules/documentCanvas/canvas/canvas.js +++ b/modules/documentCanvas/canvas/canvas.js @@ -86,8 +86,60 @@ $.extend(Canvas.prototype, { if(suffixOutside.length > 0) wrapperElement.after({text: suffixOutside}); return wrapperElement; - } + }, + list: {} +}); +$.extend(Canvas.prototype.list, { + create: function(params) { + if(!(params.element1.parent().sameNode(params.element2.parent()))) + return false; + + var parent = params.element1.parent(); + + if(parent.childIndex(params.element1) > parent.childIndex(params.element2)) { + var tmp = params.element1; + params.element1 = params.element2; + params.element2 = tmp; + } + + var elementsToWrap = []; + + var place = 'before'; + var canvas = this; + parent.children().forEach(function(element) { + if(element.sameNode(params.element1)) + place = 'inside'; + if(place === 'inside') { + if(element instanceof documentElement.DocumentTextElement) { + element = element.wrapWithNodeElement({tag: 'div', klass: 'list.item'}); + if(element.children()[0].sameNode(params.element1)) + params.element1 = element; + } + element.setWlxmlClass('item'); + elementsToWrap.push(element); + } + if(element.sameNode(params.element2)) + return false; + }); + + var listElement = documentElement.DocumentNodeElement.create({tag: 'div', klass: 'list-items' + (params.type === 'enum' ? '-enum' : '')}); + + var toret; + if(parent.is('list')) { + listElement.wrap({tag: 'div', klass: 'item'}); + toret = listElement.parent(); + } else { + toret = listElement; + } + + params.element1.before(toret); + + elementsToWrap.forEach(function(element) { + element.detach(); + listElement.append(element); + }); + } }); return { diff --git a/modules/documentCanvas/canvas/canvas.test3.js b/modules/documentCanvas/canvas/canvas.test3.js index dc29f5d..56f8ee6 100644 --- a/modules/documentCanvas/canvas/canvas.test3.js +++ b/modules/documentCanvas/canvas/canvas.test3.js @@ -350,6 +350,29 @@ describe('Canvas', function() { }); }); + describe('Lists api', function() { + it('allows creation of a list from existing sibling DocumentElements', function() { + var c = canvas.fromXML('\ +
\ + Alice\ +
has
\ + a\ +
cat
\ +
'), + section = c.doc(), + textAlice = section.children()[0], + divCat = section.children()[3] + + c.list.create({element1: textAlice, element2: divCat}); + + expect(section.children().length).to.equal(1, 'section has one child element'); + + var list = section.children()[0]; + expect(list.is('list')).to.equal(true, 'section\'s only child is a list'); + expect(list.children().length).to.equal(4, 'list contains four elements'); + }); + }); + }); }); diff --git a/modules/documentCanvas/canvas/documentElement.js b/modules/documentCanvas/canvas/documentElement.js index d82f8e4..da9b148 100644 --- a/modules/documentCanvas/canvas/documentElement.js +++ b/modules/documentCanvas/canvas/documentElement.js @@ -1,6 +1,7 @@ define([ -'libs/jquery-1.9.1.min' -], function($) { +'libs/jquery-1.9.1.min', +'libs/underscore-min' +], function($, _) { 'use strict'; @@ -49,7 +50,7 @@ $.extend(DocumentElement.prototype, { }, wrapWithNodeElement: function(wlxmlNode) { - this.$element.wrap($('<' + wlxmlNode.tag + ' class="' + wlxmlNode.klass + '"">')[0]); + this.$element.wrap($('<' + wlxmlNode.tag + ' class="' + wlxmlNode.klass.replace('.', '-') + '">')[0]); return documentElementFromHTMLElement(this.$element.parent().get(0), this.canvas); }, @@ -104,6 +105,14 @@ $.extend(DocumentNodeElement.prototype, { }, after: function(params) { manipulate(this, params, 'after'); + }, + setWlxmlClass: function(klass) { + this.$element.attr('class', klass); + }, + is: function(what) { + if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.$element.attr('class'))) + return true; + return false; } });