From 459e75ab69c8d8e428040eceaf3b1c7b407f72e5 Mon Sep 17 00:00:00 2001 From: Egil Moeller Date: Tue, 12 May 2015 23:24:40 +0200 Subject: [PATCH] Less static code in templates --- src/static/js/error_handler.js | 13 ++++ src/static/js/main.js | 66 ++++++++++++++++ src/static/js/require-kernel-config.js | 12 +++ src/templates/pad.html | 100 ++----------------------- 4 files changed, 99 insertions(+), 92 deletions(-) create mode 100644 src/static/js/error_handler.js create mode 100644 src/static/js/main.js create mode 100644 src/static/js/require-kernel-config.js diff --git a/src/static/js/error_handler.js b/src/static/js/error_handler.js new file mode 100644 index 000000000..c1dea8210 --- /dev/null +++ b/src/static/js/error_handler.js @@ -0,0 +1,13 @@ +(function() { + // Display errors on page load to the user + // (Gets overridden by padutils.setupGlobalExceptionHandler) + var originalHandler = window.onerror; + window.onerror = function(msg, url, line) { + var box = document.getElementById('editorloadingbox'); + box.innerHTML = '

An error occured while loading the pad

' + + '

'+msg+' ' + + 'in '+ url +' (line '+ line +')

'; + // call original error handler + if(typeof(originalHandler) == 'function') originalHandler.call(null, arguments); + }; +})(); diff --git a/src/static/js/main.js b/src/static/js/main.js new file mode 100644 index 000000000..fa2f30a6e --- /dev/null +++ b/src/static/js/main.js @@ -0,0 +1,66 @@ +var clientVars = {}; +(function () { + var pathComponents = location.pathname.split('/'); + + // Strip 'p' and the padname from the pathname and set as baseURL + var baseURL = pathComponents.slice(0,pathComponents.length-2).join('/') + '/'; + + require.setRootURI(baseURL + "javascripts/src"); + require.setLibraryURI(baseURL + "javascripts/lib"); + require.setGlobalKeyPath("require"); + + window.requireKernel = require; + + requirejs.config({ + baseUrl: baseURL + "static/plugins", + paths: {'underscore': baseURL + "static/plugins/underscore/underscore"}, + }); + + requirejs( + [ + 'ep_etherpad-lite/static/js/rjquery', + 'ep_etherpad-lite/static/js/pluginfw/client_plugins', + 'ep_etherpad-lite/static/js/pluginfw/hooks' + ], function ($, plugins, hooks) { +console.log("hooks & plugins modules loaded"); + window.$ = $; // Expose jQuery #HACK + window.jQuery = $; + + browser = require('ep_etherpad-lite/static/js/browser').browser; + if ((!browser.msie) && (!(browser.mozilla && browser.version.indexOf("1.8.") == 0))) { + document.domain = document.domain; // for comet + } + + plugins.baseURL = baseURL; + plugins.update(function () { + hooks.plugins = plugins; + +console.log("hooks.plugins initialized"); + + // Call documentReady hook + $(function() { + hooks.aCallAll('documentReady'); + }); + + requirejs( + [ + 'ep_etherpad-lite/static/js/pad', + 'ep_etherpad-lite/static/js/chat', + 'ep_etherpad-lite/static/js/pad_editbar', + ], function (padMod, chatMod, padEditbarMod) { +console.log("pad loaded"); + + padMod.baseURL = baseURL; + padMod.init(); + + /* TODO: These globals shouldn't exist. */ + pad = padMod.pad; + chat = chatMod.chat; + padeditbar = padEditbarMod.padeditbar; + padimpexp = window.requireKernel('ep_etherpad-lite/static/js/pad_impexp').padimpexp; + } + ); + }); + } + ); +}()); diff --git a/src/static/js/require-kernel-config.js b/src/static/js/require-kernel-config.js new file mode 100644 index 000000000..1fb440831 --- /dev/null +++ b/src/static/js/require-kernel-config.js @@ -0,0 +1,12 @@ +(function () { + var pathComponents = location.pathname.split('/'); + + // Strip 'p' and the padname from the pathname and set as baseURL + var baseURL = pathComponents.slice(0,pathComponents.length-2).join('/') + '/'; + + require.setRootURI(baseURL + "javascripts/src"); + require.setLibraryURI(baseURL + "javascripts/lib"); + require.setGlobalKeyPath("require"); + + window.requireKernel = require; +}()); diff --git a/src/templates/pad.html b/src/templates/pad.html index 43a39a4d7..5308a0782 100644 --- a/src/templates/pad.html +++ b/src/templates/pad.html @@ -349,99 +349,15 @@ <% e.end_block(); %> <% e.begin_block("scripts"); %> - + + + + - - + <% e.begin_block("customScripts"); %> + + <% e.end_block(); %> - - - <% e.begin_block("customScripts"); %> - - <% e.end_block(); %> - - - + <% e.end_block(); %>