smartxml: Automatically rollback transaction on error thrown
[fnpeditor.git] / src / wlxml / wlxml.js
index c3ed84f..4f6e356 100644 (file)
@@ -2,13 +2,13 @@ define([
     'libs/jquery',
     'libs/underscore',
     'smartxml/smartxml',
-    'smartxml/transformations'
-], function($, _, smartxml, transformations) {
+    'smartxml/transformations',
+    'wlxml/extensions/metadata/metadata'
+], function($, _, smartxml, transformations, metadataExtension) {
     
 'use strict';
 
 /* globals Node */
-var metadataKey = 'wlxml.metadata';
 
 
 var AttributesList = function() {};
@@ -17,10 +17,26 @@ AttributesList.prototype.keys = function() {
     return _.keys(this);
 };
 
+var getClassLists = function(klassName) {
+    var toret = [],
+        classParts = [''].concat(klassName.split('.')),
+        classCurrent;
+
+    classParts.forEach(function(part) {
+        classCurrent = classCurrent ? classCurrent + '.' + part : part;
+        toret.push(classCurrent);
+    });
+    return toret;
+};
+
 var installObject = function(instance, klass) {
-    var methods = instance.document.classMethods[klass] || {},
-        transformations = instance.document.classTransformations[klass] || {};
+    var methods = {},
+        transformations = {};
 
+    getClassLists(klass).forEach(function(klassName) {
+        _.extend(methods, instance.document.classMethods[klassName] || {});
+        _.extend(methods, instance.document.classTransformations[klassName] || {});
+    });
     instance.object = Object.create(_.extend({}, methods, transformations));
     _.keys(methods).concat(_.keys(transformations)).forEach(function(key) {
         instance.object[key] = _.bind(instance.object[key], instance);
@@ -79,26 +95,6 @@ $.extend(WLXMLElementNode.prototype, smartxml.ElementNode.prototype, {
         return attrName !== 'class' &&_.contains(_.keys(this.getMetaAttributes()), attrName);
     },
 
-    getMetadata: function() {
-        return this.getData(metadataKey) || [];
-    },
-
-    addMetadataRow: function(row) {
-        this.setMetadataRow(null, row);
-    },
-
-    setMetadataRow: function(index, row) {
-        var metadata = this.getData(metadataKey) || [];
-        if(typeof index !== 'number' || index > metadata.length - 1) {
-            metadata.push(row);
-            index = metadata.length - 1;
-        } else {
-            metadata[index] = _.extend(metadata[index], row);
-        }
-        this.setData(metadataKey, metadata);
-        this.triggerChangeEvent('metadataChange', {index: index});
-    },
-
     _getXMLDOMToDump: function() {
         var DOM = this._$.clone(true, true),
             doc = this.document;
@@ -182,7 +178,7 @@ WLXMLDocumentNode.prototype = Object.create(smartxml.DocumentNode.prototype);
 var WLXMLDocument = function(xml, options) {
     this.classMethods = {};
     this.classTransformations = {};
-    smartxml.Document.call(this, xml);
+    smartxml.Document.call(this, xml, [metadataExtension]);
     this.options = options;
 };
 
@@ -201,6 +197,18 @@ $.extend(WLXMLDocument.prototype, {
         var doc = this,
             prefixLength = 'dc:'.length;
 
+        $(nativeNode).find('metadata').each(function() {
+            var metadataNode = $(this),
+                owner = doc.createDocumentNode(metadataNode.parent()[0]),
+                metadata = owner.getMetadata();
+                
+            metadataNode.children().each(function() {
+                metadata.add({key: (this.tagName).toLowerCase().substr(prefixLength), value: $(this).text()}, {undoable: false});
+            });
+            metadataNode.remove();
+        });
+        nativeNode.normalize();
+
         $(nativeNode).find(':not(iframe)').addBack().contents()
             .filter(function() {return this.nodeType === Node.TEXT_NODE;})
             .each(function() {
@@ -284,21 +292,14 @@ $.extend(WLXMLDocument.prototype, {
                 el.replaceWith(document.createTextNode(text.transformed));
             });
         
-        $(nativeNode).find('metadata').each(function() {
-            var metadataNode = $(this),
-                owner = doc.createDocumentNode(metadataNode.parent()[0]);
-                
-            metadataNode.children().each(function() {
-                owner.addMetadataRow({key: (this.tagName).toLowerCase().substr(prefixLength), value: $(this).text()});
-            });
-            metadataNode.remove();
-        });
+
     },
 
     registerClassTransformation: function(Transformation, className) {
         var thisClassTransformations = (this.classTransformations[className] = this.classTransformations[className] || {});
-        thisClassTransformations[Transformation.prototype.name] = function(args) {
+        thisClassTransformations[Transformation.prototype.name] = function() {
             var nodeInstance = this;
+            var args = Array.prototype.slice.call(arguments, 0);
             return nodeInstance.transform(Transformation, args);
         };
     },