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 document_version = sandbox.getBootstrappedData().version;
20 var history = sandbox.getBootstrappedData().history;
21 var documentDirty = false;
22 var draftDirty = false;
24 var wlxmlDocument, text;
26 var loadDocument = function(text) {
27 logger.debug('loading document');
29 wlxmlDocument = wlxml.WLXMLDocumentFromXML(text);
32 alert(gettext('This document contains errors and can\'t be loaded. :(')); // TODO
33 wlxmlDocument = wlxml.WLXMLDocumentFromXML(stubDocument);
36 wlxmlDocument.registerExtension(listExtension);
37 sandbox.getPlugins().forEach(function(plugin) {
38 if(plugin.documentExtension) {
39 wlxmlDocument.registerExtension(plugin.documentExtension);
43 var modificationFlag = true;
44 var handleChange = function() {
47 modificationFlag = true;
49 wlxmlDocument.on('change', handleChange);
50 wlxmlDocument.on('contentSet', handleChange);
52 if(window.localStorage) {
53 window.setInterval(function() {
54 if(modificationFlag) {
55 modificationFlag = false;
58 if(wlxmlDocument && documentDirty && draftDirty) {
59 logger.debug('Saving draft to local storage.');
60 sandbox.publish('savingStarted', 'local');
61 window.localStorage.setItem(getLocalStorageKey(), wlxmlDocument.toXML());
62 sandbox.publish('savingEnded', 'success', 'local');
65 }, sandbox.getConfig().autoSaveInterval || 2500);
67 sandbox.publish('ready');
70 function readCookie(name) {
71 /* global escape, unescape, document */
72 var nameEQ = escape(name) + '=';
73 var ca = document.cookie.split(';');
74 for (var i = 0; i < ca.length; i++) {
76 while (c.charAt(0) === ' ') {
77 c = c.substring(1, c.length);
79 if (c.indexOf(nameEQ) === 0) {
80 return unescape(c.substring(nameEQ.length, c.length));
88 beforeSend: function(xhr, settings) {
89 if (!(/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type))) {
90 xhr.setRequestHeader('X-CSRFToken', readCookie('csrftoken'));
95 var reloadHistory = function() {
98 url: sandbox.getConfig().documentHistoryUrl(document_id),
99 success: function(data) {
101 sandbox.publish('historyItemAdded', data.slice(-1)[0]);
106 var getLocalStorageKey = function() {
107 return 'draft-id:' + document_id + '-ver:' + document_version;
114 if(window.localStorage) {
115 text = window.localStorage.getItem(getLocalStorageKey());
117 logger.debug('Local draft exists');
118 var dialog = Dialog.create({
119 title: gettext('Local draft of a document exists'),
120 text: gettext('Unsaved local draft of this version of the document exists in your browser. Do you want to load it instead?'),
121 executeButtonText: gettext('Yes, restore local draft'),
122 cancelButtonText: gettext('No, use version loaded from the server')
124 dialog.on('cancel', function() {
125 logger.debug('Bootstrapped version chosen');
126 text = sandbox.getBootstrappedData().document;
129 dialog.on('execute', function(event) {
130 logger.debug('Local draft chosen');
134 dialog.on('close', function() {
138 loadDocument(sandbox.getBootstrappedData().document);
141 loadDocument(sandbox.getBootstrappedData().document);
144 getDocument: function() {
145 return wlxmlDocument;
147 saveDocument: function() {
148 var documentSaveForm = $.extend({
150 content_field_name: 'text',
151 version_field_name: 'version'
153 sandbox.getConfig().documentSaveForm
155 dialog = Dialog.create({
156 fields: documentSaveForm.fields,
157 title: gettext('Save Document'),
158 executeButtonText: gettext('Save')
161 dialog.on('execute', function(event) {
162 sandbox.publish('savingStarted', 'remote');
164 var formData = event.formData;
165 formData[documentSaveForm.content_field_name] = wlxmlDocument.toXML();
166 formData[documentSaveForm.version_field_name] = document_version;
167 if(sandbox.getConfig().jsonifySentData) {
168 formData = JSON.stringify(formData);
171 dialog.toggleButtons(false);
174 url: sandbox.getConfig().documentSaveUrl(document_id),
176 success: function(data) {
178 sandbox.publish('savingEnded', 'success', 'remote', data.version);
179 document_version = data.version;
182 error: function() {event.error(); sandbox.publish('savingEnded', 'error', 'remote');}
185 dialog.on('cancel', function() {
191 getHistory: function() {
194 fetchDiff: function(ver1, ver2) {
197 url: sandbox.getConfig().documentDiffUrl(document_id),
198 data: {from: ver1, to: ver2},
199 success: function(data) {
200 sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2});
204 restoreVersion: function(version) {
205 var documentRestoreForm = $.extend({
207 version_field_name: 'version'
209 sandbox.getConfig().documentRestoreForm
211 dialog = Dialog.create({
212 fields: documentRestoreForm.fields,
213 title: gettext('Restore Version'),
214 executeButtonText: gettext('Restore')
217 dialog.on('execute', function(event) {
218 var formData = event.formData;
219 formData[documentRestoreForm.version_field_name] = version;
220 sandbox.publish('restoringStarted', {version: version});
221 if(sandbox.getConfig().jsonifySentData) {
222 formData = JSON.stringify(formData);
227 url: sandbox.getConfig().documentRestoreUrl(document_id),
229 success: function(data) {
230 document_version = data.version;
232 wlxmlDocument.loadXML(data.document);
233 documentDirty = false;
234 sandbox.publish('documentReverted', data.version);
241 dropDraft: function() {
242 logger.debug('Dropping a draft...');
243 wlxmlDocument.loadXML(sandbox.getBootstrappedData().document);
245 logger.debug('Draft dropped');
247 getDocumentId: function() {
250 getDocumentVersion: function() {
251 return document_version;