'libs/sinon',
'modules/documentCanvas/canvas/canvas',
'modules/documentCanvas/canvas/utils',
+'modules/documentCanvas/canvas/documentElement',
'wlxml/wlxml',
-], function($, chai, sinon, canvas, utils, wlxml) {
+], function($, chai, sinon, canvas, utils, documentElement, wlxml) {
'use strict';
/* global describe, it, beforeEach, afterEach */
describe('Custom elements based on wlxml class attribute', function() {
it('allows custom rendering', function() {
- var c = getCanvasFromXML('<section><div class="testClass"></div></section>', [
- {tag: 'div', klass: 'testClass', prototype: {
+ var prototype = $.extend({}, documentElement.DocumentNodeElement.prototype, {
init: function() {
this._container().append('<test></test>');
}
- }, extending: {tag: 'div'}}
+ }),
+ c = getCanvasFromXML('<section><div class="testClass"></div></section>', [
+ {tag: 'div', klass: 'testClass', prototype: prototype}
]);
expect(c.doc().children()[0]._container().children('test').length).to.equal(1); // @!
});
it('allows handling changes to internal structure of rendered node', function() {
- var c = getCanvasFromXML('<section><div class="testClass"><a></a></div></section>', [
- {tag: 'div', klass: 'testClass', prototype: {
+ var prototype = $.extend({}, documentElement.DocumentNodeElement.prototype, {
init: function() {
this.header = $('<h1>');
this._container().append(this.header);
void(event);
this.refresh2();
}
- }, extending: {tag: 'div'}}
+ });
+
+ var c = getCanvasFromXML('<section><div class="testClass"><a></a></div></section>', [
+ {tag: 'div', klass: 'testClass', prototype: prototype}
]);
var node = c.wlxmlDocument.root.contents()[0],
$.extend(DocumentNodeElement.prototype, {
defaultDisplayStyle: 'block',
+ init: function() {},
addWidget: function(widget) {
this.dom.children('.canvas-widgets').append(widget.DOM ? widget.DOM : widget);
},
wlxml = require('wlxml/wlxml');
-var ElementsRegister = function(basePrototype, defaultPrototype) {
+var ElementsRegister = function(BaseType) {
this._register = {};
- this.basePrototype = basePrototype;
- this.DefaultType = this.createCanvasElementType(defaultPrototype);
+ this.BaseType = BaseType;
};
_.extend(ElementsRegister.prototype, {
- createCanvasElementType: function(elementPrototype, extending) {
- var inheritFrom = this.basePrototype;
- if(extending && extending.tag) {
- inheritFrom = this.getElement(extending);
- }
+ createCanvasElementType: function(elementPrototype) {
+ var register = this;
var Constructor = function() {
- if(!this.super) {
- this.super = inheritFrom.prototype;
- }
- inheritFrom.apply(this, Array.prototype.slice.call(arguments, 0));
-
+ register.BaseType.apply(this, Array.prototype.slice.call(arguments, 0));
};
- Constructor.prototype = Object.create(inheritFrom.prototype);
- _.extend(Constructor.prototype, elementPrototype);
+ Constructor.prototype = elementPrototype;
return Constructor;
},
register: function(params) {
params.prototype = params.prototype || Object.create({});
this._register[params.tag] = this._register[params.tag] || {};
- this._register[params.tag][params.klass] = this.createCanvasElementType(params.prototype, params.extending);
+ this._register[params.tag][params.klass] = this.createCanvasElementType(params.prototype);
},
getElement: function(params) {
params.klass = params.klass || '';
}.bind(this));
}
if(!Factory) {
- Factory = this.DefaultType;
+ Factory = this.BaseType;
}
return Factory;
}
describe('Elements register', function() {
it('registers element for a tag', function() {
var register = new ElementsRegister(documentElement.DocumentNodeElement),
- prototype = {testMethod: function(){}};
+ prototype = Object.create({});
register.register({
tag: 'div',
prototype: prototype,
});
-
var Element = register.getElement({tag: 'div'});
- expect(Element.prototype.testMethod).to.equal(prototype.testMethod, '1');
- expect(Element.prototype instanceof documentElement.DocumentNodeElement).to.equal(true, '2');
+ expect(Element.prototype).to.equal(prototype);
});
it('registers element for a class', function() {
var register = new ElementsRegister(documentElement.DocumentNodeElement),
- prototype = {testMethod: function(){}};
+ prototype = Object.create({});
register.register({
tag: 'div',
prototype: prototype,
});
var Element = register.getElement({tag: 'div', klass: 'a.b.c'});
- expect(Element.prototype.testMethod).to.equal(prototype.testMethod, '1');
- expect(Element.prototype instanceof documentElement.DocumentNodeElement).to.equal(true, '2');
- });
- it('allows inheriting from selected element', function() {
- var register = new ElementsRegister(documentElement.DocumentNodeElement),
- method1 = function() {},
- method2 = function() {};
-
- register.register({
- tag: 'div',
- prototype: {method1: method1}
- });
-
- register.register({
- tag: 'div',
- klass: 'a',
- prototype: {method2: method2},
- extending: {tag: 'div'}
- });
-
- var Element = register.getElement({tag: 'div', klass: 'a'});
- expect(Element.prototype.method2).to.equal(method2, '2');
- expect(Element.prototype instanceof register.getElement({tag: 'div'})).to.equal(true, '2');
+ expect(Element.prototype).to.equal(prototype);
});
});
};
void(labelWidget); // for linters; labelWidget is unused on purpose for now
+var DocumentNodeElement = documentElement.DocumentNodeElement;
-var generic = {
+var generic = Object.create(DocumentNodeElement.prototype);
+
+$.extend(generic, {
onNodeAttrChange: function(event) {
if(event.meta.attr === 'class') {
this.setWlxmlClass(event.meta.newVal); //
this.refreshPath();
},
init: function() {
+ DocumentNodeElement.prototype.init.call(this);
this._container()
.attr('wlxml-tag', this.wlxmlNode.getTagName());
this.setWlxmlClass(this.wlxmlNode.getClass());
});
return toret;
},
-};
+});
return generic;
define(function(require) {
'use strict';
-var $ = require('libs/jquery');
+var $ = require('libs/jquery'),
+ genericElement = require('modules/documentCanvas/canvas/genericElement'); // TODO: This should be accessible via plugin infrastructure
var widgets = {
};
-var comment = {
+var comment = Object.create(genericElement);
+$.extend(comment, {
init: function() {
- this.super.init.call(this);
+ genericElement.init.call(this);
this.commentAdnotation = widgets.commentAdnotation(this.wlxmlNode);
this.addWidget(this.commentAdnotation, 'show');
this.commentAdnotation.DOM.show();
onMetadataRemoved: function(event) {
return this.onMetadataChanged(event);
}
-};
+});
-var footnote = {
+var footnote = Object.create(genericElement);
+$.extend(footnote, {
init: function() {
- this.super.init.call(this);
+ genericElement.init.call(this);
var clickHandler = function() {
this.toggle(true);
}.bind(this);
this.trigger('elementToggled', toggle, this);
}
}
-};
+});
return [
- {tag: 'aside', klass: 'comment', prototype: comment, extending: {tag: 'div'}},
- {tag: 'aside', klass: 'footnote', prototype: footnote, extending: {tag: 'aside'}}
+ {tag: 'aside', klass: 'comment', prototype: comment},
+ {tag: 'aside', klass: 'footnote', prototype: footnote}
];