js: Remove some pointless IIFEs.

Some of these were there because they predate block-scoped const/let.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2021-05-06 12:49:45 -07:00
committed by Tim Abbott
parent 707d23d1e8
commit fc9481a24e
9 changed files with 159 additions and 178 deletions

View File

@@ -607,13 +607,13 @@ export function initialize() {
// MISC
(function () {
{
const sel = ["#stream_filters", "#global_filters", "#user_presences"].join(", ");
$(sel).on("click", "a", function () {
this.blur();
});
})();
}
popovers.register_click_handlers();
emoji_picker.register_click_handlers();
@@ -765,7 +765,7 @@ export function initialize() {
// Don't focus links on context menu.
$("body").on("contextmenu", "a", (e) => e.target.blur());
(function () {
{
const map = {
".stream-description-editable": {
on_start: stream_edit.set_raw_description,
@@ -841,7 +841,7 @@ export function initialize() {
$(`[data-make-editable='${CSS.escape(selector)}']`).html("");
}
});
})();
}
// HOTSPOTS

View File

@@ -14,44 +14,41 @@ import * as keydown_util from "./keydown_util";
*/
export function toggle(opts) {
const component = (function render_component(opts) {
const _component = $("<div class='tab-switcher'></div>");
if (opts.html_class) {
// add a check inside passed arguments in case some extra
// classes need to be added for correct alignment or other purposes
_component.addClass(opts.html_class);
}
for (const [i, value] of opts.values.entries()) {
// create a tab with a tab-id so they don't have to be referenced
// by text value which can be inconsistent.
const tab = $("<div>", {
class: "ind-tab",
"data-tab-key": value.key,
"data-tab-id": i,
tabindex: 0,
});
const component = $("<div class='tab-switcher'></div>");
if (opts.html_class) {
// add a check inside passed arguments in case some extra
// classes need to be added for correct alignment or other purposes
component.addClass(opts.html_class);
}
for (const [i, value] of opts.values.entries()) {
// create a tab with a tab-id so they don't have to be referenced
// by text value which can be inconsistent.
const tab = $("<div>", {
class: "ind-tab",
"data-tab-key": value.key,
"data-tab-id": i,
tabindex: 0,
});
/* istanbul ignore if */
if (value.label_html !== undefined) {
const html = value.label_html;
tab.html(html);
} else {
tab.text(value.label);
}
// add proper classes for styling in CSS.
if (i === 0) {
// this should be default selected unless otherwise specified.
tab.addClass("first selected");
} else if (i === opts.values.length - 1) {
tab.addClass("last");
} else {
tab.addClass("middle");
}
_component.append(tab);
/* istanbul ignore if */
if (value.label_html !== undefined) {
const html = value.label_html;
tab.html(html);
} else {
tab.text(value.label);
}
return _component;
})(opts);
// add proper classes for styling in CSS.
if (i === 0) {
// this should be default selected unless otherwise specified.
tab.addClass("first selected");
} else if (i === opts.values.length - 1) {
tab.addClass("last");
} else {
tab.addClass("middle");
}
component.append(tab);
}
const meta = {
$ind_tab: component.find(".ind-tab"),
@@ -103,25 +100,23 @@ export function toggle(opts) {
return false;
}
(function () {
meta.$ind_tab.on("click", function () {
const idx = $(this).data("tab-id");
select_tab(idx);
});
meta.$ind_tab.on("click", function () {
const idx = $(this).data("tab-id");
select_tab(idx);
});
keydown_util.handle({
elem: meta.$ind_tab,
handlers: {
left_arrow: maybe_go_left,
right_arrow: maybe_go_right,
},
});
keydown_util.handle({
elem: meta.$ind_tab,
handlers: {
left_arrow: maybe_go_left,
right_arrow: maybe_go_right,
},
});
// We should arguably default opts.selected to 0.
if (typeof opts.selected === "number") {
select_tab(opts.selected);
}
})();
// We should arguably default opts.selected to 0.
if (typeof opts.selected === "number") {
select_tab(opts.selected);
}
const prototype = {
// Skip disabled tabs and go to the next one.

View File

@@ -245,7 +245,7 @@ export function create(opts) {
},
};
(function events() {
{
store.$parent.on("keydown", ".input", (e) => {
const char = e.keyCode || e.charCode;
@@ -380,7 +380,7 @@ export function create(opts) {
);
e.preventDefault();
});
})();
}
// the external, user-accessible prototype.
const prototype = {

View File

@@ -321,7 +321,7 @@ export function activate(raw_operators, opts) {
const select_immediately = id_info.local_select_id !== undefined;
(function fetch_messages() {
{
let anchor;
// Either we're trying to center the narrow around a
@@ -354,7 +354,7 @@ export function activate(raw_operators, opts) {
maybe_report_narrow_time(msg_list);
},
});
})();
}
if (select_immediately) {
update_selection({

View File

@@ -85,21 +85,17 @@ function elem_to_user_id(elem) {
// and push the $.fn.data($o, "popover") results to an array.
// this is needed so that when we try to unload popovers, we can kill all dead
// ones that no longer have valid parents in the DOM.
(function (popover) {
$.fn.popover = function (...args) {
// apply the jQuery object as `this`, and popover function arguments.
popover.apply(this, args);
const old_popover = $.fn.popover;
$.fn.popover = Object.assign(function (...args) {
// apply the jQuery object as `this`, and popover function arguments.
old_popover.apply(this, args);
// if there is a valid "popover" key in the jQuery data object then
// push it to the array.
if (this.data("popover")) {
list_of_popovers.push(this.data("popover"));
}
};
// add back all shallow properties of $.fn.popover to the new proxied version.
Object.assign($.fn.popover, popover);
})($.fn.popover);
// if there is a valid "popover" key in the jQuery data object then
// push it to the array.
if (this.data("popover")) {
list_of_popovers.push(this.data("popover"));
}
}, old_popover);
function copy_email_handler(e) {
const email_el = $(e.trigger.parentElement);
@@ -1325,7 +1321,7 @@ export function register_click_handlers() {
e.preventDefault();
});
(function () {
{
let last_scroll = 0;
$(".app").on("scroll", () => {
@@ -1346,7 +1342,7 @@ export function register_click_handlers() {
// retrigger `hide_all` while still scrolling.
last_scroll = date;
});
})();
}
}
export function any_active() {

View File

@@ -123,45 +123,40 @@ function left_userlist_get_new_heights() {
}
export function watch_manual_resize(element) {
return (function on_box_resize(cb) {
const box = document.querySelector(element);
const box = document.querySelector(element);
if (!box) {
blueslip.error("Bad selector in watch_manual_resize: " + element);
return undefined;
}
if (!box) {
blueslip.error("Bad selector in watch_manual_resize: " + element);
return undefined;
}
const meta = {
box,
height: null,
mousedown: false,
};
const meta = {
box,
height: null,
mousedown: false,
};
const box_handler = function () {
meta.mousedown = true;
meta.height = meta.box.clientHeight;
};
meta.box.addEventListener("mousedown", box_handler);
const box_handler = function () {
meta.mousedown = true;
meta.height = meta.box.clientHeight;
};
meta.box.addEventListener("mousedown", box_handler);
// If the user resizes the textarea manually, we use the
// callback to stop autosize from adjusting the height.
const body_handler = function () {
if (meta.mousedown === true) {
meta.mousedown = false;
if (meta.height !== meta.box.clientHeight) {
meta.height = meta.box.clientHeight;
cb.call(meta.box, meta.height);
}
// If the user resizes the textarea manually, we use the
// callback to stop autosize from adjusting the height.
// It will be re-enabled when this component is next opened.
const body_handler = function () {
if (meta.mousedown === true) {
meta.mousedown = false;
if (meta.height !== meta.box.clientHeight) {
meta.height = meta.box.clientHeight;
autosize.destroy($(element)).height(meta.height + "px");
}
};
document.body.addEventListener("mouseup", body_handler);
}
};
document.body.addEventListener("mouseup", body_handler);
return [box_handler, body_handler];
})((height) => {
// This callback disables autosize on the textarea. It
// will be re-enabled when this component is next opened.
autosize.destroy($(element)).height(height + "px");
});
return [box_handler, body_handler];
}
export function resize_bottom_whitespace(h) {

View File

@@ -279,10 +279,7 @@ export function populate_user_groups() {
pill_typeahead.set_up(input, pills, opts);
}
(function pill_remove() {
if (!can_edit(data.id)) {
return;
}
if (can_edit(data.id)) {
pills.onPillRemove(() => {
// onPillRemove is fired before the pill is removed from
// the DOM.
@@ -291,7 +288,7 @@ export function populate_user_groups() {
input.trigger("focus");
}, 100);
});
})();
}
}
}

View File

@@ -1030,7 +1030,7 @@ export function initialize() {
$(".subscriptions-header").removeClass("slide-left");
});
(function defocus_sub_settings() {
{
const sel = ".search-container, .streams-list, .subscriptions-header";
$("#subscriptions_table").on("click", sel, (e) => {
@@ -1038,5 +1038,5 @@ export function initialize() {
stream_edit.open_edit_panel_empty();
}
});
})();
}
}