Login   Register  
PHP Classes
elePHPant
Icontem

File: appz/type_ahead_db/autosuggestcontroller.js

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Guilherme Blanco  >  pAjax  >  appz/type_ahead_db/autosuggestcontroller.js  >  Download  
File: appz/type_ahead_db/autosuggestcontroller.js
Role: Auxiliary data
Content type: text/plain
Description: Auto Suggestion Controller Script
Class: pAjax
Do RPC calls from the browser without page reloads
Author: By
Last change:
Date: 8 years ago
Size: 1,459 bytes
 

Contents

Class file image Download
/*
 * AutoSuggestController based on Nicholas C.Zackas code
 */
function AutoSuggestController(oTextBox, oProvider) {
    this.provider = oProvider;
    this.textbox = oTextBox;

    this.init();
}


var _p = AutoSuggestController.prototype;


_p.init = function () {
    var oThis = this;

    this.textbox.onkeyup = function (e) { oThis.handleKeyUp(e); }
}


_p.selectRange = function (iStart, iLength) {
    if (this.textbox.createTextRange) {
        var oRange = this.textbox.createTextRange();
        oRange.moveStart("character", iStart);
        oRange.moveEnd("character", iLength - this.textbox.value.length);
        oRange.select();
    } else if (this.textbox.setSelectionRange)
        this.textbox.setSelectionRange(iStart, iLength);

    this.textbox.focus();
}


_p.typeAhead = function (sSuggestion) {
    if (this.textbox.createTextRange || this.textbox.setSelectionRange) {
        var iLength = this.textbox.value.length;
        this.textbox.value = sSuggestion;
        this.selectRange(iLength, sSuggestion.length);
    }
}


_p.autoSuggest = function (aSuggestions) {
	if (aSuggestions.length > 0)
        this.typeAhead(aSuggestions[0]);
}


_p.handleKeyUp = function (e) {
    var oEvent = window.event || e;
    var code = oEvent.keyCode;

    if (!(code < 32 || (code >= 33 && code <= 46) || (code >= 112 && code <= 123)))
        this.provider.requestSuggestions(this);
}