⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 autocomplete-debug.js

📁 这是YUI的源码及相关示例。里面有很多很炫的Javascript效果。
💻 JS
📖 第 1 页 / 共 5 页
字号:
        var elFooter = document.createElement("div");        elFooter.className = "yui-ac-ft";        elFooter.style.display = "none";        this._elFooter = this._elContent.appendChild(elFooter);    }    else {        YAHOO.log("Could not initialize the container","warn",this.toString());    }};/** * Clears out contents of container body and creates up to * YAHOO.widget.AutoComplete#maxResultsDisplayed &lt;li&gt; elements in an * &lt;ul&gt; element. * * @method _initListEl * @private */YAHOO.widget.AutoComplete.prototype._initListEl = function() {    var nListLength = this.maxResultsDisplayed;        var elList = this._elList || document.createElement("ul");    var elListItem;    while(elList.childNodes.length < nListLength) {        elListItem = document.createElement("li");        elListItem.style.display = "none";        elListItem._nItemIndex = elList.childNodes.length;        elList.appendChild(elListItem);    }    if(!this._elList) {        var elBody = this._elBody;        YAHOO.util.Event.purgeElement(elBody, true);        elBody.innerHTML = "";        this._elList = elBody.appendChild(elList);    }    };/** * Enables interval detection for IME support. * * @method _enableIntervalDetection * @re  * @private */YAHOO.widget.AutoComplete.prototype._enableIntervalDetection = function() {    var oSelf = this;    if(!oSelf._queryInterval && oSelf.queryInterval) {        oSelf._queryInterval = setInterval(function() { oSelf._onInterval(); }, oSelf.queryInterval);        YAHOO.log("Interval set", "info", this.toString());    }};/** * Enables query triggers based on text input detection by intervals (rather * than by key events). * * @method _onInterval * @private */YAHOO.widget.AutoComplete.prototype._onInterval = function() {    var currValue = this._elTextbox.value;    var lastValue = this._sLastTextboxValue;    if(currValue != lastValue) {        this._sLastTextboxValue = currValue;        this._sendQuery(currValue);    }};/** * Cancels text input detection by intervals. * * @method _clearInterval * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance. * @private */YAHOO.widget.AutoComplete.prototype._clearInterval = function() {    if(this._queryInterval) {        clearInterval(this._queryInterval);        this._queryInterval = null;        YAHOO.log("Interval cleared", "info", this.toString());    }};/** * Whether or not key is functional or should be ignored. Note that the right * arrow key is NOT an ignored key since it triggers queries for certain intl * charsets. * * @method _isIgnoreKey * @param nKeycode {Number} Code of key pressed. * @return {Boolean} True if key should be ignored, false otherwise. * @private */YAHOO.widget.AutoComplete.prototype._isIgnoreKey = function(nKeyCode) {    if((nKeyCode == 9) || (nKeyCode == 13)  || // tab, enter            (nKeyCode == 16) || (nKeyCode == 17) || // shift, ctl            (nKeyCode >= 18 && nKeyCode <= 20) || // alt, pause/break,caps lock            (nKeyCode == 27) || // esc            (nKeyCode >= 33 && nKeyCode <= 35) || // page up,page down,end            /*(nKeyCode >= 36 && nKeyCode <= 38) || // home,left,up            (nKeyCode == 40) || // down*/            (nKeyCode >= 36 && nKeyCode <= 40) || // home,left,up, right, down            (nKeyCode >= 44 && nKeyCode <= 45) || // print screen,insert            (nKeyCode == 229) // Bug 2041973: Korean XP fires 2 keyup events, the key and 229        ) {         return true;    }    return false;};/** * Makes query request to the DataSource. * * @method _sendQuery * @param sQuery {String} Query string. * @private */YAHOO.widget.AutoComplete.prototype._sendQuery = function(sQuery) {    // Widget has been effectively turned off    if(this.minQueryLength < 0) {        this._toggleContainer(false);        YAHOO.log("Property minQueryLength is less than 0", "info", this.toString());        return;    }    // Delimiter has been enabled    var aDelimChar = (this.delimChar) ? this.delimChar : null;    if(aDelimChar) {        // Loop through all possible delimiters and find the rightmost one in the query        // A " " may be a false positive if they are defined as delimiters AND        // are used to separate delimited queries        var nDelimIndex = -1;        for(var i = aDelimChar.length-1; i >= 0; i--) {            var nNewIndex = sQuery.lastIndexOf(aDelimChar[i]);            if(nNewIndex > nDelimIndex) {                nDelimIndex = nNewIndex;            }        }        // If we think the last delimiter is a space (" "), make sure it is NOT        // a false positive by also checking the char directly before it        if(aDelimChar[i] == " ") {            for (var j = aDelimChar.length-1; j >= 0; j--) {                if(sQuery[nDelimIndex - 1] == aDelimChar[j]) {                    nDelimIndex--;                    break;                }            }        }        // A delimiter has been found in the query so extract the latest query from past selections        if(nDelimIndex > -1) {            var nQueryStart = nDelimIndex + 1;            // Trim any white space from the beginning...            while(sQuery.charAt(nQueryStart) == " ") {                nQueryStart += 1;            }            // ...and save the rest of the string for later            this._sPastSelections = sQuery.substring(0,nQueryStart);            // Here is the query itself            sQuery = sQuery.substr(nQueryStart);        }        // No delimiter found in the query, so there are no selections from past queries        else {            this._sPastSelections = "";        }    }    // Don't search queries that are too short    if((sQuery && (sQuery.length < this.minQueryLength)) || (!sQuery && this.minQueryLength > 0)) {        if(this._nDelayID != -1) {            clearTimeout(this._nDelayID);        }        this._toggleContainer(false);        YAHOO.log("Query \"" + sQuery + "\" is too short", "info", this.toString());        return;    }    sQuery = encodeURIComponent(sQuery);    this._nDelayID = -1;    // Reset timeout ID because request is being made        // Subset matching    if(this.dataSource.queryMatchSubset || this.queryMatchSubset) { // backward compat        var oResponse = this.getSubsetMatches(sQuery);        if(oResponse) {            this.handleResponse(sQuery, oResponse, {query: sQuery});            return;        }    }        if(this.responseStripAfter) {        this.dataSource.doBeforeParseData = this.preparseRawResponse;    }    if(this.applyLocalFilter) {        this.dataSource.doBeforeCallback = this.filterResults;    }        var sRequest = this.generateRequest(sQuery);    this.dataRequestEvent.fire(this, sQuery, sRequest);    YAHOO.log("Sending query \"" + sRequest + "\"", "info", this.toString());    this.dataSource.sendRequest(sRequest, {            success : this.handleResponse,            failure : this.handleResponse,            scope   : this,            argument: {                query: sQuery            }    });};/** * Populates the array of &lt;li&gt; elements in the container with query * results. * * @method _populateList * @param sQuery {String} Original request. * @param oResponse {Object} Response object. * @param oPayload {MIXED} (optional) Additional argument(s) * @private */YAHOO.widget.AutoComplete.prototype._populateList = function(sQuery, oResponse, oPayload) {    // Clear previous timeout    if(this._nTypeAheadDelayID != -1) {        clearTimeout(this._nTypeAheadDelayID);    }            sQuery = (oPayload && oPayload.query) ? oPayload.query : sQuery;        // Pass data through abstract method for any transformations    var ok = this.doBeforeLoadData(sQuery, oResponse, oPayload);    // Data is ok    if(ok && !oResponse.error) {        this.dataReturnEvent.fire(this, sQuery, oResponse.results);                // Continue only if instance is still focused (i.e., user hasn't already moved on)        // Null indicates initialized state, which is ok too        if(this._bFocused || (this._bFocused === null)) {                        //TODO: is this still necessary?            /*var isOpera = (YAHOO.env.ua.opera);            var contentStyle = this._elContent.style;            contentStyle.width = (!isOpera) ? null : "";            contentStyle.height = (!isOpera) ? null : "";*/                    // Store state for this interaction            var sCurQuery = decodeURIComponent(sQuery);            this._sCurQuery = sCurQuery;            this._bItemSelected = false;                    var allResults = oResponse.results,                nItemsToShow = Math.min(allResults.length,this.maxResultsDisplayed),                sMatchKey = (this.dataSource.responseSchema.fields) ?                     (this.dataSource.responseSchema.fields[0].key || this.dataSource.responseSchema.fields[0]) : 0;                        if(nItemsToShow > 0) {                // Make sure container and helpers are ready to go                if(!this._elList || (this._elList.childNodes.length < nItemsToShow)) {                    this._initListEl();                }                this._initContainerHelperEls();                                var allListItemEls = this._elList.childNodes;                // Fill items with data from the bottom up                for(var i = nItemsToShow-1; i >= 0; i--) {                    var elListItem = allListItemEls[i],                    oResult = allResults[i];                                        // Backward compatibility                    if(this.resultTypeList) {                        // Results need to be converted back to an array                        var aResult = [];                        // Match key is first                        aResult[0] = (YAHOO.lang.isString(oResult)) ? oResult : oResult[sMatchKey] || oResult[this.key];                        // Add additional data to the result array                        var fields = this.dataSource.responseSchema.fields;                        if(YAHOO.lang.isArray(fields) && (fields.length > 1)) {                            for(var k=1, len=fields.length; k<len; k++) {                                aResult[aResult.length] = oResult[fields[k].key || fields[k]];                            }                        }                        // No specific fields defined, so pass along entire data object                        else {                            // Already an array                            if(YAHOO.lang.isArray(oResult)) {                                aResult = oResult;                            }                            // Simple string                             else if(YAHOO.lang.isString(oResult)) {                                aResult = [oResult];                            }                            // Object                            else {                                aResult[1] = oResult;                            }                        }                        oResult = aResult;                    }                    // The matching value, including backward compatibility for array format and safety net                    elListItem._sResultMatch = (YAHOO.lang.isString(oResult)) ? oResult : (YAHOO.lang.isArray(oResult)) ? oResult[0] : (oResult[sMatchKey] || "");                    elListItem._oResultData = oResult; // Additional data                    elListItem.innerHTML = this.formatResult(oResult, sCurQuery, elListItem._sResultMatch);                    elListItem.style.display = "";                }                        // Clear out extraneous items                if(nItemsToShow < allListItemEls.length) {                    var extraListItem;                    for(var j = allListItemEls.length-1; j >= nItemsToShow; j--) {                        extraListItem = allListItemEls[j];                        extraListItem.style.display = "none";                    }                }                                this._nDisplayedItems = nItemsToShow;                                this.containerPopulateEvent.fire(this, sQuery, allResults);                                // Highlight the first item                if(this.autoHighlight) {                    var elFirstListItem = this._elList.firstChild;                    this._toggleHighlight(elFirstListItem,"to");                    this.itemArrowToEvent.fire(this, elFirstListItem);                    YAHOO.log("Arrowed to first item", "info", this.toString());                    this._typeAhead(elFirstListItem,sQuery);                }                // Unhighlight any previous time                else {                    this._toggleHighlight(this._elCurListItem,"from");                }                        // Expand the container                ok = this.doBeforeExpandContainer(this._elTextbox, this._elContainer, sQuery, allResults);                this._toggleContainer(ok);            }            else {                this._toggleContainer(false);            }            YAHOO.log("Container populated with " + nItemsToShow +  " list items", "info", this.toString());            return;        }    }    // Error    else {        this.dataErrorEvent.fire(this, sQuery);    }            YAHOO.log("Could not populate list", "info", this.toString());    };/** * When forceSelection is true and the user attempts * leave the text input box without selecting an item from the query results, * the user selection is cleared. * * @method _clearSelection * @private */YAHOO.widget.AutoComplete.prototype._clearSelection = function() {    var sValue = this._elTextbox.value;    //TODO: need to check against all delimChars?    var sChar = (this.delimChar) ? this.delimChar[0] : null;    var nIndex = (sChar) ? sValue.lastIndexOf(sChar, sValue.length-2) : -1;    if(nIndex > -1) {        this._elTextbox.value = sValue.substring(0,nIndex);    }    else {         this._elTextbox.value = "";    }    this._sPastSelections = this._elTextbox.value;    // Fire custom event    this.selectionEnforceEvent.fire(this);    YAHOO.log("Selection enforced", "info", this.toString());};/** * Whether or not user-typed value in the text input box matches any of the * query results. * * @method _textMatchesOption * @return {HTMLElement} Matching li

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -