wlxml: metadata wip - first approach
[fnpeditor.git] / src / wlxml / wlxml.js
index b560982..c3ed84f 100644 (file)
@@ -7,13 +7,9 @@ define([
     
 'use strict';
 
     
 'use strict';
 
-// utils
+/* globals Node */
+var metadataKey = 'wlxml.metadata';
 
 
-var isMetaAttribute = function(attrName) {
-    return attrName.substr(0, 5) === 'meta-';
-};
-
-//
 
 var AttributesList = function() {};
 AttributesList.prototype = Object.create({});
 
 var AttributesList = function() {};
 AttributesList.prototype = Object.create({});
@@ -22,24 +18,14 @@ AttributesList.prototype.keys = function() {
 };
 
 var installObject = function(instance, klass) {
 };
 
 var installObject = function(instance, klass) {
-    var methods = instance.document.classMethods[klass] || instance.document.classTransformations;
-    if(methods) {
-        instance.object = Object.create(_.extend({
-            transform: function(name, args) {
-                // TODO: refactor with DocumentElement.transform
-                var Transformation = instance.document.classTransformations[klass].get(name),
-                    transformation;
-                if(Transformation) {
-                    transformation = new Transformation(instance.document, instance, args);
-                }
-                return instance.document.transform(transformation);
-            }
-        }, methods));
-        _.keys(methods).forEach(function(key) {
-            instance.object[key] = _.bind(instance.object[key], instance);
-        });
-    }
-}
+    var methods = instance.document.classMethods[klass] || {},
+        transformations = instance.document.classTransformations[klass] || {};
+
+    instance.object = Object.create(_.extend({}, methods, transformations));
+    _.keys(methods).concat(_.keys(transformations)).forEach(function(key) {
+        instance.object[key] = _.bind(instance.object[key], instance);
+    });
+};
 
 var WLXMLElementNode = function(nativeNode, document) {
     smartxml.ElementNode.call(this, nativeNode, document);
 
 var WLXMLElementNode = function(nativeNode, document) {
     smartxml.ElementNode.call(this, nativeNode, document);
@@ -52,7 +38,6 @@ $.extend(WLXMLElementNode.prototype, smartxml.ElementNode.prototype, {
         return this.getAttr('class') || '';
     },
     setClass: function(klass) {
         return this.getAttr('class') || '';
     },
     setClass: function(klass) {
-        var methods, object;
         if(klass !== this.klass) {
             installObject(this, klass);
             return this.setAttr('class', klass);
         if(klass !== this.klass) {
             installObject(this, klass);
             return this.setAttr('class', klass);
@@ -71,27 +56,52 @@ $.extend(WLXMLElementNode.prototype, smartxml.ElementNode.prototype, {
             classDesc = this.document.options.wlxmlClasses[classCurrent];
             if(classDesc) {
                 _.keys(classDesc.attrs).forEach(function(attrName) {
             classDesc = this.document.options.wlxmlClasses[classCurrent];
             if(classDesc) {
                 _.keys(classDesc.attrs).forEach(function(attrName) {
-                    toret[attrName] = _.extend({value: this.getAttr('meta-' + attrName)}, classDesc.attrs[attrName]);
+                    toret[attrName] = _.extend({value: this.getAttr(attrName)}, classDesc.attrs[attrName]);
                 }.bind(this));
             }
         }.bind(this));
         return toret;
     },
     setMetaAttribute: function(key, value) {
                 }.bind(this));
             }
         }.bind(this));
         return toret;
     },
     setMetaAttribute: function(key, value) {
-        this.setAttr('meta-'+key, value);
+        this.setAttr(key, value);
     },
     getOtherAttributes: function() {
     },
     getOtherAttributes: function() {
-        var toret = {};
+        var toret = {},
+            node = this;
         this.getAttrs().forEach(function(attr) {
         this.getAttrs().forEach(function(attr) {
-            if(attr.name !== 'class' && !isMetaAttribute(attr.name)) {
-                toret[attr.name] = attr.value;
+            if(attr.name !== 'class' && !node.isMetaAttribute(attr.name)) {
+                toret[attr.name] = {value: attr.value};
             }
         });
         return toret;
     },
             }
         });
         return toret;
     },
+    isMetaAttribute: function(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() {
 
     _getXMLDOMToDump: function() {
-        var DOM = this._$.clone(true, true);
+        var DOM = this._$.clone(true, true),
+            doc = this.document;
 
         DOM.find('*').addBack().each(function() {
             var el = $(this),
 
         DOM.find('*').addBack().each(function() {
             var el = $(this),
@@ -101,7 +111,7 @@ $.extend(WLXMLElementNode.prototype, smartxml.ElementNode.prototype, {
                 data = el.data();
 
 
                 data = el.data();
 
 
-            var txt;
+            var txt, documentNode, metaNode;
 
             if(data[formatter_prefix+ 'orig_before']) {
                 txt = idx > 0 && contents[idx-1].nodeType === Node.TEXT_NODE ? contents[idx-1] : null;
 
             if(data[formatter_prefix+ 'orig_before']) {
                 txt = idx > 0 && contents[idx-1].nodeType === Node.TEXT_NODE ? contents[idx-1] : null;
@@ -131,8 +141,23 @@ $.extend(WLXMLElementNode.prototype, smartxml.ElementNode.prototype, {
                     el.append(data[formatter_prefix+ 'orig_end']);
                 }
             }
                     el.append(data[formatter_prefix+ 'orig_end']);
                 }
             }
+
+
+            if(this.nodeType === Node.ELEMENT_NODE) {
+                documentNode = doc.createDocumentNode(this);
+                metaNode = $('<metadata>');
+                documentNode.getMetadata().forEach(function(row) {
+                    metaNode.append('<dc:'+ row.key + '>' + row.value + '</dc:' + row.key + '>');
+                });
+                if(metaNode.children().length) {
+                    $(this).prepend(metaNode);
+                }
+            }
+
         });
 
         });
 
+        
+
         return DOM;
     }
 });
         return DOM;
     }
 });
@@ -151,15 +176,14 @@ $.extend(WLXMLElementNode.prototype, smartxml.ElementNode.prototype, {
 
 var WLXMLDocumentNode = function() {
     smartxml.DocumentNode.apply(this, arguments);
 
 var WLXMLDocumentNode = function() {
     smartxml.DocumentNode.apply(this, arguments);
-}
+};
 WLXMLDocumentNode.prototype = Object.create(smartxml.DocumentNode.prototype);
 
 var WLXMLDocument = function(xml, options) {
 WLXMLDocumentNode.prototype = Object.create(smartxml.DocumentNode.prototype);
 
 var WLXMLDocument = function(xml, options) {
-    smartxml.Document.call(this, xml);
-    this.options = options;
-
     this.classMethods = {};
     this.classTransformations = {};
     this.classMethods = {};
     this.classTransformations = {};
+    smartxml.Document.call(this, xml);
+    this.options = options;
 };
 
 var formatter_prefix = '_wlxml_formatter_';
 };
 
 var formatter_prefix = '_wlxml_formatter_';
@@ -170,7 +194,14 @@ $.extend(WLXMLDocument.prototype, {
     ElementNodeFactory: WLXMLElementNode,
     loadXML: function(xml) {
         smartxml.Document.prototype.loadXML.call(this, xml, {silent: true});
     ElementNodeFactory: WLXMLElementNode,
     loadXML: function(xml) {
         smartxml.Document.prototype.loadXML.call(this, xml, {silent: true});
-        $(this.dom).find(':not(iframe)').addBack().contents()
+        this.trigger('contentSet');
+    },
+
+    normalizeXML: function(nativeNode) {
+        var doc = this,
+            prefixLength = 'dc:'.length;
+
+        $(nativeNode).find(':not(iframe)').addBack().contents()
             .filter(function() {return this.nodeType === Node.TEXT_NODE;})
             .each(function() {
                 var el = $(this),
             .filter(function() {return this.nodeType === Node.TEXT_NODE;})
             .each(function() {
                 var el = $(this),
@@ -249,15 +280,27 @@ $.extend(WLXMLDocument.prototype, {
                         }
                     //}
                 }
                         }
                     //}
                 }
-
+                /* globals document */
                 el.replaceWith(document.createTextNode(text.transformed));
             });
                 el.replaceWith(document.createTextNode(text.transformed));
             });
-        this.trigger('contentSet');
+        
+        $(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) {
     },
 
     registerClassTransformation: function(Transformation, className) {
-        var thisClassTransformations = (this.classTransformations[className] = this.classTransformations[className] || new transformations.TransformationStorage());
-        return thisClassTransformations.register(Transformation);
+        var thisClassTransformations = (this.classTransformations[className] = this.classTransformations[className] || {});
+        thisClassTransformations[Transformation.prototype.name] = function(args) {
+            var nodeInstance = this;
+            return nodeInstance.transform(Transformation, args);
+        };
     },
 
     registerClassMethod: function(methodName, method, className) {
     },
 
     registerClassMethod: function(methodName, method, className) {
@@ -268,19 +311,7 @@ $.extend(WLXMLDocument.prototype, {
     registerExtension: function(extension) {
         //debugger;
         smartxml.Document.prototype.registerExtension.call(this, extension);
     registerExtension: function(extension) {
         //debugger;
         smartxml.Document.prototype.registerExtension.call(this, extension);
-        var doc = this,
-            existingPropertyNames = _.values(this);
-
-        var getTrans = function(desc, methodName) {
-            if(typeof desc === 'function') {
-                desc = {impl: desc};
-            }
-            if(!desc.impl) {
-                throw new Error('Got transformation description without implementation.')
-            }
-            desc.name = desc.name || methodName;
-            return desc;
-        };
+        var doc = this;
 
         _.pairs(extension.wlxmlClass).forEach(function(pair) {
             var className = pair[0],
 
         _.pairs(extension.wlxmlClass).forEach(function(pair) {
             var className = pair[0],
@@ -293,9 +324,10 @@ $.extend(WLXMLDocument.prototype, {
             });
 
             _.pairs(classExtension.transformations || {}).forEach(function(pair) {
             });
 
             _.pairs(classExtension.transformations || {}).forEach(function(pair) {
-                var transformation = getTrans(pair[1], pair[0]);
-                doc.registerClassTransformation(transformations.createContextTransformation(transformation), className);
-            }); 
+                var name = pair[0],
+                    desc = pair[1];
+                doc.registerClassTransformation(transformations.createContextTransformation(desc, name), className);
+            });
         });
 
     }
         });
 
     }
@@ -303,8 +335,8 @@ $.extend(WLXMLDocument.prototype, {
 });
 
 var wlxmlClasses = {
 });
 
 var wlxmlClasses = {
-    'uri': {
-        attrs: {uri: {type: 'string'}}
+    'link': {
+        attrs: {href: {type: 'string'}}
     }
 };
 
     }
 };