Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
typeahead: Run prettier for automatic format cleanup.
evykassirer7ccb833b35e0
diff --git a/web/third/bootstrap-typeahead/typeahead.js b/web/third/bootstrap-typeahead/typeahead.jsindex ede389c73de..1545136b396 100644--- a/web/third/bootstrap-typeahead/typeahead.js+++ b/web/third/bootstrap-typeahead/typeahead.js@@ -104,14 +104,14 @@* append to, where before it could only be appended to `body`.* Since it's in the right part of the DOM, we don't need to do* the manual positioning in the show() function.- *+ ** 11. Add `openInputFieldOnKeyUp` option:- *+ ** If the typeahead isn't shown yet, the `lookup` call in the keyup* handler 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).- *+ ** 12. Add `closeInputFieldOnHide` option:** Some input fields like search have visual changes that need to happen@@ -125,9 +125,9 @@* functionality. The `tabIsEnter` option (default true) lets this be* turned off so that tab only does one thing while focus is in the* typeahead -- move focus to the next element.- *+ ** 14. Don't act on blurs that change focus within the `parentElement`:- *+ ** This allows us to have things like a close button, and be able* to move focus there without the typeahead closing.* ============================================================ */@@ -136,510 +136,549 @@ import {insertTextIntoField} from "text-field-edit";import {get_string_diff} from "../../src/util";-!function($){-- function get_pseudo_keycode(e) {- const isComposing = (event.originalEvent && event.originalEvent.isComposing) || false;- /* We treat IME compose enter keypresses as a separate -13 key. */- if (e.keyCode === 13 && isComposing) {- return -13;- }- return e.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 || this.automated;- this.trigger_selection = this.options.trigger_selection || this.trigger_selection;- this.on_move = this.options.on_move;- this.on_escape = this.options.on_escape;- this.header = this.options.header || this.header;- this.option_label = this.options.option_label || this.option_label;-- 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.substring(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()- }-- , set_value () {- const val = this.$menu.find('.active').data('typeahead-value')- this.$element.is("[contenteditable]") ? this.$element.html(val) : this.$element.val(val);-- if (this.on_move) {- this.on_move();- }- }-- , updater (item) {- return item+!(function ($) {+ function get_pseudo_keycode(e) {+ const isComposing = (event.originalEvent && event.originalEvent.isComposing) || false;+ /* We treat IME compose enter keypresses as a separate -13 key. */+ if (e.keyCode === 13 && isComposing) {+ return -13;+ }+ return e.keyCode;}- , automated() {- return false;- }-- , trigger_selection() {- return false;- }-- , header() {- // return a string to show in typeahead header or false.- return false;- }-- , option_label (matching_items, item) {- // return a string to show in typeahead items or false.- return false- }-- , 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();- }-- // 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;+ /* 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 || this.automated;+ this.trigger_selection = this.options.trigger_selection || this.trigger_selection;+ this.on_move = this.options.on_move;+ this.on_escape = this.options.on_escape;+ this.header = this.options.header || this.header;+ this.option_label = this.options.option_label || this.option_label;if (this.fixed) {- // Relative to screen instead of to page- pos = this.$element[0].getBoundingClientRect();- } else {- pos = this.$element.offset();- }-- pos = $.extend({}, pos, {- height: this.$element[0].offsetHeight- })-- // Zulip patch: Workaround for iOS safari problems- pos.top = this.$element.get_offset_to_window().top;-- let top_pos = pos.top + pos.height- if (this.dropup) {- top_pos = pos.top - this.$container.outerHeight()- }-- // Zulip patch: Avoid typeahead going off top of screen.- if (top_pos < 0) {- top_pos = 0;+ 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.substring(from, to_after);+ // select / highlight the minimal text to be replaced+ this.$element[0].setSelectionRange(from, to_before);+ insertTextIntoField(this.$element[0], replacement);+ }- this.$container.css({- top: top_pos- , left: pos.left- })- }-- this.$container.show()- this.shown = true- this.mouse_moved_since_typeahead = false- return this- }-- , hide () {- this.$container.hide()- this.shown = false- if (this.options.closeInputFieldOnHide !== null) {- this.options.closeInputFieldOnHide();- }- return this- }+ return this.hide();+ },- , lookup (hideOnEmpty) {- let items+ set_value() {+ const val = this.$menu.find(".active").data("typeahead-value");+ this.$element.is("[contenteditable]")+ ? this.$element.html(val)+ : this.$element.val(val);- this.query = this.$element.is("[contenteditable]") ? this.$element.text() : this.$element.val();+ if (this.on_move) {+ this.on_move();+ }+ },++ updater(item) {+ return item;+ },++ automated() {+ return false;+ },++ trigger_selection() {+ return false;+ },++ header() {+ // return a string to show in typeahead header or false.+ return false;+ },++ option_label(matching_items, item) {+ // return a string to show in typeahead items or false.+ return false;+ },++ 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();+ }- if ((!this.options.helpOnEmptyStrings || hideOnEmpty) && (!this.query || this.query.length < this.options.minLength)) {- return this.shown ? this.hide() : this- }+ // 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 {+ pos = this.$element.offset();+ }++ pos = $.extend({}, pos, {+ height: this.$element[0].offsetHeight,+ });++ // Zulip patch: Workaround for iOS safari problems+ pos.top = this.$element.get_offset_to_window().top;++ let top_pos = pos.top + pos.height;+ if (this.dropup) {+ top_pos = pos.top - this.$container.outerHeight();+ }++ // Zulip patch: Avoid typeahead going off top of screen.+ if (top_pos < 0) {+ top_pos = 0;+ }++ this.$container.css({+ top: top_pos,+ left: pos.left,+ });+ }- items = typeof this.source === "function" ? this.source(this.query, this.process.bind(this)) : this.source+ this.$container.show();+ this.shown = true;+ this.mouse_moved_since_typeahead = false;+ return this;+ },++ hide() {+ this.$container.hide();+ this.shown = false;+ if (this.options.closeInputFieldOnHide !== null) {+ this.options.closeInputFieldOnHide();+ }+ return this;+ },- if (!items && this.shown) {this.hide();}- return items ? this.process(items) : this- }+ lookup(hideOnEmpty) {+ let items;- , process (items) {- const that = this+ this.query = this.$element.is("[contenteditable]")+ ? this.$element.text()+ : this.$element.val();- const matching_items = $.grep(items, (item) => this.matcher(item));+ if (+ (!this.options.helpOnEmptyStrings || hideOnEmpty) &&+ (!this.query || this.query.length < this.options.minLength)+ ) {+ return this.shown ? this.hide() : this;+ }- const final_items = this.sorter(matching_items);+ items =+ typeof this.source === "function"+ ? this.source(this.query, this.process.bind(this))+ : this.source;- 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();- }+ if (!items && this.shown) {+ this.hide();+ }+ return items ? this.process(items) : this;+ },- , matcher (item) {- return ~item.toLowerCase().indexOf(this.query.toLowerCase())- }+ process(items) {+ const that = this;- , sorter (items) {- const beginswith = []- ; const caseSensitive = []- ; const caseInsensitive = []- ; let item+ const matching_items = $.grep(items, (item) => this.matcher(item));- while (item = items.shift()) {- if (!item.toLowerCase().indexOf(this.query.toLowerCase())) {beginswith.push(item)}- else if (~item.indexOf(this.query)) {caseSensitive.push(item)}- else {caseInsensitive.push(item)}- }+ const final_items = this.sorter(matching_items);- return [...beginswith, ...caseSensitive, ...caseInsensitive]- }+ 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().indexOf(this.query.toLowerCase());+ },++ sorter(items) {+ const beginswith = [];+ const caseSensitive = [];+ const caseInsensitive = [];+ let item;++ while ((item = items.shift())) {+ if (!item.toLowerCase().indexOf(this.query.toLowerCase())) {+ beginswith.push(item);+ } else if (~item.indexOf(this.query)) {+ caseSensitive.push(item);+ } else {+ caseInsensitive.push(item);+ }+ }- , render (final_items, matching_items) {- const that = this+ return [...beginswith, ...caseSensitive, ...caseInsensitive];+ },- const items = $(final_items).map((index, item) => {- const i = $(that.options.item).data('typeahead-value', item)- const item_html = i.find('a').html(that.highlighter(item))+ render(final_items, matching_items) {+ const that = this;- const option_label_html = that.option_label(matching_items, item)+ const items = $(final_items).map((index, item) => {+ const i = $(that.options.item).data("typeahead-value", item);+ const item_html = i.find("a").html(that.highlighter(item));- if (option_label_html) {- item_html.append(option_label_html).addClass("typeahead-option-label");- }- return i[0]- })+ const option_label_html = that.option_label(matching_items, item);- items.first().addClass('active')- this.$menu.html(items)- return this- }+ if (option_label_html) {+ item_html.append(option_label_html).addClass("typeahead-option-label");+ }+ return i[0];+ });- , next (event) {- const active = this.$menu.find('.active').removeClass('active')- ; let next = active.next()+ items.first().addClass("active");+ this.$menu.html(items);+ return this;+ },- if (!next.length) {- next = $(this.$menu.find('li')[0])- }+ next(event) {+ const active = this.$menu.find(".active").removeClass("active");+ let next = active.next();- next.addClass('active')+ if (!next.length) {+ next = $(this.$menu.find("li")[0]);+ }- if (this.options.naturalSearch) {- this.set_value();- }- }+ next.addClass("active");- , prev (event) {- const active = this.$menu.find('.active').removeClass('active')- ; let prev = active.prev()+ if (this.options.naturalSearch) {+ this.set_value();+ }+ },- if (!prev.length) {- prev = this.$menu.find('li').last()- }+ prev(event) {+ const active = this.$menu.find(".active").removeClass("active");+ let prev = active.prev();- prev.addClass('active')+ if (!prev.length) {+ prev = this.$menu.find("li").last();+ }- if (this.options.naturalSearch) {- this.set_value();- }- }+ prev.addClass("active");- , 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))+ if (this.options.naturalSearch) {+ this.set_value();+ }+ },- if (this.eventSupported('keydown')) {- this.$element.on('keydown', this.keydown.bind(this))- }+ 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));- this.$menu- .on('click', 'li', this.click.bind(this))- .on('mouseenter', 'li', this.mouseenter.bind(this))- .on('mousemove', 'li', this.mousemove.bind(this))+ if (this.eventSupported("keydown")) {+ this.$element.on("keydown", this.keydown.bind(this));+ }- $(window).on('resize', this.resizeHandler.bind(this));- }+ this.$menu+ .on("click", "li", this.click.bind(this))+ .on("mouseenter", "li", this.mouseenter.bind(this))+ .on("mousemove", "li", this.mousemove.bind(this));- , unlisten () {- this.$container.remove();- const events = ["blur", "keydown", "keyup", "keypress", "mousemove"];- for (const event_ of events) {- this.$element.off(event_);- }- this.$element.removeData("typeahead");- }+ $(window).on("resize", this.resizeHandler.bind(this));+ },- , eventSupported(eventName) {- let isSupported = eventName in this.$element- if (!isSupported) {- this.$element.setAttribute(eventName, 'return;')- isSupported = typeof this.$element[eventName] === 'function'- }- return isSupported- }+ 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;+ },- , 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();- }- }-- , move (e) {- if (!this.shown) {return}- const pseudo_keycode = get_pseudo_keycode(e);-- switch(pseudo_keycode) {- case 9: // tab- if (!this.options.tabIsEnter) {return}- 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- }-- 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) {+ if (!this.shown) {+ return;+ }+ const pseudo_keycode = get_pseudo_keycode(e);++ switch (pseudo_keycode) {+ case 9: // tab+ if (!this.options.tabIsEnter) {+ return;+ }+ 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;+ }- , keydown (e) {- const pseudo_keycode = get_pseudo_keycode(e);- if (this.trigger_selection(e)) {- if (!this.shown) {return;}- e.preventDefault();- this.select(e);- }- this.suppressKeyPressRepeat = !~$.inArray(pseudo_keycode, [40,38,9,13,27]);- this.move(e)- }+ this.maybeStopAdvance(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}- 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-- // to stop typeahead from showing up momentarily- // when shift + tabbing to the topic field- case 16: // shift- if (e.currentTarget.id === "stream_message_recipient_topic") {- return;- }-- default:- var hideOnEmpty = false- // backspace- if (e.keyCode === 8 && this.options.helpOnEmptyStrings && // Support for inputs to set the hideOnEmpty option explicitly to false- // to display typeahead after hitting backspace to clear the input.- (typeof(this.options.hideOnEmpty) === undefined || this.options.hideOnEmpty)) {- hideOnEmpty = true;+ 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);+ }+ },++ keydown(e) {+ const pseudo_keycode = get_pseudo_keycode(e);+ if (this.trigger_selection(e)) {+ if (!this.shown) {+ return;+ }+ e.preventDefault();+ this.select(e);+ }+ this.suppressKeyPressRepeat = !~$.inArray(pseudo_keycode, [40, 38, 9, 13, 27]);+ 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;+ }+ 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;++ // to stop typeahead from showing up momentarily+ // when shift + tabbing to the topic field+ case 16: // shift+ if (e.currentTarget.id === "stream_message_recipient_topic") {+ return;+ }++ default:+ var hideOnEmpty = false;+ // backspace+ if (+ e.keyCode === 8 &&+ this.options.helpOnEmptyStrings && // Support for inputs to set the hideOnEmpty option explicitly to false+ // to display typeahead after hitting backspace to clear the input.+ (typeof this.options.hideOnEmpty === undefined || this.options.hideOnEmpty)+ ) {+ hideOnEmpty = true;+ }+ 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(hideOnEmpty);}- 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(hideOnEmpty)- }-- this.maybeStopAdvance(e);-- 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;- }- const that = this- setTimeout(() => {- if (!that.$container.is(':hover')) {- that.hide();- } else if (that.shown) {- // refocus the input if the user clicked on the typeahead- // so that clicking elsewhere registers as a blur and hides- // the typeahead.- that.$element.trigger("focus");- }- }, 150)- }- , element_click (e) {- // 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)- }+ this.maybeStopAdvance(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')- }+ e.preventDefault();+ },- }--- /* 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-}(window.jQuery);+ 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;+ }+ const that = this;+ setTimeout(() => {+ if (!that.$container.is(":hover")) {+ that.hide();+ } else if (that.shown) {+ // refocus the input if the user clicked on the typeahead+ // so that clicking elsewhere registers as a blur and hides+ // the typeahead.+ that.$element.trigger("focus");+ }+ }, 150);+ },++ element_click(e) {+ // 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;+})(window.jQuery);