height wip: sourceEditor
[fnpeditor.git] / modules / rng / rng.js
index 6029e7c..276b0db 100644 (file)
@@ -4,10 +4,12 @@ define([
 'views/tabs/tabs',\r
 'libs/text!./mainLayout.html',\r
 'libs/text!./editingLayout.html',\r
 'views/tabs/tabs',\r
 'libs/text!./mainLayout.html',\r
 'libs/text!./editingLayout.html',\r
-], function(layout, vbox, tabs, mainLayoutTemplate, visualEditingLayoutTemplate) {\r
+'libs/text!./diffLayout.html',\r
+], function(layout, vbox, tabs, mainLayoutTemplate, visualEditingLayoutTemplate, diffLayoutTemplate) {\r
+\r
+'use strict';\r
 \r
 return function(sandbox) {\r
 \r
 return function(sandbox) {\r
-    'use strict';\r
     \r
     function addMainTab(title, slug, view) {\r
         views.mainTabs.addTab(title, slug, view);\r
     \r
     function addMainTab(title, slug, view) {\r
         views.mainTabs.addTab(title, slug, view);\r
@@ -19,6 +21,34 @@ return function(sandbox) {
         metadataEditor: false,\r
     };\r
     \r
         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
+            var reason, doc;\r
+            if(slug === 'sourceEditor') {\r
+                doc = sandbox.getModule('sourceEditor').getDocument();\r
+                reason = 'source_edit';\r
+                dirty.sourceEditor = false;\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
+                reason = 'edit';\r
+                dirty.documentCanvas = dirty.metadataEditor = false;\r
+            }\r
+            sandbox.getModule('data').commitDocument(doc, reason);\r
+        }\r
+    }\r
+    \r
     var commands = {\r
         highlightDocumentNode: function(wlxmlNode, origin) {\r
             ['documentCanvas', 'nodeBreadCrumbs', 'nodeFamilyTree'].forEach(function(moduleName) {\r
     var commands = {\r
         highlightDocumentNode: function(wlxmlNode, origin) {\r
             ['documentCanvas', 'nodeBreadCrumbs', 'nodeFamilyTree'].forEach(function(moduleName) {\r
@@ -38,6 +68,19 @@ return function(sandbox) {
             sandbox.getModule('nodeFamilyTree').setNode(wlxmlNode);\r
             sandbox.getModule('nodeBreadCrumbs').setNode(wlxmlNode);\r
             \r
             sandbox.getModule('nodeFamilyTree').setNode(wlxmlNode);\r
             sandbox.getModule('nodeBreadCrumbs').setNode(wlxmlNode);\r
             \r
+        },\r
+        resetDocument: function(document, reason) {\r
+            var modules = [];\r
+            if(reason === 'source_edit')\r
+                modules = ['documentCanvas', 'metadataEditor'];\r
+            else if (reason === 'edit')\r
+                modules = ['sourceEditor'];\r
+            else if (reason === 'revert')\r
+                modules = ['documentCanvas', 'metadataEditor', 'sourceEditor'];\r
+                \r
+            modules.forEach(function(moduleName) {\r
+                sandbox.getModule(moduleName).setDocument(document);\r
+            });\r
         }\r
     }\r
     \r
         }\r
     }\r
     \r
@@ -47,29 +90,22 @@ return function(sandbox) {
         mainTabs: (new tabs.View()).render(),\r
         visualEditing: new layout.Layout(visualEditingLayoutTemplate),\r
         visualEditingSidebar: (new tabs.View({stacked: true})).render(),\r
         mainTabs: (new tabs.View()).render(),\r
         visualEditing: new layout.Layout(visualEditingLayoutTemplate),\r
         visualEditingSidebar: (new tabs.View({stacked: true})).render(),\r
-        currentNodePaneLayout: new vbox.VBox()\r
+        currentNodePaneLayout: new vbox.VBox(),\r
+        diffLayout: new layout.Layout(diffLayoutTemplate)\r
     }\r
     \r
     views.visualEditing.setView('rightColumn', views.visualEditingSidebar.getAsView());\r
     addMainTab('Edytor', 'editor', views.visualEditing.getAsView());\r
     \r
     }\r
     \r
     views.visualEditing.setView('rightColumn', views.visualEditingSidebar.getAsView());\r
     addMainTab('Edytor', 'editor', views.visualEditing.getAsView());\r
     \r
+    addMainTab('Historia', 'history', views.diffLayout.getAsView());\r
+    \r
     sandbox.getDOM().append(views.mainLayout.getAsView());\r
     \r
     views.visualEditingSidebar.addTab({icon: 'pencil'}, 'edit', views.currentNodePaneLayout.getAsView());\r
 \r
     views.mainTabs.on('tabSelected', function(event) {\r
         if(event.prevSlug) {\r
     sandbox.getDOM().append(views.mainLayout.getAsView());\r
     \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
+            synchronizeTab(event.prevSlug);\r
         }\r
     });\r
     \r
         }\r
     });\r
     \r
@@ -94,20 +130,12 @@ return function(sandbox) {
         ready: function() {\r
             views.mainLayout.setView('mainView', views.mainTabs.getAsView());\r
             \r
         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', 'diffViewer'], function(moduleName) {\r
                 sandbox.getModule(moduleName).start();\r
             });\r
         },\r
         documentChanged: function(document, reason) {\r
                 sandbox.getModule(moduleName).start();\r
             });\r
         },\r
         documentChanged: function(document, reason) {\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
+            commands.resetDocument(document, reason);\r
         },\r
         savingStarted: function() {\r
             sandbox.getModule('mainBar').setCommandEnabled('save', false);\r
         },\r
         savingStarted: function() {\r
             sandbox.getModule('mainBar').setCommandEnabled('save', false);\r
@@ -115,16 +143,34 @@ return function(sandbox) {
         },\r
         savingEnded: function(status) {\r
             sandbox.getModule('mainBar').setCommandEnabled('save', true);\r
         },\r
         savingEnded: function(status) {\r
             sandbox.getModule('mainBar').setCommandEnabled('save', true);\r
-            sandbox.getModule('indicator').clearMessage();\r
+            sandbox.getModule('indicator').clearMessage({message:'Dokument zapisany'});\r
+        },\r
+        restoringStarted: function(event) {\r
+            sandbox.getModule('mainBar').setCommandEnabled('save', false);\r
+            sandbox.getModule('indicator').showMessage(gettext('Restoring version ' + event.version + '...'));\r
+        },\r
+        historyItemAdded: function(item) {\r
+            sandbox.getModule('documentHistory').addHistory([item], {animate: true});\r
+        },\r
+        diffFetched: function(diff) {\r
+            sandbox.getModule('diffViewer').setDiff(diff);\r
+        },\r
+        documentReverted: function(event) {\r
+            commands.resetDocument(event.document, 'revert');\r
+            sandbox.getModule('mainBar').setCommandEnabled('save', true);\r
+            sandbox.getModule('indicator').clearMessage({message:'Wersja ' + event.reverted_version + ' przywrócona'});\r
+            sandbox.getModule('mainBar').setVersion(event.current_version);\r
         }\r
     }\r
     \r
     eventHandlers.mainBar = {\r
         ready: function() {\r
         }\r
     }\r
     \r
     eventHandlers.mainBar = {\r
         ready: function() {\r
+            sandbox.getModule('mainBar').setVersion(sandbox.getModule('data').getDocumentVersion());\r
             views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());\r
         },\r
         'cmd.save': function() {\r
             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
         }\r
     }\r
     \r
@@ -231,6 +277,28 @@ return function(sandbox) {
         }        \r
     }\r
     \r
         }        \r
     }\r
     \r
+    eventHandlers.documentHistory = {\r
+        ready: function() {\r
+            sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());\r
+            views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());\r
+        },\r
+        compare: function(ver1, ver2) {\r
+            sandbox.getModule('data').fetchDiff(ver1, ver2);\r
+        },\r
+        restoreVersion: function(event) {\r
+            sandbox.getModule('data').restoreVersion(event);\r
+        },\r
+        displayVersion: function(event) {\r
+            window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());\r
+        }\r
+    }\r
+    \r
+    eventHandlers.diffViewer = {\r
+        ready: function() {\r
+            views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());\r
+        }\r
+    }\r
+    \r
     /* api */\r
     \r
     return {\r
     /* api */\r
     \r
     return {\r