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 {
});
});
+ describe('Lists api', function() {
+ it('allows creation of a list from existing sibling DocumentElements', function() {
+ var c = canvas.fromXML('\
+ <section>\
+ Alice\
+ <div>has</div>\
+ a\
+ <div>cat</div>\
+ </section>'),
+ 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');
+ });
+ });
+
});
});
define([
-'libs/jquery-1.9.1.min'
-], function($) {
+'libs/jquery-1.9.1.min',
+'libs/underscore-min'
+], function($, _) {
'use strict';
},
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);
},
},
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;
}
});