var currentTag = $(this);
if(currentTag.attr('wlxml-tag'))
return;
+
+ var meta = {};
+ for(var i = 0; i < this.attributes.length; i++) {
+ var attr = this.attributes[i];
+ if(attr.name.substr(0, 5) === 'meta-')
+ meta[attr.name.substr(5)] = attr.value;
+ }
+
var element = documentElement.DocumentNodeElement.createDOM({
tag: currentTag.prop('tagName').toLowerCase(),
- klass: currentTag.attr('class')
+ klass: currentTag.attr('class'),
+ meta: meta
});
element.append(currentTag.contents());
section.setWlxmlClass(null);
expect(section.getWlxmlClass()).to.be.undefined;
});
+
+
+
+ describe('element has meta attributes', function() {
+ it('can change its meta attributes', function() {
+ var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
+ span = c.doc().children()[0];
+
+ expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
+ span.setWlxmlMetaAttr('uri', 'otheruri');
+ expect(span.getWlxmlMetaAttr('uri')).to.equal('otheruri');
+ });
+
+ it('changes its meta attributes with class change', function() {
+ var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
+ span = c.doc().children()[0];
+
+ expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
+ span.setWlxmlClass('author');
+ expect(span.getWlxmlMetaAttr('uri')).to.be.undefined;
+ });
+
+ it('keeps meta attribute value on class change if a new class has this attribute', function() {
+ var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
+ span = c.doc().children()[0];
+ span.setWlxmlClass('uri.some.subclass');
+ expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
+ });
+ });
});
it('returns DocumentNodeElement instance from HTMLElement', function() {
define([
'libs/jquery-1.9.1.min',
-'libs/underscore-min'
-], function($, _) {
+'libs/underscore-min',
+'modules/documentCanvas/classAttributes'
+], function($, _, classAttributes) {
'use strict';
.attr('wlxml-tag', params.tag);
if(params.klass)
dom.attr('wlxml-class', params.klass.replace(/\./g, '-'));
+ if(params.meta) {
+ _.keys(params.meta).forEach(function(key) {
+ dom.attr('wlxml-meta-'+key, params.meta[key]);
+ });
+ }
return dom;
},
return undefined;
},
setWlxmlClass: function(klass) {
+ this.getWlxmlMetaAttrs().forEach(function(attr) {
+ if(!classAttributes.hasMetaAttr(klass, attr.name))
+ this.dom().removeAttr('wlxml-meta-' + attr.name);
+ }, this);
+
if(klass)
this.dom().attr('wlxml-class', klass.replace(/\./g, '-'));
else
if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.dom().attr('wlxml-class')))
return true;
return false;
+ },
+
+
+ getWlxmlMetaAttr: function(attr) {
+ return this.dom().attr('wlxml-meta-'+attr);
+ },
+ getWlxmlMetaAttrs: function() {
+ var toret = [];
+ var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
+ attrList.all.forEach(function(attr) {
+ toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
+ }, this);
+ return toret;
+ },
+ setWlxmlMetaAttr: function(attr, value) {
+ this.dom().attr('wlxml-meta-'+attr, value);
}
});
if(!klass)
return false;
- var parts = klass.split('-');
+ var parts = klass.split('.');
var partialClass = '';
for(var i = 0; i < parts.length; i++) {
- partialClass += (partialClass === '' ? '' : '-') + parts[i];
+ partialClass += (partialClass === '' ? '' : '.') + parts[i];
if(dict[partialClass] && dict[partialClass][attrName])
return true;
}
klass = klass || '';
var toret = {own: [], inheritedFrom: {}, all: []};
- var parts = klass.split('-');
+ var parts = klass.split('.');
var partialClass = '';
var generate = function(klass) {
toret.own = generate(klass);
for(var i = 0; i < parts.length; i++) {
- partialClass += (partialClass === '' ? '' : '-') + parts[i];
+ partialClass += (partialClass === '' ? '' : '.') + parts[i];
var list = generate(partialClass);
if(list.length > 0) {
toret.inheritedFrom[partialClass] = generate(partialClass);
return transformations.toXML.getXML(canvas.getContent());
},
modifyCurrentNodeElement: function(attr, value) {
+ var currentNodeElement = canvas.getCurrentNodeElement();
if(attr === 'class' || attr === 'tag') {
- canvas.getCurrentNodeElement()['setWlxml'+(attr[0].toUpperCase() + attr.substring(1))](value);
+ currentNodeElement['setWlxml'+(attr[0].toUpperCase() + attr.substring(1))](value);
+ } else {
+ currentNodeElement.setWlxmlMetaAttr(attr, value);
}
},
highlightElement: function(element) {
+++ /dev/null
-define([
-'libs/chai',
-'modules/documentCanvas/classAttributes'
-], function(chai, classAttributes) {
-
-var stubDict = {
- 'klass': {
- 'prop': 'string'
- },
- 'klass-sub1': {
- 'prop1': 'string'
- },
- 'klass-sub1-sub2': {
- 'prop2': 'string'
- }
-};
-
-var assert = chai.assert;
-
-suite('Class attributes', function() {
- test('class has own attribute', function() {
- assert.ok(classAttributes.hasMetaAttr('klass-sub1-sub2', 'prop2', stubDict));
- });
-
- test('class has attributes from parent classes', function() {
- assert.ok(classAttributes.hasMetaAttr('klass-sub1-sub2', 'prop', stubDict));
- assert.ok(classAttributes.hasMetaAttr('klass-sub1-sub2', 'prop1', stubDict));
- });
-
- test('list of class meta attributes', function() {
- var attrList = classAttributes.getMetaAttrsList('klass-sub1-sub2', stubDict);
-
- assert.deepEqual(attrList.own, [{name: 'prop2', type: 'string'}]);
- assert.deepEqual(attrList.inheritedFrom['klass-sub1'], [{name: 'prop1', type: 'string'}]);
- assert.deepEqual(attrList.inheritedFrom.klass, [{name: 'prop', type: 'string'}]);
- assert.deepEqual(attrList.all.sort(), [
- {name: 'prop', type: 'string'},
- {name: 'prop1', type: 'string'},
- {name: 'prop2', type: 'string'}
- ].sort(), 'all values');
- });
-
- test('class without meta attrs', function() {
- var attrList = classAttributes.getMetaAttrsList('some-class', {});
- assert.deepEqual(attrList.own, [], 'empty own list');
- assert.deepEqual(attrList.inheritedFrom, {}, 'empty inherited dict');
- assert.deepEqual(attrList.all, [], 'empty all list');
- });
-
- test('empty class', function() {
- var attrList = classAttributes.getMetaAttrsList('', {});
- assert.deepEqual(attrList.own, [], 'empty own list');
- assert.deepEqual(attrList.inheritedFrom, {}, 'empty inherited dict');
- assert.deepEqual(attrList.all, [], 'empty all list');
- });
-});
-
-});
\ No newline at end of file
--- /dev/null
+define([
+'libs/chai',
+'modules/documentCanvas/classAttributes'
+], function(chai, classAttributes) {
+
+var stubDict = {
+ 'klass': {
+ 'prop': 'string'
+ },
+ 'klass.sub1': {
+ 'prop1': 'string'
+ },
+ 'klass.sub1.sub2': {
+ 'prop2': 'string'
+ }
+};
+
+var assert = chai.assert;
+
+describe('Class attributes', function() {
+ it('class has own attribute', function() {
+ assert.ok(classAttributes.hasMetaAttr('klass.sub1.sub2', 'prop2', stubDict));
+ });
+
+ it('class has attributes from parent classes', function() {
+ assert.ok(classAttributes.hasMetaAttr('klass.sub1.sub2', 'prop', stubDict));
+ assert.ok(classAttributes.hasMetaAttr('klass.sub1.sub2', 'prop1', stubDict));
+ });
+
+ it('list of class meta attributes', function() {
+ var attrList = classAttributes.getMetaAttrsList('klass.sub1.sub2', stubDict);
+
+ assert.deepEqual(attrList.own, [{name: 'prop2', type: 'string'}]);
+ assert.deepEqual(attrList.inheritedFrom['klass.sub1'], [{name: 'prop1', type: 'string'}]);
+ assert.deepEqual(attrList.inheritedFrom.klass, [{name: 'prop', type: 'string'}]);
+ assert.deepEqual(attrList.all.sort(), [
+ {name: 'prop', type: 'string'},
+ {name: 'prop1', type: 'string'},
+ {name: 'prop2', type: 'string'}
+ ].sort(), 'all values');
+ });
+
+ it('class without meta attrs', function() {
+ var attrList = classAttributes.getMetaAttrsList('some.class', {});
+ assert.deepEqual(attrList.own, [], 'empty own list');
+ assert.deepEqual(attrList.inheritedFrom, {}, 'empty inherited dict');
+ assert.deepEqual(attrList.all, [], 'empty all list');
+ });
+
+ it('empty class', function() {
+ var attrList = classAttributes.getMetaAttrsList('', {});
+ assert.deepEqual(attrList.own, [], 'empty own list');
+ assert.deepEqual(attrList.inheritedFrom, {}, 'empty inherited dict');
+ assert.deepEqual(attrList.all, [], 'empty all list');
+ });
+});
+
+});
\ No newline at end of file
},
setNodeElement: function(nodeElement) {
view.find('.rng-module-nodePane-tagSelect').val(nodeElement.getWlxmlTag());
- view.find('.rng-module-nodePane-classSelect').val((nodeElement.getWlxmlClass() || '').replace(/\./g, '-'));
- //var widget = metaWidget.create({attrs:canvasNode.getMetaAttrs()});
- //widget.on('valueChanged', function(key, value) {
- // sandbox.publish('nodeChanged', key, value);
- //});
- //view.find('.metaFields').empty().append(widget.el);
+ var escapedClassName = (nodeElement.getWlxmlClass() || '').replace(/\./g, '-')
+ view.find('.rng-module-nodePane-classSelect').val(escapedClassName);
+
+ var widget = metaWidget.create({attrs:nodeElement.getWlxmlMetaAttrs()});
+ widget.on('valueChanged', function(key, value) {
+ sandbox.publish('nodeElementChange', key, value);
+ });
+ view.find('.metaFields').empty().append(widget.el);
}
};