+ var data = sandbox.getBootstrappedData();
+ var document_version = data.version;
+
+
+ var wlxmlDocument, text;
+
+ var loadDocument = function(text, isDraft, draftTimestamp) {
+ logger.debug('loading document');
+ try {
+ wlxmlDocument = wlxml.WLXMLDocumentFromXML(text, {editorConfig: sandbox.getConfig()}, Document);
+ } catch(e) {
+ logger.exception(e);
+ alert(gettext('This document contains errors and can\'t be loaded. :(')); // TODO
+ wlxmlDocument = wlxml.WLXMLDocumentFromXML(stubDocument, {}, Document);
+ }
+
+ Object.keys(data)
+ .filter(function(key) {
+ return key !== 'history' && key !== 'document';
+ })
+ .forEach(function(key) {
+ wlxmlDocument.setProperty(key, data[key]);
+ });
+
+ wlxmlDocument.registerExtension(listExtension);
+ sandbox.getPlugins().forEach(function(plugin) {
+ if(plugin.documentExtension) {
+ wlxmlDocument.registerExtension(plugin.documentExtension);
+ }
+ });
+
+ var modificationFlag = true;
+ var handleChange = function() {
+ documentDirty = true;
+ draftDirty = true;
+ modificationFlag = true;
+ };
+ wlxmlDocument.on('change', handleChange);
+ wlxmlDocument.on('contentSet', handleChange);
+
+ if(window.localStorage) {
+ window.setInterval(function() {
+ if(modificationFlag) {
+ modificationFlag = false;
+ return;
+ }
+ if(wlxmlDocument && documentDirty && draftDirty) {
+ var timestamp = datetime.currentStrfmt();
+ logger.debug('Saving draft to local storage.');
+ sandbox.publish('savingStarted', 'local');
+ window.localStorage.setItem(getLocalStorageKey().content, wlxmlDocument.toXML());
+ window.localStorage.setItem(getLocalStorageKey().contentTimestamp, timestamp);
+ sandbox.publish('savingEnded', 'success', 'local', {timestamp: timestamp});
+ draftDirty = false;
+ }
+ }, sandbox.getConfig().autoSaveInterval || 2500);
+ }
+ sandbox.publish('ready', isDraft, draftTimestamp);
+ };