rng.modules.rng = function(sandbox) {\r
\r
- function addTab(title, view) {\r
- sandbox.getModule('tabsModule').addTab(title, view);\r
+ function addTab(title, slug, view) {\r
+ sandbox.getModule('tabsManager').addTab(title, slug, view);\r
}\r
\r
/* Events handling */\r
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
};\r
\r
eventHandlers.sourceEditor = {\r
ready: function() {\r
- addTab('Source', sandbox.getModule('sourceEditor').getView());\r
+ addTab('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
+ }\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