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