919ce68a0303094c49862d4739692237ed8f6dd8
[fnpeditor.git] / src / editor / modules / data / data.js
1 define([
2     'libs/jquery',
3     'views/dialog/dialog',
4     'wlxml/wlxml',
5     'wlxml/extensions/list/list',
6     'fnpjs/logging/logging',
7     'fnpjs/datetime',
8     './document'
9 ], function($, Dialog, wlxml, listExtension, logging, datetime, Document) {
10
11 'use strict';
12 /* global gettext, alert, window */
13
14 var logger = logging.getLogger('editor.modules.data');
15
16
17 return function(sandbox) {
18
19     var data = sandbox.getBootstrappedData(),
20         documentDirty = false,
21         draftDirty = false,
22         wlxmlDocument;
23
24
25     var loadDocument = function(text, isDraft, draftTimestamp) {
26         logger.debug('loading document');
27         var xmlValid = true;
28         try {
29             wlxmlDocument = wlxml.WLXMLDocumentFromXML(text, {editorConfig: sandbox.getConfig()}, Document.Document);
30         } catch(e) {
31             logger.exception(e);
32             alert(gettext('The content of this document seems to be invalid - only XML source editing will be possible. :(')); // TODO
33             wlxmlDocument = wlxml.WLXMLDocumentFromXML(text, {}, Document.DumbDocument);
34             xmlValid = false;
35         }
36
37         Object.keys(data)
38             .filter(function(key) {
39                 return key !== 'history' && key !== 'document';
40             })
41             .forEach(function(key) {
42                 wlxmlDocument.setProperty(key, data[key]);
43             });
44
45         wlxmlDocument.registerExtension(listExtension);
46         sandbox.getPlugins().forEach(function(plugin) {
47             if(plugin.documentExtension) {
48                 wlxmlDocument.registerExtension(plugin.documentExtension);
49             }
50         });
51         
52         var modificationFlag = true;
53         var handleChange = function() {
54             documentDirty = true;
55             draftDirty = true;
56             modificationFlag = true;
57         };
58         wlxmlDocument.on('change', handleChange);
59         wlxmlDocument.on('contentSet', handleChange);
60
61         if(window.localStorage) {
62             window.setInterval(function() {
63                 var timestamp = datetime.currentStrfmt(),
64                     key = getLocalStorageKey();
65                 if(modificationFlag) {
66                     modificationFlag = false;
67                     return;
68                 }
69                 if(wlxmlDocument && documentDirty && draftDirty) {
70                     logger.debug('Saving draft to local storage.');
71                     sandbox.publish('savingStarted', 'local');
72                     window.localStorage.setItem(key.content, wlxmlDocument.toXML());
73                     window.localStorage.setItem(key.contentTimestamp, timestamp);
74                     sandbox.publish('savingEnded', 'success', 'local', {timestamp: timestamp});
75                     draftDirty = false;
76                 }
77             }, sandbox.getConfig().autoSaveInterval || 2500);
78         }
79         sandbox.publish('ready', isDraft, draftTimestamp, xmlValid);
80     };
81     
82     function readCookie(name) {
83         /* global escape, unescape, document */
84         var nameEQ = escape(name) + '=';
85         var ca = document.cookie.split(';');
86         for (var i = 0; i < ca.length; i++) {
87             var c = ca[i];
88             while (c.charAt(0) === ' ') {
89                 c = c.substring(1, c.length);
90             }
91             if (c.indexOf(nameEQ) === 0) {
92                 return unescape(c.substring(nameEQ.length, c.length));
93             }
94         }
95         return null;
96     }
97     
98     $.ajaxSetup({
99         crossDomain: false,
100         beforeSend: function(xhr, settings) {
101             if (!(/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type))) {
102                 xhr.setRequestHeader('X-CSRFToken', readCookie('csrftoken'));
103             }
104         }
105     });
106     
107     var reloadHistory = function() {
108         $.ajax({
109             method: 'get',
110             url: sandbox.getConfig().documentHistoryUrl(data.document_id),
111             success: function(history) {
112                 data.history = history;
113                 sandbox.publish('historyItemAdded', history.slice(-1)[0]);
114             },
115         });
116     };
117
118     var getLocalStorageKey = function(forVersion) {
119         var base = 'draft-id:' + data.document_id + '-ver:' + (forVersion || wlxmlDocument.properties.version);
120         return {
121             content: base,
122             contentTimestamp: base + '-content-timestamp'
123         };
124     };
125
126    
127     return {
128         start: function() {
129             var text;
130             if(window.localStorage) {
131                 text = window.localStorage.getItem(getLocalStorageKey(data.version).content);
132
133                 var timestamp = window.localStorage.getItem(getLocalStorageKey(data.version).contentTimestamp),
134                     usingDraft;
135                 if(text) {
136                     logger.debug('Local draft exists');
137                     var dialog = Dialog.create({
138                         title: gettext('Local draft of a document exists'),
139                         text: gettext('Unsaved local draft of this version of the document exists in your browser. Do you want to load it instead?'),
140                         executeButtonText: gettext('Yes, restore local draft'),
141                         cancelButtonText: gettext('No, use version loaded from the server')
142                     });
143                     dialog.on('cancel', function() {
144                         logger.debug('Bootstrapped version chosen');
145                         usingDraft = false;
146                         text = sandbox.getBootstrappedData().document;
147                         
148                     });
149                     dialog.on('execute', function(event) {
150                         logger.debug('Local draft chosen');
151                         usingDraft = true;
152                         event.success();
153                     });
154                     dialog.show();
155                     dialog.on('close', function() {
156                         loadDocument(text, usingDraft, timestamp);
157                     });
158                 } else {
159                     loadDocument(sandbox.getBootstrappedData().document, false);
160                 }
161             } else {
162                 loadDocument(sandbox.getBootstrappedData().document, false);
163             }
164         },
165         getDocument: function() {
166             return wlxmlDocument;
167         },
168         saveDocument: function() {
169             var documentSaveForm = $.extend({
170                         fields: [],
171                         content_field_name: 'text',
172                         version_field_name: 'version'
173                     },
174                     sandbox.getConfig().documentSaveForm
175                 ),
176                 dialog = Dialog.create({
177                     fields: documentSaveForm.fields,
178                     title: gettext('Save Document'),
179                     executeButtonText: gettext('Save'),
180                     cancelButtonText: gettext('Cancel')
181                 });
182             /* Set stage field initial value to current document stage. */
183             for (var i in documentSaveForm.fields) {
184                 if (documentSaveForm.fields[i].name == 'textsave-stage') {
185                     documentSaveForm.fields[i].initialValue = data.stage;
186                 }
187             }
188             
189             dialog.on('execute', function(event) {
190                 sandbox.publish('savingStarted', 'remote');
191
192                 var formData = event.formData;
193                 formData[documentSaveForm.content_field_name] = wlxmlDocument.toXML();
194                 formData[documentSaveForm.version_field_name] = wlxmlDocument.properties.version;
195                 if(sandbox.getConfig().jsonifySentData) {
196                     formData = JSON.stringify(formData);
197                 }
198
199                 dialog.toggleButtons(false);
200                 $.ajax({
201                     method: 'post',
202                     url: sandbox.getConfig().documentSaveUrl(data.document_id),
203                     data: formData,
204                     success: function(ajax_data) {
205                         event.success();
206                         sandbox.publish('savingEnded', 'success', 'remote', ajax_data);
207
208                         Object.keys(ajax_data)
209                             .filter(function(key) {
210                                 return key !== 'text';
211                             })
212                             .forEach(function(key) {
213                                 wlxmlDocument.setProperty(key, ajax_data[key]);
214                                 // ugly, but whatever
215                                 data[key] = ajax_data[key];
216                             });
217
218                         reloadHistory();
219                     },
220                     error: function(data) {
221                         event.error();
222                         sandbox.publish('savingEnded', 'error', 'remote');
223                         var dialog = Dialog.create({
224                             title: gettext('Error'),
225                             text: JSON.parse(data.responseText).text.join('\n'),
226                             executeButtonText: gettext('Close')
227                         });
228                         dialog.show();
229                         dialog.on('execute', function(e) {
230                             e.success();
231                         });
232                     }
233                 });
234             });
235             dialog.on('cancel', function() {
236             });
237             dialog.show();
238             
239
240         },
241         getHistory: function() {
242             return data.history;
243         },
244         fetchDiff: function(ver1, ver2) {
245             $.ajax({
246                 method: 'get',
247                 url: sandbox.getConfig().documentDiffUrl(data.document_id),
248                 data: {from: ver1, to: ver2},
249                 success: function(data) {
250                     sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2});
251                 },
252             });
253         },
254         restoreVersion: function(version) {
255             var documentRestoreForm = $.extend({
256                         fields: [],
257                         version_field_name: 'version'
258                     },
259                     sandbox.getConfig().documentRestoreForm
260                 ),
261                 dialog = Dialog.create({
262                     fields: documentRestoreForm.fields,
263                     title: gettext('Restore Version'),
264                     executeButtonText: gettext('Restore'),
265                     cancelButtonText: gettext('Cancel')
266                 });
267
268             dialog.on('execute', function(event) {
269                 var formData = event.formData;
270                 formData[documentRestoreForm.version_field_name] = version;
271                 sandbox.publish('restoringStarted', {version: version});
272                 if(sandbox.getConfig().jsonifySentData) {
273                     formData = JSON.stringify(formData);
274                 }
275                 $.ajax({
276                     method: 'post',
277                     dataType: 'json',
278                     url: sandbox.getConfig().documentRestoreUrl(data.document_id),
279                     data: formData,
280                     success: function(ajax_data) {
281                         Object.keys(ajax_data)
282                             .filter(function(key) {
283                                 return key !== 'document';
284                             })
285                             .forEach(function(key) {
286                                 wlxmlDocument.setProperty(key, ajax_data[key]);
287                                 // ugly, but whatever
288                                 data[key] = ajax_data[key];
289                             });
290                         reloadHistory();
291                         wlxmlDocument.loadXML(ajax_data.document);
292                         documentDirty = false;
293                         sandbox.publish('documentReverted', ajax_data.version);
294                         event.success();
295                     },
296                 });
297             });
298             dialog.show();
299         },
300         publishVersion: function(revision) {
301             var documentPublishForm = $.extend({
302                         fields: [],
303                         revision_field_name: 'revision'
304                     },
305                     sandbox.getConfig().documentPublishForm
306                 ),
307                 dialog = Dialog.create({
308                     fields: documentPublishForm.fields,
309                     title: gettext('Publish'),
310                     executeButtonText: gettext('Publish'),
311                     cancelButtonText: gettext('Cancel')
312                 });
313
314             dialog.on('execute', function(event) {
315                 var formData = event.formData;
316                 formData[documentPublishForm.revision_field_name] = revision;
317                 sandbox.publish('publishingStarted', {version: revision});
318                 if(sandbox.getConfig().jsonifySentData) {
319                     formData = JSON.stringify(formData);
320                 }
321                 $.ajax({
322                     method: 'post',
323                     //dataType: 'json',
324                     dataType: 'text',
325                     url: sandbox.getConfig().documentPublishUrl,
326                     data: formData,
327                     success: function(data) {
328                         reloadHistory();
329                         sandbox.publish('documentPublished');
330                         event.success();
331                     },
332                 });
333             });
334             dialog.show();
335         },
336         dropDraft: function() {
337             logger.debug('Dropping a draft...');
338             wlxmlDocument.loadXML(sandbox.getBootstrappedData().document);
339             draftDirty = false;
340             logger.debug('Draft dropped');
341             sandbox.publish('draftDropped');
342         }
343     };
344 };
345
346 });