POE-XUL

 view release on metacpan or  search on metacpan

javascript/src/multikeypress.js  view on Meta::CPAN


// ------------------------------------------------------------------
// 
_.start_timeout = function ( multi ) {
    fb_log( "suppressonselect=%s",
            this.menulist.getAttribute( 'suppressonselect' )
          );
    fb_log( "timeout multiplier=" + multi );
    var self = this;
    this.tID = window.setTimeout( function () { self.timedout() }, 
                                  this.timeout * multi 
                                );
    return false;
}



// ------------------------------------------------------------------
// When the wait times out, the pressed buffer is reset to empty and
// the new item is sent to the server
_.timedout = function () {
    this.pressed = '';
    delete this['tID'];

    if( this.new_index != -1 )      // select the new item
        this.select( this.new_index );

    // sync the input to the drop down 
    var len = 2;
    if( this.textbox.value.length != len ) {
        this.textbox.value = 
                this.menulist.selectedItem.label.substr(0, len);
    }

    this.new_index = -1;

    this.submit();
}

// ------------------------------------------------------------------
// Stop any timeout from happening
_.drop_timeout = function () {
    if( this.tID ) {
        window.clearTimeout( this.tID );
        delete this['tID'];
    }
}


// ------------------------------------------------------------------
// Find the item that best matches the pressed buffer.
_.select_item = function ( shorter ) {

    fb_log( "pressed='" + this.pressed + "'" );

    var menumenulist = this.menulist;
    if( this.pressed == '' ) {
        return;
    }

    var items = menumenulist.menupopup.childNodes;
    this.new_index = -1;
    var maybe = -1;
    var pressed = this.pressed.toLowerCase();
    var w = 1;
    if( shorter ) {
        if( menumenulist.selectedItem.label.substr(0,pressed.length).toLowerCase()
            == pressed ) {
            fb_log( "no change" );
            return;
        }
    }

    while( w <= pressed.length ) {
        var match = 0;
        for( var q = (maybe < 0 ? 0 : maybe ) ; 
                 q < items.length && ! match;
                 q++ ) {

            var text = items[q].label.toLowerCase();
            var p0 = pressed.substr( 0, w );
            var t0 = text.substr( 0, w );
            if( p0 == t0 ) {
                maybe = q;
                match = 1;
                // fb_log( "match " + p0 + ">=" + t0 + " label=" + items[maybe].label );
            }
            else if( p0 > t0 ) {
                maybe = q;
                // fb_log( "maybe " + p0 + ">=" + t0 + " label=" + items[maybe].label );
            }
            else {
                // fb_log( "not " + p0 + ">=" + t0 );
            }
        }
        w++;
    }

    if( maybe > -1 ) {
        fb_log( "match " + items[maybe].label );
        this.new_index = maybe;
        this.select( this.new_index );
    }
}

// ------------------------------------------------------------------
// Setting selectedItem randomly triggers onChange.  So we do it explictly.
_.submit = function () {

    this.changed = 0;
    if( $application ) {
        fb_log( "submit #" + this.menulist.selectedIndex );
        
        var pop = this.menulist.childNodes[0];
        var target = pop.childNodes[ this.menulist.selectedIndex ];
        var e = { target: target };
        $application.fireEvent_Command( e );
    }
}


// ------------------------------------------------------------------
// It still does it's fucking around, despite the preventDefault()
// So the selection is done at timeout, when it's time to submit the
// new value
_.select = function ( new_index ) {
    fb_log( "want #" + new_index );
    if( this.menulist.selectedIndex != new_index ) {
        // Get this change to the server fast
        this.changed = 2;
    }
    this.menulist.selectedIndex = new_index;
    fb_log( "selected #" + this.menulist.selectedIndex );
    this.menulist.selectedItem = this.menulist.menupopup.childNodes[new_index];
    fb_log( "select label=" + this.menulist.menupopup.childNodes[new_index].label );

//    this.textbox.value = this.menulist.selectedItem.label.substr( 0, 2 );
}

// ------------------------------------------------------------------
// Wouldn't it be nice to know what element had focus previously? 
// Because we don't, we don't know when to skip this element.  90% of
// users won't know about shift-tab anyway
_.focus_menulist = function (event) {
}

// ------------------------------------------------------------------
// If the user selects something in the menulist, we want to update 
// the textbox
_.command_menulist = function (event) {

    this.textbox.value = event.target.value;
    return;

    var source = event.target;
    var text = source.label;
    fb_log( "select=" + text );
    this.textbox.value = source.substr( 0, this.width );
}



( run in 1.566 second using v1.01-cache-2.11-cpan-364913b4093 )