X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/fd6329db57fc3cc1cf2305da8d0127de4f2198e2..b0bea8d363c03e64a67eb18c4e8702c69ecb72cc:/src/smartxml/smartxml.js diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index d74ec13..bf64c27 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -10,9 +10,6 @@ define([ 'use strict'; /* globals Node */ -var TEXT_NODE = Node.TEXT_NODE; - - var DocumentNode = function(nativeNode, document) { if(!document) { @@ -48,6 +45,10 @@ $.extend(DocumentNode.prototype, { }, getPath: function(ancestor) { + if(!(this.document.containsNode(this))) { + return null; + } + var nodePath = [this].concat(this.parents()), toret, idx; ancestor = ancestor || this.document.root; @@ -235,8 +236,9 @@ var registerTransformation = function(desc, name, target) { var Transformation = transformations.createContextTransformation(desc, name); //+ to sie powinna nazywac registerTransformationFromDesc or sth //+ ew. spr czy nie override (tylko jesli powyzej sa prototypy to trudno do nich dojsc) - target[name] = function(args) { - var instance = this; + target[name] = function() { + var instance = this, + args = Array.prototype.slice.call(arguments, 0); return instance.transform(Transformation, args); } }; @@ -264,6 +266,8 @@ var Document = function(xml) { this._nodeTransformations = {}; this._textNodeTransformations = {}; this._elementNodeTransformations = {}; + + this.registerExtension(coreTransformations); }; $.extend(Document.prototype, Backbone.Events, { @@ -298,14 +302,21 @@ $.extend(Document.prototype, Backbone.Events, { var toret = new Factory(from, this); _.extend(toret, this._nodeMethods); _.extend(toret, typeMethods); + _.extend(toret, this._nodeTransformations); _.extend(toret, typeTransformations); + + toret.__super__ = _.extend({}, this._nodeMethods, this._nodeTransformations); + _.keys(toret.__super__).forEach(function(key) { + toret.__super__[key] = _.bind(toret.__super__[key], toret); + }); + return toret; }, loadXML: function(xml, options) { options = options || {}; - defineDocumentProperties(this, $(parseXML(xml))); + this._defineDocumentProperties($(parseXML(xml))); if(!options.silent) { this.trigger('contentSet'); } @@ -409,6 +420,7 @@ $.extend(Document.prototype, Backbone.Events, { transform: function(Transformation, args) { //console.log('transform'); var toret, transformation; + //debugger; // ref: odrebnie przygotowanie transformacji, odrebnie jej wykonanie (to pierwsze to analog transform z node) @@ -419,13 +431,15 @@ $.extend(Document.prototype, Backbone.Events, { } if(transformation) { this._transformationLevel++; - toret = transformation.run(); - if(this._transformationLevel === 1) { + toret = transformation.run({beUndoable:this._transformationLevel === 1}); + if(this._transformationLevel === 1 && !this._undoInProgress) { this.undoStack.push(transformation); } this._transformationLevel--; //console.log('clearing redo stack'); - this.redoStack = []; + if(!this._undoInProgress) { + this.redoStack = []; + } return toret; } else { throw new Error('Transformation ' + transformation + ' doesn\'t exist!'); @@ -434,15 +448,20 @@ $.extend(Document.prototype, Backbone.Events, { undo: function() { var transformation = this.undoStack.pop(); if(transformation) { + this._undoInProgress = true; transformation.undo(); + this._undoInProgress = false; this.redoStack.push(transformation); } }, redo: function() { var transformation = this.redoStack.pop(); if(transformation) { - transformation.run(); + this._transformationLevel++; + transformation.run({beUndoable: true}); + this._transformationLevel--; this.undoStack.push(transformation); + } }, @@ -452,23 +471,23 @@ $.extend(Document.prototype, Backbone.Events, { toret = toret.contents()[idx]; }); return toret; + }, + + _defineDocumentProperties: function($document) { + var doc = this; + Object.defineProperty(doc, 'root', {get: function() { + return doc.createDocumentNode($document[0]); + }, configurable: true}); + Object.defineProperty(doc, 'dom', {get: function() { + return $document[0]; + }, configurable: true}); } }); -var defineDocumentProperties = function(doc, $document) { - Object.defineProperty(doc, 'root', {get: function() { - return doc.createDocumentNode($document[0]); - }, configurable: true}); - Object.defineProperty(doc, 'dom', {get: function() { - return $document[0]; - }, configurable: true}); -}; - return { documentFromXML: function(xml) { var doc = new Document(xml); - doc.registerExtension(coreTransformations); return doc; },