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 wlxmlDocument.on('change', function() {
47 modificationFlag = true;
49 if(window.localStorage) {
50 window.setInterval(function() {
51 if(modificationFlag) {
52 modificationFlag = false;
55 if(wlxmlDocument && documentDirty && draftDirty) {
56 logger.debug('Saving draft to local storage.');
57 sandbox.publish('savingStarted', 'local');
58 window.localStorage.setItem(getLocalStorageKey(), wlxmlDocument.toXML());
59 sandbox.publish('savingEnded', 'success', 'local');
62 }, sandbox.getConfig().autoSaveInterval || 2500);
64 sandbox.publish('ready');
67 function readCookie(name) {
68 /* global escape, unescape, document */
69 var nameEQ = escape(name) + '=';
70 var ca = document.cookie.split(';');
71 for (var i = 0; i < ca.length; i++) {
73 while (c.charAt(0) === ' ') {
74 c = c.substring(1, c.length);
76 if (c.indexOf(nameEQ) === 0) {
77 return unescape(c.substring(nameEQ.length, c.length));
85 beforeSend: function(xhr, settings) {
86 if (!(/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type))) {
87 xhr.setRequestHeader('X-CSRFToken', readCookie('csrftoken'));
92 var reloadHistory = function() {
95 url: sandbox.getConfig().documentHistoryUrl(document_id),
96 success: function(data) {
98 sandbox.publish('historyItemAdded', data.slice(-1)[0]);
103 var getLocalStorageKey = function() {
104 return 'draft-id:' + document_id + '-ver:' + document_version;
111 if(window.localStorage) {
112 text = window.localStorage.getItem(getLocalStorageKey());
114 logger.debug('Local draft exists');
115 var dialog = Dialog.create({
116 title: gettext('Local draft of a document exists'),
117 text: gettext('Unsaved local draft of this version of the document exists in your browser. Do you want to load it instead?'),
118 executeButtonText: gettext('Yes, restore local draft'),
119 cancelButtonText: gettext('No, use version loaded from the server')
121 dialog.on('cancel', function() {
122 logger.debug('Bootstrapped version chosen');
123 text = sandbox.getBootstrappedData().document;
126 dialog.on('execute', function(event) {
127 logger.debug('Local draft chosen');
131 dialog.on('close', function() {
135 loadDocument(sandbox.getBootstrappedData().document);
138 loadDocument(sandbox.getBootstrappedData().document);
141 getDocument: function() {
142 return wlxmlDocument;
144 saveDocument: function() {
145 var documentSaveForm = $.extend({
147 content_field_name: 'text',
148 version_field_name: 'version'
150 sandbox.getConfig().documentSaveForm
152 dialog = Dialog.create({
153 fields: documentSaveForm.fields,
154 title: gettext('Save Document'),
155 executeButtonText: gettext('Save')
158 dialog.on('execute', function(event) {
159 sandbox.publish('savingStarted', 'remote');
161 var formData = event.formData;
162 formData[documentSaveForm.content_field_name] = wlxmlDocument.toXML();
163 formData[documentSaveForm.version_field_name] = document_version;
164 if(sandbox.getConfig().jsonifySentData) {
165 formData = JSON.stringify(formData);
168 dialog.toggleButtons(false);
171 url: sandbox.getConfig().documentSaveUrl(document_id),
173 success: function(data) {
175 sandbox.publish('savingEnded', 'success', 'remote', data.version);
176 document_version = data.version;
179 error: function() {event.error(); sandbox.publish('savingEnded', 'error', 'remote');}
182 dialog.on('cancel', function() {
188 getHistory: function() {
191 fetchDiff: function(ver1, ver2) {
194 url: sandbox.getConfig().documentDiffUrl(document_id),
195 data: {from: ver1, to: ver2},
196 success: function(data) {
197 sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2});
201 restoreVersion: function(version) {
202 var documentRestoreForm = $.extend({
204 version_field_name: 'version'
206 sandbox.getConfig().documentRestoreForm
208 dialog = Dialog.create({
209 fields: documentRestoreForm.fields,
210 title: gettext('Restore Version'),
211 executeButtonText: gettext('Restore')
214 dialog.on('execute', function(event) {
215 var formData = event.formData;
216 formData[documentRestoreForm.version_field_name] = version;
217 sandbox.publish('restoringStarted', {version: version});
218 if(sandbox.getConfig().jsonifySentData) {
219 formData = JSON.stringify(formData);
224 url: sandbox.getConfig().documentRestoreUrl(document_id),
226 success: function(data) {
227 document_version = data.version;
229 wlxmlDocument.loadXML(data.document);
230 documentDirty = false;
231 sandbox.publish('documentReverted', data.version);
238 getDocumentId: function() {
241 getDocumentVersion: function() {
242 return document_version;