Synchronizing changes
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 12 Jun 2013 10:16:30 +0000 (12:16 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 12 Jun 2013 10:16:30 +0000 (12:16 +0200)
modules/documentCanvas/documentCanvas.js
modules/documentCanvas/transformations.js
modules/metadataEditor/metadataEditor.js
modules/rng/rng.js
modules/sourceEditor/sourceEditor.js
views/tabs/tabs.js

index 8f3a3c6..df11ea0 100644 (file)
@@ -232,11 +232,16 @@ return function(sandbox) {
             var transformed = transformations.fromXML.getDocumentDescription(xml);\r
             view.setBody(transformed.HTMLTree);\r
             view.selectFirstNode();\r
-            //isDirty = false;\r
+            sandbox.publish('documentSet');\r
+        },\r
+        getDocument: function() {\r
+            return transformations.toXML.getXML(view.getBody());\r
         },\r
         modifyCurrentNode: function(attr, value) {\r
-            if(view.currentNode)\r
+            if(view.currentNode) {\r
                 view.currentNode.attr('wlxml-'+attr, value);\r
+                sandbox.publish('contentEdited');\r
+            }\r
         },\r
         highlightNode: function(wlxmlNode) {\r
             view.highlightNode(wlxmlNode);\r
index cd20d2b..de72efd 100644 (file)
@@ -54,9 +54,9 @@ define(['libs/jquery-1.9.1.min'], function($) {
     }\r
 \r
     transformations.toXML = {\r
-        getXML: function(documentDescription) {\r
+        getXML: function(body) {\r
             \r
-            var inner = $(documentDescription.HTMLTree);\r
+            var inner = $(body);\r
             var toret = $('<div></div>');\r
             toret.append(inner);\r
             \r
@@ -80,25 +80,7 @@ define(['libs/jquery-1.9.1.min'], function($) {
                 toret.append(div.contents());\r
                 return toret;\r
             });\r
-            \r
-            var meta = $('<metadata></metadata>\n');\r
-            _.each(_.keys(documentDescription.metadata), function(key) {\r
-                meta.append('\n\t<dc:'+key+'>' + documentDescription.metadata[key] + '</dc:'+key+'>');\r
-            });\r
-            meta.append('\n');\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
-            \r
-            \r
+\r
             return vkbeautify.xml(toret.html());\r
         }\r
     }\r
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
index 6f65c19..6029e7c 100644 (file)
@@ -13,6 +13,12 @@ return function(sandbox) {
         views.mainTabs.addTab(title, slug, view);\r
     }\r
     \r
+    var dirty = {\r
+        sourceEditor: false,\r
+        documentCanvas: false,\r
+        metadataEditor: false,\r
+    };\r
+    \r
     var commands = {\r
         highlightDocumentNode: function(wlxmlNode, origin) {\r
             ['documentCanvas', 'nodeBreadCrumbs', 'nodeFamilyTree'].forEach(function(moduleName) {\r
@@ -51,6 +57,21 @@ return function(sandbox) {
     \r
     views.visualEditingSidebar.addTab({icon: 'pencil'}, 'edit', views.currentNodePaneLayout.getAsView());\r
 \r
+    views.mainTabs.on('tabSelected', function(event) {\r
+        if(event.prevSlug) {\r
+            if(event.prevSlug === 'sourceEditor' && dirty.sourceEditor) {\r
+                sandbox.getModule('data').commitDocument(sandbox.getModule('sourceEditor').getDocument(), 'source_edit');\r
+            }\r
+            if(event.prevSlug === 'editor' && (dirty.documentCanvas || dirty.metadataEditor)) {\r
+                var doc = dirty.documentCanvas ? sandbox.getModule('documentCanvas').getDocument() : sandbox.getModule('data').getDocument();\r
+                if(dirty.metadataEditor) {\r
+                    doc = sandbox.getModule('metadataEditor').attachMetadata(doc);\r
+                }\r
+                sandbox.getModule('data').commitDocument(doc, 'edit');\r
+            }\r
+            \r
+        }\r
+    });\r
     \r
     /* Events handling */\r
     \r
@@ -58,8 +79,14 @@ return function(sandbox) {
      \r
     eventHandlers.sourceEditor = {\r
         ready: function() {\r
-            addMainTab(gettext('Source'), 'source',  sandbox.getModule('sourceEditor').getView());\r
+            addMainTab(gettext('Source'), 'sourceEditor',  sandbox.getModule('sourceEditor').getView());\r
             sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());\r
+        },\r
+        xmlChanged: function() {\r
+            dirty.sourceEditor = true;\r
+        },\r
+        documentSet: function() {\r
+            dirty.sourceEditor = false;\r
         }\r
     };\r
     \r
@@ -72,8 +99,15 @@ return function(sandbox) {
             });\r
         },\r
         documentChanged: function(document, reason) {\r
-            var slug = (reason === 'visual_edit' ? 'source' : 'visual');\r
-            sandbox.getModule(slug+'Editor').setDocument(document);\r
+            var modules = [];\r
+            if(reason === 'source_edit')\r
+                modules = ['documentCanvas', 'metadataEditor'];\r
+            else if (reason === 'edit')\r
+                modules = ['sourceEditor'];\r
+                \r
+            modules.forEach(function(moduleName) {\r
+                sandbox.getModule(moduleName).setDocument(document);\r
+            });\r
         },\r
         savingStarted: function() {\r
             sandbox.getModule('mainBar').setCommandEnabled('save', false);\r
@@ -107,13 +141,16 @@ return function(sandbox) {
             sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());\r
             views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());\r
         },\r
+        documentSet: function() {\r
+            dirty.documentCanvas = false;\r
+        },\r
         \r
         nodeSelected: function(wlxmlNode) {\r
             commands.selectNode(wlxmlNode);\r
         },\r
         \r
         contentChanged: function() {\r
-        \r
+            dirty.documentCanvas = true;\r
         },\r
         \r
         nodeHovered: function(wlxmlNode) {\r
@@ -137,9 +174,15 @@ return function(sandbox) {
     \r
     eventHandlers.metadataEditor = {\r
         ready: function() {\r
-            sandbox.getModule('metadataEditor').setMetadata(sandbox.getModule('data').getDocument());\r
+            sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());\r
             views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());\r
-        }\r
+        },\r
+        metadataChanged: function(metadata) {\r
+            dirty.metadataEditor = true;\r
+        },\r
+        metadataSet: function() {\r
+            dirty.metadataEditor = false;\r
+        },\r
     };\r
     \r
     eventHandlers.nodeFamilyTree = {\r
index 248c02d..bd56c43 100644 (file)
@@ -3,17 +3,16 @@ define(function() {
 return function(sandbox) {\r
 \r
     var view = $(sandbox.getTemplate('main')());\r
-    var isDirty = false;\r
     \r
     var editor = ace.edit(view.find('#rng-sourceEditor-editor')[0]);\r
     editor.setTheme("ace/theme/chrome");\r
     editor.getSession().setMode("ace/mode/xml");\r
     $('textarea', view).on('keyup', function() {\r
-        isDirty = true;\r
+        sandbox.publish('xmlChanged');\r
     });\r
     \r
     editor.getSession().on('change', function() {\r
-        isDirty = true;\r
+        sandbox.publish('xmlChanged');\r
     })\r
     return {\r
         start: function() {\r
@@ -25,18 +24,11 @@ return function(sandbox) {
         setDocument: function(document) {\r
             editor.setValue(document);\r
             editor.gotoLine(0)\r
-            isDirty = false;\r
+            sandbox.publish('documentSet');\r
         },\r
         getDocument: function() {\r
             return editor.getValue();\r
-        },\r
-        isDirty: function() {\r
-            return isDirty;\r
-        },\r
-        setDirty: function(dirty) {\r
-            isDirty = dirty;\r
         }\r
-    \r
     }\r
 };\r
 \r
index 52c8160..f533785 100644 (file)
@@ -57,8 +57,9 @@ define([
                 this.nodes.tabBar.find('.active').removeClass('active');\r
                 this.nodes.tabBar.find('a[href="#'+slug+'"]').parent().addClass('active');\r
                 \r
+                var prevSlug = this.selectedTab;\r
                 this.selectedTab = slug;\r
-                this.trigger('tabSelected', slug);\r
+                this.trigger('tabSelected', {slug: slug, prevSlug: prevSlug});\r
             }\r
         },\r
         \r