$.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);
},
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) {
this.undoStack = [];
this.redoStack = [];
this._transformationLevel = 0;
- this.transformations = new transformations.TransformationStorage();
this._nodeMethods = {};
- this._nodeTransformations = new transformations.TransformationStorage();
+ this._nodeTransformations = {};
};
$.extend(Document.prototype, Backbone.Events, {
}
var toret = new Factory(from, this);
_.extend(toret, this._nodeMethods);
- toret.transformations = this._nodeTransformations;
+ _.extend(toret, this._nodeTransformations);
return toret;
},
},
registerDocumentTransformation: function(desc, name) {
- registerTransformation(desc, name, this.transformations);
+ registerTransformation(desc, name, this);
},
registerNodeTransformation: function(desc, name) {
});
},
- 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();
textNode.transform('testTransformation');
}).to.throw(Error);
expect(function() {
- doc.transform('testTransformation');
+ doc.testTransformation();
}).to.throw(Error);
expect(doc.testMethod).to.be.undefined;
expect(elementNode.testMethod).to.be.undefined;
}}};
doc.registerExtension(extension);
- expect(doc.transform('testTransformation')).to.equal(doc, 'context is set to a document instance');
- expect(doc.transform('testTransformation2')).to.equal(doc, 'context is set to a document instance');
+ expect(doc.testTransformation()).to.equal(doc, 'context is set to a document instance');
+ expect(doc.testTransformation2()).to.equal(doc, 'context is set to a document instance');
});
it('allows adding method to a DocumentNode instance', function() {
doc.registerExtension(extension);
- expect(elementNode.transform('testTransformation').sameNode(elementNode)).to.equal(true, '1');
- expect(elementNode.transform('testTransformation2').sameNode(elementNode)).to.equal(true, '2');
- expect(textNode.transform('testTransformation').sameNode(textNode)).to.equal(true, '3');
- expect(textNode.transform('testTransformation2').sameNode(textNode)).to.equal(true, '4');
+ /* refresh */
+ elementNode = doc.root;
+ textNode = doc.root.contents()[0];
+
+ expect(elementNode.testTransformation().sameNode(elementNode)).to.equal(true, '1');
+ expect(elementNode.testTransformation2().sameNode(elementNode)).to.equal(true, '2');
+ expect(textNode.testTransformation().sameNode(textNode)).to.equal(true, '3');
+ expect(textNode.testTransformation2().sameNode(textNode)).to.equal(true, '4');
});
});
if(!desc.impl) {
throw new Error('Got transformation description without implementation.')
}
- desc.name = desc.name || name;
return desc;
};
toret.createGenericTransformation = function(desc, name) {
- desc = getTransDesc(desc, name);
+ desc = getTransDesc(desc);
var GenericTransformation = function(document, args) {
this.args = args || {};
}
};
_.extend(GenericTransformation.prototype, {
- name: desc.name,
+ name: name,
run: function() {
var changeRoot;
if(!desc.undo) {
reference.after(toAdd);
}
if(!params.merge && listIsNested) {
- return this.transform('extractItems', {item1: extractedItems[0], item2: extractedItems[extractedItems.length-1]});
+ return this.extractItems({item1: extractedItems[0], item2: extractedItems[extractedItems.length-1]});
}
return true;
},
div1 = section.contents()[1],
textA = section.contents()[2];
- doc.transform('createList', {node1: div1, node2: textA});
+ doc.createList({node1: div1, node2: textA});
expect(section.contents().length).to.equal(3, 'section has three child nodes');
itemC = outerList.contents('.item')[2];
- doc.transform('createList', {node1: itemB, node2: itemC});
+ doc.createList({node1: itemB, node2: itemC});
var outerListItems = outerList.contents('.item'),
innerList = outerListItems[1].contents()[0];
item1 = list.contents()[1],
item2 = list.contents()[2];
- doc.transform('extractItems', {item1: item1, item2: item2});
+ doc.extractItems({item1: item1, item2: item2});
var section = doc.root,
list1 = section.contents()[0],
item2 = list.contents()[1],
item3 = list.contents()[2];
- doc.transform('extractItems', {item1: item1, item2: item2});
+ doc.extractItems({item1: item1, item2: item2});
var section = doc.root,
oldItem1 = section.contents()[0],
item2 = list.contents()[1],
item3 = list.contents()[2];
- doc.transform('extractItems', {item1: item2, item2: item3});
+ doc.extractItems({item1: item2, item2: item3});
var section = doc.root,
oldItem1 = section.contents()[1],
item1 = list.contents()[0],
item2 = list.contents()[1];
- doc.transform('extractItems', {item1: item1, item2: item2});
+ doc.extractItems({item1: item1, item2: item2});
var section = doc.root,
oldItem1 = section.contents()[0],
nestedList = list.contents()[1].contents()[0],
nestedListItem = nestedList.contents()[1];
- doc.transform('extractItems', {item1: nestedListItem, item2: nestedListItem});
+ doc.extractItems({item1: nestedListItem, item2: nestedListItem}); //@@ name!
var section = doc.root,
list = section.contents()[0],
nestedListItem1 = nestedList.contents()[1],
nestedListItem2 = nestedList.contents()[2];
- doc.transform('extractItems', {item1: nestedListItem1, item2: nestedListItem2});
+ doc.extractItems({item1: nestedListItem1, item2: nestedListItem2});
var section = doc.root,
list = section.contents()[0],
nestedListItem1 = nestedList.contents()[0],
nestedListItem2 = nestedList.contents()[1];
- doc.transform('extractItems', {item1: nestedListItem1, item2: nestedListItem2});
+ doc.extractItems({item1: nestedListItem1, item2: nestedListItem2});
var section = doc.root,
list = section.contents()[0],
nestedListItem1 = nestedList.contents()[0],
nestedListItem2 = nestedList.contents()[1];
- doc.transform('extractItems', {item1: nestedListItem1, item2: nestedListItem2});
+ doc.extractItems({item1: nestedListItem1, item2: nestedListItem2});
var section = doc.root,
list = section.contents()[0],
nestedList = list.contents()[1].contents()[0],
nestedListItem = nestedList.contents()[0];
- var test = doc.transform('extractItems', {item1: nestedListItem, item2: nestedListItem, merge: false});
+ var test = doc.extractItems({item1: nestedListItem, item2: nestedListItem, merge: false});
expect(test).to.equal(true, 'extraction status ok');