Fixing canvas.createList
[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 document_version = sandbox.getBootstrappedData().version\r
10     var history = sandbox.getBootstrappedData().history;\r
11 \r
12     \r
13     if(doc === '') {\r
14         doc = '<section\n\\r
15         xmlns="http://nowoczesnapolska.org.pl/sst#"\n\\r
16         xmlns:xlink="http://www.w3.org/1999/xlink"\n\\r
17         xmlns:dc="http://purl.org/dc/elements/1.1/"\n\\r
18         xmlns:dcterms="http://purl.org/dc/terms/"\n\\r
19     >\n\\r
20         <metadata>\n\\r
21         </metadata>\n\\r
22         <div class="p"></div>\n\\r
23     </section>';\r
24     }\r
25     \r
26     \r
27     function readCookie(name) {\r
28         var nameEQ = escape(name) + "=";\r
29         var ca = document.cookie.split(';');\r
30         for (var i = 0; i < ca.length; i++) {\r
31             var c = ca[i];\r
32             while (c.charAt(0) == ' ') c = c.substring(1, c.length);\r
33             if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length, c.length));\r
34         }\r
35         return null;\r
36     }\r
37     \r
38     $.ajaxSetup({\r
39         crossDomain: false,\r
40         beforeSend: function(xhr, settings) {\r
41             if (!(/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type))) {\r
42                 xhr.setRequestHeader("X-CSRFToken", readCookie('csrftoken'));\r
43             }\r
44         }\r
45     });\r
46     \r
47     var reloadHistory = function() {\r
48         $.ajax({\r
49             method: 'get',\r
50             url: '/' + gettext('editor') + '/' + document_id + '/history',\r
51             success: function(data) {\r
52                 history = data; \r
53                 sandbox.publish('historyItemAdded', data.slice(-1)[0]);\r
54             },\r
55         });\r
56     }\r
57     \r
58     return {\r
59         start: function() {\r
60             sandbox.publish('ready');\r
61         },\r
62         getDocument: function() {\r
63             return doc;\r
64         },\r
65         commitDocument: function(newDocument, reason) {\r
66             doc = newDocument;\r
67             sandbox.publish('documentChanged', doc, reason);\r
68         },\r
69         saveDocument: function() {\r
70 \r
71             var dialog = saveDialog.create();\r
72             dialog.on('save', function(event) {\r
73                 sandbox.publish('savingStarted');\r
74                 dialog.toggleButtons(false);\r
75                 $.ajax({\r
76                     method: 'post',\r
77                     url: '/' + gettext('editor') + '/' + document_id,\r
78                     data: JSON.stringify({document:doc, description: event.data.description}),\r
79                     success: function() {\r
80                         event.success();\r
81                         sandbox.publish('savingEnded', 'success');\r
82                         reloadHistory();\r
83                     },\r
84                     error: function() {event.error(); sandbox.publish('savingEnded', 'error');}\r
85                 });\r
86                 console.log('save');\r
87             });\r
88             dialog.on('cancel', function() {\r
89             });\r
90             dialog.show();\r
91             \r
92 \r
93         },\r
94         getHistory: function() {\r
95             return history;\r
96         },\r
97         fetchDiff: function(ver1, ver2) {\r
98             $.ajax({\r
99                 method: 'get',\r
100                 url: '/' + gettext('editor') + '/' + document_id + '/diff',\r
101                 data: {from: ver1, to: ver2},\r
102                 success: function(data) {\r
103                     sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2})\r
104                 },\r
105             });\r
106         },\r
107         restoreVersion: function(options) {\r
108             if(options.version && options.description) {\r
109                 sandbox.publish('restoringStarted', {version: options.version});\r
110                 $.ajax({\r
111                     method: 'post',\r
112                     dataType: 'json',\r
113                     url: '/' + gettext('editor') + '/' + document_id + '/revert',\r
114                     data: JSON.stringify(options),\r
115                     success: function(data) {\r
116                         doc = data.document;\r
117                         document_version = data.version;\r
118                         reloadHistory();\r
119                         sandbox.publish('documentReverted', data);\r
120                     },\r
121                 }); \r
122             }\r
123         },\r
124         getDocumentId: function() {\r
125             return document_id;\r
126         },\r
127         getDocumentVersion: function() {\r
128             return document_version;\r
129         }\r
130     }\r
131 };\r
132 \r
133 });