5 'wlxml/extensions/list/list',
6 'fnpjs/logging/logging',
8 ], function($, Dialog, wlxml, listExtension, logging, datetime) {
11 /* global gettext, alert, window */
13 var logger = logging.getLogger('editor.modules.data'),
14 stubDocument = '<section><div>' + gettext('This is an empty document.') + '</div></section>';
17 return function(sandbox) {
19 var document_id = sandbox.getBootstrappedData().document_id;
20 var history = sandbox.getBootstrappedData().history;
21 var documentDirty = false;
22 var draftDirty = false;
24 var documentProperties = {};
25 var data = sandbox.getBootstrappedData();
27 .filter(function(key) {
28 return key !== 'history' && key !== 'document';
30 .forEach(function(key) {
31 documentProperties[key] = data[key];
34 var wlxmlDocument, text;
36 var loadDocument = function(text, isDraft, draftTimestamp) {
37 logger.debug('loading document');
39 wlxmlDocument = wlxml.WLXMLDocumentFromXML(text);
42 alert(gettext('This document contains errors and can\'t be loaded. :(')); // TODO
43 wlxmlDocument = wlxml.WLXMLDocumentFromXML(stubDocument);
46 wlxmlDocument.registerExtension(listExtension);
47 sandbox.getPlugins().forEach(function(plugin) {
48 if(plugin.documentExtension) {
49 wlxmlDocument.registerExtension(plugin.documentExtension);
53 var modificationFlag = true;
54 var handleChange = function() {
57 modificationFlag = true;
59 wlxmlDocument.on('change', handleChange);
60 wlxmlDocument.on('contentSet', handleChange);
62 if(window.localStorage) {
63 window.setInterval(function() {
64 if(modificationFlag) {
65 modificationFlag = false;
68 if(wlxmlDocument && documentDirty && draftDirty) {
69 var timestamp = datetime.currentStrfmt();
70 logger.debug('Saving draft to local storage.');
71 sandbox.publish('savingStarted', 'local');
72 window.localStorage.setItem(getLocalStorageKey().content, wlxmlDocument.toXML());
73 window.localStorage.setItem(getLocalStorageKey().contentTimestamp, timestamp);
74 sandbox.publish('savingEnded', 'success', 'local', {timestamp: timestamp});
77 }, sandbox.getConfig().autoSaveInterval || 2500);
79 sandbox.publish('ready', isDraft, draftTimestamp);
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++) {
88 while (c.charAt(0) === ' ') {
89 c = c.substring(1, c.length);
91 if (c.indexOf(nameEQ) === 0) {
92 return unescape(c.substring(nameEQ.length, c.length));
100 beforeSend: function(xhr, settings) {
101 if (!(/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type))) {
102 xhr.setRequestHeader('X-CSRFToken', readCookie('csrftoken'));
107 var reloadHistory = function() {
110 url: sandbox.getConfig().documentHistoryUrl(document_id),
111 success: function(data) {
113 sandbox.publish('historyItemAdded', data.slice(-1)[0]);
118 var getLocalStorageKey = function() {
119 var base = 'draft-id:' + document_id + '-ver:' + documentProperties.version;
122 contentTimestamp: base + '-content-timestamp'
129 if(window.localStorage) {
130 text = window.localStorage.getItem(getLocalStorageKey().content);
132 var timestamp = window.localStorage.getItem(getLocalStorageKey().contentTimestamp),
135 logger.debug('Local draft exists');
136 var dialog = Dialog.create({
137 title: gettext('Local draft of a document exists'),
138 text: gettext('Unsaved local draft of this version of the document exists in your browser. Do you want to load it instead?'),
139 executeButtonText: gettext('Yes, restore local draft'),
140 cancelButtonText: gettext('No, use version loaded from the server')
142 dialog.on('cancel', function() {
143 logger.debug('Bootstrapped version chosen');
145 text = sandbox.getBootstrappedData().document;
148 dialog.on('execute', function(event) {
149 logger.debug('Local draft chosen');
154 dialog.on('close', function() {
155 loadDocument(text, usingDraft, timestamp);
158 loadDocument(sandbox.getBootstrappedData().document, false);
161 loadDocument(sandbox.getBootstrappedData().document, false);
164 getDocument: function() {
165 return wlxmlDocument;
167 saveDocument: function() {
168 var documentSaveForm = $.extend({
170 content_field_name: 'text',
171 version_field_name: 'version'
173 sandbox.getConfig().documentSaveForm
175 dialog = Dialog.create({
176 fields: documentSaveForm.fields,
177 title: gettext('Save Document'),
178 executeButtonText: gettext('Save')
181 dialog.on('execute', function(event) {
182 sandbox.publish('savingStarted', 'remote');
184 var formData = event.formData;
185 formData[documentSaveForm.content_field_name] = wlxmlDocument.toXML();
186 formData[documentSaveForm.version_field_name] = documentProperties.version;
187 if(sandbox.getConfig().jsonifySentData) {
188 formData = JSON.stringify(formData);
191 dialog.toggleButtons(false);
194 url: sandbox.getConfig().documentSaveUrl(document_id),
196 success: function(data) {
198 sandbox.publish('savingEnded', 'success', 'remote', data);
201 .filter(function(key) {
202 return key !== 'text';
204 .forEach(function(key) {
205 documentProperties[key] = data[key];
210 error: function() {event.error(); sandbox.publish('savingEnded', 'error', 'remote');}
213 dialog.on('cancel', function() {
219 getHistory: function() {
222 fetchDiff: function(ver1, ver2) {
225 url: sandbox.getConfig().documentDiffUrl(document_id),
226 data: {from: ver1, to: ver2},
227 success: function(data) {
228 sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2});
232 restoreVersion: function(version) {
233 var documentRestoreForm = $.extend({
235 version_field_name: 'version'
237 sandbox.getConfig().documentRestoreForm
239 dialog = Dialog.create({
240 fields: documentRestoreForm.fields,
241 title: gettext('Restore Version'),
242 executeButtonText: gettext('Restore')
245 dialog.on('execute', function(event) {
246 var formData = event.formData;
247 formData[documentRestoreForm.version_field_name] = version;
248 sandbox.publish('restoringStarted', {version: version});
249 if(sandbox.getConfig().jsonifySentData) {
250 formData = JSON.stringify(formData);
255 url: sandbox.getConfig().documentRestoreUrl(document_id),
257 success: function(data) {
259 .filter(function(key) {
260 return key !== 'document';
262 .forEach(function(key) {
263 documentProperties = data[key];
266 wlxmlDocument.loadXML(data.document);
267 documentDirty = false;
268 sandbox.publish('documentReverted', data.version);
275 dropDraft: function() {
276 logger.debug('Dropping a draft...');
277 wlxmlDocument.loadXML(sandbox.getBootstrappedData().document);
279 logger.debug('Draft dropped');
280 sandbox.publish('draftDropped');
282 getDocumentId: function() {
285 getDocumentProperties: function() {
286 return documentProperties;