From 7c65715e2717203d2fd13f40b5935720005b152e Mon Sep 17 00:00:00 2001 From: "Friedrich W. H. Kossebau" Date: Wed, 14 Jan 2015 08:17:18 +0100 Subject: [PATCH 1/4] Fix ViewerJS/index.html not having charset definition in first 1024 bytes --- programs/viewer/index.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/programs/viewer/index.html b/programs/viewer/index.html index a4b989e6..fac2a259 100644 --- a/programs/viewer/index.html +++ b/programs/viewer/index.html @@ -1,4 +1,7 @@ + + + - - - ViewerJS - + From 3f0ebaf9d9456549e5842a0dd31c525c1e6dea85 Mon Sep 17 00:00:00 2001 From: "Friedrich W. H. Kossebau" Date: Fri, 16 Jan 2015 14:26:07 +0100 Subject: [PATCH 4/4] ViewerJS: Concentrate url parsing in PluginLoader.js, ask server for mimetype no more document.location/window.location handling all over index.html, PluginLoader.js and viewer.js Allows to user viewer.js for other usecases where parameters are not derived from document.location, but passed in by other ways --- programs/viewer/PluginLoader.js | 203 ++++++++++++++++++++++++++------ programs/viewer/index.html | 2 +- programs/viewer/viewer.js | 37 ++---- 3 files changed, 175 insertions(+), 67 deletions(-) diff --git a/programs/viewer/PluginLoader.js b/programs/viewer/PluginLoader.js index 4828e548..1b546528 100644 --- a/programs/viewer/PluginLoader.js +++ b/programs/viewer/PluginLoader.js @@ -1,5 +1,5 @@ /** - * Copyright (C) 2012 KO GmbH + * Copyright (C) 2012-2015 KO GmbH * * @licstart * This file is part of WebODF. @@ -24,49 +24,178 @@ /*global document, window, Viewer, ODFViewerPlugin*/ -var viewer; - -function loadPlugin(pluginName, callback) { - "use strict"; - var script, style; - - // Load script - script = document.createElement('script'); - script.async = false; - script.onload = callback; - script.src = pluginName + '.js'; - script.type = 'text/javascript'; - document.getElementsByTagName('head')[0].appendChild(script); -} - -function loadDocument(documentUrl) { +function loadDocument() { "use strict"; - var Plugin; + var pluginRegistry = [ + (function() { + var odfMimetypes = [ + 'application/vnd.oasis.opendocument.text', + 'application/vnd.oasis.opendocument.text-flat-xml', + 'application/vnd.oasis.opendocument.text-template', + 'application/vnd.oasis.opendocument.presentation', + 'application/vnd.oasis.opendocument.presentation-flat-xml', + 'application/vnd.oasis.opendocument.presentation-template', + 'application/vnd.oasis.opendocument.spreadsheet', + 'application/vnd.oasis.opendocument.spreadsheet-flat-xml', + 'application/vnd.oasis.opendocument.spreadsheet-template']; + var odfFileExtensions = [ + 'odt', + 'fodt', + 'ott', + 'odp', + 'fodp', + 'otp', + 'ods', + 'fods', + 'ots']; - if (documentUrl) { - var extension = documentUrl.split('.').pop(); + return { + supportsMimetype: function(mimetype) { + return (odfMimetypes.indexOf(mimetype) !== -1); + }, + supportsFileExtension: function(extension) { + return (odfFileExtensions.indexOf(extension) !== -1); + }, + path: "./ODFViewerPlugin", + getClass: function() { return ODFViewerPlugin; } + }; + }()) + ]; - switch (extension) { - case 'odt': - case 'fodt': - case 'ott': - case 'odp': - case 'fodp': - case 'otp': - case 'ods': - case 'fods': - case 'ots': - loadPlugin('./ODFViewerPlugin', function () { - Plugin = ODFViewerPlugin; - }); - break; - } + function loadPlugin(pluginName, callback) { + "use strict"; + var script, style; + + // Load script + script = document.createElement('script'); + script.async = false; + script.onload = callback; + script.src = pluginName + '.js'; + script.type = 'text/javascript'; + document.getElementsByTagName('head')[0].appendChild(script); } + + function estimateTypeByHeaderContentType(documentUrl, cb) { + var xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function() { + var mimetype, matchingPluginData; + if (xhr.readyState === 4) { + if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 0) { + mimetype = xhr.getResponseHeader('content-type'); + + if (mimetype) { + pluginRegistry.some(function(pluginData) { + if (pluginData.supportsMimetype(mimetype)) { + matchingPluginData = pluginData; + console.log('Found plugin by mimetype and xhr head: ' + mimetype); + return true; + } + return false; + }); + } + } + cb(matchingPluginData); + } + }; + xhr.open("HEAD", documentUrl, true); + xhr.send(); + } + + + function doEstimateTypeByFileExtension(extension) { + var matchingPluginData; + + pluginRegistry.some(function(pluginData) { + if (pluginData.supportsFileExtension(extension)) { + matchingPluginData = pluginData; + return true; + } + return false; + }); + + return matchingPluginData; + } + + + function estimateTypeByFileExtension(extension) { + var matchingPluginData = doEstimateTypeByFileExtension(extension) + + if (matchingPluginData) { + console.log('Found plugin by parameter type: ' + extension); + } + + return matchingPluginData; + } + + + function estimateTypeByFileExtensionFromPath(documentUrl) { + // See to get any path from the url and grep what could be a file extension + var documentPath = documentUrl.split('?')[0], + extension = documentPath.split('.').pop(), + matchingPluginData = doEstimateTypeByFileExtension(extension) + + if (matchingPluginData) { + console.log('Found plugin by file extension from path: ' + extension); + } + + return matchingPluginData; + } + + + function parseSearchParameters(location) { + var parameters = {}, + search = location.search || "?"; + + search.substr(1).split('&').forEach(function (q) { + // skip empty strings + if (!q) { + return; + } + // if there is no '=', have it handled as if given key was set to undefined + var s = q.split('=', 2); + parameters[decodeURIComponent(s[0])] = decodeURIComponent(s[1]); + }); + + return parameters; + } + + window.onload = function () { - if (Plugin) { - viewer = new Viewer(new Plugin()); + var viewer, + documentUrl = document.location.hash.substring(1), + parameters = parseSearchParameters(document.location), + Plugin; + + if (documentUrl) { + // try to guess the title as filename from the location, if not set by parameter + if (!parameters.title) { + parameters.title = documentUrl.replace(/^.*[\\\/]/, ''); + } + + parameters.documentUrl = documentUrl; + + // trust the server most + estimateTypeByHeaderContentType(documentUrl, function(pluginData) { + if (!pluginData) { + if (parameters.type) { + pluginData = estimateTypeByFileExtension(parameters.type); + } else { + // last ressort: try to guess from path + pluginData = estimateTypeByFileExtensionFromPath(documentUrl); + } + } + + if (pluginData) { + loadPlugin(pluginData.path, function () { + Plugin = pluginData.getClass(); + viewer = new Viewer(new Plugin(), parameters); + }); + } else { + viewer = new Viewer(); + } + }); } else { viewer = new Viewer(); } diff --git a/programs/viewer/index.html b/programs/viewer/index.html index d8648e1c..54fa3aff 100644 --- a/programs/viewer/index.html +++ b/programs/viewer/index.html @@ -59,7 +59,7 @@ limitations under the License. diff --git a/programs/viewer/viewer.js b/programs/viewer/viewer.js index e649dc74..6fe3241a 100644 --- a/programs/viewer/viewer.js +++ b/programs/viewer/viewer.js @@ -1,5 +1,5 @@ /** - * Copyright (C) 2012-2014 KO GmbH + * Copyright (C) 2012-2015 KO GmbH * * @licstart * This file is part of WebODF. @@ -44,7 +44,7 @@ /*global document, window*/ -function Viewer(viewerPlugin) { +function Viewer(viewerPlugin, parameters) { "use strict"; var self = this, @@ -69,7 +69,6 @@ function Viewer(viewerPlugin) { dialogOverlay = document.getElementById('dialogOverlay'), toolbarRight = document.getElementById('toolbarRight'), aboutDialog, - filename, pages = [], currentPage, scaleChangeTimer, @@ -258,37 +257,17 @@ function Viewer(viewerPlugin) { return kDefaultScale; } - function parseSearchParameters(location) { - var parameters = {}, - search = location.search || "?"; - search.substr(1).split('&').forEach(function (q) { - var s = q.split('=', 2); - parameters[s[0]] = s[1]; - }); - return parameters; - } - this.initialize = function () { - var location = String(document.location), - parameters = parseSearchParameters(document.location), - pos = location.indexOf('#'), - initialScale, + var initialScale, element; - location = location.substr(pos + 1); - if (pos === -1 || location.length === 0) { - console.log('Could not parse file path argument.'); - return; - } + initialScale = readZoomParameter(parameters.zoom); - initialScale = readZoomParameter(parameters['zoom']); - - url = location; - filename = url.replace(/^.*[\\\/]/, ''); - document.title = filename; + url = parameters.documentUrl; + document.title = parameters.title; var documentName = document.getElementById('documentName'); documentName.innerHTML = ""; - documentName.appendChild(documentName.ownerDocument.createTextNode(document.title)); + documentName.appendChild(documentName.ownerDocument.createTextNode(parameters.title)); viewerPlugin.onLoad = function () { document.getElementById('pluginVersion').innerHTML = viewerPlugin.getPluginVersion(); @@ -321,7 +300,7 @@ function Viewer(viewerPlugin) { delayedRefresh(); }; - viewerPlugin.initialize(canvasContainer, location); + viewerPlugin.initialize(canvasContainer, url); }; /**