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 document_id = sandbox.getBootstrappedData().document_id;
20 var history = sandbox.getBootstrappedData().history;
21 var documentDirty = false;
22 var draftDirty = false;
24 var data = sandbox.getBootstrappedData();
25 var document_version = data.version;
28 var wlxmlDocument, text;
30 var loadDocument = function(text, isDraft, draftTimestamp) {
31 logger.debug('loading document');
34 wlxmlDocument = wlxml.WLXMLDocumentFromXML(text, {editorConfig: sandbox.getConfig()}, Document.Document);
37 alert(gettext('The content of this document seems to be invalid - only XML source editing will be possible. :(')); // TODO
38 wlxmlDocument = wlxml.WLXMLDocumentFromXML(text, {}, Document.DumbDocument);
43 .filter(function(key) {
44 return key !== 'history' && key !== 'document';
46 .forEach(function(key) {
47 wlxmlDocument.setProperty(key, data[key]);
50 wlxmlDocument.registerExtension(listExtension);
51 sandbox.getPlugins().forEach(function(plugin) {
52 if(plugin.documentExtension) {
53 wlxmlDocument.registerExtension(plugin.documentExtension);
57 var modificationFlag = true;
58 var handleChange = function() {
61 modificationFlag = true;
63 wlxmlDocument.on('change', handleChange);
64 wlxmlDocument.on('contentSet', handleChange);
66 if(window.localStorage) {
67 window.setInterval(function() {
68 if(modificationFlag) {
69 modificationFlag = false;
72 if(wlxmlDocument && documentDirty && draftDirty) {
73 var timestamp = datetime.currentStrfmt();
74 logger.debug('Saving draft to local storage.');
75 sandbox.publish('savingStarted', 'local');
76 window.localStorage.setItem(getLocalStorageKey().content, wlxmlDocument.toXML());
77 window.localStorage.setItem(getLocalStorageKey().contentTimestamp, timestamp);
78 sandbox.publish('savingEnded', 'success', 'local', {timestamp: timestamp});
81 }, sandbox.getConfig().autoSaveInterval || 2500);
83 sandbox.publish('ready', isDraft, draftTimestamp, xmlValid);
86 function readCookie(name) {
87 /* global escape, unescape, document */
88 var nameEQ = escape(name) + '=';
89 var ca = document.cookie.split(';');
90 for (var i = 0; i < ca.length; i++) {
92 while (c.charAt(0) === ' ') {
93 c = c.substring(1, c.length);
95 if (c.indexOf(nameEQ) === 0) {
96 return unescape(c.substring(nameEQ.length, c.length));
104 beforeSend: function(xhr, settings) {
105 if (!(/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type))) {
106 xhr.setRequestHeader('X-CSRFToken', readCookie('csrftoken'));
111 var reloadHistory = function() {
114 url: sandbox.getConfig().documentHistoryUrl(document_id),
115 success: function(data) {
117 sandbox.publish('historyItemAdded', data.slice(-1)[0]);
122 var getLocalStorageKey = function(forVersion) {
123 var base = 'draft-id:' + document_id + '-ver:' + (forVersion || wlxmlDocument.properties.version);
126 contentTimestamp: base + '-content-timestamp'
133 if(window.localStorage) {
134 text = window.localStorage.getItem(getLocalStorageKey(document_version).content);
136 var timestamp = window.localStorage.getItem(getLocalStorageKey(document_version).contentTimestamp),
139 logger.debug('Local draft exists');
140 var dialog = Dialog.create({
141 title: gettext('Local draft of a document exists'),
142 text: gettext('Unsaved local draft of this version of the document exists in your browser. Do you want to load it instead?'),
143 executeButtonText: gettext('Yes, restore local draft'),
144 cancelButtonText: gettext('No, use version loaded from the server')
146 dialog.on('cancel', function() {
147 logger.debug('Bootstrapped version chosen');
149 text = sandbox.getBootstrappedData().document;
152 dialog.on('execute', function(event) {
153 logger.debug('Local draft chosen');
158 dialog.on('close', function() {
159 loadDocument(text, usingDraft, timestamp);
162 loadDocument(sandbox.getBootstrappedData().document, false);
165 loadDocument(sandbox.getBootstrappedData().document, false);
168 getDocument: function() {
169 return wlxmlDocument;
171 saveDocument: function() {
172 var documentSaveForm = $.extend({
174 content_field_name: 'text',
175 version_field_name: 'version'
177 sandbox.getConfig().documentSaveForm
179 dialog = Dialog.create({
180 fields: documentSaveForm.fields,
181 title: gettext('Save Document'),
182 executeButtonText: gettext('Save'),
183 cancelButtonText: gettext('Cancel')
186 dialog.on('execute', function(event) {
187 sandbox.publish('savingStarted', 'remote');
189 var formData = event.formData;
190 formData[documentSaveForm.content_field_name] = wlxmlDocument.toXML();
191 formData[documentSaveForm.version_field_name] = wlxmlDocument.properties.version;
192 if(sandbox.getConfig().jsonifySentData) {
193 formData = JSON.stringify(formData);
196 dialog.toggleButtons(false);
199 url: sandbox.getConfig().documentSaveUrl(document_id),
201 success: function(data) {
203 sandbox.publish('savingEnded', 'success', 'remote', data);
206 .filter(function(key) {
207 return key !== 'text';
209 .forEach(function(key) {
210 wlxmlDocument.setProperty(key, data[key]);
215 error: function() {event.error(); sandbox.publish('savingEnded', 'error', 'remote');}
218 dialog.on('cancel', function() {
224 getHistory: function() {
227 fetchDiff: function(ver1, ver2) {
230 url: sandbox.getConfig().documentDiffUrl(document_id),
231 data: {from: ver1, to: ver2},
232 success: function(data) {
233 sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2});
237 restoreVersion: function(version) {
238 var documentRestoreForm = $.extend({
240 version_field_name: 'version'
242 sandbox.getConfig().documentRestoreForm
244 dialog = Dialog.create({
245 fields: documentRestoreForm.fields,
246 title: gettext('Restore Version'),
247 executeButtonText: gettext('Restore'),
248 cancelButtonText: gettext('Cancel')
251 dialog.on('execute', function(event) {
252 var formData = event.formData;
253 formData[documentRestoreForm.version_field_name] = version;
254 sandbox.publish('restoringStarted', {version: version});
255 if(sandbox.getConfig().jsonifySentData) {
256 formData = JSON.stringify(formData);
261 url: sandbox.getConfig().documentRestoreUrl(document_id),
263 success: function(data) {
265 .filter(function(key) {
266 return key !== 'document';
268 .forEach(function(key) {
269 wlxmlDocument.setProperty(key, data[key]);
272 wlxmlDocument.loadXML(data.document);
273 documentDirty = false;
274 sandbox.publish('documentReverted', data.version);
281 dropDraft: function() {
282 logger.debug('Dropping a draft...');
283 wlxmlDocument.loadXML(sandbox.getBootstrappedData().document);
285 logger.debug('Draft dropped');
286 sandbox.publish('draftDropped');