|
1 | 1 | /*!
|
2 |
| - * typeahead.js 0.9.2 |
| 2 | + * typeahead.js 0.9.3 |
3 | 3 | * https://github.com/twitter/typeahead
|
4 | 4 | * Copyright 2013 Twitter, Inc. and other contributors; Licensed MIT
|
5 | 5 | */
|
6 | 6 |
|
7 | 7 | (function($) {
|
8 |
| - var VERSION = "0.9.2"; |
| 8 | + var VERSION = "0.9.3"; |
9 | 9 | var utils = {
|
10 | 10 | isMsie: function() {
|
11 | 11 | var match = /(msie) ([\w.]+)/i.exec(navigator.userAgent);
|
|
190 | 190 | var ls, methods;
|
191 | 191 | try {
|
192 | 192 | ls = window.localStorage;
|
| 193 | + ls.setItem("~~~", "!"); |
| 194 | + ls.removeItem("~~~"); |
193 | 195 | } catch (err) {
|
194 | 196 | ls = null;
|
195 | 197 | }
|
|
744 | 746 | nextIndex = $suggestions.length - 1;
|
745 | 747 | }
|
746 | 748 | $underCursor = $suggestions.eq(nextIndex).addClass("tt-is-under-cursor");
|
| 749 | + this._ensureVisibility($underCursor); |
747 | 750 | this.trigger("cursorMoved", extractSuggestion($underCursor));
|
748 | 751 | },
|
749 | 752 | _getSuggestions: function() {
|
750 | 753 | return this.$menu.find(".tt-suggestions > .tt-suggestion");
|
751 | 754 | },
|
| 755 | + _ensureVisibility: function($el) { |
| 756 | + var menuHeight = this.$menu.height() + parseInt(this.$menu.css("paddingTop"), 10) + parseInt(this.$menu.css("paddingBottom"), 10), menuScrollTop = this.$menu.scrollTop(), elTop = $el.position().top, elBottom = elTop + $el.outerHeight(true); |
| 757 | + if (elTop < 0) { |
| 758 | + this.$menu.scrollTop(menuScrollTop + elTop); |
| 759 | + } else if (menuHeight < elBottom) { |
| 760 | + this.$menu.scrollTop(menuScrollTop + (elBottom - menuHeight)); |
| 761 | + } |
| 762 | + }, |
752 | 763 | destroy: function() {
|
753 | 764 | this.$menu.off(".tt");
|
754 | 765 | this.$menu = null;
|
|
764 | 775 | close: function() {
|
765 | 776 | if (this.isOpen) {
|
766 | 777 | this.isOpen = false;
|
| 778 | + this.isMouseOverDropdown = false; |
767 | 779 | this._hide();
|
768 | 780 | this.$menu.find(".tt-suggestions > .tt-suggestion").removeClass("tt-is-under-cursor");
|
769 | 781 | this.trigger("closed");
|
|
812 | 824 | elBuilder = document.createElement("div");
|
813 | 825 | fragment = document.createDocumentFragment();
|
814 | 826 | utils.each(suggestions, function(i, suggestion) {
|
| 827 | + suggestion.dataset = dataset.name; |
815 | 828 | compiledHtml = dataset.template(suggestion.datum);
|
816 | 829 | elBuilder.innerHTML = wrapper.replace("%body", compiledHtml);
|
817 | 830 | $el = $(elBuilder.firstChild).css(css.suggestion).data("suggestion", suggestion);
|
|
901 | 914 | this.inputView = new InputView({
|
902 | 915 | input: $input,
|
903 | 916 | hint: $hint
|
904 |
| - }).on("focused", this._openDropdown).on("blured", this._closeDropdown).on("blured", this._setInputValueToQuery).on("enterKeyed", this._handleSelection).on("queryChanged", this._clearHint).on("queryChanged", this._clearSuggestions).on("queryChanged", this._getSuggestions).on("whitespaceChanged", this._updateHint).on("queryChanged whitespaceChanged", this._openDropdown).on("queryChanged whitespaceChanged", this._setLanguageDirection).on("escKeyed", this._closeDropdown).on("escKeyed", this._setInputValueToQuery).on("tabKeyed upKeyed downKeyed", this._managePreventDefault).on("upKeyed downKeyed", this._moveDropdownCursor).on("upKeyed downKeyed", this._openDropdown).on("tabKeyed leftKeyed rightKeyed", this._autocomplete); |
| 917 | + }).on("focused", this._openDropdown).on("blured", this._closeDropdown).on("blured", this._setInputValueToQuery).on("enterKeyed tabKeyed", this._handleSelection).on("queryChanged", this._clearHint).on("queryChanged", this._clearSuggestions).on("queryChanged", this._getSuggestions).on("whitespaceChanged", this._updateHint).on("queryChanged whitespaceChanged", this._openDropdown).on("queryChanged whitespaceChanged", this._setLanguageDirection).on("escKeyed", this._closeDropdown).on("escKeyed", this._setInputValueToQuery).on("tabKeyed upKeyed downKeyed", this._managePreventDefault).on("upKeyed downKeyed", this._moveDropdownCursor).on("upKeyed downKeyed", this._openDropdown).on("tabKeyed leftKeyed rightKeyed", this._autocomplete); |
905 | 918 | }
|
906 | 919 | utils.mixin(TypeaheadView.prototype, EventTarget, {
|
907 | 920 | _managePreventDefault: function(e) {
|
|
970 | 983 | this.inputView.setInputValue(suggestion.value);
|
971 | 984 | byClick ? this.inputView.focus() : e.data.preventDefault();
|
972 | 985 | byClick && utils.isMsie() ? utils.defer(this.dropdownView.close) : this.dropdownView.close();
|
973 |
| - this.eventBus.trigger("selected", suggestion.datum); |
| 986 | + this.eventBus.trigger("selected", suggestion.datum, suggestion.dataset); |
974 | 987 | }
|
975 | 988 | },
|
976 | 989 | _getSuggestions: function() {
|
|
1000 | 1013 | if (hint !== "" && query !== hint) {
|
1001 | 1014 | suggestion = this.dropdownView.getFirstSuggestion();
|
1002 | 1015 | this.inputView.setInputValue(suggestion.value);
|
1003 |
| - this.eventBus.trigger("autocompleted", suggestion.datum); |
| 1016 | + this.eventBus.trigger("autocompleted", suggestion.datum, suggestion.dataset); |
1004 | 1017 | }
|
1005 | 1018 | },
|
1006 | 1019 | _propagateEvent: function(e) {
|
|
0 commit comments