/*! * typeahead.js 0.10.1 * https://github.com/twitter/typeahead.js * Copyright 2013 Twitter, Inc. and other contributors; Licensed MIT */ (function($) { var _ = { isMsie: function() { return /(msie|trident)/i.test(navigator.userAgent) ? navigator.userAgent.match(/(msie |rv:)(\d+(.\d+)?)/i)[2] : false; }, isBlankString: function(str) { return !str || /^\s*$/.test(str); }, escapeRegExChars: function(str) { return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); }, isString: function(obj) { return typeof obj === "string"; }, isNumber: function(obj) { return typeof obj === "number"; }, isArray: $.isArray, isFunction: $.isFunction, isObject: $.isPlainObject, isUndefined: function(obj) { return typeof obj === "undefined"; }, bind: $.proxy, each: function(collection, cb) { $.each(collection, reverseArgs); function reverseArgs(index, value) { return cb(value, index); } }, map: $.map, filter: $.grep, every: function(obj, test) { var result = true; if (!obj) { return result; } $.each(obj, function(key, val) { if (!(result = test.call(null, val, key, obj))) { return false; } }); return !!result; }, some: function(obj, test) { var result = false; if (!obj) { return result; } $.each(obj, function(key, val) { if (result = test.call(null, val, key, obj)) { return false; } }); return !!result; }, mixin: $.extend, getUniqueId: function() { var counter = 0; return function() { return counter++; }; }(), templatify: function templatify(obj) { return $.isFunction(obj) ? obj : template; function template() { return String(obj); } }, defer: function(fn) { setTimeout(fn, 0); }, debounce: function(func, wait, immediate) { var timeout, result; return function() { var context = this, args = arguments, later, callNow; later = function() { timeout = null; if (!immediate) { result = func.apply(context, args); } }; callNow = immediate && !timeout; clearTimeout(timeout); timeout = setTimeout(later, wait); if (callNow) { result = func.apply(context, args); } return result; }; }, throttle: function(func, wait) { var context, args, timeout, result, previous, later; previous = 0; later = function() { previous = new Date(); timeout = null; result = func.apply(context, args); }; return function() { var now = new Date(), remaining = wait - (now - previous); context = this; args = arguments; if (remaining <= 0) { clearTimeout(timeout); timeout = null; previous = now; result = func.apply(context, args); } else if (!timeout) { timeout = setTimeout(later, remaining); } return result; }; }, noop: function() {} }; var html = { wrapper: '', dropdown: '', dataset: '
', suggestions: '', suggestion: '" + displayFn(context) + "
"; } } function isValidName(str) { return /^[_a-zA-Z0-9-]+$/.test(str); } }(); var Dropdown = function() { function Dropdown(o) { var that = this, onSuggestionClick, onSuggestionMouseEnter, onSuggestionMouseLeave; o = o || {}; if (!o.menu) { $.error("menu is required"); } this.isOpen = false; this.isEmpty = true; this.datasets = _.map(o.datasets, initializeDataset); onSuggestionClick = _.bind(this._onSuggestionClick, this); onSuggestionMouseEnter = _.bind(this._onSuggestionMouseEnter, this); onSuggestionMouseLeave = _.bind(this._onSuggestionMouseLeave, this); this.$menu = $(o.menu).on("click.tt", ".tt-suggestion", onSuggestionClick).on("mouseenter.tt", ".tt-suggestion", onSuggestionMouseEnter).on("mouseleave.tt", ".tt-suggestion", onSuggestionMouseLeave); _.each(this.datasets, function(dataset) { that.$menu.append(dataset.getRoot()); dataset.onSync("rendered", that._onRendered, that); }); } _.mixin(Dropdown.prototype, EventEmitter, { _onSuggestionClick: function onSuggestionClick($e) { this.trigger("suggestionClicked", $($e.currentTarget)); }, _onSuggestionMouseEnter: function onSuggestionMouseEnter($e) { this._removeCursor(); this._setCursor($($e.currentTarget), true); }, _onSuggestionMouseLeave: function onSuggestionMouseLeave($e) { this._removeCursor(); }, _onRendered: function onRendered() { this.isEmpty = _.every(this.datasets, isDatasetEmpty); this.isEmpty ? this._hide() : this.isOpen && this._show(); this.trigger("datasetRendered"); function isDatasetEmpty(dataset) { return dataset.isEmpty(); } }, _hide: function() { this.$menu.hide(); }, _show: function() { this.$menu.css("display", "block"); }, _getSuggestions: function getSuggestions() { return this.$menu.find(".tt-suggestion"); }, _getCursor: function getCursor() { return this.$menu.find(".tt-cursor").first(); }, _setCursor: function setCursor($el, silent) { $el.first().addClass("tt-cursor"); !silent && this.trigger("cursorMoved"); }, _removeCursor: function removeCursor() { this._getCursor().removeClass("tt-cursor"); }, _moveCursor: function moveCursor(increment) { var $suggestions, $oldCursor, newCursorIndex, $newCursor; if (!this.isOpen) { return; } $oldCursor = this._getCursor(); $suggestions = this._getSuggestions(); this._removeCursor(); newCursorIndex = $suggestions.index($oldCursor) + increment; newCursorIndex = (newCursorIndex + 1) % ($suggestions.length + 1) - 1; if (newCursorIndex === -1) { this.trigger("cursorRemoved"); return; } else if (newCursorIndex < -1) { newCursorIndex = $suggestions.length - 1; } this._setCursor($newCursor = $suggestions.eq(newCursorIndex)); this._ensureVisible($newCursor); }, _ensureVisible: function ensureVisible($el) { var elTop, elBottom, menuScrollTop, menuHeight; elTop = $el.position().top; elBottom = elTop + $el.outerHeight(true); menuScrollTop = this.$menu.scrollTop(); menuHeight = this.$menu.height() + parseInt(this.$menu.css("paddingTop"), 10) + parseInt(this.$menu.css("paddingBottom"), 10); if (elTop < 0) { this.$menu.scrollTop(menuScrollTop + elTop); } else if (menuHeight < elBottom) { this.$menu.scrollTop(menuScrollTop + (elBottom - menuHeight)); } }, close: function close() { if (this.isOpen) { this.isOpen = false; this._removeCursor(); this._hide(); this.trigger("closed"); } }, open: function open() { if (!this.isOpen) { this.isOpen = true; !this.isEmpty && this._show(); this.trigger("opened"); } }, setLanguageDirection: function setLanguageDirection(dir) { this.$menu.css(dir === "ltr" ? css.ltr : css.rtl); }, moveCursorUp: function moveCursorUp() { this._moveCursor(-1); }, moveCursorDown: function moveCursorDown() { this._moveCursor(+1); }, getDatumForSuggestion: function getDatumForSuggestion($el) { var datum = null; if ($el.length) { datum = { raw: Dataset.extractDatum($el), value: Dataset.extractValue($el), datasetName: Dataset.extractDatasetName($el) }; } return datum; }, getDatumForCursor: function getDatumForCursor() { return this.getDatumForSuggestion(this._getCursor().first()); }, getDatumForTopSuggestion: function getDatumForTopSuggestion() { return this.getDatumForSuggestion(this._getSuggestions().first()); }, update: function update(query) { _.each(this.datasets, updateDataset); function updateDataset(dataset) { dataset.update(query); } }, empty: function empty() { _.each(this.datasets, clearDataset); this.isEmpty = true; function clearDataset(dataset) { dataset.clear(); } }, isVisible: function isVisible() { return this.isOpen && !this.isEmpty; }, destroy: function destroy() { this.$menu.off(".tt"); this.$menu = null; _.each(this.datasets, destroyDataset); function destroyDataset(dataset) { dataset.destroy(); } } }); return Dropdown; function initializeDataset(oDataset) { return new Dataset(oDataset); } }(); var Typeahead = function() { var attrsKey = "ttAttrs"; function Typeahead(o) { var $menu, $input, $hint, datasets; o = o || {}; if (!o.input) { $.error("missing input"); } this.autoselect = !!o.autoselect; this.minLength = _.isNumber(o.minLength) ? o.minLength : 1; this.$node = buildDomStructure(o.input, o.withHint); $menu = this.$node.find(".tt-dropdown-menu"); $input = this.$node.find(".tt-input"); $hint = this.$node.find(".tt-hint"); this.eventBus = o.eventBus || new EventBus({ el: $input }); this.dropdown = new Dropdown({ menu: $menu, datasets: o.datasets }).onSync("suggestionClicked", this._onSuggestionClicked, this).onSync("cursorMoved", this._onCursorMoved, this).onSync("cursorRemoved", this._onCursorRemoved, this).onSync("opened", this._onOpened, this).onSync("closed", this._onClosed, this).onAsync("datasetRendered", this._onDatasetRendered, this); this.input = new Input({ input: $input, hint: $hint }).onSync("focused", this._onFocused, this).onSync("blurred", this._onBlurred, this).onSync("enterKeyed", this._onEnterKeyed, this).onSync("tabKeyed", this._onTabKeyed, this).onSync("escKeyed", this._onEscKeyed, this).onSync("upKeyed", this._onUpKeyed, this).onSync("downKeyed", this._onDownKeyed, this).onSync("leftKeyed", this._onLeftKeyed, this).onSync("rightKeyed", this._onRightKeyed, this).onSync("queryChanged", this._onQueryChanged, this).onSync("whitespaceChanged", this._onWhitespaceChanged, this); $menu.on("mousedown.tt", function($e) { if (_.isMsie() && _.isMsie() < 9) { $input[0].onbeforedeactivate = function() { window.event.returnValue = false; $input[0].onbeforedeactivate = null; }; } $e.preventDefault(); }); } _.mixin(Typeahead.prototype, { _onSuggestionClicked: function onSuggestionClicked(type, $el) { var datum; if (datum = this.dropdown.getDatumForSuggestion($el)) { this._select(datum); } }, _onCursorMoved: function onCursorMoved() { var datum = this.dropdown.getDatumForCursor(); this.input.clearHint(); this.input.setInputValue(datum.value, true); this.eventBus.trigger("cursorchanged", datum.raw, datum.datasetName); }, _onCursorRemoved: function onCursorRemoved() { this.input.resetInputValue(); this._updateHint(); }, _onDatasetRendered: function onDatasetRendered() { this._updateHint(); }, _onOpened: function onOpened() { this._updateHint(); this.eventBus.trigger("opened"); }, _onClosed: function onClosed() { this.input.clearHint(); this.eventBus.trigger("closed"); }, _onFocused: function onFocused() { this.dropdown.empty(); this.dropdown.open(); }, _onBlurred: function onBlurred() { this.dropdown.close(); }, _onEnterKeyed: function onEnterKeyed(type, $e) { var cursorDatum, topSuggestionDatum; cursorDatum = this.dropdown.getDatumForCursor(); topSuggestionDatum = this.dropdown.getDatumForTopSuggestion(); if (cursorDatum) { this._select(cursorDatum); $e.preventDefault(); } else if (this.autoselect && topSuggestionDatum) { this._select(topSuggestionDatum); $e.preventDefault(); } }, _onTabKeyed: function onTabKeyed(type, $e) { var datum; if (datum = this.dropdown.getDatumForCursor()) { this._select(datum); $e.preventDefault(); } else { this._autocomplete(); } }, _onEscKeyed: function onEscKeyed() { this.dropdown.close(); this.input.resetInputValue(); }, _onUpKeyed: function onUpKeyed() { var query = this.input.getQuery(); if (!this.dropdown.isOpen && query.length >= this.minLength) { this.dropdown.update(query); } this.dropdown.open(); this.dropdown.moveCursorUp(); }, _onDownKeyed: function onDownKeyed() { var query = this.input.getQuery(); if (!this.dropdown.isOpen && query.length >= this.minLength) { this.dropdown.update(query); } this.dropdown.open(); this.dropdown.moveCursorDown(); }, _onLeftKeyed: function onLeftKeyed() { this.dir === "rtl" && this._autocomplete(); }, _onRightKeyed: function onRightKeyed() { this.dir === "ltr" && this._autocomplete(); }, _onQueryChanged: function onQueryChanged(e, query) { this.input.clearHint(); this.dropdown.empty(); query.length >= this.minLength && this.dropdown.update(query); this.dropdown.open(); this._setLanguageDirection(); }, _onWhitespaceChanged: function onWhitespaceChanged() { this._updateHint(); this.dropdown.open(); }, _setLanguageDirection: function setLanguageDirection() { var dir; if (this.dir !== (dir = this.input.getLanguageDirection())) { this.dir = dir; this.$node.css("direction", dir); this.dropdown.setLanguageDirection(dir); } }, _updateHint: function updateHint() { var datum, inputValue, query, escapedQuery, frontMatchRegEx, match; datum = this.dropdown.getDatumForTopSuggestion(); if (datum && this.dropdown.isVisible() && !this.input.hasOverflow()) { inputValue = this.input.getInputValue(); query = Input.normalizeQuery(inputValue); escapedQuery = _.escapeRegExChars(query); frontMatchRegEx = new RegExp("^(?:" + escapedQuery + ")(.*$)", "i"); match = frontMatchRegEx.exec(datum.value); this.input.setHintValue(inputValue + (match ? match[1] : "")); } }, _autocomplete: function autocomplete() { var hint, query, datum; hint = this.input.getHintValue(); query = this.input.getQuery(); if (hint && query !== hint && this.input.isCursorAtEnd()) { datum = this.dropdown.getDatumForTopSuggestion(); datum && this.input.setInputValue(datum.value); this.eventBus.trigger("autocompleted", datum.raw, datum.datasetName); } }, _select: function select(datum) { this.input.clearHint(); this.input.setQuery(datum.value); this.input.setInputValue(datum.value, true); this._setLanguageDirection(); this.eventBus.trigger("selected", datum.raw, datum.datasetName); this.dropdown.close(); _.defer(_.bind(this.dropdown.empty, this.dropdown)); }, open: function open() { this.dropdown.open(); }, close: function close() { this.dropdown.close(); }, getQuery: function getQuery() { return this.input.getQuery(); }, setQuery: function setQuery(val) { this.input.setInputValue(val); }, destroy: function destroy() { this.input.destroy(); this.dropdown.destroy(); destroyDomStructure(this.$node); this.$node = null; } }); return Typeahead; function buildDomStructure(input, withHint) { var $input, $wrapper, $dropdown, $hint; $input = $(input); $wrapper = $(html.wrapper).css(css.wrapper); $dropdown = $(html.dropdown).css(css.dropdown); $hint = $input.clone().css(css.hint).css(getBackgroundStyles($input)); $hint.val("").removeData().addClass("tt-hint").removeAttr("id name placeholder").prop("disabled", true).attr({ autocomplete: "off", spellcheck: "false" }); $input.data(attrsKey, { dir: $input.attr("dir"), autocomplete: $input.attr("autocomplete"), spellcheck: $input.attr("spellcheck"), style: $input.attr("style") }); $input.addClass("tt-input").attr({ autocomplete: "off", spellcheck: false }).css(withHint ? css.input : css.inputWithNoHint); try { !$input.attr("dir") && $input.attr("dir", "auto"); } catch (e) {} return $input.wrap($wrapper).parent().prepend(withHint ? $hint : null).append($dropdown); } function getBackgroundStyles($el) { return { backgroundAttachment: $el.css("background-attachment"), backgroundClip: $el.css("background-clip"), backgroundColor: $el.css("background-color"), backgroundImage: $el.css("background-image"), backgroundOrigin: $el.css("background-origin"), backgroundPosition: $el.css("background-position"), backgroundRepeat: $el.css("background-repeat"), backgroundSize: $el.css("background-size") }; } function destroyDomStructure($node) { var $input = $node.find(".tt-input"); _.each($input.data(attrsKey), function(val, key) { _.isUndefined(val) ? $input.removeAttr(key) : $input.attr(key, val); }); $input.detach().removeData(attrsKey).removeClass("tt-input").insertAfter($node); $node.remove(); } }(); (function() { var old, typeaheadKey, methods; old = $.fn.typeahead; typeaheadKey = "ttTypeahead"; methods = { initialize: function initialize(o, datasets) { datasets = _.isArray(datasets) ? datasets : [].slice.call(arguments, 1); o = o || {}; return this.each(attach); function attach() { var $input = $(this), eventBus, typeahead; _.each(datasets, function(d) { d.highlight = !!o.highlight; }); typeahead = new Typeahead({ input: $input, eventBus: eventBus = new EventBus({ el: $input }), withHint: _.isUndefined(o.hint) ? true : !!o.hint, minLength: o.minLength, autoselect: o.autoselect, datasets: datasets }); $input.data(typeaheadKey, typeahead); } }, open: function open() { return this.each(openTypeahead); function openTypeahead() { var $input = $(this), typeahead; if (typeahead = $input.data(typeaheadKey)) { typeahead.open(); } } }, close: function close() { return this.each(closeTypeahead); function closeTypeahead() { var $input = $(this), typeahead; if (typeahead = $input.data(typeaheadKey)) { typeahead.close(); } } }, val: function val(newVal) { return !arguments.length ? getQuery(this.first()) : this.each(setQuery); function setQuery() { var $input = $(this), typeahead; if (typeahead = $input.data(typeaheadKey)) { typeahead.setQuery(newVal); } } function getQuery($input) { var typeahead, query; if (typeahead = $input.data(typeaheadKey)) { query = typeahead.getQuery(); } return query; } }, destroy: function destroy() { return this.each(unattach); function unattach() { var $input = $(this), typeahead; if (typeahead = $input.data(typeaheadKey)) { typeahead.destroy(); $input.removeData(typeaheadKey); } } } }; $.fn.typeahead = function(method) { if (methods[method]) { return methods[method].apply(this, [].slice.call(arguments, 1)); } else { return methods.initialize.apply(this, arguments); } }; $.fn.typeahead.noConflict = function noConflict() { $.fn.typeahead = old; return this; }; })(); })(window.jQuery);