X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/33bebd4a55d538b7229e3d6cd0673e39ee28a81e..0a765eadd374d46fc8f4ebe88f9f97a4d8fae622:/src/smartxml/smartxml.js?ds=sidebyside diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index d912904..ac59532 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -38,12 +38,8 @@ var DocumentNode = function(nativeNode, document) { $.extend(DocumentNode.prototype, { - transform: function(name, args) { - var Transformation = this.transformations.get(name), - transformation; - if(Transformation) { - transformation = new Transformation(this.document, this, args); - } + transform: function(Transformation, args) { + var transformation = new Transformation(this.document, this, args); return this.document.transform(transformation); }, @@ -473,7 +469,10 @@ var parseXML = function(xml) { var registerTransformation = function(desc, name, target) { var Transformation = transformations.createContextTransformation(desc, name); - target.register(Transformation); + target[name] = function(args) { + var instance = this; + return instance.transform(Transformation, args); + } }; var registerMethod = function(methodName, method, target) { @@ -492,10 +491,11 @@ var Document = function(xml) { this.undoStack = []; this.redoStack = []; this._transformationLevel = 0; - this.transformations = new transformations.TransformationStorage(); this._nodeMethods = {}; - this._nodeTransformations = new transformations.TransformationStorage(); + this._textNodeMethods = {}; + this._elementNodeMethods = {}; + this._nodeTransformations = {}; }; $.extend(Document.prototype, Backbone.Events, { @@ -517,15 +517,18 @@ $.extend(Document.prototype, Backbone.Events, { from = node[0]; } } - var Factory; + var Factory, typeMethods; if(from.nodeType === Node.TEXT_NODE) { Factory = this.TextNodeFactory; + typeMethods = this._textNodeMethods; } else if(from.nodeType === Node.ELEMENT_NODE) { Factory = this.ElementNodeFactory; + typeMethods = this._elementNodeMethods; } var toret = new Factory(from, this); _.extend(toret, this._nodeMethods); - toret.transformations = this._nodeTransformations; + _.extend(toret, typeMethods); + _.extend(toret, this._nodeTransformations); return toret; }, @@ -675,16 +678,19 @@ $.extend(Document.prototype, Backbone.Events, { return insertion.ofNode; }, - registerMethod: function(methodName, method) { - registerMethod(methodName, method, this); - }, - - registerNodeMethod: function(methodName, method) { - registerMethod(methodName, method, this._nodeMethods); + registerMethod: function(methodName, method, dstName) { + var doc = this; + var destination = { + document: doc, + documentNode: doc._nodeMethods, + textNode: doc._textNodeMethods, + elementNode: doc._elementNodeMethods + }[dstName]; + registerMethod(methodName, method, destination); }, registerDocumentTransformation: function(desc, name) { - registerTransformation(desc, name, this.transformations); + registerTransformation(desc, name, this); }, registerNodeTransformation: function(desc, name) { @@ -696,16 +702,15 @@ $.extend(Document.prototype, Backbone.Events, { var doc = this, existingPropertyNames = _.values(this); - ['document', 'documentNode'].forEach(function(dstName) { + ['document', 'documentNode', 'elementNode', 'textNode'].forEach(function(dstName) { var dstExtension = extension[dstName]; if(dstExtension) { if(dstExtension.methods) { _.pairs(dstExtension.methods).forEach(function(pair) { var methodName = pair[0], - method = pair[1], - operation; - operation = {document: 'registerMethod', documentNode: 'registerNodeMethod'}[dstName]; - doc[operation](methodName, method); + method = pair[1]; + + doc.registerMethod(methodName, method, dstName); }); } @@ -723,15 +728,15 @@ $.extend(Document.prototype, Backbone.Events, { }); }, - transform: function(transformation, args) { + transform: function(Transformation, args) { //console.log('transform'); - var Transformation, toret; - if(typeof transformation === 'string') { - Transformation = this.transformations.get(transformation); - if(Transformation) { - transformation = new Transformation(this, this, args); - } - } + var toret, transformation; + + if(typeof Transformation === 'function') { + transformation = new Transformation(this, this, args); + } else { + transformation = Transformation; + } if(transformation) { this._transformationLevel++; toret = transformation.run();