X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/a5871ab5512d87a33e7e95451d26ee7d3e789742..93ea4b37138c4f617a9f8caa75485821d5df4041:/modules/rng.js?ds=sidebyside diff --git a/modules/rng.js b/modules/rng.js index 0d40c2e..ffb228d 100644 --- a/modules/rng.js +++ b/modules/rng.js @@ -1,7 +1,9 @@ -rng.modules.rng = function(sandbox) { +define(function() { - function addTab(title, view) { - sandbox.getModule('tabsModule').addTab(title, view); +return function(sandbox) { + + function addTab(title, slug, view) { + sandbox.getModule('tabsManager').addTab(title, slug, view); } /* Events handling */ @@ -11,30 +13,82 @@ rng.modules.rng = function(sandbox) { eventHandlers.skelton = { ready: function() { sandbox.getModule('tabsManager').start(); + }, + 'cmd.save': function() { + var editorSlugs = ['visual', 'source']; + var slug = sandbox.getModule('tabsManager').getCurrentSlug(); + if(_.contains(editorSlugs, slug)) { + var editor = sandbox.getModule(slug+'Editor'); + if(editor.isDirty()) { + sandbox.getModule('data').commitDocument(editor.getDocument(), slug + '_edit'); + editor.setDirty(false); + } + } + sandbox.getModule('data').saveDocument(); } }; eventHandlers.tabsManager = { ready: function() { sandbox.getModule('skelton').setMainView(sandbox.getModule('tabsManager').getView()); - _.each(['sourceEditor', 'visualEditor'], function(moduleName) { + _.each(['visualEditor', 'sourceEditor'], function(moduleName) { sandbox.getModule(moduleName).start(); }); + }, + leaving: function(slug) { + if(slug === 'source' || slug === 'visual') { + var editor = sandbox.getModule(slug+'Editor'); + if(editor.isDirty()) { + sandbox.getModule('data').commitDocument(editor.getDocument(), slug + '_edit'); + editor.setDirty(false); + } + } + }, + showed: function(slug) { + if(slug === 'visual') + sandbox.getModule('visualEditor').onShowed(); } }; eventHandlers.sourceEditor = { ready: function() { - addTab('Source', sandbox.getModule('sourceEditor').getView()); + addTab(gettext('Source'), 'source', sandbox.getModule('sourceEditor').getView()); + sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument()); + } + }; + + eventHandlers.visualEditor = { + ready: function() { + sandbox.getModule('visualEditor').setDocument(sandbox.getModule('data').getDocument()); + addTab(gettext('Visual'), 'visual', sandbox.getModule('visualEditor').getView()); + } }; + eventHandlers.data = { + ready: function() { + sandbox.getModule('skelton').start(); + }, + documentChanged: function(document, reason) { + var slug = (reason === 'visual_edit' ? 'source' : 'visual'); + sandbox.getModule(slug+'Editor').setDocument(document); + }, + savingStarted: function() { + sandbox.getModule('skelton').deactivateCommand('save'); + sandbox.getModule('skelton').showMessage(gettext('Saving...')); + }, + savingEnded: function(status) { + sandbox.getModule('skelton').activateCommand('save'); + sandbox.getModule('skelton').clearMessage(); + } + } + /* api */ return { start: function() { - sandbox.getModule('skelton').start(); + sandbox.getModule('data').start(); }, handleEvent: function(moduleName, eventName, args) { if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) { @@ -42,4 +96,6 @@ rng.modules.rng = function(sandbox) { } } } -}; \ No newline at end of file +}; + +}); \ No newline at end of file