File "jquery.autocomplete.min.js"

Full Path: /home/humancap/cl.humancap.com.my/admin/assets/javascripts/plugins/jquery.autocomplete.min.js
File size: 18.71 KB
MIME-type: text/plain
Charset: utf-8

!function (factory) { "use strict"; "function" == typeof define && define.amd ? define(["jquery"], factory) : "object" == typeof exports && "function" == typeof require ? factory(require("jquery")) : factory(jQuery) }(function ($) { "use strict"; var utils = { escapeRegExChars: function (value) { return value.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&") }, createNode: function (containerClass) { var div = document.createElement("div"); return div.className = containerClass, div.style.position = "absolute", div.style.display = "none", div } }, keys_ESC = 27, keys_TAB = 9, keys_RETURN = 13, keys_LEFT = 37, keys_UP = 38, keys_RIGHT = 39, keys_DOWN = 40, noop = $.noop; function Autocomplete (el, options) { var that = this; this.element = el, this.el = $(el), this.suggestions = [], this.badQueries = [], this.selectedIndex = -1, this.currentValue = this.element.value, this.timeoutId = null, this.cachedResponse = {}, this.onChangeTimeout = null, this.onChange = null, this.isLocal = !1, this.suggestionsContainer = null, this.noSuggestionsContainer = null, this.options = $.extend(!0, {}, Autocomplete.defaults, options), this.classes = { selected: "autocomplete-selected", suggestion: "autocomplete-suggestion" }, this.hint = null, this.hintValue = "", this.selection = null, this.initialize(), this.setOptions(options) } function _lookupFilter (suggestion, originalQuery, queryLowerCase) { return -1 !== suggestion.value.toLowerCase().indexOf(queryLowerCase) } function _transformResult (response) { return "string" == typeof response ? $.parseJSON(response) : response } function _formatResult (suggestion, currentValue) { if (!currentValue) return suggestion.value; var pattern = "(" + utils.escapeRegExChars(currentValue) + ")"; return suggestion.value.replace(new RegExp(pattern, "gi"), "<strong>$1</strong>").replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/&lt;(\/?strong)&gt;/g, "<$1>") } function _formatGroup (suggestion, category) { return '<div class="autocomplete-group">' + category + "</div>" } Autocomplete.utils = utils, $.Autocomplete = Autocomplete, Autocomplete.defaults = { ajaxSettings: {}, autoSelectFirst: !1, appendTo: "body", serviceUrl: null, lookup: null, onSelect: null, width: "auto", minChars: 1, maxHeight: 300, deferRequestBy: 0, params: {}, formatResult: _formatResult, formatGroup: _formatGroup, delimiter: null, zIndex: 9999, type: "GET", noCache: !1, onSearchStart: noop, onSearchComplete: noop, onSearchError: noop, preserveInput: !1, containerClass: "autocomplete-suggestions", tabDisabled: !1, dataType: "text", currentRequest: null, triggerSelectOnValidInput: !0, preventBadQueries: !0, lookupFilter: _lookupFilter, paramName: "query", transformResult: _transformResult, showNoSuggestionNotice: !1, noSuggestionNotice: "No results", orientation: "bottom", forceFixPosition: !1 }, Autocomplete.prototype = { initialize: function () { var that = this, suggestionSelector = "." + that.classes.suggestion, selected = that.classes.selected, options = that.options, container; that.element.setAttribute("autocomplete", "off"), that.noSuggestionsContainer = $('<div class="autocomplete-no-suggestion"></div>').html(this.options.noSuggestionNotice).get(0), that.suggestionsContainer = Autocomplete.utils.createNode(options.containerClass), (container = $(that.suggestionsContainer)).appendTo(options.appendTo || "body"), "auto" !== options.width && container.css("width", options.width), container.on("mouseover.autocomplete", suggestionSelector, function () { that.activate($(this).data("index")) }), container.on("mouseout.autocomplete", function () { that.selectedIndex = -1, container.children("." + selected).removeClass(selected) }), container.on("click.autocomplete", suggestionSelector, function () { that.select($(this).data("index")) }), container.on("click.autocomplete", function () { clearTimeout(that.blurTimeoutId) }), that.fixPositionCapture = function () { that.visible && that.fixPosition() }, $(window).on("resize.autocomplete", that.fixPositionCapture), that.el.on("keydown.autocomplete", function (e) { that.onKeyPress(e) }), that.el.on("keyup.autocomplete", function (e) { that.onKeyUp(e) }), that.el.on("blur.autocomplete", function () { that.onBlur() }), that.el.on("focus.autocomplete", function () { that.onFocus() }), that.el.on("change.autocomplete", function (e) { that.onKeyUp(e) }), that.el.on("input.autocomplete", function (e) { that.onKeyUp(e) }) }, onFocus: function () { var that = this; this.fixPosition(), this.el.val().length >= this.options.minChars && this.onValueChange() }, onBlur: function () { var that = this, options = that.options, value = that.el.val(), query = that.getQuery(value); that.blurTimeoutId = setTimeout(function () { that.hide(), that.selection && that.currentValue !== query && (options.onInvalidateSelection || $.noop).call(that.element) }, 200) }, abortAjax: function () { var that = this; this.currentRequest && (this.currentRequest.abort(), this.currentRequest = null) }, setOptions: function (suppliedOptions) { var that = this, options = $.extend({}, this.options, suppliedOptions); this.isLocal = Array.isArray(options.lookup), this.isLocal && (options.lookup = this.verifySuggestionsFormat(options.lookup)), options.orientation = this.validateOrientation(options.orientation, "bottom"), $(this.suggestionsContainer).css({ "max-height": options.maxHeight + "px", width: options.width + "px", "z-index": options.zIndex }), this.options = options }, clearCache: function () { this.cachedResponse = {}, this.badQueries = [] }, clear: function () { this.clearCache(), this.currentValue = "", this.suggestions = [] }, disable: function () { var that = this; this.disabled = !0, clearTimeout(this.onChangeTimeout), this.abortAjax() }, enable: function () { this.disabled = !1 }, fixPosition: function () { var that = this, $container = $(this.suggestionsContainer), containerParent = $container.parent().get(0); if (containerParent === document.body || this.options.forceFixPosition) { var orientation = this.options.orientation, containerHeight = $container.outerHeight(), height = this.el.outerHeight(), offset = this.el.offset(), styles = { top: offset.top, left: offset.left }; if ("auto" === orientation) { var viewPortHeight = $(window).height(), scrollTop = $(window).scrollTop(), topOverflow = -scrollTop + offset.top - containerHeight, bottomOverflow = scrollTop + viewPortHeight - (offset.top + height + containerHeight); orientation = Math.max(topOverflow, bottomOverflow) === topOverflow ? "top" : "bottom" } if (styles.top += "top" === orientation ? -containerHeight : height, containerParent !== document.body) { var opacity = $container.css("opacity"), parentOffsetDiff; this.visible || $container.css("opacity", 0).show(), parentOffsetDiff = $container.offsetParent().offset(), styles.top -= parentOffsetDiff.top, styles.top += containerParent.scrollTop, styles.left -= parentOffsetDiff.left, this.visible || $container.css("opacity", opacity).hide() } "auto" === this.options.width && (styles.width = this.el.outerWidth() + "px"), $container.css(styles) } }, isCursorAtEnd: function () { var that = this, valLength = this.el.val().length, selectionStart = this.element.selectionStart, range; return "number" == typeof selectionStart ? selectionStart === valLength : !document.selection || ((range = document.selection.createRange()).moveStart("character", -valLength), valLength === range.text.length) }, onKeyPress: function (e) { var that = this; if (this.disabled || this.visible || e.which !== keys_DOWN || !this.currentValue) { if (!this.disabled && this.visible) { switch (e.which) { case keys_ESC: this.el.val(this.currentValue), this.hide(); break; case keys_RIGHT: if (this.hint && this.options.onHint && this.isCursorAtEnd()) { this.selectHint(); break } return; case keys_TAB: if (this.hint && this.options.onHint) return void this.selectHint(); if (-1 === this.selectedIndex) return void this.hide(); if (this.select(this.selectedIndex), !1 === this.options.tabDisabled) return; break; case keys_RETURN: if (-1 === this.selectedIndex) return void this.hide(); this.select(this.selectedIndex); break; case keys_UP: this.moveUp(); break; case keys_DOWN: this.moveDown(); break; default: return }e.stopImmediatePropagation(), e.preventDefault() } } else this.suggest() }, onKeyUp: function (e) { var that = this; if (!that.disabled) { switch (e.which) { case keys_UP: case keys_DOWN: return }clearTimeout(that.onChangeTimeout), that.currentValue !== that.el.val() && (that.findBestHint(), that.options.deferRequestBy > 0 ? that.onChangeTimeout = setTimeout(function () { that.onValueChange() }, that.options.deferRequestBy) : that.onValueChange()) } }, onValueChange: function () { if (this.ignoreValueChange) this.ignoreValueChange = !1; else { var that = this, options = this.options, value = this.el.val(), query = this.getQuery(value); this.selection && this.currentValue !== query && (this.selection = null, (options.onInvalidateSelection || $.noop).call(this.element)), clearTimeout(this.onChangeTimeout), this.currentValue = value, this.selectedIndex = -1, options.triggerSelectOnValidInput && this.isExactMatch(query) ? this.select(0) : query.length < options.minChars ? this.hide() : this.getSuggestions(query) } }, isExactMatch: function (query) { var suggestions = this.suggestions; return 1 === suggestions.length && suggestions[0].value.toLowerCase() === query.toLowerCase() }, getQuery: function (value) { var delimiter = this.options.delimiter, parts; return delimiter ? (parts = value.split(delimiter), $.trim(parts[parts.length - 1])) : value }, getSuggestionsLocal: function (query) { var that = this, options = this.options, queryLowerCase = query.toLowerCase(), filter = options.lookupFilter, limit = parseInt(options.lookupLimit, 10), data; return data = { suggestions: $.grep(options.lookup, function (suggestion) { return filter(suggestion, query, queryLowerCase) }) }, limit && data.suggestions.length > limit && (data.suggestions = data.suggestions.slice(0, limit)), data }, getSuggestions: function (q) { var response, that = this, options = that.options, serviceUrl = options.serviceUrl, params, cacheKey, ajaxSettings; options.params[options.paramName] = q, !1 !== options.onSearchStart.call(that.element, options.params) && (params = options.ignoreParams ? null : options.params, $.isFunction(options.lookup) ? options.lookup(q, function (data) { that.suggestions = data.suggestions, that.suggest(), options.onSearchComplete.call(that.element, q, data.suggestions) }) : (that.isLocal ? response = that.getSuggestionsLocal(q) : ($.isFunction(serviceUrl) && (serviceUrl = serviceUrl.call(that.element, q)), cacheKey = serviceUrl + "?" + $.param(params || {}), response = that.cachedResponse[cacheKey]), response && Array.isArray(response.suggestions) ? (that.suggestions = response.suggestions, that.suggest(), options.onSearchComplete.call(that.element, q, response.suggestions)) : that.isBadQuery(q) ? options.onSearchComplete.call(that.element, q, []) : (that.abortAjax(), ajaxSettings = { url: serviceUrl, data: params, type: options.type, dataType: options.dataType }, $.extend(ajaxSettings, options.ajaxSettings), that.currentRequest = $.ajax(ajaxSettings).done(function (data) { var result; that.currentRequest = null, result = options.transformResult(data, q), that.processResponse(result, q, cacheKey), options.onSearchComplete.call(that.element, q, result.suggestions) }).fail(function (jqXHR, textStatus, errorThrown) { options.onSearchError.call(that.element, q, jqXHR, textStatus, errorThrown) })))) }, isBadQuery: function (q) { if (!this.options.preventBadQueries) return !1; for (var badQueries = this.badQueries, i = badQueries.length; i--;)if (0 === q.indexOf(badQueries[i])) return !0; return !1 }, hide: function () { var that = this, container = $(this.suggestionsContainer); $.isFunction(this.options.onHide) && this.visible && this.options.onHide.call(this.element, container), this.visible = !1, this.selectedIndex = -1, clearTimeout(this.onChangeTimeout), $(this.suggestionsContainer).hide(), this.signalHint(null) }, suggest: function () { if (this.suggestions.length) { var that = this, options = this.options, groupBy = options.groupBy, formatResult = options.formatResult, value = this.getQuery(this.currentValue), className = this.classes.suggestion, classSelected = this.classes.selected, container = $(this.suggestionsContainer), noSuggestionsContainer = $(this.noSuggestionsContainer), beforeRender = options.beforeRender, html = "", category, formatGroup = function (suggestion, index) { var currentCategory = suggestion.data[groupBy]; return category === currentCategory ? "" : (category = currentCategory, options.formatGroup(suggestion, category)) }; options.triggerSelectOnValidInput && this.isExactMatch(value) ? this.select(0) : ($.each(this.suggestions, function (i, suggestion) { groupBy && (html += formatGroup(suggestion, value, i)), html += '<div class="' + className + '" data-index="' + i + '">' + formatResult(suggestion, value, i) + "</div>" }), this.adjustContainerWidth(), noSuggestionsContainer.detach(), container.html(html), $.isFunction(beforeRender) && beforeRender.call(this.element, container, this.suggestions), this.fixPosition(), container.show(), options.autoSelectFirst && (this.selectedIndex = 0, container.scrollTop(0), container.children("." + className).first().addClass(classSelected)), this.visible = !0, this.findBestHint()) } else this.options.showNoSuggestionNotice ? this.noSuggestions() : this.hide() }, noSuggestions: function () { var that = this, beforeRender = this.options.beforeRender, container = $(this.suggestionsContainer), noSuggestionsContainer = $(this.noSuggestionsContainer); this.adjustContainerWidth(), noSuggestionsContainer.detach(), container.empty(), container.append(noSuggestionsContainer), $.isFunction(beforeRender) && beforeRender.call(this.element, container, this.suggestions), this.fixPosition(), container.show(), this.visible = !0 }, adjustContainerWidth: function () { var that = this, options = this.options, width, container = $(this.suggestionsContainer); "auto" === options.width ? (width = this.el.outerWidth(), container.css("width", width > 0 ? width : 300)) : "flex" === options.width && container.css("width", "") }, findBestHint: function () { var that = this, value = this.el.val().toLowerCase(), bestMatch = null; value && ($.each(this.suggestions, function (i, suggestion) { var foundMatch = 0 === suggestion.value.toLowerCase().indexOf(value); return foundMatch && (bestMatch = suggestion), !foundMatch }), this.signalHint(bestMatch)) }, signalHint: function (suggestion) { var hintValue = "", that = this; suggestion && (hintValue = this.currentValue + suggestion.value.substr(this.currentValue.length)), this.hintValue !== hintValue && (this.hintValue = hintValue, this.hint = suggestion, (this.options.onHint || $.noop)(hintValue)) }, verifySuggestionsFormat: function (suggestions) { return suggestions.length && "string" == typeof suggestions[0] ? $.map(suggestions, function (value) { return { value: value, data: null } }) : suggestions }, validateOrientation: function (orientation, fallback) { return orientation = $.trim(orientation || "").toLowerCase(), -1 === $.inArray(orientation, ["auto", "bottom", "top"]) && (orientation = fallback), orientation }, processResponse: function (result, originalQuery, cacheKey) { var that = this, options = this.options; result.suggestions = this.verifySuggestionsFormat(result.suggestions), options.noCache || (this.cachedResponse[cacheKey] = result, options.preventBadQueries && !result.suggestions.length && this.badQueries.push(originalQuery)), originalQuery === this.getQuery(this.currentValue) && (this.suggestions = result.suggestions, this.suggest()) }, activate: function (index) { var that = this, activeItem, selected = this.classes.selected, container = $(this.suggestionsContainer), children = container.find("." + this.classes.suggestion); return container.find("." + selected).removeClass(selected), this.selectedIndex = index, -1 !== this.selectedIndex && children.length > this.selectedIndex ? (activeItem = children.get(this.selectedIndex), $(activeItem).addClass(selected), activeItem) : null }, selectHint: function () { var that = this, i = $.inArray(this.hint, this.suggestions); this.select(i) }, select: function (i) { var that = this; this.hide(), this.onSelect(i) }, moveUp: function () { var that = this; if (-1 !== this.selectedIndex) return 0 === this.selectedIndex ? ($(this.suggestionsContainer).children("." + this.classes.suggestion).first().removeClass(this.classes.selected), this.selectedIndex = -1, this.ignoreValueChange = !1, this.el.val(this.currentValue), void this.findBestHint()) : void this.adjustScroll(this.selectedIndex - 1) }, moveDown: function () { var that = this; this.selectedIndex !== this.suggestions.length - 1 && this.adjustScroll(this.selectedIndex + 1) }, adjustScroll: function (index) { var that = this, activeItem = this.activate(index); if (activeItem) { var offsetTop, upperBound, lowerBound, heightDelta = $(activeItem).outerHeight(); offsetTop = activeItem.offsetTop, lowerBound = (upperBound = $(this.suggestionsContainer).scrollTop()) + this.options.maxHeight - heightDelta, offsetTop < upperBound ? $(this.suggestionsContainer).scrollTop(offsetTop) : offsetTop > lowerBound && $(this.suggestionsContainer).scrollTop(offsetTop - this.options.maxHeight + heightDelta), this.options.preserveInput || (this.ignoreValueChange = !0, this.el.val(this.getValue(this.suggestions[index].value))), this.signalHint(null) } }, onSelect: function (index) { var that = this, onSelectCallback = this.options.onSelect, suggestion = this.suggestions[index]; this.currentValue = this.getValue(suggestion.value), this.currentValue === this.el.val() || this.options.preserveInput || this.el.val(this.currentValue), this.signalHint(null), this.suggestions = [], this.selection = suggestion, $.isFunction(onSelectCallback) && onSelectCallback.call(this.element, suggestion) }, getValue: function (value) { var that = this, delimiter = this.options.delimiter, currentValue, parts; return delimiter ? 1 === (parts = (currentValue = this.currentValue).split(delimiter)).length ? value : currentValue.substr(0, currentValue.length - parts[parts.length - 1].length) + value : value }, dispose: function () { var that = this; this.el.off(".autocomplete").removeData("autocomplete"), $(window).off("resize.autocomplete", this.fixPositionCapture), $(this.suggestionsContainer).remove() } }, $.fn.devbridgeAutocomplete = function (options, args) { var dataKey = "autocomplete"; return arguments.length ? this.each(function () { var inputElement = $(this), instance = inputElement.data(dataKey); "string" == typeof options ? instance && "function" == typeof instance[options] && instance[options](args) : (instance && instance.dispose && instance.dispose(), instance = new Autocomplete(this, options), inputElement.data(dataKey, instance)) }) : this.first().data(dataKey) }, $.fn.autocomplete || ($.fn.autocomplete = $.fn.devbridgeAutocomplete) });