editor: canvas - initiate commands transactions via Document.transaction
[fnpeditor.git] / src / wlxml / wlxml.js
index c3ed84f..4f6e356 100644 (file)
@@ -2,13 +2,13 @@ define([
     'libs/jquery',
     'libs/underscore',
     'smartxml/smartxml',
     '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 */
     
 'use strict';
 
 /* globals Node */
-var metadataKey = 'wlxml.metadata';
 
 
 var AttributesList = function() {};
 
 
 var AttributesList = function() {};
@@ -17,10 +17,26 @@ AttributesList.prototype.keys = function() {
     return _.keys(this);
 };
 
     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 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);
     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);
     },
 
         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;
     _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 = {};
 var WLXMLDocument = function(xml, options) {
     this.classMethods = {};
     this.classTransformations = {};
-    smartxml.Document.call(this, xml);
+    smartxml.Document.call(this, xml, [metadataExtension]);
     this.options = options;
 };
 
     this.options = options;
 };
 
@@ -201,6 +197,18 @@ $.extend(WLXMLDocument.prototype, {
         var doc = this,
             prefixLength = 'dc:'.length;
 
         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() {
         $(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));
             });
         
                 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] || {});
     },
 
     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 nodeInstance = this;
+            var args = Array.prototype.slice.call(arguments, 0);
             return nodeInstance.transform(Transformation, args);
         };
     },
             return nodeInstance.transform(Transformation, args);
         };
     },