1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
|
// Copyright 2007 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview A combo box control that allows user input with
* auto-suggestion from a limited set of options.
*
* @see ../demos/combobox.html
*/
goog.provide('goog.ui.ComboBox');
goog.provide('goog.ui.ComboBoxItem');
goog.require('goog.Timer');
goog.require('goog.asserts');
goog.require('goog.dom');
goog.require('goog.dom.InputType');
goog.require('goog.dom.TagName');
goog.require('goog.dom.classlist');
goog.require('goog.events.EventType');
goog.require('goog.events.InputHandler');
goog.require('goog.events.KeyCodes');
goog.require('goog.events.KeyHandler');
goog.require('goog.log');
goog.require('goog.positioning.Corner');
goog.require('goog.positioning.MenuAnchoredPosition');
goog.require('goog.string');
goog.require('goog.style');
goog.require('goog.ui.Component');
goog.require('goog.ui.ItemEvent');
goog.require('goog.ui.LabelInput');
goog.require('goog.ui.Menu');
goog.require('goog.ui.MenuItem');
goog.require('goog.ui.MenuSeparator');
goog.require('goog.ui.registry');
goog.require('goog.userAgent');
/**
* A ComboBox control.
* @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
* @param {goog.ui.Menu=} opt_menu Optional menu component.
* This menu is disposed of by this control.
* @param {goog.ui.LabelInput=} opt_labelInput Optional label input.
* This label input is disposed of by this control.
* @extends {goog.ui.Component}
* @constructor
*/
goog.ui.ComboBox = function(opt_domHelper, opt_menu, opt_labelInput) {
goog.ui.Component.call(this, opt_domHelper);
this.labelInput_ = opt_labelInput || new goog.ui.LabelInput();
this.enabled_ = true;
// TODO(user): Allow lazy creation of menus/menu items
this.menu_ = opt_menu || new goog.ui.Menu(this.getDomHelper());
this.setupMenu_();
};
goog.inherits(goog.ui.ComboBox, goog.ui.Component);
goog.tagUnsealableClass(goog.ui.ComboBox);
/**
* Number of milliseconds to wait before dismissing combobox after blur.
* @type {number}
*/
goog.ui.ComboBox.BLUR_DISMISS_TIMER_MS = 250;
/**
* A logger to help debugging of combo box behavior.
* @type {goog.log.Logger}
* @private
*/
goog.ui.ComboBox.prototype.logger_ = goog.log.getLogger('goog.ui.ComboBox');
/**
* Whether the combo box is enabled.
* @type {boolean}
* @private
*/
goog.ui.ComboBox.prototype.enabled_;
/**
* Keyboard event handler to manage key events dispatched by the input element.
* @type {goog.events.KeyHandler}
* @private
*/
goog.ui.ComboBox.prototype.keyHandler_;
/**
* Input handler to take care of firing events when the user inputs text in
* the input.
* @type {goog.events.InputHandler?}
* @private
*/
goog.ui.ComboBox.prototype.inputHandler_ = null;
/**
* The last input token.
* @type {?string}
* @private
*/
goog.ui.ComboBox.prototype.lastToken_ = null;
/**
* A LabelInput control that manages the focus/blur state of the input box.
* @type {goog.ui.LabelInput?}
* @private
*/
goog.ui.ComboBox.prototype.labelInput_ = null;
/**
* Drop down menu for the combo box. Will be created at construction time.
* @type {goog.ui.Menu?}
* @private
*/
goog.ui.ComboBox.prototype.menu_ = null;
/**
* The cached visible count.
* @type {number}
* @private
*/
goog.ui.ComboBox.prototype.visibleCount_ = -1;
/**
* The input element.
* @type {Element}
* @private
*/
goog.ui.ComboBox.prototype.input_ = null;
/**
* The match function. The first argument for the match function will be
* a MenuItem's caption and the second will be the token to evaluate.
* @type {Function}
* @private
*/
goog.ui.ComboBox.prototype.matchFunction_ = goog.string.startsWith;
/**
* Element used as the combo boxes button.
* @type {Element}
* @private
*/
goog.ui.ComboBox.prototype.button_ = null;
/**
* Default text content for the input box when it is unchanged and unfocussed.
* @type {string}
* @private
*/
goog.ui.ComboBox.prototype.defaultText_ = '';
/**
* Name for the input box created
* @type {string}
* @private
*/
goog.ui.ComboBox.prototype.fieldName_ = '';
/**
* Timer identifier for delaying the dismissal of the combo menu.
* @type {?number}
* @private
*/
goog.ui.ComboBox.prototype.dismissTimer_ = null;
/**
* True if the unicode inverted triangle should be displayed in the dropdown
* button. Defaults to false.
* @type {boolean} useDropdownArrow
* @private
*/
goog.ui.ComboBox.prototype.useDropdownArrow_ = false;
/**
* Create the DOM objects needed for the combo box. A span and text input.
* @override
*/
goog.ui.ComboBox.prototype.createDom = function() {
this.input_ = this.getDomHelper().createDom(goog.dom.TagName.INPUT, {
name: this.fieldName_,
type: goog.dom.InputType.TEXT,
autocomplete: 'off'
});
this.button_ = this.getDomHelper().createDom(
goog.dom.TagName.SPAN, goog.getCssName('goog-combobox-button'));
this.setElementInternal(
this.getDomHelper().createDom(
goog.dom.TagName.SPAN, goog.getCssName('goog-combobox'), this.input_,
this.button_));
if (this.useDropdownArrow_) {
goog.dom.setTextContent(this.button_, '\u25BC');
goog.style.setUnselectable(this.button_, true /* unselectable */);
}
this.input_.setAttribute('label', this.defaultText_);
this.labelInput_.decorate(this.input_);
this.menu_.setFocusable(false);
if (!this.menu_.isInDocument()) {
this.addChild(this.menu_, true);
}
};
/**
* Enables/Disables the combo box.
* @param {boolean} enabled Whether to enable (true) or disable (false) the
* combo box.
*/
goog.ui.ComboBox.prototype.setEnabled = function(enabled) {
this.enabled_ = enabled;
this.labelInput_.setEnabled(enabled);
goog.dom.classlist.enable(
goog.asserts.assert(this.getElement()),
goog.getCssName('goog-combobox-disabled'), !enabled);
};
/**
* @return {boolean} Whether the menu item is enabled.
*/
goog.ui.ComboBox.prototype.isEnabled = function() {
return this.enabled_;
};
/** @override */
goog.ui.ComboBox.prototype.enterDocument = function() {
goog.ui.ComboBox.superClass_.enterDocument.call(this);
var handler = this.getHandler();
handler.listen(
this.getElement(), goog.events.EventType.MOUSEDOWN,
this.onComboMouseDown_);
handler.listen(
this.getDomHelper().getDocument(), goog.events.EventType.MOUSEDOWN,
this.onDocClicked_);
handler.listen(this.input_, goog.events.EventType.BLUR, this.onInputBlur_);
this.keyHandler_ = new goog.events.KeyHandler(this.input_);
handler.listen(
this.keyHandler_, goog.events.KeyHandler.EventType.KEY,
this.handleKeyEvent);
this.inputHandler_ = new goog.events.InputHandler(this.input_);
handler.listen(
this.inputHandler_, goog.events.InputHandler.EventType.INPUT,
this.onInputEvent_);
handler.listen(
this.menu_, goog.ui.Component.EventType.ACTION, this.onMenuSelected_);
};
/** @override */
goog.ui.ComboBox.prototype.exitDocument = function() {
this.keyHandler_.dispose();
delete this.keyHandler_;
this.inputHandler_.dispose();
this.inputHandler_ = null;
goog.ui.ComboBox.superClass_.exitDocument.call(this);
};
/**
* Combo box currently can't decorate elements.
* @return {boolean} The value false.
* @override
*/
goog.ui.ComboBox.prototype.canDecorate = function() {
return false;
};
/** @override */
goog.ui.ComboBox.prototype.disposeInternal = function() {
goog.ui.ComboBox.superClass_.disposeInternal.call(this);
this.clearDismissTimer_();
this.labelInput_.dispose();
this.menu_.dispose();
this.labelInput_ = null;
this.menu_ = null;
this.input_ = null;
this.button_ = null;
};
/**
* Dismisses the menu and resets the value of the edit field.
*/
goog.ui.ComboBox.prototype.dismiss = function() {
this.clearDismissTimer_();
this.hideMenu_();
this.menu_.setHighlightedIndex(-1);
};
/**
* Adds a new menu item at the end of the menu.
* @param {goog.ui.MenuItem} item Menu item to add to the menu.
*/
goog.ui.ComboBox.prototype.addItem = function(item) {
this.menu_.addChild(item, true);
this.visibleCount_ = -1;
};
/**
* Adds a new menu item at a specific index in the menu.
* @param {goog.ui.MenuItem} item Menu item to add to the menu.
* @param {number} n Index at which to insert the menu item.
*/
goog.ui.ComboBox.prototype.addItemAt = function(item, n) {
this.menu_.addChildAt(item, n, true);
this.visibleCount_ = -1;
};
/**
* Removes an item from the menu and disposes it.
* @param {goog.ui.MenuItem} item The menu item to remove.
*/
goog.ui.ComboBox.prototype.removeItem = function(item) {
var child = this.menu_.removeChild(item, true);
if (child) {
child.dispose();
this.visibleCount_ = -1;
}
};
/**
* Remove all of the items from the ComboBox menu
*/
goog.ui.ComboBox.prototype.removeAllItems = function() {
for (var i = this.getItemCount() - 1; i >= 0; --i) {
this.removeItem(this.getItemAt(i));
}
};
/**
* Removes a menu item at a given index in the menu.
* @param {number} n Index of item.
*/
goog.ui.ComboBox.prototype.removeItemAt = function(n) {
var child = this.menu_.removeChildAt(n, true);
if (child) {
child.dispose();
this.visibleCount_ = -1;
}
};
/**
* Returns a reference to the menu item at a given index.
* @param {number} n Index of menu item.
* @return {goog.ui.MenuItem?} Reference to the menu item.
*/
goog.ui.ComboBox.prototype.getItemAt = function(n) {
return /** @type {goog.ui.MenuItem?} */ (this.menu_.getChildAt(n));
};
/**
* Returns the number of items in the list, including non-visible items,
* such as separators.
* @return {number} Number of items in the menu for this combobox.
*/
goog.ui.ComboBox.prototype.getItemCount = function() {
return this.menu_.getChildCount();
};
/**
* @return {goog.ui.Menu} The menu that pops up.
*/
goog.ui.ComboBox.prototype.getMenu = function() {
return this.menu_;
};
/**
* @return {Element} The input element.
*/
goog.ui.ComboBox.prototype.getInputElement = function() {
return this.input_;
};
/**
* @return {goog.ui.LabelInput} A LabelInput control that manages the
* focus/blur state of the input box.
*/
goog.ui.ComboBox.prototype.getLabelInput = function() {
return this.labelInput_;
};
/**
* @return {number} The number of visible items in the menu.
* @private
*/
goog.ui.ComboBox.prototype.getNumberOfVisibleItems_ = function() {
if (this.visibleCount_ == -1) {
var count = 0;
for (var i = 0, n = this.menu_.getChildCount(); i < n; i++) {
var item = this.menu_.getChildAt(i);
if (!(item instanceof goog.ui.MenuSeparator) && item.isVisible()) {
count++;
}
}
this.visibleCount_ = count;
}
return this.visibleCount_;
};
/**
* Sets the match function to be used when filtering the combo box menu.
* @param {Function} matchFunction The match function to be used when filtering
* the combo box menu.
*/
goog.ui.ComboBox.prototype.setMatchFunction = function(matchFunction) {
this.matchFunction_ = matchFunction;
};
/**
* @return {Function} The match function for the combox box.
*/
goog.ui.ComboBox.prototype.getMatchFunction = function() {
return this.matchFunction_;
};
/**
* Sets the default text for the combo box.
* @param {string} text The default text for the combo box.
*/
goog.ui.ComboBox.prototype.setDefaultText = function(text) {
this.defaultText_ = text;
if (this.labelInput_) {
this.labelInput_.setLabel(this.defaultText_);
}
};
/**
* @return {string} text The default text for the combox box.
*/
goog.ui.ComboBox.prototype.getDefaultText = function() {
return this.defaultText_;
};
/**
* Sets the field name for the combo box.
* @param {string} fieldName The field name for the combo box.
*/
goog.ui.ComboBox.prototype.setFieldName = function(fieldName) {
this.fieldName_ = fieldName;
};
/**
* @return {string} The field name for the combo box.
*/
goog.ui.ComboBox.prototype.getFieldName = function() {
return this.fieldName_;
};
/**
* Set to true if a unicode inverted triangle should be displayed in the
* dropdown button.
* This option defaults to false for backwards compatibility.
* @param {boolean} useDropdownArrow True to use the dropdown arrow.
*/
goog.ui.ComboBox.prototype.setUseDropdownArrow = function(useDropdownArrow) {
this.useDropdownArrow_ = !!useDropdownArrow;
};
/**
* Sets the current value of the combo box.
* @param {string} value The new value.
*/
goog.ui.ComboBox.prototype.setValue = function(value) {
if (this.labelInput_.getValue() != value) {
this.labelInput_.setValue(value);
this.handleInputChange_();
}
};
/**
* @return {string} The current value of the combo box.
*/
goog.ui.ComboBox.prototype.getValue = function() {
return this.labelInput_.getValue();
};
/**
* @return {string} HTML escaped token.
*/
goog.ui.ComboBox.prototype.getToken = function() {
// TODO(user): Remove HTML escaping and fix the existing calls.
return goog.string.htmlEscape(this.getTokenText_());
};
/**
* @return {string} The token for the current cursor position in the
* input box, when multi-input is disabled it will be the full input value.
* @private
*/
goog.ui.ComboBox.prototype.getTokenText_ = function() {
// TODO(user): Implement multi-input such that getToken returns a substring
// of the whole input delimited by commas.
return goog.string.trim(this.labelInput_.getValue().toLowerCase());
};
/**
* @private
*/
goog.ui.ComboBox.prototype.setupMenu_ = function() {
var sm = this.menu_;
sm.setVisible(false);
sm.setAllowAutoFocus(false);
sm.setAllowHighlightDisabled(true);
};
/**
* Shows the menu if it isn't already showing. Also positions the menu
* correctly, resets the menu item visibilities and highlights the relevant
* item.
* @param {boolean} showAll Whether to show all items, with the first matching
* item highlighted.
* @private
*/
goog.ui.ComboBox.prototype.maybeShowMenu_ = function(showAll) {
var isVisible = this.menu_.isVisible();
var numVisibleItems = this.getNumberOfVisibleItems_();
if (isVisible && numVisibleItems == 0) {
goog.log.fine(this.logger_, 'no matching items, hiding');
this.hideMenu_();
} else if (!isVisible && numVisibleItems > 0) {
if (showAll) {
goog.log.fine(this.logger_, 'showing menu');
this.setItemVisibilityFromToken_('');
this.setItemHighlightFromToken_(this.getTokenText_());
}
// In Safari 2.0, when clicking on the combox box, the blur event is
// received after the click event that invokes this function. Since we want
// to cancel the dismissal after the blur event is processed, we have to
// wait for all event processing to happen.
goog.Timer.callOnce(this.clearDismissTimer_, 1, this);
this.showMenu_();
}
this.positionMenu();
};
/**
* Positions the menu.
* @protected
*/
goog.ui.ComboBox.prototype.positionMenu = function() {
if (this.menu_ && this.menu_.isVisible()) {
var position = new goog.positioning.MenuAnchoredPosition(
this.getElement(), goog.positioning.Corner.BOTTOM_START, true);
position.reposition(
this.menu_.getElement(), goog.positioning.Corner.TOP_START);
}
};
/**
* Show the menu and add an active class to the combo box's element.
* @private
*/
goog.ui.ComboBox.prototype.showMenu_ = function() {
this.menu_.setVisible(true);
goog.dom.classlist.add(
goog.asserts.assert(this.getElement()),
goog.getCssName('goog-combobox-active'));
};
/**
* Hide the menu and remove the active class from the combo box's element.
* @private
*/
goog.ui.ComboBox.prototype.hideMenu_ = function() {
this.menu_.setVisible(false);
goog.dom.classlist.remove(
goog.asserts.assert(this.getElement()),
goog.getCssName('goog-combobox-active'));
};
/**
* Clears the dismiss timer if it's active.
* @private
*/
goog.ui.ComboBox.prototype.clearDismissTimer_ = function() {
if (this.dismissTimer_) {
goog.Timer.clear(this.dismissTimer_);
this.dismissTimer_ = null;
}
};
/**
* Event handler for when the combo box area has been clicked.
* @param {goog.events.BrowserEvent} e The browser event.
* @private
*/
goog.ui.ComboBox.prototype.onComboMouseDown_ = function(e) {
// We only want this event on the element itself or the input or the button.
if (this.enabled_ &&
(e.target == this.getElement() || e.target == this.input_ ||
goog.dom.contains(this.button_, /** @type {Node} */ (e.target)))) {
if (this.menu_.isVisible()) {
goog.log.fine(this.logger_, 'Menu is visible, dismissing');
this.dismiss();
} else {
goog.log.fine(this.logger_, 'Opening dropdown');
this.maybeShowMenu_(true);
if (goog.userAgent.OPERA) {
// select() doesn't focus <input> elements in Opera.
this.input_.focus();
}
this.input_.select();
this.menu_.setMouseButtonPressed(true);
// Stop the click event from stealing focus
e.preventDefault();
}
}
// Stop the event from propagating outside of the combo box
e.stopPropagation();
};
/**
* Event handler for when the document is clicked.
* @param {goog.events.BrowserEvent} e The browser event.
* @private
*/
goog.ui.ComboBox.prototype.onDocClicked_ = function(e) {
if (!goog.dom.contains(
this.menu_.getElement(), /** @type {Node} */ (e.target))) {
this.dismiss();
}
};
/**
* Handle the menu's select event.
* @param {goog.events.Event} e The event.
* @private
*/
goog.ui.ComboBox.prototype.onMenuSelected_ = function(e) {
var item = /** @type {!goog.ui.MenuItem} */ (e.target);
// Stop propagation of the original event and redispatch to allow the menu
// select to be cancelled at this level. i.e. if a menu item should cause
// some behavior such as a user prompt instead of assigning the caption as
// the value.
if (this.dispatchEvent(
new goog.ui.ItemEvent(
goog.ui.Component.EventType.ACTION, this, item))) {
var caption = item.getCaption();
goog.log.fine(
this.logger_, 'Menu selection: ' + caption + '. Dismissing menu');
if (this.labelInput_.getValue() != caption) {
this.labelInput_.setValue(caption);
this.dispatchEvent(goog.ui.Component.EventType.CHANGE);
}
this.dismiss();
}
e.stopPropagation();
};
/**
* Event handler for when the input box looses focus -- hide the menu
* @param {goog.events.BrowserEvent} e The browser event.
* @private
*/
goog.ui.ComboBox.prototype.onInputBlur_ = function(e) {
this.clearDismissTimer_();
this.dismissTimer_ = goog.Timer.callOnce(
this.dismiss, goog.ui.ComboBox.BLUR_DISMISS_TIMER_MS, this);
};
/**
* Handles keyboard events from the input box. Returns true if the combo box
* was able to handle the event, false otherwise.
* @param {goog.events.KeyEvent} e Key event to handle.
* @return {boolean} Whether the event was handled by the combo box.
* @protected
* @suppress {visibility} performActionInternal
*/
goog.ui.ComboBox.prototype.handleKeyEvent = function(e) {
var isMenuVisible = this.menu_.isVisible();
// Give the menu a chance to handle the event.
if (isMenuVisible && this.menu_.handleKeyEvent(e)) {
return true;
}
// The menu is either hidden or didn't handle the event.
var handled = false;
switch (e.keyCode) {
case goog.events.KeyCodes.ESC:
// If the menu is visible and the user hit Esc, dismiss the menu.
if (isMenuVisible) {
goog.log.fine(
this.logger_, 'Dismiss on Esc: ' + this.labelInput_.getValue());
this.dismiss();
handled = true;
}
break;
case goog.events.KeyCodes.TAB:
// If the menu is open and an option is highlighted, activate it.
if (isMenuVisible) {
var highlighted = this.menu_.getHighlighted();
if (highlighted) {
goog.log.fine(
this.logger_, 'Select on Tab: ' + this.labelInput_.getValue());
highlighted.performActionInternal(e);
handled = true;
}
}
break;
case goog.events.KeyCodes.UP:
case goog.events.KeyCodes.DOWN:
// If the menu is hidden and the user hit the up/down arrow, show it.
if (!isMenuVisible) {
goog.log.fine(this.logger_, 'Up/Down - maybe show menu');
this.maybeShowMenu_(true);
handled = true;
}
break;
}
if (handled) {
e.preventDefault();
}
return handled;
};
/**
* Handles the content of the input box changing.
* @param {goog.events.Event} e The INPUT event to handle.
* @private
*/
goog.ui.ComboBox.prototype.onInputEvent_ = function(e) {
// If the key event is text-modifying, update the menu.
goog.log.fine(
this.logger_, 'Key is modifying: ' + this.labelInput_.getValue());
this.handleInputChange_();
};
/**
* Handles the content of the input box changing, either because of user
* interaction or programmatic changes.
* @private
*/
goog.ui.ComboBox.prototype.handleInputChange_ = function() {
var token = this.getTokenText_();
this.setItemVisibilityFromToken_(token);
if (goog.dom.getActiveElement(this.getDomHelper().getDocument()) ==
this.input_) {
// Do not alter menu visibility unless the user focus is currently on the
// combobox (otherwise programmatic changes may cause the menu to become
// visible).
this.maybeShowMenu_(false);
}
var highlighted = this.menu_.getHighlighted();
if (token == '' || !highlighted || !highlighted.isVisible()) {
this.setItemHighlightFromToken_(token);
}
this.lastToken_ = token;
this.dispatchEvent(goog.ui.Component.EventType.CHANGE);
};
/**
* Loops through all menu items setting their visibility according to a token.
* @param {string} token The token.
* @private
*/
goog.ui.ComboBox.prototype.setItemVisibilityFromToken_ = function(token) {
var isVisibleItem = false;
var count = 0;
var recheckHidden = !this.matchFunction_(token, this.lastToken_);
for (var i = 0, n = this.menu_.getChildCount(); i < n; i++) {
var item = this.menu_.getChildAt(i);
if (item instanceof goog.ui.MenuSeparator) {
// Ensure that separators are only shown if there is at least one visible
// item before them.
item.setVisible(isVisibleItem);
isVisibleItem = false;
} else if (item instanceof goog.ui.MenuItem) {
if (!item.isVisible() && !recheckHidden) continue;
var caption = item.getCaption();
var visible = this.isItemSticky_(item) ||
caption && this.matchFunction_(caption.toLowerCase(), token);
if (typeof item.setFormatFromToken == 'function') {
item.setFormatFromToken(token);
}
item.setVisible(!!visible);
isVisibleItem = visible || isVisibleItem;
} else {
// Assume all other items are correctly using their visibility.
isVisibleItem = item.isVisible() || isVisibleItem;
}
if (!(item instanceof goog.ui.MenuSeparator) && item.isVisible()) {
count++;
}
}
this.visibleCount_ = count;
};
/**
* Highlights the first token that matches the given token.
* @param {string} token The token.
* @private
*/
goog.ui.ComboBox.prototype.setItemHighlightFromToken_ = function(token) {
if (token == '') {
this.menu_.setHighlightedIndex(-1);
return;
}
for (var i = 0, n = this.menu_.getChildCount(); i < n; i++) {
var item = this.menu_.getChildAt(i);
var caption = item.getCaption();
if (caption && this.matchFunction_(caption.toLowerCase(), token)) {
this.menu_.setHighlightedIndex(i);
if (item.setFormatFromToken) {
item.setFormatFromToken(token);
}
return;
}
}
this.menu_.setHighlightedIndex(-1);
};
/**
* Returns true if the item has an isSticky method and the method returns true.
* @param {goog.ui.MenuItem} item The item.
* @return {boolean} Whether the item has an isSticky method and the method
* returns true.
* @private
*/
goog.ui.ComboBox.prototype.isItemSticky_ = function(item) {
return typeof item.isSticky == 'function' && item.isSticky();
};
/**
* Class for combo box items.
* @param {goog.ui.ControlContent} content Text caption or DOM structure to
* display as the content of the item (use to add icons or styling to
* menus).
* @param {*=} opt_data Identifying data for the menu item.
* @param {goog.dom.DomHelper=} opt_domHelper Optional dom helper used for dom
* interactions.
* @param {goog.ui.MenuItemRenderer=} opt_renderer Optional renderer.
* @constructor
* @extends {goog.ui.MenuItem}
*/
goog.ui.ComboBoxItem = function(
content, opt_data, opt_domHelper, opt_renderer) {
goog.ui.ComboBoxItem.base(
this, 'constructor', content, opt_data, opt_domHelper, opt_renderer);
};
goog.inherits(goog.ui.ComboBoxItem, goog.ui.MenuItem);
goog.tagUnsealableClass(goog.ui.ComboBoxItem);
// Register a decorator factory function for goog.ui.ComboBoxItems.
goog.ui.registry.setDecoratorByClassName(
goog.getCssName('goog-combobox-item'), function() {
// ComboBoxItem defaults to using MenuItemRenderer.
return new goog.ui.ComboBoxItem(null);
});
/**
* Whether the menu item is sticky, non-sticky items will be hidden as the
* user types.
* @type {boolean}
* @private
*/
goog.ui.ComboBoxItem.prototype.isSticky_ = false;
/**
* Sets the menu item to be sticky or not sticky.
* @param {boolean} sticky Whether the menu item should be sticky.
*/
goog.ui.ComboBoxItem.prototype.setSticky = function(sticky) {
this.isSticky_ = sticky;
};
/**
* @return {boolean} Whether the menu item is sticky.
*/
goog.ui.ComboBoxItem.prototype.isSticky = function() {
return this.isSticky_;
};
/**
* Sets the format for a menu item based on a token, bolding the token.
* @param {string} token The token.
*/
goog.ui.ComboBoxItem.prototype.setFormatFromToken = function(token) {
if (this.isEnabled()) {
var caption = this.getCaption();
var index = caption.toLowerCase().indexOf(token);
if (index >= 0) {
var domHelper = this.getDomHelper();
this.setContent([
domHelper.createTextNode(caption.substr(0, index)),
domHelper.createDom(
goog.dom.TagName.B, null, caption.substr(index, token.length)),
domHelper.createTextNode(caption.substr(index + token.length))
]);
}
}
};
|