- var wlxmlDocument = wlxml.WLXMLDocumentFromXML(sandbox.getBootstrappedData().document);
-
- if(doc === '') {
- doc = '<section\n\
- xmlns="http://nowoczesnapolska.org.pl/sst#"\n\
- xmlns:xlink="http://www.w3.org/1999/xlink"\n\
- xmlns:dc="http://purl.org/dc/elements/1.1/"\n\
- xmlns:dcterms="http://purl.org/dc/terms/"\n\
- >\n\
- <metadata>\n\
- </metadata>\n\
- <div class="p"></div>\n\
- </section>';
- }
-
+ var wlxmlDocument, text;
+
+ var loadDocument = function(text) {
+ logger.debug('loading document');
+ try {
+ wlxmlDocument = wlxml.WLXMLDocumentFromXML(text);
+ } catch(e) {
+ logger.exception(e);
+ alert(gettext('This document contains errors and can\'t be loaded. :(')); // TODO
+ wlxmlDocument = wlxml.WLXMLDocumentFromXML(stubDocument);
+ }
+
+ 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) {
+ logger.debug('Saving draft to local storage.');
+ sandbox.publish('savingStarted', 'local');
+ window.localStorage.setItem(getLocalStorageKey(), wlxmlDocument.toXML());
+ sandbox.publish('savingEnded', 'success', 'local');
+ draftDirty = false;
+ }
+ }, sandbox.getConfig().autoSaveInterval || 2500);
+ }
+ sandbox.publish('ready');
+ };