Moving sourceEditor module to its own directory with its own styles
[fnpeditor.git] / modules / data.js
index 2799499..0fab1a7 100644 (file)
@@ -1,19 +1,74 @@
-rng.modules.data = function(sandbox) {\r
+define(function() {\r
 \r
-    var document = sandbox.getBootstrappedData().document;\r
+return function(sandbox) {\r
 \r
+    var doc = sandbox.getBootstrappedData().document;\r
+    var document_id = sandbox.getBootstrappedData().document_id;\r
+\r
+    \r
+    if(doc === '') {\r
+        doc = '<section\n\\r
+        xmlns="http://nowoczesnapolska.org.pl/sst#"\n\\r
+        xmlns:xlink="http://www.w3.org/1999/xlink"\n\\r
+        xmlns:dc="http://purl.org/dc/elements/1.1/"\n\\r
+        xmlns:dcterms="http://purl.org/dc/terms/"\n\\r
+    >\n\\r
+        <metadata>\n\\r
+        </metadata>\n\\r
+        <div class="p"></div>\n\\r
+    </section>';\r
+    }\r
+    \r
+    \r
+    function readCookie(name) {\r
+        var nameEQ = escape(name) + "=";\r
+        var ca = document.cookie.split(';');\r
+        for (var i = 0; i < ca.length; i++) {\r
+            var c = ca[i];\r
+            while (c.charAt(0) == ' ') c = c.substring(1, c.length);\r
+            if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length, c.length));\r
+        }\r
+        return null;\r
+    }\r
+    \r
+    $.ajaxSetup({\r
+        crossDomain: false,\r
+        beforeSend: function(xhr, settings) {\r
+            if (!(/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type))) {\r
+                xhr.setRequestHeader("X-CSRFToken", readCookie('csrftoken'));\r
+            }\r
+        }\r
+    });\r
+    \r
     return {\r
         start: function() {\r
             sandbox.publish('ready');\r
         },\r
         getDocument: function() {\r
-            return document;\r
+            return doc;\r
         },\r
         commitDocument: function(newDocument, reason) {\r
-            document = newDocument;\r
-            sandbox.publish('documentChanged', document, reason);\r
+            doc = newDocument;\r
+            sandbox.publish('documentChanged', doc, reason);\r
+        },\r
+        saveDocument: function() {\r
+            sandbox.publish('savingStarted');\r
+            $.ajax({\r
+                method: 'post',\r
+                url: '/' + gettext('editor') + '/' + document_id,\r
+                data: JSON.stringify({document:doc}),\r
+                success: function() {sandbox.publish('savingEnded', 'success');},\r
+                error: function() {sandbox.publish('savingEnded', 'error');}\r
+            });\r
+        },\r
+        fakeSave: function() {\r
+            sandbox.publish('savingStarted');\r
+            window.setTimeout(function() {\r
+                sandbox.publish('savingEnded', 'success')\r
+            }, 2000);\r
         }\r
         \r
     }\r
+};\r
 \r
-};
\ No newline at end of file
+});
\ No newline at end of file