X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/0add3b13ce4ef42557d9ca33342a159b80a4f42f..6312e8b640d0aa87fa8c2bdac6132f6d3a7d1e7b:/modules/data.js 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}) + }); } }