5 'wlxml/extensions/list/list',
6 'fnpjs/logging/logging',
7 ], function($, Dialog, wlxml, listExtension, logging) {
10 /* global gettext, alert, window */
12 var logger = logging.getLogger('editor.modules.data'),
13 stubDocument = '<section><div>' + gettext('This is an empty document.') + '</div></section>';
16 return function(sandbox) {
18 var document_id = sandbox.getBootstrappedData().document_id;
19 var history = sandbox.getBootstrappedData().history;
20 var documentDirty = false;
21 var draftDirty = false;
23 var documentProperties = {};
24 var data = sandbox.getBootstrappedData();
26 .filter(function(key) {
27 return key !== 'history' && key !== 'document';
29 .forEach(function(key) {
30 documentProperties[key] = data[key];
33 var wlxmlDocument, text;
35 var loadDocument = function(text) {
36 logger.debug('loading document');
38 wlxmlDocument = wlxml.WLXMLDocumentFromXML(text);
41 alert(gettext('This document contains errors and can\'t be loaded. :(')); // TODO
42 wlxmlDocument = wlxml.WLXMLDocumentFromXML(stubDocument);
45 wlxmlDocument.registerExtension(listExtension);
46 sandbox.getPlugins().forEach(function(plugin) {
47 if(plugin.documentExtension) {
48 wlxmlDocument.registerExtension(plugin.documentExtension);
52 var modificationFlag = true;
53 var handleChange = function() {
56 modificationFlag = true;
58 wlxmlDocument.on('change', handleChange);
59 wlxmlDocument.on('contentSet', handleChange);
61 if(window.localStorage) {
62 window.setInterval(function() {
63 if(modificationFlag) {
64 modificationFlag = false;
67 if(wlxmlDocument && documentDirty && draftDirty) {
68 logger.debug('Saving draft to local storage.');
69 sandbox.publish('savingStarted', 'local');
70 window.localStorage.setItem(getLocalStorageKey(), wlxmlDocument.toXML());
71 sandbox.publish('savingEnded', 'success', 'local');
74 }, sandbox.getConfig().autoSaveInterval || 2500);
76 sandbox.publish('ready');
79 function readCookie(name) {
80 /* global escape, unescape, document */
81 var nameEQ = escape(name) + '=';
82 var ca = document.cookie.split(';');
83 for (var i = 0; i < ca.length; i++) {
85 while (c.charAt(0) === ' ') {
86 c = c.substring(1, c.length);
88 if (c.indexOf(nameEQ) === 0) {
89 return unescape(c.substring(nameEQ.length, c.length));
97 beforeSend: function(xhr, settings) {
98 if (!(/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type))) {
99 xhr.setRequestHeader('X-CSRFToken', readCookie('csrftoken'));
104 var reloadHistory = function() {
107 url: sandbox.getConfig().documentHistoryUrl(document_id),
108 success: function(data) {
110 sandbox.publish('historyItemAdded', data.slice(-1)[0]);
115 var getLocalStorageKey = function() {
116 return 'draft-id:' + document_id + '-ver:' + documentProperties.version;
123 if(window.localStorage) {
124 text = window.localStorage.getItem(getLocalStorageKey());
126 logger.debug('Local draft exists');
127 var dialog = Dialog.create({
128 title: gettext('Local draft of a document exists'),
129 text: gettext('Unsaved local draft of this version of the document exists in your browser. Do you want to load it instead?'),
130 executeButtonText: gettext('Yes, restore local draft'),
131 cancelButtonText: gettext('No, use version loaded from the server')
133 dialog.on('cancel', function() {
134 logger.debug('Bootstrapped version chosen');
135 text = sandbox.getBootstrappedData().document;
138 dialog.on('execute', function(event) {
139 logger.debug('Local draft chosen');
143 dialog.on('close', function() {
147 loadDocument(sandbox.getBootstrappedData().document);
150 loadDocument(sandbox.getBootstrappedData().document);
153 getDocument: function() {
154 return wlxmlDocument;
156 saveDocument: function() {
157 var documentSaveForm = $.extend({
159 content_field_name: 'text',
160 version_field_name: 'version'
162 sandbox.getConfig().documentSaveForm
164 dialog = Dialog.create({
165 fields: documentSaveForm.fields,
166 title: gettext('Save Document'),
167 executeButtonText: gettext('Save')
170 dialog.on('execute', function(event) {
171 sandbox.publish('savingStarted', 'remote');
173 var formData = event.formData;
174 formData[documentSaveForm.content_field_name] = wlxmlDocument.toXML();
175 formData[documentSaveForm.version_field_name] = documentProperties.version;
176 if(sandbox.getConfig().jsonifySentData) {
177 formData = JSON.stringify(formData);
180 dialog.toggleButtons(false);
183 url: sandbox.getConfig().documentSaveUrl(document_id),
185 success: function(data) {
187 sandbox.publish('savingEnded', 'success', 'remote', data);
190 .filter(function(key) {
191 return key !== 'text';
193 .forEach(function(key) {
194 documentProperties[key] = data[key];
199 error: function() {event.error(); sandbox.publish('savingEnded', 'error', 'remote');}
202 dialog.on('cancel', function() {
208 getHistory: function() {
211 fetchDiff: function(ver1, ver2) {
214 url: sandbox.getConfig().documentDiffUrl(document_id),
215 data: {from: ver1, to: ver2},
216 success: function(data) {
217 sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2});
221 restoreVersion: function(version) {
222 var documentRestoreForm = $.extend({
224 version_field_name: 'version'
226 sandbox.getConfig().documentRestoreForm
228 dialog = Dialog.create({
229 fields: documentRestoreForm.fields,
230 title: gettext('Restore Version'),
231 executeButtonText: gettext('Restore')
234 dialog.on('execute', function(event) {
235 var formData = event.formData;
236 formData[documentRestoreForm.version_field_name] = version;
237 sandbox.publish('restoringStarted', {version: version});
238 if(sandbox.getConfig().jsonifySentData) {
239 formData = JSON.stringify(formData);
244 url: sandbox.getConfig().documentRestoreUrl(document_id),
246 success: function(data) {
248 .filter(function(key) {
249 return key !== 'document';
251 .forEach(function(key) {
252 documentProperties = data[key];
255 wlxmlDocument.loadXML(data.document);
256 documentDirty = false;
257 sandbox.publish('documentReverted', data.version);
264 dropDraft: function() {
265 logger.debug('Dropping a draft...');
266 wlxmlDocument.loadXML(sandbox.getBootstrappedData().document);
268 logger.debug('Draft dropped');
270 getDocumentId: function() {
273 getDocumentProperties: function() {
274 return documentProperties;