wlxml: metadata wip - part of a new api approach
[fnpeditor.git] / src / wlxml / wlxml.js
index 7f6e931..539d792 100644 (file)
@@ -2,8 +2,9 @@ define([
     'libs/jquery',
     'libs/underscore',
     'smartxml/smartxml',
-    'smartxml/transformations'
-], function($, _, smartxml, transformations) {
+    'smartxml/transformations',
+    'wlxml/extensions/metadata/metadata'
+], function($, _, smartxml, transformations, metadataExtension) {
     
 'use strict';
 
@@ -79,7 +80,8 @@ $.extend(WLXMLElementNode.prototype, smartxml.ElementNode.prototype, {
     },
 
     _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),
@@ -89,7 +91,7 @@ $.extend(WLXMLElementNode.prototype, smartxml.ElementNode.prototype, {
                 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;
@@ -119,8 +121,23 @@ $.extend(WLXMLElementNode.prototype, smartxml.ElementNode.prototype, {
                     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;
     }
 });
@@ -143,11 +160,10 @@ var WLXMLDocumentNode = function() {
 WLXMLDocumentNode.prototype = Object.create(smartxml.DocumentNode.prototype);
 
 var WLXMLDocument = function(xml, options) {
-    smartxml.Document.call(this, xml);
-    this.options = options;
-
     this.classMethods = {};
     this.classTransformations = {};
+    smartxml.Document.call(this, xml, [metadataExtension]);
+    this.options = options;
 };
 
 var formatter_prefix = '_wlxml_formatter_';
@@ -162,6 +178,9 @@ $.extend(WLXMLDocument.prototype, {
     },
 
     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() {
@@ -244,6 +263,16 @@ $.extend(WLXMLDocument.prototype, {
                 /* globals document */
                 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.addMetadata({key: (this.tagName).toLowerCase().substr(prefixLength), value: $(this).text()});
+            });
+            metadataNode.remove();
+        });
     },
 
     registerClassTransformation: function(Transformation, className) {
@@ -286,8 +315,8 @@ $.extend(WLXMLDocument.prototype, {
 });
 
 var wlxmlClasses = {
-    'uri': {
-        attrs: {uri: {type: 'string'}}
+    'link': {
+        attrs: {href: {type: 'string'}}
     }
 };