+ },
+
+ trigger: function() {
+ //console.log('trigger: ' + arguments[0] + (arguments[1] ? ', ' + arguments[1].type : ''));
+ Backbone.Events.trigger.apply(this, arguments);
+ },
+
+ getNodeInsertion: function(node) {
+ var insertion = {};
+ if(node instanceof DocumentNode) {
+ insertion.ofNode = node;
+ insertion.insertsNew = !this.containsNode(node);
+ } else {
+ insertion.ofNode = this.createDocumentNode(node);
+ insertion.insertsNew = true;
+ }
+ return insertion;
+ },
+
+ replaceRoot: function(node) {
+ var insertion = this.getNodeInsertion(node);
+ this.root.detach();
+ defineDocumentProperties(this, insertion.ofNode._$);
+ insertion.ofNode.triggerChangeEvent('nodeAdded');
+ return insertion.ofNode;
+ },
+
+ 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);
+ }
+ }
+ if(transformation) {
+ this._transformationLevel++;
+ toret = transformation.run();
+ if(this._transformationLevel === 1) {
+ this.undoStack.push(transformation);
+ }
+ this._transformationLevel--;
+ console.log('clearing redo stack');
+ this.redoStack = [];
+ return toret;
+ } else {
+ throw new Error('Transformation ' + transformation + ' doesn\'t exist!');
+ }
+ },
+ undo: function() {
+ var transformation = this.undoStack.pop();
+ if(transformation) {
+ transformation.undo();
+ this.redoStack.push(transformation);
+ }
+ },
+ redo: function() {
+ var transformation = this.redoStack.pop();
+ if(transformation) {
+ transformation.run();
+ this.undoStack.push(transformation);
+ }
+ },
+
+ getNodeByPath: function(path) {
+ var toret = this.root;
+ path.forEach(function(idx) {
+ toret = toret.contents()[idx];
+ });
+ return toret;