X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/0add3b13ce4ef42557d9ca33342a159b80a4f42f..6384a81fa481cb43fddca23742e37761592be66d:/modules/data.js diff --git a/modules/data.js b/modules/data.js index 2799499..5a48572 100644 --- a/modules/data.js +++ b/modules/data.js @@ -1,19 +1,67 @@ -rng.modules.data = function(sandbox) { +define(function() { - var document = sandbox.getBootstrappedData().document; +return function(sandbox) { + var doc = sandbox.getBootstrappedData().document; + var document_id = sandbox.getBootstrappedData().document_id; + + + if(doc === '') { + doc = '\n\ + \n\ + \n\ +
\n\ + '; + } + + + 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() { + sandbox.publish('savingStarted'); + $.ajax({ + method: 'post', + url: '/' + gettext('editor') + '/' + document_id, + data: JSON.stringify({document:doc}), + success: function() {sandbox.publish('savingEnded', 'success');}, + error: function() {sandbox.publish('savingEnded', 'error');} + }); + } } +}; -}; \ No newline at end of file +}); \ No newline at end of file