From: Aleksander Ɓukasz Date: Thu, 16 Jan 2014 08:44:43 +0000 (+0100) Subject: wlxml: metadata wip - part of a new api approach X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/85331f59b2aa174291878f90633f7189fd221628?hp=266a16ec421df5bc8f2565884dd410918f1f1df4 wlxml: metadata wip - part of a new api approach --- diff --git a/src/wlxml/extensions/metadata/metadata.js b/src/wlxml/extensions/metadata/metadata.js index 4136673..86bd8ee 100644 --- a/src/wlxml/extensions/metadata/metadata.js +++ b/src/wlxml/extensions/metadata/metadata.js @@ -5,6 +5,34 @@ define(function(require) { var _ = require('libs/underscore'), metadataKey = 'wlxml.metadata'; +var Row = function(key, value) { + this.key = key; + this.value = value; +}; +_.extend(Row.prototype, { + setKey: function(key) { + this.key = key; + }, + getKey: function() { + return this.key; + }, + setValue: function(value) { + this.value = value; + }, + getValue: function() { + return this.value; + } +}); + +// var Metadata = function(node) { +// this._rows = []; +// } + +// _.extend(Metadata.prototype, { +// forEach: function(callback) { +// this. +// } +// }) var methods = { getMetadata: function() { @@ -13,20 +41,12 @@ var methods = { }; var transformations = { - addMetadataRow: function(row) { - this.setMetadataRow(null, row); - }, - - setMetadataRow: function(index, row) { - var metadata = this.getData(metadataKey) || []; - if(typeof index !== 'number' || index > metadata.length - 1) { - metadata.push(row); - index = metadata.length - 1; - } else { - metadata[index] = _.extend(metadata[index], row); - } + addMetadata: function(desc) { + var metadata = this.getData(metadataKey) || [], + row = new Row(desc.key, desc.value); + metadata.push(row); this.setData(metadataKey, metadata); - this.triggerChangeEvent('metadataChange', {index: index}); + return row; } }; diff --git a/src/wlxml/extensions/metadata/metadata.test.js b/src/wlxml/extensions/metadata/metadata.test.js index 00e5c17..5de573b 100644 --- a/src/wlxml/extensions/metadata/metadata.test.js +++ b/src/wlxml/extensions/metadata/metadata.test.js @@ -16,22 +16,42 @@ var getDocumentFromXML = function(xml, options) { describe.only('Metadata API', function() { - it('allows to set metadata on an element node', function() { + it('returns empty metadata for node without metadata', function() { var doc = getDocumentFromXML('
'); - expect(doc.root.getMetadata()).to.deep.equal([]); - doc.root.addMetadataRow({key: 'key', value: 'value'}); - expect(doc.root.getMetadata()).to.deep.equal([{key: 'key', value: 'value'}]); + expect(doc.root.getMetadata().length).to.equal(0); }); + it('allows to set metadata on an element node', function() { + var doc = getDocumentFromXML('
'); - it('reads node\'s metadata from its metadata child node', function() { - var doc = getDocumentFromXML('
value
'); - expect(doc.root.getMetadata()).to.deep.equal([{key: 'key', value: 'value'}]); + var row = doc.root.addMetadata({key: 'key', value: 'value'}), + metadata = doc.root.getMetadata(); + + expect(metadata.length).to.equal(1); + expect(metadata[0]).to.equal(row, 'aaa'); + + expect(row.getKey()).to.equal('key'); + expect(row.getValue()).to.equal('value'); + }); + // it('allows to remove specific metadata row', function() { + // var doc = getDocumentFromXML('
valuevalue
'), + // metadata = doc.root.getMetadata(); + // expect(metadata.length).to.equal(2); + // row.remove(); + // expect(metadata.length) + // expect(metadata[0].getValue()).to.equal('value'); + // }); + it('reads node\'s metadata from source of its metadata child node', function() { + var doc = getDocumentFromXML('
value
'), + metadata = doc.root.getMetadata(); + expect(metadata.length).to.equal(1); + expect(metadata[0].getKey()).to.equal('key'); + expect(metadata[0].getValue()).to.equal('value'); }); it('serializes node\'s metadata to its metadata child node', function() { var doc = getDocumentFromXML('
'); - doc.root.addMetadataRow({key: 'key', value: 'value'}); + doc.root.addMetadata({key: 'key', value: 'value'}); var metadataNodes = $(doc.toXML()).children('metadata'), keyNodes = metadataNodes.children(); @@ -41,10 +61,11 @@ describe.only('Metadata API', function() { expect(keyNodes[0].tagName.toLowerCase()).to.equal('dc:key'); expect($(keyNodes[0]).text()).to.equal('value'); }); - it('doesnt show metadata node on nodes contents', function() { + it('doesn\'t show metadata node on nodes contents', function() { var doc = getDocumentFromXML('
value
'); expect(doc.root.contents()).to.have.length(0); }); + }); diff --git a/src/wlxml/wlxml.js b/src/wlxml/wlxml.js index 6c6c40c..539d792 100644 --- a/src/wlxml/wlxml.js +++ b/src/wlxml/wlxml.js @@ -269,7 +269,7 @@ $.extend(WLXMLDocument.prototype, { owner = doc.createDocumentNode(metadataNode.parent()[0]); metadataNode.children().each(function() { - owner.addMetadataRow({key: (this.tagName).toLowerCase().substr(prefixLength), value: $(this).text()}); + owner.addMetadata({key: (this.tagName).toLowerCase().substr(prefixLength), value: $(this).text()}); }); metadataNode.remove(); });