POE-XUL

 view release on metacpan or  search on metacpan

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

}


_.isNotCrashed = function () {
    if( !this.crashed )
        return;
    alert( "This application has crashed\n" + this.crashed );
}


// ------------------------------------------------------------------
_.exception = function ( type, EXs ) {
    var msg = [];
    for( var q=0 ; q<EXs.length ; q++ ) {
        var ex = EXs[q];
        var file = ex.fileName || 'N/A';
        var line = ex.lineNumber || 'N/A';
        msg.push( (ex.message || ex.description) + 
                        "\n  File " + file + " line " + line );
    }

    $application.crash( type + " ERROR " + msg.join( "\n" ) );
}

// events ---------------------------------------------------------------------

_.fireEvent_Command = function (domEvent) {
    var source = domEvent.target;
    // fb_dir( { tagName: source.tagName, Command: domEvent } );
    if (source.tagName == 'menuitem' || source.tagName == 'menulist' ) {
        this.fireEvent_Command_Select( domEvent );
    } 
    else if (source.tagName == 'radio') {
        this.fireEvent_Command_RadioClick( domEvent );
    }
    else if ( source.tagName == 'splitter' ) {
        // ignore the clicks on a splitter
        return;
    }
    else {
        if( FormatedField ) {
            var bp = domEvent.target.getAttribute( 'bypass' );
            if( !bp && !FormatedField.form_validate() ) {
                return;
            }
        }

        // fb_log( "Command->Click " + domEvent.type );
        this.fireEvent('Click', domEvent, {});
    }
}

_.fireEvent_Command_Select = function ( domEvent ) {
    var target = domEvent.target;

    var realTarget = target;
    fb_log( 'target is %s', target.tagName );
    if( target.tagName == 'menuitem' ) {
        realTarget = target.parentNode;
    }
    // realTarget is the menupopup, search-list or menu

    if (realTarget.tagName == 'search-list') {
        // $debug( 'Select SearchList ' + realTarget.selectedIndex );
        this.fireEvent( 'Select', 
                        { 'target': realTarget },
                        { 'selectedIndex': realTarget.selectedIndex }
                      );
        return;
    } 

    fb_log( 'realTarget is %s', realTarget.tagName );
    if( realTarget.tagName == 'menupopup' )
        realTarget = realTarget.parentNode;
    // realTarget must now be the menulist
    fb_log( 'realTarget is finally %s#%s', realTarget.tagName, realTarget.id );

    fb_dir( { selectedIndex: realTarget.selectedIndex, 
                   realTarget: realTarget } );

    if (realTarget.tagName == 'menu') {
        // fb_log( "menu->Click" );
        this.fireEvent('Click', domEvent, {});
    } 
    else {
        // menulist: mozilla doesn't set selectedIndex properly!
        // Same with button, it seems
        var I;
        if (realTarget.tagName == 'button' || 
                realTarget.tagName == 'menulist' ) {
            var children = target.parentNode.childNodes;
            I = children.length;
            var trueI; 
            fb_log( children );
            fb_log( "looking for %s#%s", target.tagName, target.id );
            while (I--) {
                // fb_log( "%i is %s#%s", I, children[I].tagName, children[I].id );
                if (children[I] == target) {
                    trueI = I;
                    break;
                }
            }
            if( trueI === undefined ) {
                I = realTarget.selectedIndex;
                fb_log( "Can't find %s#%s in %s#%s.  I hope %i is the true index.",
                            target.tagName, target.id, 
                            realTarget.tagName, realTarget.id, I );
            }
            else if( trueI == realTarget.selectedIndex ) {
                I = trueI;
                fb_log( "%s#%s had the correct index=%i", 
                        realTarget.tagName, realTarget.id, I );
            }
            else {
                realTarget.selectedIndex = trueI;
                I = trueI;
                fb_log( "%s#%s's true index=%i", 
                        realTarget.tagName, realTarget.id, I );
            }
        } 
        else { 
            I = realTarget.selectedIndex;
        }
        var params = {'selectedIndex': I };

        // Send event to app server
        this.fireEvent( 'Select', {'target': realTarget}, params );
    }
}

