From ac31ad7e5c9642a6703ae75ca072a43255e4a230 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Mon, 1 Jul 2013 15:34:15 +0200 Subject: [PATCH] canvasNode: meta attributes --- modules/documentCanvas/canvasNode.js | 30 ++++++++++++++++++- .../documentCanvas/tests/canvasNode.test.js | 28 +++++++++++++++-- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/modules/documentCanvas/canvasNode.js b/modules/documentCanvas/canvasNode.js index 8cf61b1..d9475cb 100644 --- a/modules/documentCanvas/canvasNode.js +++ b/modules/documentCanvas/canvasNode.js @@ -1,4 +1,4 @@ -define(['libs/jquery-1.9.1.min'], function($) { +define(['libs/jquery-1.9.1.min', 'libs/underscore-min'], function($, _) { 'use strict'; @@ -20,6 +20,12 @@ var CanvasNode = function(desc) { this.dom.attr('wlxml-class', desc.klass); if(desc.content) this.dom.text(desc.content); + if(desc.meta) { + var c = this; + _.keys(desc.meta).forEach(function(key) { + c.dom.attr('wlxml-meta-'+key, desc.meta[key]); + }); + } this.dom.attr('id', 'xxxxxxxx-xxxx-xxxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);})); } }; @@ -77,6 +83,28 @@ CanvasNode.prototype.isOfClass = function(klass) { return this.getClass() && this.getClass().substr(0, klass.length) === klass; }; +CanvasNode.prototype.getMetaAttr = function(attr) { + return this.dom.attr('wlxml-meta-'+attr); +} + +CanvasNode.prototype.getMetaAttrs = function() { + var toret = []; + var metaAttrPrefix = 'wlxml-meta-'; + + var attrs = this.dom.get(0).attributes; + for(var i = 0; i < attrs.length; i++) { + var attr = attrs[i]; + if(attr.name.substr(0, metaAttrPrefix.length) === metaAttrPrefix) { + toret.push({name: attr.name.substr(metaAttrPrefix.length), value: attr.value}); + } + } + return toret; +} + +CanvasNode.prototype.setMetaAttr = function(attr, value) { + this.dom.attr('wlxml-meta-'+attr, value); +} + return { create: function(desc) { return new CanvasNode(desc); diff --git a/modules/documentCanvas/tests/canvasNode.test.js b/modules/documentCanvas/tests/canvasNode.test.js index a1595f7..5dccf7b 100644 --- a/modules/documentCanvas/tests/canvasNode.test.js +++ b/modules/documentCanvas/tests/canvasNode.test.js @@ -18,17 +18,24 @@ var assertDomEqual = function(lhs, rhs) { suite('Create canvas node', function() { test('from description', function() { - var node = canvasNode.create({tag: 'header', klass: 'some-class', content: 'some text content'}); + var node = canvasNode.create({ + tag: 'header', + klass: 'some-class', + content: 'some text content', + meta: {uri: 'some uri'} + }); assert.equal(node.getTag(), 'header'); assert.equal(node.getClass(), 'some-class'); assert.equal(node.getContent(), 'some text content'); - assertDomEqual($('
some text content
'), node.dom); + assert.equal(node.getMetaAttr('uri'), 'some uri'); + assertDomEqual($('
some text content
'), node.dom); }); test('from dom object', function() { - var node = canvasNode.create($('
')); + var node = canvasNode.create($('
')); assert.equal(node.getTag(), 'header'); assert.equal(node.getClass(), 'some-class'); + assert.equal(node.getMetaAttr('uri'), 'some uri'); //assertDomEqual($('
'), node.dom); }); }); @@ -58,5 +65,20 @@ suite('comparing nodes', function() { }); }); +suite('meta attributes', function() { + test('get list of node\'s meta attributes', function() { + var node = canvasNode.create({tag: 'tag', klass: 'klass', meta: {a:1, b:2}}); + var attrs = node.getMetaAttrs(); + var expected = [{name: 'a', value: '1'}, {name:'b', value: '2'}]; + + assert.deepEqual(attrs.sort(), expected.sort()); + }); + + test('set meta attribute', function() { + var node = canvasNode.create({tag: 'tag', meta: {a:'1'}}); + node.setMetaAttr('a', '2'); + assert.equal(node.dom.attr('wlxml-meta-a'), '2'); + }) +}) }); \ No newline at end of file -- 2.20.1