Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

typeahead: Import jquery instead of passing it to a wrapper function.

evykassirer099810e80f32
diff --git a/web/third/bootstrap-typeahead/typeahead.js b/web/third/bootstrap-typeahead/typeahead.js
index 0ca9ddd3858..5b30d189d07 100644
--- a/web/third/bootstrap-typeahead/typeahead.js
+++ b/web/third/bootstrap-typeahead/typeahead.js
@@ -132,532 +132,528 @@
* to move focus there without the typeahead closing.
* ============================================================ */
+import $ from "jquery";
import {insertTextIntoField} from "text-field-edit";
import {get_string_diff} from "../../src/util";
-!(function ($) {
- function get_pseudo_keycode(event) {
- const isComposing = (event.originalEvent && event.originalEvent.isComposing) || false;
- /* We treat IME compose enter keypresses as a separate -13 key. */
- if (event.keyCode === 13 && isComposing) {
- return -13;
- }
- return event.keyCode;
+function get_pseudo_keycode(event) {
+ const isComposing = (event.originalEvent && event.originalEvent.isComposing) || false;
+ /* We treat IME compose enter keypresses as a separate -13 key. */
+ if (event.keyCode === 13 && isComposing) {
+ return -13;
+ }
+ return event.keyCode;
+}
+
+/* TYPEAHEAD PUBLIC CLASS DEFINITION
+ * ================================= */
+
+const Typeahead = function (element, options) {
+ this.$element = $(element);
+ this.options = $.extend({}, $.fn.typeahead.defaults, options);
+ this.matcher = this.options.matcher ?? this.matcher;
+ this.sorter = this.options.sorter ?? this.sorter;
+ this.highlighter = this.options.highlighter;
+ this.updater = this.options.updater ?? this.updater;
+ this.$container = $(this.options.container).appendTo(this.options.parentElement ?? "body");
+ this.$menu = $(this.options.menu).appendTo(this.$container);
+ this.$header = $(this.options.header_html).appendTo(this.$container);
+ this.source = this.options.source;
+ this.shown = false;
+ this.mouse_moved_since_typeahead = false;
+ this.dropup = this.options.dropup;
+ this.fixed = this.options.fixed ?? false;
+ this.automated = this.options.automated ?? (() => false);
+ this.trigger_selection = this.options.trigger_selection ?? (() => false);
+ this.on_move = this.options.on_move;
+ this.on_escape = this.options.on_escape;
+ // return a string to show in typeahead header or false.
+ this.header = this.options.header ?? (() => false);
+ // return a string to show in typeahead items or false.
+ this.option_label = this.options.option_label ?? (() => false);
+
+ if (this.fixed) {
+ this.$container.css("position", "fixed");
}
+ // The naturalSearch option causes arrow keys to immediately
+ // update the search box with the underlying values from the
+ // search suggestions.
+ this.listen();
+};
+
+Typeahead.prototype = {
+ constructor: Typeahead,
+
+ select(e) {
+ const val = this.$menu.find(".active").data("typeahead-value");
+ if (this.$element.is("[contenteditable]")) {
+ this.$element.html(this.updater(val, e)).trigger("change");
+ // Empty textContent after the change event handler
+ // converts the input text to html elements.
+ this.$element.html("");
+ } else {
+ const after_text = this.updater(val, e);
+ const [from, to_before, to_after] = get_string_diff(this.$element.val(), after_text);
+ const replacement = after_text.slice(from, to_after);
+ // select / highlight the minimal text to be replaced
+ this.$element[0].setSelectionRange(from, to_before);
+ insertTextIntoField(this.$element[0], replacement);
+ }
- /* TYPEAHEAD PUBLIC CLASS DEFINITION
- * ================================= */
-
- const Typeahead = function (element, options) {
- this.$element = $(element);
- this.options = $.extend({}, $.fn.typeahead.defaults, options);
- this.matcher = this.options.matcher ?? this.matcher;
- this.sorter = this.options.sorter ?? this.sorter;
- this.highlighter = this.options.highlighter;
- this.updater = this.options.updater ?? this.updater;
- this.$container = $(this.options.container).appendTo(this.options.parentElement ?? "body");
- this.$menu = $(this.options.menu).appendTo(this.$container);
- this.$header = $(this.options.header_html).appendTo(this.$container);
- this.source = this.options.source;
- this.shown = false;
- this.mouse_moved_since_typeahead = false;
- this.dropup = this.options.dropup;
- this.fixed = this.options.fixed ?? false;
- this.automated = this.options.automated ?? (() => false);
- this.trigger_selection = this.options.trigger_selection ?? (() => false);
- this.on_move = this.options.on_move;
- this.on_escape = this.options.on_escape;
- // return a string to show in typeahead header or false.
- this.header = this.options.header ?? (() => false);
- // return a string to show in typeahead items or false.
- this.option_label = this.options.option_label ?? (() => false);
-
- if (this.fixed) {
- this.$container.css("position", "fixed");
+ return this.hide();
+ },
+
+ set_value() {
+ const val = this.$menu.find(".active").data("typeahead-value");
+ if (this.$element.is("[contenteditable]")) {
+ this.$element.html(val);
+ } else {
+ this.$element.val(val);
}
- // The naturalSearch option causes arrow keys to immediately
- // update the search box with the underlying values from the
- // search suggestions.
- this.listen();
- };
-
- Typeahead.prototype = {
- constructor: Typeahead,
-
- select(e) {
- const val = this.$menu.find(".active").data("typeahead-value");
- if (this.$element.is("[contenteditable]")) {
- this.$element.html(this.updater(val, e)).trigger("change");
- // Empty textContent after the change event handler
- // converts the input text to html elements.
- this.$element.html("");
- } else {
- const after_text = this.updater(val, e);
- const [from, to_before, to_after] = get_string_diff(
- this.$element.val(),
- after_text,
- );
- const replacement = after_text.slice(from, to_after);
- // select / highlight the minimal text to be replaced
- this.$element[0].setSelectionRange(from, to_before);
- insertTextIntoField(this.$element[0], replacement);
- }
- return this.hide();
- },
+ if (this.on_move) {
+ this.on_move();
+ }
+ },
+
+ updater(item) {
+ return item;
+ },
+
+ show() {
+ const header_text = this.header();
+ if (header_text) {
+ this.$header.find("span#typeahead-header-text").html(header_text);
+ this.$header.show();
+ } else {
+ this.$header.hide();
+ }
- set_value() {
- const val = this.$menu.find(".active").data("typeahead-value");
- if (this.$element.is("[contenteditable]")) {
- this.$element.html(val);
+ // If a parent element was specified, we shouldn't manually
+ // position the element, since it's already in the right place.
+ if (!this.options.parentElement) {
+ let pos;
+
+ if (this.fixed) {
+ // Relative to screen instead of to page
+ pos = this.$element[0].getBoundingClientRect();
} else {
- this.$element.val(val);
+ pos = this.$element.offset();
}
- if (this.on_move) {
- this.on_move();
- }
- },
+ pos = $.extend({}, pos, {
+ height: this.$element[0].offsetHeight,
+ });
- updater(item) {
- return item;
- },
+ // Zulip patch: Workaround for iOS safari problems
+ pos.top = this.$element.get_offset_to_window().top;
- show() {
- const header_text = this.header();
- if (header_text) {
- this.$header.find("span#typeahead-header-text").html(header_text);
- this.$header.show();
- } else {
- this.$header.hide();
+ let top_pos = pos.top + pos.height;
+ if (this.dropup) {
+ top_pos = pos.top - this.$container.outerHeight();
}
- // If a parent element was specified, we shouldn't manually
- // position the element, since it's already in the right place.
- if (!this.options.parentElement) {
- let pos;
+ // Zulip patch: Avoid typeahead going off top of screen.
+ if (top_pos < 0) {
+ top_pos = 0;
+ }
- if (this.fixed) {
- // Relative to screen instead of to page
- pos = this.$element[0].getBoundingClientRect();
- } else {
- pos = this.$element.offset();
- }
+ this.$container.css({
+ top: top_pos,
+ left: pos.left,
+ });
+ }
- pos = $.extend({}, pos, {
- height: this.$element[0].offsetHeight,
- });
+ this.$container.show();
+ this.shown = true;
+ this.mouse_moved_since_typeahead = false;
+ return this;
+ },
- // Zulip patch: Workaround for iOS safari problems
- pos.top = this.$element.get_offset_to_window().top;
+ hide() {
+ this.$container.hide();
+ this.shown = false;
+ if (this.options.closeInputFieldOnHide !== null) {
+ this.options.closeInputFieldOnHide();
+ }
+ return this;
+ },
+
+ lookup(hideOnEmpty) {
+ this.query = this.$element.is("[contenteditable]")
+ ? this.$element.text()
+ : this.$element.val();
+
+ if (
+ (!this.options.helpOnEmptyStrings || hideOnEmpty) &&
+ (!this.query || this.query.length < this.options.minLength)
+ ) {
+ return this.shown ? this.hide() : this;
+ }
- let top_pos = pos.top + pos.height;
- if (this.dropup) {
- top_pos = pos.top - this.$container.outerHeight();
- }
+ const items =
+ typeof this.source === "function"
+ ? this.source(this.query, this.process.bind(this))
+ : this.source;
- // Zulip patch: Avoid typeahead going off top of screen.
- if (top_pos < 0) {
- top_pos = 0;
- }
+ if (!items && this.shown) {
+ this.hide();
+ }
+ return items ? this.process(items) : this;
+ },
- this.$container.css({
- top: top_pos,
- left: pos.left,
- });
- }
+ process(items) {
+ const matching_items = $.grep(items, (item) => this.matcher(item));
- this.$container.show();
- this.shown = true;
- this.mouse_moved_since_typeahead = false;
- return this;
- },
+ const final_items = this.sorter(matching_items);
- hide() {
- this.$container.hide();
- this.shown = false;
- if (this.options.closeInputFieldOnHide !== null) {
- this.options.closeInputFieldOnHide();
- }
+ if (!final_items.length) {
+ return this.shown ? this.hide() : this;
+ }
+ if (this.automated()) {
+ this.select();
return this;
- },
-
- lookup(hideOnEmpty) {
- this.query = this.$element.is("[contenteditable]")
- ? this.$element.text()
- : this.$element.val();
-
- if (
- (!this.options.helpOnEmptyStrings || hideOnEmpty) &&
- (!this.query || this.query.length < this.options.minLength)
- ) {
- return this.shown ? this.hide() : this;
+ }
+ return this.render(final_items.slice(0, this.options.items), matching_items).show();
+ },
+
+ matcher(item) {
+ return item.toLowerCase().includes(this.query.toLowerCase());
+ },
+
+ sorter(items) {
+ const beginswith = [];
+ const caseSensitive = [];
+ const caseInsensitive = [];
+ let item;
+
+ while ((item = items.shift())) {
+ if (item.toLowerCase().startsWith(this.query.toLowerCase())) {
+ beginswith.push(item);
+ } else if (item.includes(this.query)) {
+ caseSensitive.push(item);
+ } else {
+ caseInsensitive.push(item);
}
+ }
- const items =
- typeof this.source === "function"
- ? this.source(this.query, this.process.bind(this))
- : this.source;
+ return [...beginswith, ...caseSensitive, ...caseInsensitive];
+ },
- if (!items && this.shown) {
- this.hide();
+ render(final_items, matching_items) {
+ const $items = $(final_items).map((_index, item) => {
+ const $i = $(this.options.item).data("typeahead-value", item);
+ const $item_html = $i.find("a").html(this.highlighter(item));
+
+ const option_label_html = this.option_label(matching_items, item);
+
+ if (option_label_html) {
+ $item_html.append(option_label_html).addClass("typeahead-option-label");
}
- return items ? this.process(items) : this;
- },
+ return $i[0];
+ });
- process(items) {
- const matching_items = $.grep(items, (item) => this.matcher(item));
+ $items.first().addClass("active");
+ this.$menu.html($items);
+ return this;
+ },
- const final_items = this.sorter(matching_items);
+ next() {
+ const $active = this.$menu.find(".active").removeClass("active");
+ let $next = $active.next();
- if (!final_items.length) {
- return this.shown ? this.hide() : this;
- }
- if (this.automated()) {
- this.select();
- return this;
- }
- return this.render(final_items.slice(0, this.options.items), matching_items).show();
- },
-
- matcher(item) {
- return item.toLowerCase().includes(this.query.toLowerCase());
- },
-
- sorter(items) {
- const beginswith = [];
- const caseSensitive = [];
- const caseInsensitive = [];
- let item;
-
- while ((item = items.shift())) {
- if (item.toLowerCase().startsWith(this.query.toLowerCase())) {
- beginswith.push(item);
- } else if (item.includes(this.query)) {
- caseSensitive.push(item);
- } else {
- caseInsensitive.push(item);
- }
- }
+ if (!$next.length) {
+ $next = $(this.$menu.find("li")[0]);
+ }
- return [...beginswith, ...caseSensitive, ...caseInsensitive];
- },
+ $next.addClass("active");
- render(final_items, matching_items) {
- const $items = $(final_items).map((_index, item) => {
- const $i = $(this.options.item).data("typeahead-value", item);
- const $item_html = $i.find("a").html(this.highlighter(item));
+ if (this.options.naturalSearch) {
+ this.set_value();
+ }
+ },
- const option_label_html = this.option_label(matching_items, item);
+ prev() {
+ const $active = this.$menu.find(".active").removeClass("active");
+ let $prev = $active.prev();
- if (option_label_html) {
- $item_html.append(option_label_html).addClass("typeahead-option-label");
- }
- return $i[0];
- });
+ if (!$prev.length) {
+ $prev = this.$menu.find("li").last();
+ }
- $items.first().addClass("active");
- this.$menu.html($items);
- return this;
- },
+ $prev.addClass("active");
- next() {
- const $active = this.$menu.find(".active").removeClass("active");
- let $next = $active.next();
+ if (this.options.naturalSearch) {
+ this.set_value();
+ }
+ },
- if (!$next.length) {
- $next = $(this.$menu.find("li")[0]);
- }
+ listen() {
+ this.$element
+ .on("blur", this.blur.bind(this))
+ .on("keypress", this.keypress.bind(this))
+ .on("keyup", this.keyup.bind(this))
+ .on("click", this.element_click.bind(this));
- $next.addClass("active");
+ if (this.eventSupported("keydown")) {
+ this.$element.on("keydown", this.keydown.bind(this));
+ }
- if (this.options.naturalSearch) {
- this.set_value();
- }
- },
+ this.$menu
+ .on("click", "li", this.click.bind(this))
+ .on("mouseenter", "li", this.mouseenter.bind(this))
+ .on("mousemove", "li", this.mousemove.bind(this));
- prev() {
- const $active = this.$menu.find(".active").removeClass("active");
- let $prev = $active.prev();
+ $(window).on("resize", this.resizeHandler.bind(this));
+ },
- if (!$prev.length) {
- $prev = this.$menu.find("li").last();
- }
+ unlisten() {
+ this.$container.remove();
+ const events = ["blur", "keydown", "keyup", "keypress", "mousemove"];
+ for (const event_ of events) {
+ this.$element.off(event_);
+ }
+ this.$element.removeData("typeahead");
+ },
+
+ eventSupported(eventName) {
+ let isSupported = eventName in this.$element;
+ if (!isSupported) {
+ this.$element.setAttribute(eventName, "return;");
+ isSupported = typeof this.$element[eventName] === "function";
+ }
+ return isSupported;
+ },
- $prev.addClass("active");
+ resizeHandler() {
+ if (this.shown) {
+ this.show();
+ }
+ },
+
+ maybeStopAdvance(e) {
+ const pseudo_keycode = get_pseudo_keycode(e);
+ if (
+ (this.options.stopAdvance || (pseudo_keycode !== 9 && pseudo_keycode !== 13)) &&
+ $.inArray(e.keyCode, this.options.advanceKeyCodes)
+ ) {
+ e.stopPropagation();
+ }
+ },
- if (this.options.naturalSearch) {
- this.set_value();
- }
- },
+ move(e) {
+ if (!this.shown) {
+ return;
+ }
+ const pseudo_keycode = get_pseudo_keycode(e);
- listen() {
- this.$element
- .on("blur", this.blur.bind(this))
- .on("keypress", this.keypress.bind(this))
- .on("keyup", this.keyup.bind(this))
- .on("click", this.element_click.bind(this));
+ switch (pseudo_keycode) {
+ case 9: // tab
+ if (!this.options.tabIsEnter) {
+ return;
+ }
+ e.preventDefault();
+ break;
- if (this.eventSupported("keydown")) {
- this.$element.on("keydown", this.keydown.bind(this));
- }
+ case 13: // enter
+ case 27: // escape
+ e.preventDefault();
+ break;
- this.$menu
- .on("click", "li", this.click.bind(this))
- .on("mouseenter", "li", this.mouseenter.bind(this))
- .on("mousemove", "li", this.mousemove.bind(this));
+ case 38: // up arrow
+ e.preventDefault();
+ this.prev();
+ break;
- $(window).on("resize", this.resizeHandler.bind(this));
- },
+ case 40: // down arrow
+ e.preventDefault();
+ this.next();
+ break;
+ }
- unlisten() {
- this.$container.remove();
- const events = ["blur", "keydown", "keyup", "keypress", "mousemove"];
- for (const event_ of events) {
- this.$element.off(event_);
- }
- this.$element.removeData("typeahead");
- },
-
- eventSupported(eventName) {
- let isSupported = eventName in this.$element;
- if (!isSupported) {
- this.$element.setAttribute(eventName, "return;");
- isSupported = typeof this.$element[eventName] === "function";
- }
- return isSupported;
- },
+ this.maybeStopAdvance(e);
+ },
- resizeHandler() {
- if (this.shown) {
- this.show();
- }
- },
-
- maybeStopAdvance(e) {
- const pseudo_keycode = get_pseudo_keycode(e);
- if (
- (this.options.stopAdvance || (pseudo_keycode !== 9 && pseudo_keycode !== 13)) &&
- $.inArray(e.keyCode, this.options.advanceKeyCodes)
- ) {
- e.stopPropagation();
- }
- },
+ mousemove(e) {
+ if (!this.mouse_moved_since_typeahead) {
+ /* Undo cursor disabling in mouseenter handler. */
+ $(e.currentTarget).find("a").css("cursor", "");
+ this.mouse_moved_since_typeahead = true;
+ this.mouseenter(e);
+ }
+ },
- move(e) {
+ keydown(e) {
+ const pseudo_keycode = get_pseudo_keycode(e);
+ if (this.trigger_selection(e)) {
if (!this.shown) {
return;
}
- const pseudo_keycode = get_pseudo_keycode(e);
-
- switch (pseudo_keycode) {
- case 9: // tab
- if (!this.options.tabIsEnter) {
- return;
- }
- e.preventDefault();
- break;
-
- case 13: // enter
- case 27: // escape
- e.preventDefault();
- break;
-
- case 38: // up arrow
- e.preventDefault();
- this.prev();
- break;
-
- case 40: // down arrow
- e.preventDefault();
- this.next();
- break;
- }
+ e.preventDefault();
+ this.select(e);
+ }
+ this.suppressKeyPressRepeat = ![40, 38, 9, 13, 27].includes(pseudo_keycode);
+ this.move(e);
+ },
+
+ keypress(e) {
+ if (!this.suppressKeyPressRepeat) {
+ this.move(e);
+ return;
+ }
+ this.maybeStopAdvance(e);
+ },
- this.maybeStopAdvance(e);
- },
+ keyup(e) {
+ const pseudo_keycode = get_pseudo_keycode(e);
- mousemove(e) {
- if (!this.mouse_moved_since_typeahead) {
- /* Undo cursor disabling in mouseenter handler. */
- $(e.currentTarget).find("a").css("cursor", "");
- this.mouse_moved_since_typeahead = true;
- this.mouseenter(e);
- }
- },
+ switch (pseudo_keycode) {
+ case 40: // down arrow
+ case 38: // up arrow
+ break;
- keydown(e) {
- const pseudo_keycode = get_pseudo_keycode(e);
- if (this.trigger_selection(e)) {
+ case 9: // tab
+ if (!this.options.tabIsEnter) {
+ return;
+ }
if (!this.shown) {
return;
}
- e.preventDefault();
this.select(e);
- }
- this.suppressKeyPressRepeat = ![40, 38, 9, 13, 27].includes(pseudo_keycode);
- this.move(e);
- },
-
- keypress(e) {
- if (!this.suppressKeyPressRepeat) {
- this.move(e);
- return;
- }
- this.maybeStopAdvance(e);
- },
-
- keyup(e) {
- const pseudo_keycode = get_pseudo_keycode(e);
-
- switch (pseudo_keycode) {
- case 40: // down arrow
- case 38: // up arrow
- break;
-
- case 9: // tab
- if (!this.options.tabIsEnter) {
- return;
- }
- if (!this.shown) {
- return;
- }
- this.select(e);
- break;
-
- case 13: // enter
- if (!this.shown) {
- return;
- }
- this.select(e);
- break;
-
- case 27: // escape
- if (!this.shown) {
- return;
- }
- this.hide();
- if (this.on_escape) {
- this.on_escape();
- }
- break;
-
- default:
- // to stop typeahead from showing up momentarily
- // when shift (keycode 16) + tabbing to the topic field
- if (
- pseudo_keycode === 16 &&
- e.currentTarget.id === "stream_message_recipient_topic"
- ) {
- return;
- }
- if (this.options.openInputFieldOnKeyUp !== null && !this.shown) {
- // If the typeahead isn't shown yet, the `lookup` call will open it.
- // Here we make a callback to the input field before we open the
- // lookahead in case it needs to make UI changes first (e.g. widening
- // the search bar).
- this.options.openInputFieldOnKeyUp();
- }
- this.lookup(false);
- }
+ break;
- this.maybeStopAdvance(e);
+ case 13: // enter
+ if (!this.shown) {
+ return;
+ }
+ this.select(e);
+ break;
- e.preventDefault();
- },
-
- blur(e) {
- // Blurs that move focus to elsewhere within the parent element shouldn't
- // hide the typeahead.
- if (
- this.options.parentElement &&
- $(e.relatedTarget).parents(this.options.parentElement).length > 0
- ) {
- return;
- }
- setTimeout(() => {
- if (!this.$container.is(":hover")) {
- this.hide();
- } else if (this.shown) {
- // refocus the input if the user clicked on the typeahead
- // so that clicking elsewhere registers as a blur and hides
- // the typeahead.
- this.$element.trigger("focus");
+ case 27: // escape
+ if (!this.shown) {
+ return;
+ }
+ this.hide();
+ if (this.on_escape) {
+ this.on_escape();
}
- }, 150);
- },
+ break;
+
+ default:
+ // to stop typeahead from showing up momentarily
+ // when shift (keycode 16) + tabbing to the topic field
+ if (
+ pseudo_keycode === 16 &&
+ e.currentTarget.id === "stream_message_recipient_topic"
+ ) {
+ return;
+ }
+ if (this.options.openInputFieldOnKeyUp !== null && !this.shown) {
+ // If the typeahead isn't shown yet, the `lookup` call will open it.
+ // Here we make a callback to the input field before we open the
+ // lookahead in case it needs to make UI changes first (e.g. widening
+ // the search bar).
+ this.options.openInputFieldOnKeyUp();
+ }
+ this.lookup(false);
+ }
- element_click() {
- // update / hide the typeahead menu if the user clicks anywhere
- // inside the typing area, to avoid misplaced typeahead insertion.
- this.lookup();
- },
+ this.maybeStopAdvance(e);
- click(e) {
- e.stopPropagation();
- e.preventDefault();
- // The original bootstrap code expected `mouseenter` to be called
- // to set the active element before `select()`.
- // Since select() selects the element with the active class, if
- // we trigger a click from JavaScript (or with a mobile touchscreen),
- // this won't work. So we need to set the active element ourselves,
- // and the simplest way to do that is to just call the `mouseenter`
- // handler here.
- this.mouseenter(e);
- this.select(e);
- },
-
- mouseenter(e) {
- if (!this.mouse_moved_since_typeahead) {
- // Prevent the annoying interaction where your mouse happens
- // to be in the space where typeahead will open. (This would
- // result in the mouse taking priority over the keyboard for
- // what you selected). If the mouse has not been moved since
- // the appearance of the typeahead menu, we disable the
- // cursor, which in turn prevents the currently hovered
- // element from being selected. The mousemove handler
- // overrides this logic.
- $(e.currentTarget).find("a").css("cursor", "none");
- return;
- }
- this.$menu.find(".active").removeClass("active");
- $(e.currentTarget).addClass("active");
- },
- };
-
- /* TYPEAHEAD PLUGIN DEFINITION
- * =========================== */
-
- $.fn.typeahead = function (option) {
- return this.each(function () {
- const $this = $(this);
- let data = $this.data("typeahead");
- const options = typeof option === "object" && option;
- if (!data) {
- $this.data("typeahead", (data = new Typeahead(this, options)));
- }
- if (typeof option === "string") {
- data[option]();
+ e.preventDefault();
+ },
+
+ blur(e) {
+ // Blurs that move focus to elsewhere within the parent element shouldn't
+ // hide the typeahead.
+ if (
+ this.options.parentElement &&
+ $(e.relatedTarget).parents(this.options.parentElement).length > 0
+ ) {
+ return;
+ }
+ setTimeout(() => {
+ if (!this.$container.is(":hover")) {
+ this.hide();
+ } else if (this.shown) {
+ // refocus the input if the user clicked on the typeahead
+ // so that clicking elsewhere registers as a blur and hides
+ // the typeahead.
+ this.$element.trigger("focus");
}
- });
- };
-
- $.fn.typeahead.defaults = {
- source: [],
- items: 8,
- container: '<div class="typeahead dropdown-menu"></div>',
- header_html: '<p class="typeahead-header"><span id="typeahead-header-text"></span></p>',
- menu: '<ul class="typeahead-menu"></ul>',
- item: "<li><a></a></li>",
- minLength: 1,
- stopAdvance: false,
- dropup: false,
- advanceKeyCodes: [],
- openInputFieldOnKeyUp: null,
- closeInputFieldOnHide: null,
- tabIsEnter: true,
- };
-
- $.fn.typeahead.Constructor = Typeahead;
-})(window.jQuery);
+ }, 150);
+ },
+
+ element_click() {
+ // update / hide the typeahead menu if the user clicks anywhere
+ // inside the typing area, to avoid misplaced typeahead insertion.
+ this.lookup();
+ },
+
+ click(e) {
+ e.stopPropagation();
+ e.preventDefault();
+ // The original bootstrap code expected `mouseenter` to be called
+ // to set the active element before `select()`.
+ // Since select() selects the element with the active class, if
+ // we trigger a click from JavaScript (or with a mobile touchscreen),
+ // this won't work. So we need to set the active element ourselves,
+ // and the simplest way to do that is to just call the `mouseenter`
+ // handler here.
+ this.mouseenter(e);
+ this.select(e);
+ },
+
+ mouseenter(e) {
+ if (!this.mouse_moved_since_typeahead) {
+ // Prevent the annoying interaction where your mouse happens
+ // to be in the space where typeahead will open. (This would
+ // result in the mouse taking priority over the keyboard for
+ // what you selected). If the mouse has not been moved since
+ // the appearance of the typeahead menu, we disable the
+ // cursor, which in turn prevents the currently hovered
+ // element from being selected. The mousemove handler
+ // overrides this logic.
+ $(e.currentTarget).find("a").css("cursor", "none");
+ return;
+ }
+ this.$menu.find(".active").removeClass("active");
+ $(e.currentTarget).addClass("active");
+ },
+};
+
+/* TYPEAHEAD PLUGIN DEFINITION
+ * =========================== */
+
+$.fn.typeahead = function (option) {
+ return this.each(function () {
+ const $this = $(this);
+ let data = $this.data("typeahead");
+ const options = typeof option === "object" && option;
+ if (!data) {
+ $this.data("typeahead", (data = new Typeahead(this, options)));
+ }
+ if (typeof option === "string") {
+ data[option]();
+ }
+ });
+};
+
+$.fn.typeahead.defaults = {
+ source: [],
+ items: 8,
+ container: '<div class="typeahead dropdown-menu"></div>',
+ header_html: '<p class="typeahead-header"><span id="typeahead-header-text"></span></p>',
+ menu: '<ul class="typeahead-menu"></ul>',
+ item: "<li><a></a></li>",
+ minLength: 1,
+ stopAdvance: false,
+ dropup: false,
+ advanceKeyCodes: [],
+ openInputFieldOnKeyUp: null,
+ closeInputFieldOnHide: null,
+ tabIsEnter: true,
+};
+
+$.fn.typeahead.Constructor = Typeahead;