From 1aba836313d1513bbe0ca947e01176806c900233 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Wed, 10 Apr 2013 12:40:43 +0200 Subject: [PATCH] Saving document --- editor.css | 8 ++++++++ modules/data.js | 36 ++++++++++++++++++++++++++++++++---- modules/rng.js | 12 ++++++++++++ modules/skelton.js | 13 +++++++++++-- modules/tabsManager.js | 6 +++++- 5 files changed, 68 insertions(+), 7 deletions(-) diff --git a/editor.css b/editor.css index b426734..ba6ec7b 100644 --- a/editor.css +++ b/editor.css @@ -9,4 +9,12 @@ body { .rng-tab-content { display: none; +} + +#rng-skelton-menu { + float: right; +} + +#rng-skelton-menu ul { + list-style-type: none; } \ No newline at end of file diff --git a/modules/data.js b/modules/data.js index 2799499..e4b564e 100644 --- a/modules/data.js +++ b/modules/data.js @@ -1,17 +1,45 @@ rng.modules.data = function(sandbox) { - var document = sandbox.getBootstrappedData().document; + var doc = sandbox.getBootstrappedData().document; + var document_id = sandbox.getBootstrappedData().document_id; + function readCookie(name) { + var nameEQ = escape(name) + "="; + var ca = document.cookie.split(';'); + for (var i = 0; i < ca.length; i++) { + var c = ca[i]; + while (c.charAt(0) == ' ') c = c.substring(1, c.length); + if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length, c.length)); + } + return null; + } + + $.ajaxSetup({ + crossDomain: false, + beforeSend: function(xhr, settings) { + if (!(/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type))) { + xhr.setRequestHeader("X-CSRFToken", readCookie('csrftoken')); + } + } + }); + return { start: function() { sandbox.publish('ready'); }, getDocument: function() { - return document; + return doc; }, commitDocument: function(newDocument, reason) { - document = newDocument; - sandbox.publish('documentChanged', document, reason); + doc = newDocument; + sandbox.publish('documentChanged', doc, reason); + }, + saveDocument: function() { + $.ajax({ + method: 'post', + url: '/editor/' + document_id, + data: JSON.stringify({document:doc}) + }); } } diff --git a/modules/rng.js b/modules/rng.js index baa400d..4e0ca6e 100644 --- a/modules/rng.js +++ b/modules/rng.js @@ -11,6 +11,18 @@ 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(); } }; diff --git a/modules/skelton.js b/modules/skelton.js index 2dbd948..0e6749a 100644 --- a/modules/skelton.js +++ b/modules/skelton.js @@ -1,10 +1,19 @@ rng.modules.skelton = function(sandbox) { + + var view = $(sandbox.getTemplate('main')()); + + view.find('#rng-skelton-menu a').click(function(e) { + e.preventDefault(); + sandbox.publish('cmd.' + $(e.target).attr('data-cmd')); + }); + return { start: function() { + sandbox.getDOM().append(view); sandbox.publish('ready'); }, - setMainView: function(view) { - sandbox.getDOM().html(view); + setMainView: function(mainView) { + view.find('#rng-skelton-mainView').html(mainView); } } }; \ No newline at end of file diff --git a/modules/tabsManager.js b/modules/tabsManager.js index 56d647d..4103887 100644 --- a/modules/tabsManager.js +++ b/modules/tabsManager.js @@ -3,7 +3,7 @@ rng.modules.tabsManager = function(sandbox) { var $ = sandbox.$; var view = $(sandbox.getTemplate('main')()); - + var currentSlug; var tabContent = {}; function selectTab(slug) { @@ -25,6 +25,7 @@ rng.modules.tabsManager = function(sandbox) { if(prevSlug) tabContent[prevSlug].detach(); tabContent[slug].appendTo(view.find('#rng-tabsManager-content')); + currentSlug = slug; } @@ -46,6 +47,9 @@ rng.modules.tabsManager = function(sandbox) { view.find('#rng-tabsManager-tabBar').append(sandbox.getTemplate('tabHandle')({title: title, slug: slug})); if(_.values(tabContent).length === 1) selectTab(slug); + }, + getCurrentSlug: function() { + return currentSlug; } } -- 2.20.1