Refactoring
[fnpeditor.git] / modules / rng / rng.js
index e1277e5..4189902 100644 (file)
@@ -4,51 +4,138 @@ define([
 'views/tabs/tabs',\r
 'libs/text!./mainLayout.html',\r
 'libs/text!./editingLayout.html',\r
-], function(layout, vbox, tabs, mainLayoutTemplate, editingLayoutTemplate) {\r
+'libs/text!./diffLayout.html',\r
+], function(layout, vbox, tabs, mainLayoutTemplate, visualEditingLayoutTemplate, diffLayoutTemplate) {\r
+\r
+'use strict';\r
 \r
 return function(sandbox) {\r
-    'use strict';\r
     \r
-    var mainTabs = (new tabs.View()).render();\r
-    var mainLayout = new layout.Layout(mainLayoutTemplate);\r
-    var editingLayout = new layout.Layout(editingLayoutTemplate);\r
+    function addMainTab(title, slug, view) {\r
+        views.mainTabs.addTab(title, slug, view);\r
+    }\r
+    \r
+    var dirty = {\r
+        sourceEditor: false,\r
+        documentCanvas: false,\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
-    sandbox.getDOM().append(mainLayout.getAsView());\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
-    function addTab(title, slug, view) {\r
-        mainTabs.addTab(title, slug, view);\r
+    var commands = {\r
+        highlightDocumentNode: function(canvasNode, origin) {\r
+            ['documentCanvas', 'nodeBreadCrumbs', 'nodeFamilyTree'].forEach(function(moduleName) {\r
+                if(!origin || moduleName != origin)\r
+                    sandbox.getModule(moduleName).highlightNode(canvasNode)\r
+            });\r
+        },\r
+        dimDocumentNode: function(canvasNode, origin) {\r
+            ['documentCanvas', 'nodeBreadCrumbs', 'nodeFamilyTree'].forEach(function(moduleName) {\r
+                if(!origin || moduleName != origin)\r
+                    sandbox.getModule(moduleName).dimNode(canvasNode)\r
+            });\r
+        },\r
+        selectNode: function(canvasNode, origin) {\r
+            sandbox.getModule('documentCanvas').selectNode(canvasNode);\r
+            sandbox.getModule('nodePane').setNode(canvasNode);\r
+            sandbox.getModule('nodeFamilyTree').setNode(canvasNode);\r
+            sandbox.getModule('nodeBreadCrumbs').setNode(canvasNode);\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
+    var views = {\r
+        mainLayout: new layout.Layout(mainLayoutTemplate),\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
+        diffLayout: new layout.Layout(diffLayoutTemplate)\r
+    }\r
+    \r
+    views.visualEditing.setView('rightColumn', views.visualEditingSidebar.getAsView());\r
+    addMainTab('Edytor', 'editor', views.visualEditing.getAsView());\r
+    addMainTab(gettext('Source'), 'sourceEditor',  '');\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
+            synchronizeTab(event.prevSlug);\r
+        }\r
+    });\r
+    \r
     /* Events handling */\r
     \r
     var eventHandlers = {};\r
      \r
     eventHandlers.sourceEditor = {\r
         ready: function() {\r
-            addTab(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
-    };\r
-    \r
-    eventHandlers.visualEditor = {\r
-        ready: function() {\r
-            sandbox.getModule('visualEditor').setDocument(sandbox.getModule('data').getDocument());\r
-            addTab(gettext('Visual'), 'visual', sandbox.getModule('visualEditor').getView());\r
-            \r
+        },\r
+        xmlChanged: function() {\r
+            dirty.sourceEditor = true;\r
+        },\r
+        documentSet: function() {\r
+            dirty.sourceEditor = false;\r
         }\r
     };\r
     \r
     eventHandlers.data = {\r
         ready: function() {\r
-            mainLayout.setView('mainView', mainTabs.getAsView());\r
+            views.mainLayout.setView('mainView', views.mainTabs.getAsView());\r
             \r
-            _.each(['visualEditor', 'sourceEditor', 'documentCanvas', 'nodePane', 'metadataEditor', 'nodeFamilyTree', '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
-            var slug = (reason === 'visual_edit' ? 'source' : 'visual');\r
-            sandbox.getModule(slug+'Editor').setDocument(document);\r
+            commands.resetDocument(document, reason);\r
         },\r
         savingStarted: function() {\r
             sandbox.getModule('mainBar').setCommandEnabled('save', false);\r
@@ -56,67 +143,74 @@ return function(sandbox) {
         },\r
         savingEnded: function(status) {\r
             sandbox.getModule('mainBar').setCommandEnabled('save', true);\r
-            sandbox.getModule('indicator').clearMessage();\r
-        }\r
-    }\r
-    \r
-    eventHandlers.rng2 = {\r
-        ready: function() {\r
-           addTab('rng2 test', 'rng2test', sandbox.getModule('rng2').getView());\r
-           \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
-            mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());\r
+            sandbox.getModule('mainBar').setVersion(sandbox.getModule('data').getDocumentVersion());\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
     eventHandlers.indicator = {\r
         ready: function() {\r
-            mainLayout.setView('messages', sandbox.getModule('indicator').getView());\r
+            views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());\r
         }\r
     };\r
     \r
-    \r
-    var sidebar = (new tabs.View({stacked: true})).render();\r
-    var box = new vbox.VBox();\r
-    editingLayout.setView('rightColumn', sidebar.getAsView());\r
-    addTab('rng2 test', 'rng2test', editingLayout.getAsView());\r
+\r
     \r
     eventHandlers.documentCanvas = {\r
         ready: function() {\r
             sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());\r
-            editingLayout.setView('leftColumn', sandbox.getModule('documentCanvas').getView());\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
+        nodeSelected: function(canvasNode) {\r
+            commands.selectNode(canvasNode);\r
         },\r
         \r
         contentChanged: function() {\r
-        \r
+            dirty.documentCanvas = true;\r
         },\r
         \r
-        nodeHovered: function(node) {\r
-            \r
+        nodeHovered: function(canvasNode) {\r
+            commands.highlightDocumentNode(canvasNode);\r
         },\r
         \r
-        nodeBlured: function(node) {\r
-        \r
+        nodeBlured: function(canvasNode) {\r
+            commands.dimDocumentNode(canvasNode);\r
         }\r
     };\r
 \r
     eventHandlers.nodePane = {\r
         ready: function() {\r
-            //sidebar.addTab({icon: 'pencil'}, 'nodePane', sandbox.getModule('nodePane').getView());\r
-            box.appendView(sandbox.getModule('nodePane').getView());\r
-            sidebar.addTab({icon: 'pencil'}, 'edit', box.getAsView());\r
+            views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());\r
         },\r
         \r
         nodeChanged: function(attr, value) {\r
@@ -126,24 +220,81 @@ return function(sandbox) {
     \r
     eventHandlers.metadataEditor = {\r
         ready: function() {\r
-            sandbox.getModule('metadataEditor').setMetadata(sandbox.getModule('data').getDocument());\r
-            sidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());\r
-        }\r
+            sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());\r
+            views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());\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
-            //sidebar.addTab({icon: 'home'}, 'family', sandbox.getModule('nodeFamilyTree').getView());\r
-            box.appendView(sandbox.getModule('nodeFamilyTree').getView());\r
+            views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());\r
+        },\r
+        nodeEntered: function(canvasNode) {\r
+            commands.highlightDocumentNode(canvasNode, 'nodeFamilyTree');\r
+        },\r
+        nodeLeft: function(canvasNode) {\r
+            commands.dimDocumentNode(canvasNode, 'nodeFamilyTree');\r
+        },\r
+        nodeSelected: function(canvasNode) {\r
+            commands.selectNode(canvasNode);\r
+        }\r
+    };\r
+    \r
+    eventHandlers.documentToolbar = {\r
+        ready: function() {\r
+            views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());\r
+        },\r
+        toggleGrid: function(toggle) {\r
+            sandbox.getModule('documentCanvas').toggleGrid(toggle);\r
+        },\r
+        newNodeRequested: function(wlxmlTag, wlxmlClass) {\r
+                sandbox.getModule('documentCanvas').insertNewNode(wlxmlTag, wlxmlClass);\r
+        },\r
+        command: function(cmd, meta) {\r
+            sandbox.getModule('documentCanvas').command(cmd, meta);\r
+        }\r
+    };\r
+    \r
+    eventHandlers.nodeBreadCrumbs = {\r
+        ready: function() {\r
+            views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());\r
         },\r
-        nodeEntered: function(id) {\r
-            sandbox.getModule('documentCanvas').highlightNode(id);\r
+        nodeHighlighted: function(canvasNode) {\r
+            commands.highlightDocumentNode(canvasNode, 'nodeBreadCrumbs');\r
         },\r
-        nodeLeft: function(id) {\r
-            sandbox.getModule('documentCanvas').dimNode(id);\r
+        nodeDimmed: function(canvasNode) {\r
+            commands.dimDocumentNode(canvasNode, 'nodeBreadCrumbs');\r
         },\r
-        nodeSelected: function(id) {\r
-            sandbox.getModule('documentCanvas').selectNode(id);\r
+        nodeSelected: function(canvasNode) {\r
+            commands.selectNode(canvasNode);\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