From 872eb8cad5640b85c8d9286ef0af0237bf825f00 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Mon, 14 Jul 2014 15:10:52 +0200 Subject: [PATCH 1/1] editor: Allow for editing raw source in the source editor if document contains invalid xml Up until now a stub empty document was loaded. --- src/editor/modules/data/data.js | 13 +++++++------ src/editor/modules/data/document.js | 16 +++++++++++++++- src/editor/modules/rng/rng.js | 9 +++++++-- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/editor/modules/data/data.js b/src/editor/modules/data/data.js index 793d7b4..b3b9fe4 100644 --- a/src/editor/modules/data/data.js +++ b/src/editor/modules/data/data.js @@ -11,8 +11,7 @@ define([ 'use strict'; /* global gettext, alert, window */ -var logger = logging.getLogger('editor.modules.data'), - stubDocument = '
' + gettext('This is an empty document.') + '
'; +var logger = logging.getLogger('editor.modules.data'); return function(sandbox) { @@ -30,12 +29,14 @@ return function(sandbox) { var loadDocument = function(text, isDraft, draftTimestamp) { logger.debug('loading document'); + var xmlValid = true; try { - wlxmlDocument = wlxml.WLXMLDocumentFromXML(text, {editorConfig: sandbox.getConfig()}, Document); + wlxmlDocument = wlxml.WLXMLDocumentFromXML(text, {editorConfig: sandbox.getConfig()}, Document.Document); } catch(e) { logger.exception(e); - alert(gettext('This document contains errors and can\'t be loaded. :(')); // TODO - wlxmlDocument = wlxml.WLXMLDocumentFromXML(stubDocument, {}, Document); + alert(gettext('The content of this document seems to be invalid - only XML source editing will be possible. :(')); // TODO + wlxmlDocument = wlxml.WLXMLDocumentFromXML(text, {}, Document.DumbDocument); + xmlValid = false; } Object.keys(data) @@ -79,7 +80,7 @@ return function(sandbox) { } }, sandbox.getConfig().autoSaveInterval || 2500); } - sandbox.publish('ready', isDraft, draftTimestamp); + sandbox.publish('ready', isDraft, draftTimestamp, xmlValid); }; function readCookie(name) { diff --git a/src/editor/modules/data/document.js b/src/editor/modules/data/document.js index b0f7c25..15dd608 100644 --- a/src/editor/modules/data/document.js +++ b/src/editor/modules/data/document.js @@ -63,6 +63,20 @@ _.extend(Document.prototype, { } }); -return Document; +var DumbDocument = function() { + Document.apply(this, Array.prototype.slice.call(arguments, 0)); +}; +DumbDocument.prototype = Object.create(Document.prototype); +_.extend(DumbDocument.prototype, { + loadXML: function(xml) { + this._xml = xml; + this.trigger('contentSet'); + }, + toXML: function() { + return this._xml; + } +}); + +return {Document: Document, DumbDocument: DumbDocument}; }); \ No newline at end of file diff --git a/src/editor/modules/rng/rng.js b/src/editor/modules/rng/rng.js index 4160ad9..73f83d5 100644 --- a/src/editor/modules/rng/rng.js +++ b/src/editor/modules/rng/rng.js @@ -59,7 +59,7 @@ return function(sandbox) { }; eventHandlers.data = { - ready: function(usingDraft, draftTimestamp) { + ready: function(usingDraft, draftTimestamp, xmlValid) { wlxmlDocument = sandbox.getModule('data').getDocument(); views.mainLayout.setView('mainView', views.mainTabs.getAsView()); @@ -72,7 +72,12 @@ return function(sandbox) { sandbox.getModule('mainBar').setCommandEnabled('drop-draft', usingDraft); sandbox.getModule('mainBar').setCommandEnabled('save', usingDraft); - _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'mainBar', 'indicator', 'documentHistory', 'diffViewer', 'statusBar'], function(moduleName) { + + var toStart = ['sourceEditor', 'documentToolbar', 'mainBar', 'indicator', 'documentHistory', 'diffViewer', 'statusBar']; + if(xmlValid) { + toStart.push('documentCanvas'); + } + _.each(toStart, function(moduleName) { sandbox.getModule(moduleName).start(); }); -- 2.20.1