From 3cce12e13f5062f4028035fda6fe6462540e084c Mon Sep 17 00:00:00 2001 From: Jermolene Date: Mon, 5 May 2014 14:41:46 +0100 Subject: [PATCH] More refactoring of startup.js --- core/modules/startup.js | 58 ------------------ core/modules/startup/main-render.js | 2 +- core/modules/startup/password.js | 2 +- core/modules/startup/rootwidget.js | 81 ++++++++++++++++++++++++++ core/modules/startup/syncer-browser.js | 4 +- core/modules/utils/dom/dom.js | 12 ---- 6 files changed, 86 insertions(+), 73 deletions(-) create mode 100644 core/modules/startup/rootwidget.js diff --git a/core/modules/startup.js b/core/modules/startup.js index 9fef05dfc..b4e9bd5a2 100755 --- a/core/modules/startup.js +++ b/core/modules/startup.js @@ -20,8 +20,6 @@ exports.synchronous = true; // Set to `true` to enable performance instrumentation var PERFORMANCE_INSTRUMENTATION = false; -var widget = require("$:/core/modules/widgets/widget.js"); - exports.startup = function() { var modules,n,m,f; if($tw.browser) { @@ -52,8 +50,6 @@ exports.startup = function() { }); // Clear outstanding tiddler store change events to avoid an unnecessary refresh cycle at startup $tw.wiki.clearTiddlerEventQueue(); - // Set up the syncer object - $tw.syncer = new $tw.Syncer({wiki: $tw.wiki}); // Host-specific startup if($tw.browser) { // Set up our beforeunload handler @@ -71,60 +67,6 @@ exports.startup = function() { }); // Install the animator $tw.anim = new $tw.utils.Animator(); - // Create a root widget for attaching event handlers. By using it as the parentWidget for another widget tree, one can reuse the event handlers - $tw.rootWidget = new widget.widget({ - type: "widget", - children: [] - },{ - wiki: $tw.wiki, - document: document - }); - // Install the modal message mechanism - $tw.modal = new $tw.utils.Modal($tw.wiki); - $tw.rootWidget.addEventListener("tw-modal",function(event) { - $tw.modal.display(event.param); - }); - // Install the notification mechanism - $tw.notifier = new $tw.utils.Notifier($tw.wiki); - $tw.rootWidget.addEventListener("tw-notify",function(event) { - $tw.notifier.display(event.param); - }); - // Install the scroller - $tw.pageScroller = new $tw.utils.PageScroller(); - $tw.rootWidget.addEventListener("tw-scroll",function(event) { - $tw.pageScroller.handleEvent(event); - }); - // Listen for the tw-home message - $tw.rootWidget.addEventListener("tw-home",function(event) { - displayDefaultTiddlers(); - }); - // Install the save action handlers - $tw.rootWidget.addEventListener("tw-save-wiki",function(event) { - $tw.syncer.saveWiki({ - template: event.param, - downloadType: "text/plain" - }); - }); - $tw.rootWidget.addEventListener("tw-auto-save-wiki",function(event) { - $tw.syncer.saveWiki({ - method: "autosave", - template: event.param, - downloadType: "text/plain" - }); - }); - $tw.rootWidget.addEventListener("tw-download-file",function(event) { - $tw.syncer.saveWiki({ - method: "download", - template: event.param, - downloadType: "text/plain" - }); - }); - // If we're being viewed on a data: URI then give instructions for how to save - if(document.location.protocol === "data:") { - $tw.utils.dispatchCustomEvent(document,"tw-modal",{ - param: "$:/language/Modals/SaveInstructions" - }); - } } }; diff --git a/core/modules/startup/main-render.js b/core/modules/startup/main-render.js index e1466ca4a..d51cc358b 100644 --- a/core/modules/startup/main-render.js +++ b/core/modules/startup/main-render.js @@ -15,7 +15,7 @@ Main stylesheet and page rendering // Export name and synchronous status exports.name = "main-render"; exports.platforms = ["browser"]; -exports.after = ["setup-story"]; +exports.after = ["rootwidget"]; exports.synchronous = true; // Time (in ms) that we defer refreshing changes to draft tiddlers diff --git a/core/modules/startup/password.js b/core/modules/startup/password.js index 3ff7e26c7..a6d586401 100644 --- a/core/modules/startup/password.js +++ b/core/modules/startup/password.js @@ -15,7 +15,7 @@ Password handling // Export name and synchronous status exports.name = "password"; exports.platforms = ["browser"]; -exports.after = ["startup"]; +exports.after = ["rootwidget"]; exports.synchronous = true; exports.startup = function() { diff --git a/core/modules/startup/rootwidget.js b/core/modules/startup/rootwidget.js new file mode 100644 index 000000000..6791fac0d --- /dev/null +++ b/core/modules/startup/rootwidget.js @@ -0,0 +1,81 @@ +/*\ +title: $:/core/modules/startup/rootwidget.js +type: application/javascript +module-type: startup + +Setup the root widget + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +// Export name and synchronous status +exports.name = "rootwidget"; +exports.platforms = ["browser"]; +exports.after = ["setup-story"]; +exports.synchronous = true; + +var widget = require("$:/core/modules/widgets/widget.js"); + +exports.startup = function() { + // Create a root widget for attaching event handlers. By using it as the parentWidget for another widget tree, one can reuse the event handlers + $tw.rootWidget = new widget.widget({ + type: "widget", + children: [] + },{ + wiki: $tw.wiki, + document: document + }); + // Install the modal message mechanism + $tw.modal = new $tw.utils.Modal($tw.wiki); + $tw.rootWidget.addEventListener("tw-modal",function(event) { + $tw.modal.display(event.param); + }); + // Install the notification mechanism + $tw.notifier = new $tw.utils.Notifier($tw.wiki); + $tw.rootWidget.addEventListener("tw-notify",function(event) { + $tw.notifier.display(event.param); + }); + // Install the scroller + $tw.pageScroller = new $tw.utils.PageScroller(); + $tw.rootWidget.addEventListener("tw-scroll",function(event) { + $tw.pageScroller.handleEvent(event); + }); + // Listen for the tw-home message + $tw.rootWidget.addEventListener("tw-home",function(event) { + displayDefaultTiddlers(); + }); + // Install the save action handlers + $tw.rootWidget.addEventListener("tw-save-wiki",function(event) { + $tw.syncer.saveWiki({ + template: event.param, + downloadType: "text/plain" + }); + }); + $tw.rootWidget.addEventListener("tw-auto-save-wiki",function(event) { + $tw.syncer.saveWiki({ + method: "autosave", + template: event.param, + downloadType: "text/plain" + }); + }); + $tw.rootWidget.addEventListener("tw-download-file",function(event) { + $tw.syncer.saveWiki({ + method: "download", + template: event.param, + downloadType: "text/plain" + }); + }); + // If we're being viewed on a data: URI then give instructions for how to save + if(document.location.protocol === "data:") { + $tw.rootWidget.dispatchEvent({ + type: "tw-modal", + param: "$:/language/Modals/SaveInstructions" + }); + } +}; + +})(); diff --git a/core/modules/startup/syncer-browser.js b/core/modules/startup/syncer-browser.js index ddccc19fa..c5b6d70c4 100644 --- a/core/modules/startup/syncer-browser.js +++ b/core/modules/startup/syncer-browser.js @@ -15,10 +15,12 @@ Startup handling // Export name and synchronous status exports.name = "syncer-browser"; exports.platforms = ["browser"]; -exports.after = ["main-render"]; +exports.after = ["rootwidget"]; exports.synchronous = true; exports.startup = function() { + // Set up the syncer object + $tw.syncer = new $tw.Syncer({wiki: $tw.wiki}); // Listen out for login/logout/refresh events in the browser $tw.rootWidget.addEventListener("tw-login",function() { $tw.syncer.handleLoginEvent(); diff --git a/core/modules/utils/dom/dom.js b/core/modules/utils/dom/dom.js index 417723504..3d611f768 100644 --- a/core/modules/utils/dom/dom.js +++ b/core/modules/utils/dom/dom.js @@ -157,17 +157,5 @@ exports.addEventListeners = function(domNode,events) { }); }; -/* -Construct and dispatch a custom event -*/ -exports.dispatchCustomEvent = function(target,name,members) { - var event = document.createEvent("Event"); - event.initEvent(name,true,true); - $tw.utils.each(members,function(member,name) { - event[name] = member; - }); - target.dispatchEvent(event); -}; - })();