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

View File

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

View File

@@ -321,7 +321,7 @@ export function activate(raw_operators, opts) {
const select_immediately = id_info.local_select_id !== undefined; const select_immediately = id_info.local_select_id !== undefined;
(function fetch_messages() { {
let anchor; let anchor;
// Either we're trying to center the narrow around a // 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); maybe_report_narrow_time(msg_list);
}, },
}); });
})(); }
if (select_immediately) { if (select_immediately) {
update_selection({ 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. // 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 // 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. // ones that no longer have valid parents in the DOM.
(function (popover) { const old_popover = $.fn.popover;
$.fn.popover = function (...args) { $.fn.popover = Object.assign(function (...args) {
// apply the jQuery object as `this`, and popover function arguments. // apply the jQuery object as `this`, and popover function arguments.
popover.apply(this, args); old_popover.apply(this, args);
// if there is a valid "popover" key in the jQuery data object then // if there is a valid "popover" key in the jQuery data object then
// push it to the array. // push it to the array.
if (this.data("popover")) { if (this.data("popover")) {
list_of_popovers.push(this.data("popover")); list_of_popovers.push(this.data("popover"));
} }
}; }, old_popover);
// add back all shallow properties of $.fn.popover to the new proxied version.
Object.assign($.fn.popover, popover);
})($.fn.popover);
function copy_email_handler(e) { function copy_email_handler(e) {
const email_el = $(e.trigger.parentElement); const email_el = $(e.trigger.parentElement);
@@ -1325,7 +1321,7 @@ export function register_click_handlers() {
e.preventDefault(); e.preventDefault();
}); });
(function () { {
let last_scroll = 0; let last_scroll = 0;
$(".app").on("scroll", () => { $(".app").on("scroll", () => {
@@ -1346,7 +1342,7 @@ export function register_click_handlers() {
// retrigger `hide_all` while still scrolling. // retrigger `hide_all` while still scrolling.
last_scroll = date; last_scroll = date;
}); });
})(); }
} }
export function any_active() { export function any_active() {

View File

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

View File

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

View File

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

View File

@@ -106,81 +106,79 @@ export function process_fenced_code(content) {
function handler_for_fence(output_lines, fence, lang, header) { function handler_for_fence(output_lines, fence, lang, header) {
// lang is ignored except for 'quote', as we // lang is ignored except for 'quote', as we
// don't do syntax highlighting yet // don't do syntax highlighting yet
return (function () { const lines = [];
const lines = []; if (lang === "quote") {
if (lang === "quote") {
return {
handle_line(line) {
if (line === fence) {
this.done();
} else {
consume_line(lines, line);
}
},
done() {
const text = wrap_quote(lines.join("\n"));
output_lines.push("", text, "");
handler_stack.pop();
},
};
}
if (lang === "math") {
return {
handle_line(line) {
if (line === fence) {
this.done();
} else {
lines.push(line);
}
},
done() {
const text = wrap_tex(lines.join("\n"));
const placeholder = stash_func(text, true);
output_lines.push("", placeholder, "");
handler_stack.pop();
},
};
}
if (lang === "spoiler") {
return {
handle_line(line) {
if (line === fence) {
this.done();
} else {
lines.push(line);
}
},
done() {
const text = wrap_spoiler(header, lines.join("\n"), stash_func);
output_lines.push("", text, "");
handler_stack.pop();
},
};
}
return { return {
handle_line(line) { handle_line(line) {
if (line === fence) { if (line === fence) {
this.done(); this.done();
} else { } else {
lines.push(line.trimEnd()); consume_line(lines, line);
} }
}, },
done() { done() {
const text = wrap_code(lines.join("\n"), lang); const text = wrap_quote(lines.join("\n"));
// insert safe HTML that is passed through the parsing output_lines.push("", text, "");
handler_stack.pop();
},
};
}
if (lang === "math") {
return {
handle_line(line) {
if (line === fence) {
this.done();
} else {
lines.push(line);
}
},
done() {
const text = wrap_tex(lines.join("\n"));
const placeholder = stash_func(text, true); const placeholder = stash_func(text, true);
output_lines.push("", placeholder, ""); output_lines.push("", placeholder, "");
handler_stack.pop(); handler_stack.pop();
}, },
}; };
})(); }
if (lang === "spoiler") {
return {
handle_line(line) {
if (line === fence) {
this.done();
} else {
lines.push(line);
}
},
done() {
const text = wrap_spoiler(header, lines.join("\n"), stash_func);
output_lines.push("", text, "");
handler_stack.pop();
},
};
}
return {
handle_line(line) {
if (line === fence) {
this.done();
} else {
lines.push(line.trimEnd());
}
},
done() {
const text = wrap_code(lines.join("\n"), lang);
// insert safe HTML that is passed through the parsing
const placeholder = stash_func(text, true);
output_lines.push("", placeholder, "");
handler_stack.pop();
},
};
} }
function default_hander() { function default_hander() {