Showing success message after saving
[fnpeditor.git] / modules / data / data.js
1 define(['./saveDialog'], function(saveDialog) {\r
2 \r
3 'use strict';\r
4 \r
5 return function(sandbox) {\r
6 \r
7     var doc = sandbox.getBootstrappedData().document;\r
8     var document_id = sandbox.getBootstrappedData().document_id;\r
9     var history = sandbox.getBootstrappedData().history;\r
10 \r
11     \r
12     if(doc === '') {\r
13         doc = '<section\n\\r
14         xmlns="http://nowoczesnapolska.org.pl/sst#"\n\\r
15         xmlns:xlink="http://www.w3.org/1999/xlink"\n\\r
16         xmlns:dc="http://purl.org/dc/elements/1.1/"\n\\r
17         xmlns:dcterms="http://purl.org/dc/terms/"\n\\r
18     >\n\\r
19         <metadata>\n\\r
20         </metadata>\n\\r
21         <div class="p"></div>\n\\r
22     </section>';\r
23     }\r
24     \r
25     \r
26     function readCookie(name) {\r
27         var nameEQ = escape(name) + "=";\r
28         var ca = document.cookie.split(';');\r
29         for (var i = 0; i < ca.length; i++) {\r
30             var c = ca[i];\r
31             while (c.charAt(0) == ' ') c = c.substring(1, c.length);\r
32             if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length, c.length));\r
33         }\r
34         return null;\r
35     }\r
36     \r
37     $.ajaxSetup({\r
38         crossDomain: false,\r
39         beforeSend: function(xhr, settings) {\r
40             if (!(/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type))) {\r
41                 xhr.setRequestHeader("X-CSRFToken", readCookie('csrftoken'));\r
42             }\r
43         }\r
44     });\r
45     \r
46     var reloadHistory = function() {\r
47         $.ajax({\r
48             method: 'get',\r
49             url: '/' + gettext('editor') + '/' + document_id + '/history',\r
50             success: function(data) {\r
51                 history = data; \r
52                 sandbox.publish('historyItemAdded', data.slice(-1)[0]);\r
53             },\r
54         });\r
55     }\r
56     \r
57     return {\r
58         start: function() {\r
59             sandbox.publish('ready');\r
60         },\r
61         getDocument: function() {\r
62             return doc;\r
63         },\r
64         commitDocument: function(newDocument, reason) {\r
65             doc = newDocument;\r
66             sandbox.publish('documentChanged', doc, reason);\r
67         },\r
68         saveDocument: function() {\r
69 \r
70             var dialog = saveDialog.create();\r
71             dialog.on('save', function(event) {\r
72                 sandbox.publish('savingStarted');\r
73                 dialog.toggleButtons(false);\r
74                 $.ajax({\r
75                     method: 'post',\r
76                     url: '/' + gettext('editor') + '/' + document_id,\r
77                     data: JSON.stringify({document:doc, description: event.data.description}),\r
78                     success: function() {\r
79                         event.success();\r
80                         sandbox.publish('savingEnded', 'success');\r
81                         reloadHistory();\r
82                     },\r
83                     error: function() {event.error(); sandbox.publish('savingEnded', 'error');}\r
84                 });\r
85                 console.log('save');\r
86             });\r
87             dialog.on('cancel', function() {\r
88             });\r
89             dialog.show();\r
90             \r
91 \r
92         },\r
93         getHistory: function() {\r
94             return history;\r
95         }\r
96     }\r
97 };\r
98 \r
99 });