Saving document
[fnpeditor.git] / modules / rng / rng.js
index 308549c..8202346 100644 (file)
@@ -13,6 +13,58 @@ return function(sandbox) {
         views.mainTabs.addTab(title, slug, view);\r
     }\r
     \r
+    function tabIsDirty(slug) {\r
+        if(slug === 'editor' && (dirty.documentCanvas || dirty.metadataEditor))\r
+            return true;\r
+        if(slug === 'sourceEditor' && dirty.sourceEditor)\r
+            return true;\r
+        return false;\r
+    }\r
+    \r
+    var dirty = {\r
+        sourceEditor: false,\r
+        documentCanvas: false,\r
+        metadataEditor: false,\r
+    };\r
+    \r
+    var synchronizeTab = function(slug) {\r
+        if(tabIsDirty(slug)) {\r
+            if(slug === 'sourceEditor') {\r
+                sandbox.getModule('data').commitDocument(sandbox.getModule('sourceEditor').getDocument(), 'source_edit');\r
+            }\r
+            if(slug === 'editor') {\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
+    var commands = {\r
+        highlightDocumentNode: function(wlxmlNode, origin) {\r
+            ['documentCanvas', 'nodeBreadCrumbs', 'nodeFamilyTree'].forEach(function(moduleName) {\r
+                if(!origin || moduleName != origin)\r
+                    sandbox.getModule(moduleName).highlightNode(wlxmlNode)\r
+            });\r
+        },\r
+        dimDocumentNode: function(wlxmlNode, origin) {\r
+            ['documentCanvas', 'nodeBreadCrumbs', 'nodeFamilyTree'].forEach(function(moduleName) {\r
+                if(!origin || moduleName != origin)\r
+                    sandbox.getModule(moduleName).dimNode(wlxmlNode)\r
+            });\r
+        },\r
+        selectNode: function(wlxmlNode, origin) {\r
+            sandbox.getModule('documentCanvas').selectNode(wlxmlNode);\r
+            sandbox.getModule('nodePane').setNode(wlxmlNode);\r
+            sandbox.getModule('nodeFamilyTree').setNode(wlxmlNode);\r
+            sandbox.getModule('nodeBreadCrumbs').setNode(wlxmlNode);\r
+            \r
+        }\r
+    }\r
+    \r
+\r
     var views = {\r
         mainLayout: new layout.Layout(mainLayoutTemplate),\r
         mainTabs: (new tabs.View()).render(),\r
@@ -27,9 +79,12 @@ return function(sandbox) {
     sandbox.getDOM().append(views.mainLayout.getAsView());\r
     \r
     views.visualEditingSidebar.addTab({icon: 'pencil'}, 'edit', views.currentNodePaneLayout.getAsView());\r
-    \r
-    \r
 \r
+    views.mainTabs.on('tabSelected', function(event) {\r
+        if(event.prevSlug) {\r
+            synchronizeTab(event.prevSlug);\r
+        }\r
+    });\r
     \r
     /* Events handling */\r
     \r
@@ -37,8 +92,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
@@ -51,8 +112,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
@@ -69,7 +137,8 @@ return function(sandbox) {
             views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());\r
         },\r
         'cmd.save': function() {\r
-            sandbox.getModule('data').fakeSave();\r
+            synchronizeTab(views.mainTabs.getCurrentSlug());\r
+            sandbox.getModule('data').saveDocument();\r
         }\r
     }\r
     \r
@@ -86,28 +155,24 @@ 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(node) {\r
-            sandbox.getModule('nodePane').setNode(node);\r
-            sandbox.getModule('nodeFamilyTree').setNode(node);\r
-            sandbox.getModule('nodeBreadCrumbs').setNode(node);\r
+        nodeSelected: function(wlxmlNode) {\r
+            commands.selectNode(wlxmlNode);\r
         },\r
         \r
         contentChanged: function() {\r
-        \r
+            dirty.documentCanvas = true;\r
         },\r
         \r
-        nodeHovered: function(node) {\r
-            sandbox.getModule('documentCanvas').highlightNode(node.attr('id'));\r
-            sandbox.getModule('nodeFamilyTree').highlightNode(node.attr('id'));\r
-            sandbox.getModule('nodeBreadCrumbs').highlightNode(node.attr('id'));\r
-            \r
+        nodeHovered: function(wlxmlNode) {\r
+            commands.highlightDocumentNode(wlxmlNode);\r
         },\r
         \r
-        nodeBlured: function(node) {\r
-            sandbox.getModule('documentCanvas').dimNode(node.attr('id'));\r
-            sandbox.getModule('nodeFamilyTree').dimNode(node.attr('id'));\r
-            sandbox.getModule('nodeBreadCrumbs').dimNode(node.attr('id'));\r
+        nodeBlured: function(wlxmlNode) {\r
+            commands.dimDocumentNode(wlxmlNode);\r
         }\r
     };\r
 \r
@@ -123,23 +188,29 @@ 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
         ready: function() {\r
             views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());\r
         },\r
-        nodeEntered: function(id) {\r
-            sandbox.getModule('documentCanvas').highlightNode(id);\r
+        nodeEntered: function(wlxmlNode) {\r
+            commands.highlightDocumentNode(wlxmlNode, 'nodeFamilyTree');\r
         },\r
-        nodeLeft: function(id) {\r
-            sandbox.getModule('documentCanvas').dimNode(id);\r
+        nodeLeft: function(wlxmlNode) {\r
+            commands.dimDocumentNode(wlxmlNode, 'nodeFamilyTree');\r
         },\r
-        nodeSelected: function(id) {\r
-            sandbox.getModule('documentCanvas').selectNode(id);\r
+        nodeSelected: function(wlxmlNode) {\r
+            commands.selectNode(wlxmlNode);\r
         }\r
     };\r
     \r
@@ -163,14 +234,14 @@ return function(sandbox) {
         ready: function() {\r
             views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());\r
         },\r
-        nodeHighlighted: function(id) {\r
-            sandbox.getModule('documentCanvas').highlightNode(id);\r
+        nodeHighlighted: function(wlxmlNode) {\r
+            commands.highlightDocumentNode(wlxmlNode, 'nodeBreadCrumbs');\r
         },\r
-        nodeDimmed: function(id) {\r
-            sandbox.getModule('documentCanvas').dimNode(id);\r
+        nodeDimmed: function(wlxmlNode) {\r
+            commands.dimDocumentNode(wlxmlNode, 'nodeBreadCrumbs');\r
         },\r
-        nodeSelected: function(id) {\r
-            sandbox.getModule('documentCanvas').selectNode(id);\r
+        nodeSelected: function(wlxmlNode) {\r
+            commands.selectNode(wlxmlNode);\r
         }        \r
     }\r
     \r