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');
17 return function(sandbox) {
19 var data = sandbox.getBootstrappedData(),
20 documentDirty = false,
25 var loadDocument = function(text, isDraft, draftTimestamp) {
26 logger.debug('loading document');
29 wlxmlDocument = wlxml.WLXMLDocumentFromXML(text, {editorConfig: sandbox.getConfig()}, Document.Document);
32 alert(gettext('The content of this document seems to be invalid - only XML source editing will be possible. :(')); // TODO
33 wlxmlDocument = wlxml.WLXMLDocumentFromXML(text, {}, Document.DumbDocument);
38 .filter(function(key) {
39 return key !== 'history' && key !== 'document';
41 .forEach(function(key) {
42 wlxmlDocument.setProperty(key, data[key]);
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 var timestamp = datetime.currentStrfmt(),
64 key = getLocalStorageKey();
65 if(modificationFlag) {
66 modificationFlag = false;
69 if(wlxmlDocument && documentDirty && draftDirty) {
70 logger.debug('Saving draft to local storage.');
71 sandbox.publish('savingStarted', 'local');
72 window.localStorage.setItem(key.content, wlxmlDocument.toXML());
73 window.localStorage.setItem(key.contentTimestamp, timestamp);
74 sandbox.publish('savingEnded', 'success', 'local', {timestamp: timestamp});
77 }, sandbox.getConfig().autoSaveInterval || 2500);
79 sandbox.publish('ready', isDraft, draftTimestamp, xmlValid);
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(data.document_id),
111 success: function(history) {
112 data.history = history;
113 sandbox.publish('historyItemAdded', history.slice(-1)[0]);
118 var getLocalStorageKey = function(forVersion) {
119 var base = 'draft-id:' + data.document_id + '-ver:' + (forVersion || wlxmlDocument.properties.version);
122 contentTimestamp: base + '-content-timestamp'
130 if(window.localStorage) {
131 text = window.localStorage.getItem(getLocalStorageKey(data.version).content);
133 var timestamp = window.localStorage.getItem(getLocalStorageKey(data.version).contentTimestamp),
136 logger.debug('Local draft exists');
137 var dialog = Dialog.create({
138 title: gettext('Local draft of a document exists'),
139 text: gettext('Unsaved local draft of this version of the document exists in your browser. Do you want to load it instead?'),
140 executeButtonText: gettext('Yes, restore local draft'),
141 cancelButtonText: gettext('No, use version loaded from the server')
143 dialog.on('cancel', function() {
144 logger.debug('Bootstrapped version chosen');
146 text = sandbox.getBootstrappedData().document;
149 dialog.on('execute', function(event) {
150 logger.debug('Local draft chosen');
155 dialog.on('close', function() {
156 loadDocument(text, usingDraft, timestamp);
159 loadDocument(sandbox.getBootstrappedData().document, false);
162 loadDocument(sandbox.getBootstrappedData().document, false);
165 getDocument: function() {
166 return wlxmlDocument;
168 saveDocument: function() {
169 var documentSaveForm = $.extend({
171 content_field_name: 'text',
172 version_field_name: 'version'
174 sandbox.getConfig().documentSaveForm
176 dialog = Dialog.create({
177 fields: documentSaveForm.fields,
178 title: gettext('Save Document'),
179 executeButtonText: gettext('Save'),
180 cancelButtonText: gettext('Cancel')
182 /* Set stage field initial value to current document stage. */
183 for (var i in documentSaveForm.fields) {
184 if (documentSaveForm.fields[i].name == 'textsave-stage') {
185 documentSaveForm.fields[i].initialValue = data.stage;
189 dialog.on('execute', function(event) {
190 sandbox.publish('savingStarted', 'remote');
192 var formData = event.formData;
193 formData[documentSaveForm.content_field_name] = wlxmlDocument.toXML();
194 formData[documentSaveForm.version_field_name] = wlxmlDocument.properties.version;
195 if(sandbox.getConfig().jsonifySentData) {
196 formData = JSON.stringify(formData);
199 dialog.toggleButtons(false);
202 url: sandbox.getConfig().documentSaveUrl(data.document_id),
204 success: function(data) {
206 sandbox.publish('savingEnded', 'success', 'remote', data);
209 .filter(function(key) {
210 return key !== 'text';
212 .forEach(function(key) {
213 wlxmlDocument.setProperty(key, data[key]);
218 error: function(data) {
220 sandbox.publish('savingEnded', 'error', 'remote');
221 var dialog = Dialog.create({
222 title: gettext('Error'),
223 text: JSON.parse(data.responseText).text.join('\n'),
224 executeButtonText: gettext('Close')
227 dialog.on('execute', function(e) {
233 dialog.on('cancel', function() {
239 getHistory: function() {
242 fetchDiff: function(ver1, ver2) {
245 url: sandbox.getConfig().documentDiffUrl(data.document_id),
246 data: {from: ver1, to: ver2},
247 success: function(data) {
248 sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2});
252 restoreVersion: function(version) {
253 var documentRestoreForm = $.extend({
255 version_field_name: 'version'
257 sandbox.getConfig().documentRestoreForm
259 dialog = Dialog.create({
260 fields: documentRestoreForm.fields,
261 title: gettext('Restore Version'),
262 executeButtonText: gettext('Restore'),
263 cancelButtonText: gettext('Cancel')
266 dialog.on('execute', function(event) {
267 var formData = event.formData;
268 formData[documentRestoreForm.version_field_name] = version;
269 sandbox.publish('restoringStarted', {version: version});
270 if(sandbox.getConfig().jsonifySentData) {
271 formData = JSON.stringify(formData);
276 url: sandbox.getConfig().documentRestoreUrl(data.document_id),
278 success: function(data) {
280 .filter(function(key) {
281 return key !== 'document';
283 .forEach(function(key) {
284 wlxmlDocument.setProperty(key, data[key]);
287 wlxmlDocument.loadXML(data.document);
288 documentDirty = false;
289 sandbox.publish('documentReverted', data.version);
296 publishVersion: function(revision) {
297 var documentPublishForm = $.extend({
299 revision_field_name: 'revision'
301 sandbox.getConfig().documentPublishForm
303 dialog = Dialog.create({
304 fields: documentPublishForm.fields,
305 title: gettext('Publish'),
306 executeButtonText: gettext('Publish'),
307 cancelButtonText: gettext('Cancel')
310 dialog.on('execute', function(event) {
311 var formData = event.formData;
312 formData[documentPublishForm.revision_field_name] = revision;
313 sandbox.publish('publishingStarted', {version: revision});
314 if(sandbox.getConfig().jsonifySentData) {
315 formData = JSON.stringify(formData);
321 url: sandbox.getConfig().documentPublishUrl,
323 success: function(data) {
325 sandbox.publish('documentPublished');
332 dropDraft: function() {
333 logger.debug('Dropping a draft...');
334 wlxmlDocument.loadXML(sandbox.getBootstrappedData().document);
336 logger.debug('Draft dropped');
337 sandbox.publish('draftDropped');