From 7aa632729cf63ec67f636c3e4f6fdae49285ff56 Mon Sep 17 00:00:00 2001 From: Jason San Jose Date: Fri, 24 Aug 2012 16:51:19 -0700 Subject: [PATCH 1/4] Fix for #1442. Rename LoadEvents to Ready. Remove "event" terminology. --- src/brackets.js | 10 ++-- src/htmlContent/main-view.html | 4 +- src/index.html | 8 +-- src/project/ProjectManager.js | 6 +- src/project/SidebarView.js | 6 +- src/utils/Global.js | 2 +- src/utils/{LoadEvents.js => Ready.js} | 79 ++++++++++++++------------- 7 files changed, 58 insertions(+), 57 deletions(-) rename src/utils/{LoadEvents.js => Ready.js} (52%) diff --git a/src/brackets.js b/src/brackets.js index 44b8c0fcead..76f6af0961a 100644 --- a/src/brackets.js +++ b/src/brackets.js @@ -61,7 +61,7 @@ define(function (require, exports, module) { // Load dependent modules var Global = require("utils/Global"), - LoadEvents = require("utils/LoadEvents"), + Ready = require("utils/Ready"), ProjectManager = require("project/ProjectManager"), DocumentManager = require("document/DocumentManager"), EditorManager = require("editor/EditorManager"), @@ -153,7 +153,7 @@ define(function (require, exports, module) { doneLoading : false }; - LoadEvents.ready(function () { + Ready.ready(function () { brackets.test.doneLoading = true; }); } @@ -258,10 +258,10 @@ define(function (require, exports, module) { ProjectManager.openProject(initialProjectPath).done(function () { _initTest(); - // WARNING: LoadEvents.ready won't fire if ANY extension fails to + // WARNING: Ready.ready won't fire if ANY extension fails to // load or throws an error during init. To fix this, we need to // make a change to _initExtensions (filed as issue 1029) - _initExtensions().always(LoadEvents._dispatchEvent(LoadEvents.READY)); + _initExtensions().always(Ready._dispatchReady(Ready.READY)); }); // Check for updates @@ -272,7 +272,7 @@ define(function (require, exports, module) { // Localize MainViewHTML and inject into tag $('body').html(Mustache.render(MainViewHTML, Strings)); - LoadEvents._dispatchEvent(LoadEvents.HTML_CONTENT_LOAD_COMPLETE); + Ready._dispatchReady(Ready.HTML_READY); $(window.document).ready(_onReady); diff --git a/src/htmlContent/main-view.html b/src/htmlContent/main-view.html index 36554fb3eb2..c5d583ff565 100644 --- a/src/htmlContent/main-view.html +++ b/src/htmlContent/main-view.html @@ -22,8 +22,8 @@ - + require the "utils/Ready" modules and install a callback for + "htmlReady" (e.g. Ready.htmlReady(handler)) before touching the DOM. + --> + diff --git a/src/project/ProjectManager.js b/src/project/ProjectManager.js index eb471ce4f0e..02a46a296a5 100644 --- a/src/project/ProjectManager.js +++ b/src/project/ProjectManager.js @@ -45,7 +45,7 @@ define(function (require, exports, module) { require("thirdparty/jstree_pre1.0_fix_1/jquery.jstree"); // Load dependent modules - var LoadEvents = require("utils/LoadEvents"), + var Ready = require("utils/Ready"), NativeFileSystem = require("file/NativeFileSystem").NativeFileSystem, PreferencesManager = require("preferences/PreferencesManager"), DocumentManager = require("document/DocumentManager"), @@ -63,7 +63,7 @@ define(function (require, exports, module) { /** * @private * Reference to the tree control container div. Initialized by - * htmlContentLoadComplete handler + * htmlReady handler * @type {jQueryObject} */ var $projectTreeContainer; @@ -933,7 +933,7 @@ define(function (require, exports, module) { // Initialize variables and listeners that depend on the HTML DOM - LoadEvents.htmlContentLoadComplete(function () { + Ready.htmlReady(function () { $projectTreeContainer = $("#project-files-container"); $("#open-files-container").on("contentChanged", function () { diff --git a/src/project/SidebarView.js b/src/project/SidebarView.js index 1dde4b1ee48..4a8a814ab88 100644 --- a/src/project/SidebarView.js +++ b/src/project/SidebarView.js @@ -28,7 +28,7 @@ define(function (require, exports, module) { "use strict"; - var LoadEvents = require("utils/LoadEvents"), + var Ready = require("utils/Ready"), ProjectManager = require("project/ProjectManager"), WorkingSetView = require("project/WorkingSetView"), CommandManager = require("command/CommandManager"), @@ -43,7 +43,7 @@ define(function (require, exports, module) { var PREFERENCES_CLIENT_ID = "com.adobe.brackets.SidebarView", defaultPrefs = { sidebarWidth: 200, sidebarClosed: false }; - // These vars are initialized by the htmlContentLoadComplete handler + // These vars are initialized by the htmlReady handler // below since they refer to DOM elements var $sidebar, $sidebarMenuText, @@ -234,7 +234,7 @@ define(function (require, exports, module) { } // Initialize items dependent on HTML DOM - LoadEvents.htmlContentLoadComplete(function () { + Ready.htmlReady(function () { $sidebar = $("#sidebar"); $sidebarMenuText = $("#menu-view-hide-sidebar span"); $sidebarResizer = $("#sidebar-resizer"); diff --git a/src/utils/Global.js b/src/utils/Global.js index dbcb4ea3b3b..d81b0250865 100644 --- a/src/utils/Global.js +++ b/src/utils/Global.js @@ -29,7 +29,7 @@ * Initializes the global "brackets" variable and it's properties. * Modules should not access the global.brackets object until either * (a) the module requires this module, i.e. require("utils/Global") or - * (b) the module receives a "ready" event from the utils/LoadEvents module. + * (b) the module receives a "ready" event from the utils/Ready module. */ define(function (require, exports, module) { "use strict"; diff --git a/src/utils/LoadEvents.js b/src/utils/Ready.js similarity index 52% rename from src/utils/LoadEvents.js rename to src/utils/Ready.js index c75e92884d4..3088a2d762a 100644 --- a/src/utils/LoadEvents.js +++ b/src/utils/Ready.js @@ -26,32 +26,32 @@ /*global define */ /** - * Defines events to assist with module initialization. + * Defines hooks to assist with module initialization. * - * This module dispatches these events: - * - htmlContentLoadComplete - When the main application template is rendered + * This module defines 2 methods for client modules to attach callbacks: + * - htmlReady - When the main application template is rendered * - ready - When Brackets completes loading all modules and extensions * - * These are *not* jQuery events. Each event has it's own event registration. - * Each event is similar to $(document).ready in that it will call the handler - * immediately if brackets is already done loading + * These are *not* jQuery events. Each method is similar to $(document).ready + * in that it will call the handler immediately if brackets is already done + * loading. */ define(function (require, exports, module) { "use strict"; // Fires when the base htmlContent/main-view.html is loaded - var HTML_CONTENT_LOAD_COMPLETE = "htmlContentLoadComplete"; + var HTML_READY = "htmlReady"; // Fires when all extensions are loaded - var READY = "ready"; + var READY = "ready"; - var eventStatus = { HTML_CONTENT_LOAD_COMPLETE : false, READY : false }, - handlers = {}; + var status = { HTML_READY : false, READY : false }, + callbacks = {}; - handlers[HTML_CONTENT_LOAD_COMPLETE] = []; - handlers[READY] = []; + callbacks[HTML_READY] = []; + callbacks[READY] = []; - function _callEventHandler(handler) { + function _callHandler(handler) { try { // TODO (issue 1034): We *could* use a $.Deferred for this, except deferred objects enter a broken // state if any resolution callback throws an exception. Since third parties (e.g. extensions) may @@ -63,56 +63,57 @@ define(function (require, exports, module) { } } - function _dispatchEvent(type) { + function _dispatchReady(type) { var i, - eventTypeHandlers = handlers[type]; + myHandlers = callbacks[type]; - // mark this event type as fired - eventStatus[type] = true; + // mark this status complete + status[type] = true; - for (i = 0; i < eventTypeHandlers.length; i++) { - _callEventHandler(eventTypeHandlers[i]); + for (i = 0; i < myHandlers.length; i++) { + _callHandler(myHandlers[i]); } - // clear all handlers after being called - eventTypeHandlers = []; + // clear all callbacks after being called + callbacks[type] = []; } - // WARNING: This event won't fire if ANY extension fails to load or throws an error during init. - // To fix this, we need to make a change to _initExtensions (filed as issue 1029) - function _addListener(type, handler) { - if (eventStatus[type]) { - _callEventHandler(handler); + function _addListener(type, callback) { + if (status[type]) { + _callHandler(callback); } else { - handlers[type].push(handler); + callbacks[type].push(callback); } } /** - * Adds an event handler for the ready event. Handlers are called after - * htmlContentLoadComplete, the initial project is loaded, and all - * extensions are loaded. + * Adds a callback for the ready hook. Handlers are called after + * htmlReady is done, the initial project is loaded, and all extensions are + * loaded. * @param {function} handler */ - function ready(handler) { - _addListener(READY, handler); + function ready(callback) { + // WARNING: "ready" won't fire if ANY extension fails to load or + // throws an error during init. To fix this, we need to make a change + // to _initExtensions (filed as issue 1029) + _addListener(READY, callback); } /** - * Adds an event handler for the htmlContentLoadComplete event. Handlers - * are called after the main application html template is rendered. + * Adds a callback for the htmlReady hook. Handlers are called after the + * main application html template is rendered. * @param {function} handler */ - function htmlContentLoadComplete(handler) { - _addListener(HTML_CONTENT_LOAD_COMPLETE, handler); + function htmlReady(callback) { + _addListener(HTML_READY, callback); } exports.ready = ready; - exports.htmlContentLoadComplete = htmlContentLoadComplete; + exports.htmlReady = htmlReady; - exports.HTML_CONTENT_LOAD_COMPLETE = HTML_CONTENT_LOAD_COMPLETE; + exports.HTML_READY = HTML_READY; exports.READY = READY; // internal use only - exports._dispatchEvent = _dispatchEvent; + exports._dispatchReady = _dispatchReady; }); \ No newline at end of file From 323017810d9a68834e0e9da4003972a3f869a7ce Mon Sep 17 00:00:00 2001 From: Jason San Jose Date: Mon, 27 Aug 2012 15:59:25 -0700 Subject: [PATCH 2/4] Rename Ready module to AppInit --- src/brackets.js | 10 +++++----- src/index.html | 2 +- src/project/ProjectManager.js | 4 ++-- src/project/SidebarView.js | 4 ++-- src/utils/{Ready.js => AppInit.js} | 10 +++++----- 5 files changed, 15 insertions(+), 15 deletions(-) rename src/utils/{Ready.js => AppInit.js} (95%) diff --git a/src/brackets.js b/src/brackets.js index 76f6af0961a..47c6199d191 100644 --- a/src/brackets.js +++ b/src/brackets.js @@ -61,7 +61,7 @@ define(function (require, exports, module) { // Load dependent modules var Global = require("utils/Global"), - Ready = require("utils/Ready"), + AppInit = require("utils/AppInit"), ProjectManager = require("project/ProjectManager"), DocumentManager = require("document/DocumentManager"), EditorManager = require("editor/EditorManager"), @@ -153,7 +153,7 @@ define(function (require, exports, module) { doneLoading : false }; - Ready.ready(function () { + AppInit.ready(function () { brackets.test.doneLoading = true; }); } @@ -258,10 +258,10 @@ define(function (require, exports, module) { ProjectManager.openProject(initialProjectPath).done(function () { _initTest(); - // WARNING: Ready.ready won't fire if ANY extension fails to + // WARNING: AppInit.ready won't fire if ANY extension fails to // load or throws an error during init. To fix this, we need to // make a change to _initExtensions (filed as issue 1029) - _initExtensions().always(Ready._dispatchReady(Ready.READY)); + _initExtensions().always(AppInit._dispatchReady(AppInit.APP_READY)); }); // Check for updates @@ -272,7 +272,7 @@ define(function (require, exports, module) { // Localize MainViewHTML and inject into tag $('body').html(Mustache.render(MainViewHTML, Strings)); - Ready._dispatchReady(Ready.HTML_READY); + AppInit._dispatchReady(AppInit.HTML_READY); $(window.document).ready(_onReady); diff --git a/src/index.html b/src/index.html index 3766d015499..147d2a0c164 100644 --- a/src/index.html +++ b/src/index.html @@ -59,7 +59,7 @@ diff --git a/src/project/ProjectManager.js b/src/project/ProjectManager.js index 02a46a296a5..3d29bb20d0c 100644 --- a/src/project/ProjectManager.js +++ b/src/project/ProjectManager.js @@ -45,7 +45,7 @@ define(function (require, exports, module) { require("thirdparty/jstree_pre1.0_fix_1/jquery.jstree"); // Load dependent modules - var Ready = require("utils/Ready"), + var AppInit = require("utils/AppInit"), NativeFileSystem = require("file/NativeFileSystem").NativeFileSystem, PreferencesManager = require("preferences/PreferencesManager"), DocumentManager = require("document/DocumentManager"), @@ -933,7 +933,7 @@ define(function (require, exports, module) { // Initialize variables and listeners that depend on the HTML DOM - Ready.htmlReady(function () { + AppInit.htmlReady(function () { $projectTreeContainer = $("#project-files-container"); $("#open-files-container").on("contentChanged", function () { diff --git a/src/project/SidebarView.js b/src/project/SidebarView.js index 4a8a814ab88..d6347826a0e 100644 --- a/src/project/SidebarView.js +++ b/src/project/SidebarView.js @@ -28,7 +28,7 @@ define(function (require, exports, module) { "use strict"; - var Ready = require("utils/Ready"), + var AppInit = require("utils/AppInit"), ProjectManager = require("project/ProjectManager"), WorkingSetView = require("project/WorkingSetView"), CommandManager = require("command/CommandManager"), @@ -234,7 +234,7 @@ define(function (require, exports, module) { } // Initialize items dependent on HTML DOM - Ready.htmlReady(function () { + AppInit.htmlReady(function () { $sidebar = $("#sidebar"); $sidebarMenuText = $("#menu-view-hide-sidebar span"); $sidebarResizer = $("#sidebar-resizer"); diff --git a/src/utils/Ready.js b/src/utils/AppInit.js similarity index 95% rename from src/utils/Ready.js rename to src/utils/AppInit.js index 3088a2d762a..e3f0cbc9d36 100644 --- a/src/utils/Ready.js +++ b/src/utils/AppInit.js @@ -43,13 +43,13 @@ define(function (require, exports, module) { var HTML_READY = "htmlReady"; // Fires when all extensions are loaded - var READY = "ready"; + var APP_READY = "appReady"; - var status = { HTML_READY : false, READY : false }, + var status = { HTML_READY : false, APP_READY : false }, callbacks = {}; callbacks[HTML_READY] = []; - callbacks[READY] = []; + callbacks[APP_READY] = []; function _callHandler(handler) { try { @@ -96,7 +96,7 @@ define(function (require, exports, module) { // WARNING: "ready" won't fire if ANY extension fails to load or // throws an error during init. To fix this, we need to make a change // to _initExtensions (filed as issue 1029) - _addListener(READY, callback); + _addListener(APP_READY, callback); } /** @@ -112,7 +112,7 @@ define(function (require, exports, module) { exports.htmlReady = htmlReady; exports.HTML_READY = HTML_READY; - exports.READY = READY; + exports.APP_READY = APP_READY; // internal use only exports._dispatchReady = _dispatchReady; From 4af7a3e53ce9046d266a4d4c5b288c9aedc1489e Mon Sep 17 00:00:00 2001 From: Jason San Jose Date: Mon, 27 Aug 2012 16:12:30 -0700 Subject: [PATCH 3/4] Replace 'event' reference --- src/utils/Global.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/Global.js b/src/utils/Global.js index d81b0250865..90d68424a03 100644 --- a/src/utils/Global.js +++ b/src/utils/Global.js @@ -29,7 +29,7 @@ * Initializes the global "brackets" variable and it's properties. * Modules should not access the global.brackets object until either * (a) the module requires this module, i.e. require("utils/Global") or - * (b) the module receives a "ready" event from the utils/Ready module. + * (b) the module receives a "ready" callback from the utils/Ready module. */ define(function (require, exports, module) { "use strict"; From 67589ec2f38c41c5baff7d0b7c24fc95e92b5607 Mon Sep 17 00:00:00 2001 From: Jason San Jose Date: Mon, 27 Aug 2012 17:03:34 -0700 Subject: [PATCH 4/4] Code review comments. Missed some renames for appReady and AppInit. --- src/brackets.js | 4 ++-- src/utils/AppInit.js | 6 +++--- src/utils/Global.js | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/brackets.js b/src/brackets.js index 47c6199d191..99c4f504627 100644 --- a/src/brackets.js +++ b/src/brackets.js @@ -153,7 +153,7 @@ define(function (require, exports, module) { doneLoading : false }; - AppInit.ready(function () { + AppInit.appReady(function () { brackets.test.doneLoading = true; }); } @@ -258,7 +258,7 @@ define(function (require, exports, module) { ProjectManager.openProject(initialProjectPath).done(function () { _initTest(); - // WARNING: AppInit.ready won't fire if ANY extension fails to + // WARNING: AppInit.appReady won't fire if ANY extension fails to // load or throws an error during init. To fix this, we need to // make a change to _initExtensions (filed as issue 1029) _initExtensions().always(AppInit._dispatchReady(AppInit.APP_READY)); diff --git a/src/utils/AppInit.js b/src/utils/AppInit.js index e3f0cbc9d36..e6cca8e31ca 100644 --- a/src/utils/AppInit.js +++ b/src/utils/AppInit.js @@ -30,7 +30,7 @@ * * This module defines 2 methods for client modules to attach callbacks: * - htmlReady - When the main application template is rendered - * - ready - When Brackets completes loading all modules and extensions + * - appReady - When Brackets completes loading all modules and extensions * * These are *not* jQuery events. Each method is similar to $(document).ready * in that it will call the handler immediately if brackets is already done @@ -92,7 +92,7 @@ define(function (require, exports, module) { * loaded. * @param {function} handler */ - function ready(callback) { + function appReady(callback) { // WARNING: "ready" won't fire if ANY extension fails to load or // throws an error during init. To fix this, we need to make a change // to _initExtensions (filed as issue 1029) @@ -108,7 +108,7 @@ define(function (require, exports, module) { _addListener(HTML_READY, callback); } - exports.ready = ready; + exports.appReady = appReady; exports.htmlReady = htmlReady; exports.HTML_READY = HTML_READY; diff --git a/src/utils/Global.js b/src/utils/Global.js index 90d68424a03..6327cb72a4f 100644 --- a/src/utils/Global.js +++ b/src/utils/Global.js @@ -29,7 +29,7 @@ * Initializes the global "brackets" variable and it's properties. * Modules should not access the global.brackets object until either * (a) the module requires this module, i.e. require("utils/Global") or - * (b) the module receives a "ready" callback from the utils/Ready module. + * (b) the module receives a "ready" callback from the utils/AppReady module. */ define(function (require, exports, module) { "use strict";