5 'wlxml/extensions/list/list',
6 'fnpjs/logging/logging',
9 ], function($, Dialog, wlxml, listExtension, logging, datetime, Document) {
12 /* global gettext, alert, window */
14 var logger = logging.getLogger('editor.modules.data'),
15 stubDocument = '<section><div>' + gettext('This is an empty document.') + '</div></section>';
18 return function(sandbox) {
20 var document_id = sandbox.getBootstrappedData().document_id;
21 var history = sandbox.getBootstrappedData().history;
22 var documentDirty = false;
23 var draftDirty = false;
25 var data = sandbox.getBootstrappedData();
26 var document_version = data.version;
29 var wlxmlDocument, text;
31 var loadDocument = function(text, isDraft, draftTimestamp) {
32 logger.debug('loading document');
34 wlxmlDocument = wlxml.WLXMLDocumentFromXML(text, {editorConfig: sandbox.getConfig()}, Document);
37 alert(gettext('This document contains errors and can\'t be loaded. :(')); // TODO
38 wlxmlDocument = wlxml.WLXMLDocumentFromXML(stubDocument, {}, Document);
42 .filter(function(key) {
43 return key !== 'history' && key !== 'document';
45 .forEach(function(key) {
46 wlxmlDocument.setProperty(key, data[key]);
49 wlxmlDocument.registerExtension(listExtension);
50 sandbox.getPlugins().forEach(function(plugin) {
51 if(plugin.documentExtension) {
52 wlxmlDocument.registerExtension(plugin.documentExtension);
56 var modificationFlag = true;
57 var handleChange = function() {
60 modificationFlag = true;
62 wlxmlDocument.on('change', handleChange);
63 wlxmlDocument.on('contentSet', handleChange);
65 if(window.localStorage) {
66 window.setInterval(function() {
67 if(modificationFlag) {
68 modificationFlag = false;
71 if(wlxmlDocument && documentDirty && draftDirty) {
72 var timestamp = datetime.currentStrfmt();
73 logger.debug('Saving draft to local storage.');
74 sandbox.publish('savingStarted', 'local');
75 window.localStorage.setItem(getLocalStorageKey().content, wlxmlDocument.toXML());
76 window.localStorage.setItem(getLocalStorageKey().contentTimestamp, timestamp);
77 sandbox.publish('savingEnded', 'success', 'local', {timestamp: timestamp});
80 }, sandbox.getConfig().autoSaveInterval || 2500);
82 sandbox.publish('ready', isDraft, draftTimestamp);
85 function readCookie(name) {
86 /* global escape, unescape, document */
87 var nameEQ = escape(name) + '=';
88 var ca = document.cookie.split(';');
89 for (var i = 0; i < ca.length; i++) {
91 while (c.charAt(0) === ' ') {
92 c = c.substring(1, c.length);
94 if (c.indexOf(nameEQ) === 0) {
95 return unescape(c.substring(nameEQ.length, c.length));
103 beforeSend: function(xhr, settings) {
104 if (!(/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type))) {
105 xhr.setRequestHeader('X-CSRFToken', readCookie('csrftoken'));
110 var reloadHistory = function() {
113 url: sandbox.getConfig().documentHistoryUrl(document_id),
114 success: function(data) {
116 sandbox.publish('historyItemAdded', data.slice(-1)[0]);
121 var getLocalStorageKey = function(forVersion) {
122 var base = 'draft-id:' + document_id + '-ver:' + (forVersion || wlxmlDocument.properties.version);
125 contentTimestamp: base + '-content-timestamp'
132 if(window.localStorage) {
133 text = window.localStorage.getItem(getLocalStorageKey(document_version).content);
135 var timestamp = window.localStorage.getItem(getLocalStorageKey(document_version).contentTimestamp),
138 logger.debug('Local draft exists');
139 var dialog = Dialog.create({
140 title: gettext('Local draft of a document exists'),
141 text: gettext('Unsaved local draft of this version of the document exists in your browser. Do you want to load it instead?'),
142 executeButtonText: gettext('Yes, restore local draft'),
143 cancelButtonText: gettext('No, use version loaded from the server')
145 dialog.on('cancel', function() {
146 logger.debug('Bootstrapped version chosen');
148 text = sandbox.getBootstrappedData().document;
151 dialog.on('execute', function(event) {
152 logger.debug('Local draft chosen');
157 dialog.on('close', function() {
158 loadDocument(text, usingDraft, timestamp);
161 loadDocument(sandbox.getBootstrappedData().document, false);
164 loadDocument(sandbox.getBootstrappedData().document, false);
167 getDocument: function() {
168 return wlxmlDocument;
170 saveDocument: function() {
171 var documentSaveForm = $.extend({
173 content_field_name: 'text',
174 version_field_name: 'version'
176 sandbox.getConfig().documentSaveForm
178 dialog = Dialog.create({
179 fields: documentSaveForm.fields,
180 title: gettext('Save Document'),
181 executeButtonText: gettext('Save'),
182 cancelButtonText: gettext('Cancel')
185 dialog.on('execute', function(event) {
186 sandbox.publish('savingStarted', 'remote');
188 var formData = event.formData;
189 formData[documentSaveForm.content_field_name] = wlxmlDocument.toXML();
190 formData[documentSaveForm.version_field_name] = wlxmlDocument.properties.version;
191 if(sandbox.getConfig().jsonifySentData) {
192 formData = JSON.stringify(formData);
195 dialog.toggleButtons(false);
198 url: sandbox.getConfig().documentSaveUrl(document_id),
200 success: function(data) {
202 sandbox.publish('savingEnded', 'success', 'remote', data);
205 .filter(function(key) {
206 return key !== 'text';
208 .forEach(function(key) {
209 wlxmlDocument.setProperty(key, data[key]);
214 error: function() {event.error(); sandbox.publish('savingEnded', 'error', 'remote');}
217 dialog.on('cancel', function() {
223 getHistory: function() {
226 fetchDiff: function(ver1, ver2) {
229 url: sandbox.getConfig().documentDiffUrl(document_id),
230 data: {from: ver1, to: ver2},
231 success: function(data) {
232 sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2});
236 restoreVersion: function(version) {
237 var documentRestoreForm = $.extend({
239 version_field_name: 'version'
241 sandbox.getConfig().documentRestoreForm
243 dialog = Dialog.create({
244 fields: documentRestoreForm.fields,
245 title: gettext('Restore Version'),
246 executeButtonText: gettext('Restore'),
247 cancelButtonText: gettext('Cancel')
250 dialog.on('execute', function(event) {
251 var formData = event.formData;
252 formData[documentRestoreForm.version_field_name] = version;
253 sandbox.publish('restoringStarted', {version: version});
254 if(sandbox.getConfig().jsonifySentData) {
255 formData = JSON.stringify(formData);
260 url: sandbox.getConfig().documentRestoreUrl(document_id),
262 success: function(data) {
264 .filter(function(key) {
265 return key !== 'document';
267 .forEach(function(key) {
268 wlxmlDocument.setProperty(key, data[key]);
271 wlxmlDocument.loadXML(data.document);
272 documentDirty = false;
273 sandbox.publish('documentReverted', data.version);
280 dropDraft: function() {
281 logger.debug('Dropping a draft...');
282 wlxmlDocument.loadXML(sandbox.getBootstrappedData().document);
284 logger.debug('Draft dropped');
285 sandbox.publish('draftDropped');
287 getDocumentId: function() {