From: Aleksander Ɓukasz Date: Wed, 10 Apr 2013 08:33:56 +0000 (+0200) Subject: Basic communication between source and visual editors; data module X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/0add3b13ce4ef42557d9ca33342a159b80a4f42f Basic communication between source and visual editors; data module --- diff --git a/modules/data.js b/modules/data.js new file mode 100644 index 0000000..2799499 --- /dev/null +++ b/modules/data.js @@ -0,0 +1,19 @@ +rng.modules.data = function(sandbox) { + + var document = sandbox.getBootstrappedData().document; + + return { + start: function() { + sandbox.publish('ready'); + }, + getDocument: function() { + return document; + }, + commitDocument: function(newDocument, reason) { + document = newDocument; + sandbox.publish('documentChanged', document, reason); + } + + } + +}; \ No newline at end of file diff --git a/modules/rng.js b/modules/rng.js index e6f3c26..baa400d 100644 --- a/modules/rng.js +++ b/modules/rng.js @@ -20,27 +20,48 @@ rng.modules.rng = function(sandbox) { _.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); + } + } } }; eventHandlers.sourceEditor = { ready: function() { addTab('Source', 'source', sandbox.getModule('sourceEditor').getView()); + sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument()); } }; eventHandlers.visualEditor = { ready: function() { addTab('Visual', 'visual', sandbox.getModule('visualEditor').getView()); + sandbox.getModule('visualEditor').setDocument(sandbox.getModule('data').getDocument()); } }; + 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); + } + } + /* api */ return { start: function() { - sandbox.getModule('skelton').start(); + sandbox.getModule('data').start(); }, handleEvent: function(moduleName, eventName, args) { if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) { diff --git a/modules/sourceEditor.js b/modules/sourceEditor.js index ea84ee3..787bfb9 100644 --- a/modules/sourceEditor.js +++ b/modules/sourceEditor.js @@ -1,11 +1,31 @@ rng.modules.sourceEditor = function(sandbox) { + var view = $(sandbox.getTemplate('main')()); + var isDirty = false; + + $('textarea', view).on('keyup', function() { + isDirty = true; + }); + return { start: function() { sandbox.publish('ready'); }, getView: function() { - return $('

source editor

'); + return view; + }, + setDocument: function(document) { + $('textarea', view).val(document); + isDirty = false; + }, + getDocument: function() { + return $('textarea', view).val(); + }, + isDirty: function() { + return isDirty; + }, + setDirty: function(dirty) { + isDirty = dirty; } } diff --git a/modules/tabsManager.js b/modules/tabsManager.js index 1dd857d..56d647d 100644 --- a/modules/tabsManager.js +++ b/modules/tabsManager.js @@ -16,6 +16,8 @@ rng.modules.tabsManager = function(sandbox) { if(prevSlug == slug) return; + if(prevSlug) + sandbox.publish('leaving', prevSlug); tabBar.find('li').removeClass('active'); tabBar.find('a[href=#' + slug + ']').parent().addClass('active'); @@ -26,7 +28,6 @@ rng.modules.tabsManager = function(sandbox) { } - view.on('click', 'li a', function(e) { selectTab($(e.target).attr('href').substr(1)); }); diff --git a/modules/visualEditor.js b/modules/visualEditor.js index 5e503bb..b22aa7d 100644 --- a/modules/visualEditor.js +++ b/modules/visualEditor.js @@ -1,15 +1,39 @@ rng.modules.visualEditor = function(sandbox) { - var data = sandbox.getBootstrappedData(); - var view = $(sandbox.getTemplate('main')({title: data.title, content: data.text})); + var view = $(sandbox.getTemplate('main')()); + var isDirty = false; + + var document2html = function(document) { + return document; + } + + var html2document = function() { + return $('#rng-visualEditor-content').text(); + } + + $('#rng-visualEditor-content', view).on('keyup', function() { + isDirty = true; + }); - return { start: function() { sandbox.publish('ready'); }, getView: function() { return view; + }, + setDocument: function(document) { + $('#rng-visualEditor-content', view).html(document2html(document)); + isDirty = false; + }, + getDocument: function() { + return html2document(); + }, + isDirty: function() { + return isDirty; + }, + setDirty: function(dirty) { + isDirty = dirty; } }