_.fireEvent_Command_RadioClick = function ( domEvent ) {
    var source = domEvent.target;
    var realSource = source.parentNode;

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

// private --------------------------------------------------------------------

_.fireEvent = function (name, domEvent, params) {
    var source   = domEvent.target;
    var sourceId = source.id;
    if (!sourceId) return; // event could come from some unknown place
    var event = {
        'source_id' : sourceId,
        'event'   : name,
    };
    if( 0 ) {  // XUL doesn't believe in checked, it seems
        event['checked'] = source.getAttribute('checked');
    }
    else {
        event['checked'] = source.getAttribute('selected');
    }

    var key; for (key in params) event[key] = params[key];
    this.runRequest(event);
}


// ------------------------------------------------------------------
// turn an event into something we should send to the server
_.setupEvent = function ( event ) {
    if( !event ) {
        event = {};
    }
    event.app = this.applicationName;
    if( ! ("window" in event) )
        event.window = window.name;

    // fb_log( event );
    // fb_log( "window name=", window.name );

    if( ! event.app )
        this.crash( "I need an aplication name!" );

    return event;
}


// ------------------------------------------------------------------
// event should be :
//  {
//      event: "Click",
//      source_id: id,
//    For 'Change':
//      value: "new value",
//    For 'RadioClick':
//      selectedId: itemId
//      checked: source.selected
//    For 'Select':
//      selectedIndex:
//      checked: source.selected
//    Conduit will add :
//      SID: current-SID,
//      reqN: 1++,
//    Added in setupEvent :
//      app: "IGDAIP", (or the application name)
//      window: popup-window-name
//  }

_.runRequest = function (event) {
    this.isNotCrashed();

    event = this.setupEvent( event );

    if( !event.event ) 
        event.event = 'boot';
    fb_log( "app=%s event=%s", event.app, event.event );

    if( this.longEvent( event ) )
        this.status( "load" );

    var self = this;
    this.conduit.request( event, 
                          function (json) { self.runResponse( json, event ) } 
                        );
}

// ------------------------------------------------------------------
_.runResponse = function ( json, event ) {

    // fb_log( 'runResponse' );

    if( event && this.longEvent( event ) )
        this.status( "run" );

    // fb_log( json );
    if( json == null ) {
        // fb_log( event );
        // alert( "Response didn't include JSON ", window.name );
        this.crash( "Response didn't include JSON" );
    }
    else if( 'object' != typeof json && 'Array' != typeof json ) {
        this.crash( "Response isn't an array: " + typeof json );
    }
    else {
        this.runner.run( json );
        // this.status( "done" );
    }
    return;
}

// ------------------------------------------------------------------
_.longEvent = function ( event ) {
    if( !event.event )
        return 0;
    if( event.event == 'boot' )
        return 1;
    if( event.event == 'Click' )
        return 1;
    return 0;
}
_.status = function ( status ) {

    var text;
    if( status == 'load' ) {
        // document.documentElement.style.cursor = "wait";
        text = "Chargement...";

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

        window.status = ' ';
    }
    else {
        window.status = text;
    }
}

// ------------------------------------------------------------------
_.clearFormated = function () {
    if( FormatedField ) {
        FormatedField.clear_formated();
    }
}

_.cleanFormated = function () {
    if( FormatedField ) {
        FormatedField.clean_formated();
    }
}


// ------------------------------------------------------------------
_.timing = function ( what, start, end ) {
    var elapsed = end - start;
    var t;
    if( elapsed > 1000 ) {
        t = ( elapsed/1000 ) + "s";
    }
    else {
        t = elapsed + "ms";
    }
    // window.status = window.status + " " + what + ": " + t;
    // $debug( what + ": " + t + "\n" );
}

// ------------------------------------------------------------------
// Open a sub-window
_.openWindow = function ( url, id, features ) {

    fb_log( "Open window id=%s url=%s", id, url );
    this.status( 'open' );
    var win = window;
    // next little bit lifted from text.xml#text-link
    if (window instanceof Components.interfaces.nsIDOMChromeWindow) {
        while (win.opener && !win.opener.closed)
            win = win.opener;
    }

    var w = win.open( url, id, features );

    if( w ) {    
        this.other_windows[ id ] = w;
        var self = this;

        w.addEventListener( 'unload', 
                        function (e) { self.closed( id, e ); return true; }, 
                        false 
                      );
    }
    else {
        alert( "Vous devez permetre des fen\xEAtres 'popup' pour ce site." );
    }
}

// ------------------------------------------------------------------
// Close a sub-window
_.closeWindow = function ( id ) {
    fb_log( id + ".close()" );
    var w = this.other_windows[ id ];

    if( !w ) {
        // either window was already closed, or we are the popup
        if( window.name == id ) {
            w = window;
        }
        else {
            fb_log( "Why don't I have window id", id ); 
            return;
        }
    }

    if( window.name == w.name ) {
        fb_log( "Closing ourself" );
        if( w.opener && w.opener['$application'] ) {
            fb_log( "Getting main window to close us" );
            w.opener['$application'].closeWindow( id );
            return;
        }
    }

    if( w && !w.closed )
        w.close();      // this should provoke .closed()
                        // which handles the 'disconnect'
}

// ------------------------------------------------------------------
// Main window is closing, close sub-windows
_.unload = function ( e ) {
    this.unloading = 1;
    for ( var id in this.other_windows ) {
        fb_log( id + '.close()' );
        this.other_windows[ id ].close();
    }
}

// ------------------------------------------------------------------
// Sub window is closing
_.closed = function ( id, e ) {
    if( this.unloading )        // skip out early
        return;
    if( ! e.target.location ) 
        return;
    if( e.target.location.toString() == 'about:blank' )   
        return;                 // this is unloading the initial about:blank
//    fb_log( e.target.location.toString() );
    fb_log( "Window " + id + " closed" );
    if( this.other_windows[ id ] ) {
        delete this.other_windows[ id ];
        var self = this;
        window.setTimeout( function () { self.disconnect( id ); }, this.BLIP );
    }
}


// ------------------------------------------------------------------
// Disconnect a sub window is closing
_.disconnect = function ( id ) {    // TODO: make sure this is a lateCommand
    this.runRequest( { event: 'disconnect', 
                       window: id 
                   } );
}



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