Visual editor - making sure only wlxml-tags are put into edit pane navigation
[fnpeditor.git] / modules / rng.js
index 98b4619..0a39ed8 100644 (file)
@@ -11,36 +11,74 @@ rng.modules.rng = function(sandbox) {
     eventHandlers.skelton = {\r
         ready: function() {\r
             sandbox.getModule('tabsManager').start();\r
+        },\r
+        'cmd.save': function() {\r
+            var editorSlugs = ['visual', 'source'];\r
+            var slug = sandbox.getModule('tabsManager').getCurrentSlug();\r
+            if(_.contains(editorSlugs, slug)) {\r
+                var editor = sandbox.getModule(slug+'Editor');\r
+                if(editor.isDirty()) {\r
+                    sandbox.getModule('data').commitDocument(editor.getDocument(), slug + '_edit');\r
+                    editor.setDirty(false);\r
+                }\r
+            }\r
+            sandbox.getModule('data').saveDocument();\r
         }\r
     };\r
     \r
     eventHandlers.tabsManager = {\r
         ready: function() {\r
             sandbox.getModule('skelton').setMainView(sandbox.getModule('tabsManager').getView());\r
-            _.each(['sourceEditor', 'visualEditor'], function(moduleName) {\r
+            _.each(['visualEditor', 'sourceEditor'], function(moduleName) {\r
                 sandbox.getModule(moduleName).start();\r
             });\r
+        },\r
+        leaving: function(slug) {\r
+            if(slug === 'source' || slug === 'visual') {\r
+                var editor = sandbox.getModule(slug+'Editor');\r
+                if(editor.isDirty()) {\r
+                    sandbox.getModule('data').commitDocument(editor.getDocument(), slug + '_edit');\r
+                    editor.setDirty(false);\r
+                }\r
+            }\r
+        },\r
+        showed: function(slug) {\r
+            if(slug === 'visual')\r
+                sandbox.getModule('visualEditor').onShowed();\r
         }\r
     };\r
     \r
     eventHandlers.sourceEditor = {\r
         ready: function() {\r
-            addTab('Source', 'source',  sandbox.getModule('sourceEditor').getView());\r
+            addTab(gettext('Source'), 'source',  sandbox.getModule('sourceEditor').getView());\r
+            sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());\r
         }\r
     };\r
     \r
     eventHandlers.visualEditor = {\r
         ready: function() {\r
-            addTab('Visual', 'visual', sandbox.getModule('visualEditor').getView());\r
+            sandbox.getModule('visualEditor').setDocument(sandbox.getModule('data').getDocument());\r
+            addTab(gettext('Visual'), 'visual', sandbox.getModule('visualEditor').getView());\r
+            \r
         }\r
     };\r
     \r
+    eventHandlers.data = {\r
+        ready: function() {\r
+            sandbox.getModule('skelton').start();\r
+        },\r
+        documentChanged: function(document, reason) {\r
+            var slug = (reason === 'visual_edit' ? 'source' : 'visual');\r
+            sandbox.getModule(slug+'Editor').setDocument(document);\r
+        }\r
+    }\r
+    \r
     \r
     /* api */\r
     \r
     return {\r
         start: function() {\r
-            sandbox.getModule('skelton').start();\r
+            sandbox.getModule('data').start();\r
         },\r
         handleEvent: function(moduleName, eventName, args) {\r
             if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {\r