history wip: little animation on adding new item
[fnpeditor.git] / modules / rng / rng.js
index 6029e7c..9abe00c 100644 (file)
@@ -6,8 +6,9 @@ define([
 'libs/text!./editingLayout.html',\r
 ], function(layout, vbox, tabs, mainLayoutTemplate, visualEditingLayoutTemplate) {\r
 \r
+'use strict';\r
+\r
 return function(sandbox) {\r
-    'use strict';\r
     \r
     function addMainTab(title, slug, view) {\r
         views.mainTabs.addTab(title, slug, view);\r
@@ -19,6 +20,29 @@ return function(sandbox) {
         metadataEditor: false,\r
     };\r
     \r
+    var synchronizeTab = function(slug) {\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
+        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
@@ -59,17 +83,7 @@ return function(sandbox) {
 \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
+            synchronizeTab(event.prevSlug);\r
         }\r
     });\r
     \r
@@ -94,7 +108,7 @@ return function(sandbox) {
         ready: function() {\r
             views.mainLayout.setView('mainView', views.mainTabs.getAsView());\r
             \r
-            _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'nodePane', 'metadataEditor', 'nodeFamilyTree', 'nodeBreadCrumbs', 'mainBar', 'indicator'], function(moduleName) {\r
+            _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'nodePane', 'metadataEditor', 'nodeFamilyTree', 'nodeBreadCrumbs', 'mainBar', 'indicator', 'documentHistory'], function(moduleName) {\r
                 sandbox.getModule(moduleName).start();\r
             });\r
         },\r
@@ -116,6 +130,9 @@ return function(sandbox) {
         savingEnded: function(status) {\r
             sandbox.getModule('mainBar').setCommandEnabled('save', true);\r
             sandbox.getModule('indicator').clearMessage();\r
+        },\r
+        historyItemAdded: function(item) {\r
+            sandbox.getModule('documentHistory').addHistory([item], {animate: true});\r
         }\r
     }\r
     \r
@@ -124,7 +141,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
@@ -231,6 +249,13 @@ return function(sandbox) {
         }        \r
     }\r
     \r
+    eventHandlers.documentHistory = {\r
+        ready: function() {\r
+            sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());\r
+            addMainTab('Historia', 'history', sandbox.getModule('documentHistory').getView());\r
+        }\r
+    }\r
+    \r
     /* api */\r
     \r
     return {\r