From 06f9c28fd296808be4fae94a37046f3f9d7f447a Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Thu, 6 Apr 2017 16:30:13 -0700 Subject: [PATCH] settings: Lazy-load settings sections. For the settings UI, we now wait until a user goes to a particular settings section before calling the appropriate function to set up the section (which usually involves setting up click handlers and populating initial data). --- .eslintrc.json | 1 + static/js/click_handlers.js | 1 + static/js/settings.js | 11 +++----- static/js/settings_sections.js | 47 ++++++++++++++++++++++++++++++++++ static/js/ui_init.js | 1 + zproject/settings.py | 1 + 6 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 static/js/settings_sections.js diff --git a/.eslintrc.json b/.eslintrc.json index 77f92b4a86..d70a755a9f 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -38,6 +38,7 @@ "settings_muting": false, "settings_lab": false, "settings_bots": false, + "settings_sections": false, "settings": false, "resize": false, "loading": false, diff --git a/static/js/click_handlers.js b/static/js/click_handlers.js index 737b84dcf6..0c3093beb9 100644 --- a/static/js/click_handlers.js +++ b/static/js/click_handlers.js @@ -581,6 +581,7 @@ $(function () { } $(".settings-section, .settings-wrapper").removeClass("show"); + settings_sections.load_settings_section(section); $(".settings-section" + sel + ", .settings-wrapper" + sel).addClass("show"); }); diff --git a/static/js/settings.js b/static/js/settings.js index d0f38717c9..e36bc8e4aa 100644 --- a/static/js/settings.js +++ b/static/js/settings.js @@ -66,14 +66,9 @@ function _setup_page() { $(".settings-box").html(settings_tab); - alert_words_ui.set_up_alert_words(); - attachments_ui.set_up_attachments(); - settings_account.set_up(); - settings_display.set_up(); - settings_notifications.set_up(); - settings_bots.set_up(); - settings_muting.set_up(); - settings_lab.set_up(); + // Since we just swapped in a whole new settings widget, we need to + // tell settings_sections nothing is loaded. + settings_sections.reset_sections(); if (tab) { exports.launch_page(tab); diff --git a/static/js/settings_sections.js b/static/js/settings_sections.js new file mode 100644 index 0000000000..c71c7a615a --- /dev/null +++ b/static/js/settings_sections.js @@ -0,0 +1,47 @@ +var settings_sections = (function () { + +var exports = {}; + +var load_func_dict = new Dict(); // section -> function +var is_loaded = new Dict(); // section -> bool + +exports.initialize = function () { + load_func_dict.set('your-account', settings_account.set_up); + load_func_dict.set('display-settings', settings_display.set_up); + load_func_dict.set('notifications', settings_notifications.set_up); + load_func_dict.set('your-bots', settings_bots.set_up); + load_func_dict.set('alert-words', alert_words_ui.set_up_alert_words); + load_func_dict.set('uploaded-files', attachments_ui.set_up_attachments); + load_func_dict.set('muted-topics', settings_muting.set_up); + load_func_dict.set('zulip-labs', settings_lab.set_up); +}; + +exports.load_settings_section = function (section) { + if (!load_func_dict.has(section)) { + // admin sections don't have loaders yet, and that's ok + return; + } + + if (is_loaded.get(section)) { + // We only load sections once (unless somebody calls + // reset_sections). + return; + } + + var load_func = load_func_dict.get(section); + + // Do the real work here! + load_func(); + is_loaded.set(section, true); +}; + +exports.reset_sections = function () { + is_loaded.clear(); +}; + +return exports; +}()); + +if (typeof module !== 'undefined') { + module.exports = settings_sections; +} diff --git a/static/js/ui_init.js b/static/js/ui_init.js index 090a7621d8..3042e2b812 100644 --- a/static/js/ui_init.js +++ b/static/js/ui_init.js @@ -257,6 +257,7 @@ $(function () { activity.initialize(); emoji.initialize(); hotspots.initialize(); + settings_sections.initialize(); }); diff --git a/zproject/settings.py b/zproject/settings.py index e9345b5472..ea96a31207 100644 --- a/zproject/settings.py +++ b/zproject/settings.py @@ -911,6 +911,7 @@ JS_SPECS = { 'js/settings_bots.js', 'js/settings_muting.js', 'js/settings_lab.js', + 'js/settings_sections.js', 'js/settings.js', 'js/admin.js', 'js/tab_bar.js',