Fix in transformations
[fnpeditor.git] / modules / metadataEditor / metadataEditor.js
index 5c3e4fc..79302bb 100644 (file)
@@ -14,17 +14,18 @@ return function(sandbox) {
     var view = {\r
         node: $(_.template(mainTemplate)()),\r
         setup: function() {\r
+            var view = this;\r
             var metaTable = this.metaTable = this.node.find('table');\r
             \r
             this.node.find('.rng-module-metadataEditor-addBtn').click(function() {\r
                 var newRow = view._addMetaRow('', '');\r
                 $(newRow.find('td div')[0]).focus();\r
-                //isDirty = true;\r
+                sandbox.publish('metadataChanged', view.getMetadata());\r
             });\r
             \r
             this.metaTable.on('click', '.rng-visualEditor-metaRemoveBtn', function(e) {\r
                 $(e.target).closest('tr').remove();\r
-                //isDirty = true;\r
+                sandbox.publish('metadataChanged', view.getMetadata());\r
             });\r
             \r
             this.metaTable.on('keydown', '[contenteditable]', function(e) {\r
@@ -40,10 +41,16 @@ return function(sandbox) {
                     }\r
                     e.preventDefault();\r
                 }\r
-                \r
             });\r
+            \r
+            \r
+            var onKeyUp = function(e) {\r
+                if(e.which !== 13)\r
+                    sandbox.publish('metadataChanged', view.getMetadata());\r
+            }\r
+            this.metaTable.on('keyup', '[contenteditable]', _.throttle(onKeyUp, 500));\r
         },\r
-        getMetaData: function() {\r
+        getMetadata: function() {\r
             var toret = {};\r
             this.node.find('tr').each(function() {\r
                 var tr = $(this);\r
@@ -74,14 +81,32 @@ return function(sandbox) {
         start: function() {\r
             sandbox.publish('ready');\r
         },\r
-        setMetadata: function(xml) {\r
+        setDocument: function(xml) {\r
             view.setMetadata(transformations.getMetadata(xml));\r
+            sandbox.publish('metadataSet');\r
         },\r
         getMetadata: function() {\r
-            return view.getMetadata();\r
+            return transformations.getXML(view.getMetadata());\r
         },\r
         getView: function() {\r
             return view.node;\r
+        },\r
+        attachMetadata: function(document) {\r
+            var toret = $('<div>');\r
+            toret.append($(document));\r
+            var meta = $('<metadata></metadata>\n').append(transformations.getXML(view.getMetadata()));\r
+            \r
+            var metadata = toret.find('metadata');\r
+            if(metadata.length === 0) {\r
+                var section = toret.find('section');\r
+                section = section.length ? $(section[0]) : null;\r
+                if(section) {\r
+                    section.prepend(meta)\r
+                }\r
+            } else {\r
+                metadata.replaceWith(meta);\r
+            }\r
+            return toret.html();\r
         }\r
         \r
     };\r