From: Aleksander Ɓukasz Date: Tue, 16 Jul 2013 14:23:28 +0000 (+0200) Subject: DocumentNodeElement meta attributes X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/26de8c3163f128baa02c78d172effcdf38a8adbc DocumentNodeElement meta attributes --- diff --git a/modules/documentCanvas/canvas/canvas.js b/modules/documentCanvas/canvas/canvas.js index 88a8839..a5d4825 100644 --- a/modules/documentCanvas/canvas/canvas.js +++ b/modules/documentCanvas/canvas/canvas.js @@ -23,9 +23,18 @@ $.extend(Canvas.prototype, { 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()); diff --git a/modules/documentCanvas/canvas/canvas.test3.js b/modules/documentCanvas/canvas/canvas.test3.js index 030ff19..c4dc610 100644 --- a/modules/documentCanvas/canvas/canvas.test3.js +++ b/modules/documentCanvas/canvas/canvas.test3.js @@ -126,6 +126,35 @@ describe('Canvas', function() { 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('
'), + 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('
'), + 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('
'), + span = c.doc().children()[0]; + span.setWlxmlClass('uri.some.subclass'); + expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri'); + }); + }); }); it('returns DocumentNodeElement instance from HTMLElement', function() { diff --git a/modules/documentCanvas/canvas/documentElement.js b/modules/documentCanvas/canvas/documentElement.js index ba859fc..669e06a 100644 --- a/modules/documentCanvas/canvas/documentElement.js +++ b/modules/documentCanvas/canvas/documentElement.js @@ -1,7 +1,8 @@ define([ 'libs/jquery-1.9.1.min', -'libs/underscore-min' -], function($, _) { +'libs/underscore-min', +'modules/documentCanvas/classAttributes' +], function($, _, classAttributes) { 'use strict'; @@ -116,6 +117,11 @@ $.extend(DocumentNodeElement, { .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; }, @@ -196,6 +202,11 @@ $.extend(DocumentNodeElement.prototype, { 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 @@ -205,6 +216,22 @@ $.extend(DocumentNodeElement.prototype, { 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); } }); diff --git a/modules/documentCanvas/classAttributes.js b/modules/documentCanvas/classAttributes.js index fe7409a..06c9485 100644 --- a/modules/documentCanvas/classAttributes.js +++ b/modules/documentCanvas/classAttributes.js @@ -13,10 +13,10 @@ var hasMetaAttr = function(klass, attrName, dict) { 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; } @@ -28,7 +28,7 @@ var getMetaAttrsList = function(klass, dict) { klass = klass || ''; var toret = {own: [], inheritedFrom: {}, all: []}; - var parts = klass.split('-'); + var parts = klass.split('.'); var partialClass = ''; var generate = function(klass) { @@ -46,7 +46,7 @@ var getMetaAttrsList = function(klass, dict) { 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); diff --git a/modules/documentCanvas/documentCanvas.js b/modules/documentCanvas/documentCanvas.js index 1c3242a..dbbbd0e 100644 --- a/modules/documentCanvas/documentCanvas.js +++ b/modules/documentCanvas/documentCanvas.js @@ -50,8 +50,11 @@ return function(sandbox) { 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) { diff --git a/modules/documentCanvas/tests/classAttributes.test.js b/modules/documentCanvas/tests/classAttributes.test.js deleted file mode 100644 index 4f2157c..0000000 --- a/modules/documentCanvas/tests/classAttributes.test.js +++ /dev/null @@ -1,58 +0,0 @@ -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 diff --git a/modules/documentCanvas/tests/classAttributes.test3.js b/modules/documentCanvas/tests/classAttributes.test3.js new file mode 100644 index 0000000..49e0be0 --- /dev/null +++ b/modules/documentCanvas/tests/classAttributes.test3.js @@ -0,0 +1,58 @@ +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 diff --git a/modules/nodePane/nodePane.js b/modules/nodePane/nodePane.js index 48dd77a..3a4a0ef 100644 --- a/modules/nodePane/nodePane.js +++ b/modules/nodePane/nodePane.js @@ -26,13 +26,15 @@ return function(sandbox) { }, 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); } };