Clean up: removing unused template
[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         fetchDiff: function(ver1, ver2) {\r
97             $.ajax({\r
98                 method: 'get',\r
99                 url: '/' + gettext('editor') + '/' + document_id + '/diff',\r
100                 data: {from: ver1, to: ver2},\r
101                 success: function(data) {\r
102                     sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2})\r
103                 },\r
104             });\r
105         },\r
106         restoreVersion: function(options) {\r
107             if(options.version && options.description) {\r
108                 sandbox.publish('restoringStarted', {version: options.version});\r
109                 $.ajax({\r
110                     method: 'post',\r
111                     dataType: 'json',\r
112                     url: '/' + gettext('editor') + '/' + document_id + '/revert',\r
113                     data: JSON.stringify(options),\r
114                     success: function(data) {\r
115                         doc = data.document;\r
116                         reloadHistory();\r
117                         sandbox.publish('documentReverted', {document: data.document, version: data.version});\r
118                     },\r
119                 }); \r
120             }\r
121         },\r
122         getDocumentId: function() {\r
123             return document_id;\r
124         }\r
125     }\r
126 };\r
127 \r
128 });