📄 autocomplete.js
字号:
foundMatch = true; break; } } return(foundMatch);};/** * Updates in the text input box with the first query result as the user types, * selecting the substring that the user has not typed. * * @method _typeAhead * @param oItem {HTMLElement} The <li> element item whose data populates the input field. * @param sQuery {String} Query string. * @private */YAHOO.widget.AutoComplete.prototype._typeAhead = function(oItem, sQuery) { // Don't update if turned off if(!this.typeAhead || (this._nKeyCode == 8)) { return; } var oTextbox = this._oTextbox; var sValue = this._oTextbox.value; // any saved queries plus what user has typed // Don't update with type-ahead if text selection is not supported if(!oTextbox.setSelectionRange && !oTextbox.createTextRange) { return; } // Select the portion of text that the user has not typed var nStart = sValue.length; this._updateValue(oItem); var nEnd = oTextbox.value.length; this._selectText(oTextbox,nStart,nEnd); var sPrefill = oTextbox.value.substr(nStart,nEnd); this.typeAheadEvent.fire(this,sQuery,sPrefill);};/** * Selects text in the input field. * * @method _selectText * @param oTextbox {HTMLElement} Text input box element in which to select text. * @param nStart {Number} Starting index of text string to select. * @param nEnd {Number} Ending index of text selection. * @private */YAHOO.widget.AutoComplete.prototype._selectText = function(oTextbox, nStart, nEnd) { if(oTextbox.setSelectionRange) { // For Mozilla oTextbox.setSelectionRange(nStart,nEnd); } else if(oTextbox.createTextRange) { // For IE var oTextRange = oTextbox.createTextRange(); oTextRange.moveStart("character", nStart); oTextRange.moveEnd("character", nEnd-oTextbox.value.length); oTextRange.select(); } else { oTextbox.select(); }};/** * Syncs results container with its helpers. * * @method _toggleContainerHelpers * @param bShow {Boolean} True if container is expanded, false if collapsed * @private */YAHOO.widget.AutoComplete.prototype._toggleContainerHelpers = function(bShow) { var bFireEvent = false; var width = this._oContainer._oContent.offsetWidth + "px"; var height = this._oContainer._oContent.offsetHeight + "px"; if(this.useIFrame && this._oContainer._oIFrame) { bFireEvent = true; if(bShow) { this._oContainer._oIFrame.style.width = width; this._oContainer._oIFrame.style.height = height; } else { this._oContainer._oIFrame.style.width = 0; this._oContainer._oIFrame.style.height = 0; } } if(this.useShadow && this._oContainer._oShadow) { bFireEvent = true; if(bShow) { this._oContainer._oShadow.style.width = width; this._oContainer._oShadow.style.height = height; } else { this._oContainer._oShadow.style.width = 0; this._oContainer._oShadow.style.height = 0; } }};/** * Animates expansion or collapse of the container. * * @method _toggleContainer * @param bShow {Boolean} True if container should be expanded, false if container should be collapsed * @private */YAHOO.widget.AutoComplete.prototype._toggleContainer = function(bShow) { var oContainer = this._oContainer; // Implementer has container always open so don't mess with it if(this.alwaysShowContainer && this._bContainerOpen) { return; } // Clear contents of container if(!bShow) { this._oContainer._oContent.scrollTop = 0; var aItems = this._aListItems; if(aItems && (aItems.length > 0)) { for(var i = aItems.length-1; i >= 0 ; i--) { aItems[i].style.display = "none"; } } if(this._oCurItem) { this._toggleHighlight(this._oCurItem,"from"); } this._oCurItem = null; this._nDisplayedItems = 0; this._sCurQuery = null; } // Container is already closed if(!bShow && !this._bContainerOpen) { oContainer._oContent.style.display = "none"; return; } // If animation is enabled... var oAnim = this._oAnim; if(oAnim && oAnim.getEl() && (this.animHoriz || this.animVert)) { // If helpers need to be collapsed, do it right away... // but if helpers need to be expanded, wait until after the container expands if(!bShow) { this._toggleContainerHelpers(bShow); } if(oAnim.isAnimated()) { oAnim.stop(); } // Clone container to grab current size offscreen var oClone = oContainer._oContent.cloneNode(true); oContainer.appendChild(oClone); oClone.style.top = "-9000px"; oClone.style.display = "block"; // Current size of the container is the EXPANDED size var wExp = oClone.offsetWidth; var hExp = oClone.offsetHeight; // Calculate COLLAPSED sizes based on horiz and vert anim var wColl = (this.animHoriz) ? 0 : wExp; var hColl = (this.animVert) ? 0 : hExp; // Set animation sizes oAnim.attributes = (bShow) ? {width: { to: wExp }, height: { to: hExp }} : {width: { to: wColl}, height: { to: hColl }}; // If opening anew, set to a collapsed size... if(bShow && !this._bContainerOpen) { oContainer._oContent.style.width = wColl+"px"; oContainer._oContent.style.height = hColl+"px"; } // Else, set it to its last known size. else { oContainer._oContent.style.width = wExp+"px"; oContainer._oContent.style.height = hExp+"px"; } oContainer.removeChild(oClone); oClone = null; var oSelf = this; var onAnimComplete = function() { // Finish the collapse oAnim.onComplete.unsubscribeAll(); if(bShow) { oSelf.containerExpandEvent.fire(oSelf); } else { oContainer._oContent.style.display = "none"; oSelf.containerCollapseEvent.fire(oSelf); } oSelf._toggleContainerHelpers(bShow); }; // Display container and animate it oContainer._oContent.style.display = "block"; oAnim.onComplete.subscribe(onAnimComplete); oAnim.animate(); this._bContainerOpen = bShow; } // Else don't animate, just show or hide else { if(bShow) { oContainer._oContent.style.display = "block"; this.containerExpandEvent.fire(this); } else { oContainer._oContent.style.display = "none"; this.containerCollapseEvent.fire(this); } this._toggleContainerHelpers(bShow); this._bContainerOpen = bShow; }};/** * Toggles the highlight on or off for an item in the container, and also cleans * up highlighting of any previous item. * * @method _toggleHighlight * @param oNewItem {HTMLElement} The <li> element item to receive highlight behavior. * @param sType {String} Type "mouseover" will toggle highlight on, and "mouseout" will toggle highlight off. * @private */YAHOO.widget.AutoComplete.prototype._toggleHighlight = function(oNewItem, sType) { var sHighlight = this.highlightClassName; if(this._oCurItem) { // Remove highlight from old item YAHOO.util.Dom.removeClass(this._oCurItem, sHighlight); } if((sType == "to") && sHighlight) { // Apply highlight to new item YAHOO.util.Dom.addClass(oNewItem, sHighlight); this._oCurItem = oNewItem; }};/** * Toggles the pre-highlight on or off for an item in the container. * * @method _togglePrehighlight * @param oNewItem {HTMLElement} The <li> element item to receive highlight behavior. * @param sType {String} Type "mouseover" will toggle highlight on, and "mouseout" will toggle highlight off. * @private */YAHOO.widget.AutoComplete.prototype._togglePrehighlight = function(oNewItem, sType) { if(oNewItem == this._oCurItem) { return; } var sPrehighlight = this.prehighlightClassName; if((sType == "mouseover") && sPrehighlight) { // Apply prehighlight to new item YAHOO.util.Dom.addClass(oNewItem, sPrehighlight); } else { // Remove prehighlight from old item YAHOO.util.Dom.removeClass(oNewItem, sPrehighlight); }};/** * Updates the text input box value with selected query result. If a delimiter * has been defined, then the value gets appended with the delimiter. * * @method _updateValue * @param oItem {HTMLElement} The <li> element item with which to update the value. * @private */YAHOO.widget.AutoComplete.prototype._updateValue = function(oItem) { var oTextbox = this._oTextbox; var sDelimChar = (this.delimChar) ? (this.delimChar[0] || this.delimChar) : null; var sSavedQuery = this._sSavedQuery; var sResultKey = oItem._sResultKey; oTextbox.focus(); // First clear text field oTextbox.value = ""; // Grab data to put into text field if(sDelimChar) { if(sSavedQuery) { oTextbox.value = sSavedQuery; } oTextbox.value += sResultKey + sDelimChar; if(sDelimChar != " ") { oTextbox.value += " "; } } else { oTextbox.value = sResultKey; } // scroll to bottom of textarea if necessary if(oTextbox.type == "textarea") { oTextbox.scrollTop = oTextbox.scrollHeight; } // move cursor to end var end = oTextbox.value.length; this._selectText(oTextbox,end,end); this._oCurItem = oItem;};/** * Selects a result item from the container * * @method _selectItem * @param oItem {HTMLElement} The selected <li> element item. * @private */YAHOO.widget.AutoComplete.prototype._selectItem = function(oItem) { this._bItemSelected = true; this._updateValue(oItem); this._cancelIntervalDetection(this); this.itemSelectEvent.fire(this, oItem, oItem._oResultData); this._toggleContainer(false);};/** * For values updated by type-ahead, the right arrow key jumps to the end * of the textbox, otherwise the container is closed. * * @method _jumpSelection * @private */YAHOO.widget.AutoComplete.prototype._jumpSelection = function() { if(!this.typeAhead) { return; } else { this._toggleContainer(false); }};/** * Triggered by up and down arrow keys, changes the current highlighted * <li> element item. Scrolls container if necessary. * * @method _moveSelection * @param nKeyCode {Number} Code of key pressed. * @private */YAHOO.widget.AutoComplete.prototype._moveSelection = function(nKeyCode) { if(this._bContainerOpen) { // Determine current item's id number var oCurItem = this._oCurItem; var nCurItemIndex = -1; if(oCurItem) { nCurItemIndex = oCurItem._nItemIndex; } var nNewItemIndex = (nKeyCode == 40) ? (nCurItemIndex + 1) : (nCurItemIndex - 1); // Out of bounds if(nNewItemIndex < -2 || nNewItemIndex >= this._nDisplayedItems) { return; } if(oCurItem) { // Unhighlight current item this._toggleHighlight(oCurItem, "from"); this.itemArrowFromEvent.fire(this, oCurItem); } if(nNewItemIndex == -1) { // Go back to query (remove type-ahead string) if(this.delimChar && this._sSavedQuery) { if(!this._textMatchesOption()) { this._oTextbox.value = this._sSavedQuery; } else { this._oTextbox.value = this._sSavedQuery + this._sCurQuery; } } else { this._oTextbox.value = this._sCurQuery; } this._oCurItem = null; return; } if(nNewItemIndex == -2) { // Close container this._toggleContainer(false); return; } var oNewItem = this._aListItems[nNewItemIndex]; // Scroll the container if necessary var oContent = this._oContainer._oContent; var scrollOn = ((YAHOO.util.Dom.getStyle(oContent,"overflow") == "auto") || (YAHOO.util.Dom.getStyle(oContent,"overflowY") == "auto")); if(scrollOn && (nNewItemIndex > -1) && (nNewItemIndex < this._nDisplayedItems)) { // User is keying down if(nKeyCode == 40) { // Bottom of selected item is below scroll area... if((oNewItem.offsetTop+oNewItem.offsetHeight) > (oContent.scrollTop + oContent.offsetHeight)) { // Set bottom of scroll area to bottom of selected item oContent.scrollTop = (oNewItem.offsetTop+oNewItem.offsetHeight) - oContent.offsetHeight; } // Bottom of selected item is above scroll area... else if((oNewItem.offsetTop+oNewItem.offsetHeight) < oContent.scrollTop) { // Set top of selected item to top of scroll area oContent.scrollTop = oNewItem.offsetTop; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -