X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/fb3b7d9bff29aa5c99298ce9e4bae53c2eb47a7d..55826c1f7f8bfec8c90096b1c1c779e118e357fc:/src/smartxml/smartxml.js?ds=sidebyside diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index de70e9b..b449d82 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -17,12 +17,21 @@ var DocumentNode = function(nativeNode, document) { throw new Error('undefined document for a node'); } this.document = document; + this.object = {}; this._setNativeNode(nativeNode); }; $.extend(DocumentNode.prototype, { + getProperty: function(propName) { + var toret = this.object[propName]; + if(toret && _.isFunction(toret)) { + toret = toret.call(this); + } + return toret; + }, + transform: function(Transformation, args) { var transformation = new Transformation(this.document, this, args); return this.document.transform(transformation); @@ -140,12 +149,9 @@ $.extend(DocumentNode.prototype, { var node = (metaData && metaData.node) ? metaData.node : this, event = new events.ChangeEvent(type, $.extend({node: node}, metaData || {})); if(type === 'nodeDetached' || this.document.containsNode(event.meta.node)) { - if(type === 'nodeMoved') { - event.meta.parent = origParent; - } this.document.trigger('change', event); } - if((type === 'nodeAdded' || type === 'nodeMoved') && !this.document.containsNode(this) && nodeWasContained) { + if(type === 'nodeAdded' && !this.document.containsNode(this) && nodeWasContained) { event = new events.ChangeEvent('nodeDetached', {node: node, parent: origParent}); this.document.trigger('change', event); } @@ -156,10 +162,14 @@ $.extend(DocumentNode.prototype, { }, getIndex: function() { + var parent; + if(this.isRoot()) { return 0; } - return this.parent().indexOf(this); + + parent = this.parent(); + return parent ? parent.indexOf(this) : undefined; }, getNearestElementNode: function() { @@ -176,16 +186,16 @@ ElementNode.prototype = Object.create(DocumentNode.prototype); $.extend(ElementNode.prototype, { nodeType: Node.ELEMENT_NODE, - setData: function(key, value) { + setData: function(arg1, arg2) { if(arguments.length === 2) { - if(_.isUndefined(value)) { - this._$.removeData(key); + if(_.isUndefined(arg2)) { + this._$.removeData(arg1); } else { - this._$.data(key, value); + this._$.data(arg1, arg2); } } else { this._$.removeData(_.keys(this._$.data())); - this._$.data(key); + this._$.data(arg1); } }, @@ -289,7 +299,7 @@ $.extend(TextNode.prototype, { var parseXML = function(xml) { var toret = $($.trim(xml)); - if(!toret.length) { + if(toret.length !== 1) { throw new Error('Unable to parse XML: ' + xml); } return toret[0];