Changeset 168

Show
Ignore:
Timestamp:
05/02/07 18:28:40 (2 years ago)
Author:
ydnar
Message:

bugid:48991; added TypeCore namespace, delcare() function, moved event handlers

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/TypeCore.js

    r167 r168  
    9292 
    9393 
     94declare = function( n, o ) { 
     95    var ps = n.split( "." ); 
     96    var o = o || window; 
     97    for( var i = 0; i < ns.length; i++ ) { 
     98        var p = ps[ i ]; 
     99        if( !defined( o[ p ] ) ) 
     100            o[ p ] = {}; 
     101        o = o[ p ]; 
     102    } 
     103    return o; 
     104}; 
     105 
     106 
    94107/* logging */ 
    95108 
     
    98111 
    99112 
    100 /* try block */   
    101   
    102 Try = { 
    103     these: function() { 
    104         var a = arguments; 
    105         for( var i = 0; i < a.length; i++ ) { 
    106             try { 
    107                 return a[ i ](); 
    108             } catch( e ) {} 
    109         } 
    110         return undefined; 
    111     } 
    112 }; 
     113/* declare namespace */ 
     114 
     115declare( "TypeCore" ); 
    113116 
    114117 
    115118/* unique id generator */ 
    116119 
    117 Unique = { 
     120TypeCore.Unique = { 
    118121    length: 0, 
    119122     
     
    124127 
    125128 
    126 /* event methods */ 
    127  
    128 if( !defined( window.Event ) ) 
    129     Event = {}; 
    130  
    131  
    132 Event.stop = function( ev ) { 
    133     ev = ev || this; 
    134     if( ev === Event ) 
    135         ev = window.event; 
    136  
    137     // w3c 
    138     if( ev.preventDefault ) 
    139         ev.preventDefault(); 
    140     if( ev.stopPropagation ) 
    141         ev.stopPropagation(); 
    142  
    143     // ie 
    144     try { 
    145         ev.cancelBubble = true; 
    146         ev.returnValue = false; 
    147     } catch( e ) {} 
    148  
    149     return false; 
    150 }; 
    151  
    152  
    153 Event.prep = function( ev ) { 
    154     ev = ev || window.event; 
    155     if( !defined( ev.stop ) ) 
    156         ev.stop = this.stop; 
    157     if( !defined( ev.target ) ) 
    158         ev.target = ev.srcElement; 
    159     if( !defined( ev.relatedTarget ) ) { 
    160         ev.relatedTarget = (event.type == "mouseover" || event.type == "mouseenter") 
    161             ? ev.fromElement 
    162             : ev.toElement; 
    163     } 
    164     return ev; 
    165 }; 
    166  
    167  
    168 try { Event.prototype.stop = Event.stop; } 
    169 catch( e ) {} 
    170  
    171  
    172 /* object extensions */ 
    173  
    174 Function.stub = function() {}; 
    175  
    176  
    177 /* function extensions */ 
    178  
    179 extend( Function.prototype, { 
    180     bind: function( o ) { 
    181         var m = this; 
    182         return function() { 
    183             return m.apply( o, arguments ); 
    184         }; 
    185     }, 
    186      
    187      
    188     bindEventListener: function( o ) { 
    189         var m = this; // Use double closure to work around IE 6 memory leak. 
    190         return function( e ) { 
    191             try { 
    192                 event = Event.prep( e ); 
    193             } catch( e ) {} 
    194             return m.call( o, e ); 
    195         }; 
    196     }, 
    197      
    198      
    199     applySuper: function( o, args ) { 
    200         return this.__super.apply( o, args || [] ); 
    201     }, 
    202      
    203      
    204     callSuper: function( o ) { 
    205         var args = []; 
    206         for( var i = 1; i < arguments.length; i++ ) 
    207             args.push( arguments[ i ] ); 
    208         return this.__super.apply( o, args ); 
    209     } 
    210 } ); 
    211  
    212  
    213 /* class helpers */ 
    214  
    215 indirectObjects = []; 
    216 __SUBCLASS__ = { __SUBCLASS__: "__SUBCLASS__" }; 
    217  
    218  
    219 Class = function( sc ) { 
     129/* class factory */ 
     130 
     131TypeCore.indirectObjects = []; 
     132 
     133 
     134TypeCore.Class = function( sc ) { 
    220135    var c = function( s ) { 
    221136        this.constructor = arguments.callee; 
    222         if( s === __SUBCLASS__
     137        if( s === TypeCore.Class.SUBCLASS
    223138            return; 
    224139        this.init.apply( this, arguments ); 
     
    231146    c.superClass = sc.prototype; 
    232147     
    233     c.prototype = sc === Object ? new sc() : new sc( __SUBCLASS__ ); 
     148    c.prototype = sc === Object ? new sc() : new sc( TypeCore.Class.SUBCLASS ); 
    234149    extend( c.prototype, Class.prototype ); 
    235150    var a = arguments; 
     
    258173 
    259174 
    260 extend( Class, { 
     175extend( TypeCore.Class, { 
     176    SUBCLASS: {}, 
     177     
     178     
    261179    initSingleton: function() { 
    262180        if( this.singleton ) 
     
    269187 
    270188 
    271 Class.prototype = { 
     189TypeCore.Class.prototype = { 
    272190    init: function() {}, 
    273191     
     
    276194        try { 
    277195            if( this.indirectIndex ) 
    278                 indirectObjects[ this.indirectIndex ] = undefined; 
     196                TypeCore.indirectObjects[ this.indirectIndex ] = undefined; 
    279197            delete this.indirectIndex; 
    280198        } catch( e ) {} 
    281199         
    282         for( var property in this ) { 
     200        for( var p in this ) { 
    283201            try { 
    284                 if( this.hasOwnProperty( property ) ) 
    285                     delete this[ property ]; 
     202                if( this.hasOwnProperty( p ) ) 
     203                    delete this[ p ]; 
    286204            } catch( e ) {} 
    287205        } 
     
    344262 
    345263 
     264/* events */ 
     265 
     266declare( "Event" ); 
     267 
     268 
     269extend( Event, { 
     270    stop: function( ev ) { 
     271        ev = ev || this; 
     272        if( ev === Event ) 
     273            ev = window.event; 
     274 
     275        // w3c 
     276        if( ev.preventDefault ) 
     277            ev.preventDefault(); 
     278        if( ev.stopPropagation ) 
     279            ev.stopPropagation(); 
     280 
     281        // ie 
     282        try { 
     283            ev.cancelBubble = true; 
     284            ev.returnValue = false; 
     285        } catch( e ) {} 
     286 
     287        return false; 
     288    }, 
     289 
     290 
     291    prep: function( ev ) { 
     292        ev = ev || window.event; 
     293        if( !defined( ev.stop ) ) 
     294            ev.stop = this.stop; 
     295        if( !defined( ev.target ) ) 
     296            ev.target = ev.srcElement; 
     297        if( !defined( ev.relatedTarget ) ) { 
     298            ev.relatedTarget = (event.type == "mouseover" || event.type == "mouseenter") 
     299                ? ev.fromElement 
     300                : ev.toElement; 
     301        } 
     302        return ev; 
     303    }, 
     304     
     305     
     306     
     307    addEventListener: function( o, en, f, uc ) { 
     308        try { 
     309            if( o.addEventListener ) 
     310                o.addEventListener( en, f, uc ); 
     311            else if( o.attachEvent ) 
     312                o.attachEvent( "on" + en, f ); 
     313            else 
     314                o[ "on" + en ] = f; 
     315        } catch( e ) {} 
     316    }, 
     317 
     318 
     319    removeEventListener: function( o, en, f, uc ) { 
     320        try { 
     321            if( o.removeEventListener ) 
     322                o.removeEventListener( en, f, uc ); 
     323            else if( o.detachEvent ) 
     324                o.detachEvent( "on" + en, f ); 
     325            else 
     326                o[ "on" + en ] = undefined; 
     327        } catch( e ) {} 
     328    } 
     329}; 
     330 
     331 
     332try { Event.prototype.stop = Event.stop; } 
     333catch( e ) {} 
     334 
     335 
     336/* object extensions */ 
     337 
     338Function.stub = function() {}; 
     339 
     340 
     341/* function extensions */ 
     342 
     343extend( Function.prototype, { 
     344    bind: function( o ) { 
     345        var m = this; 
     346        return function() { 
     347            return m.apply( o, arguments ); 
     348        }; 
     349    }, 
     350     
     351     
     352    bindEventListener: function( o ) { 
     353        var m = this; // Use double closure to work around IE 6 memory leak. 
     354        return function( e ) { 
     355            try { 
     356                event = Event.prep( e ); 
     357            } catch( e ) {} 
     358            return m.call( o, e ); 
     359        }; 
     360    }, 
     361     
     362     
     363    applySuper: function( o, args ) { 
     364        return this.__super.apply( o, args || [] ); 
     365    }, 
     366     
     367     
     368    callSuper: function( o ) { 
     369        var args = []; 
     370        for( var i = 1; i < arguments.length; i++ ) 
     371            args.push( arguments[ i ] ); 
     372        return this.__super.apply( o, args ); 
     373    } 
     374} ); 
     375 
     376 
    346377/* string extensions */ 
    347378 
     
    817848    /* day of week, not day of month */ 
    818849    getLocaleDayString: function( d ) { 
    819         if( isNaN(d) ) 
     850        if( isNaN( d ) ) 
    820851            d = this.getDay(); 
    821852        return this.constructor.strings.localeWeekdays[ d ]; 
     
    825856    /* day of week, not day of month */ 
    826857    getLocaleDayShortString: function( d ) { 
    827         if( isNaN(d) ) 
     858        if( isNaN( d ) ) 
    828859            d = this.getDay(); 
    829860        return this.constructor.strings.localeShortWeekdays[ d ]; 
     
    832863 
    833864    getLocaleMonthString: function( m ) { 
    834         if( isNaN(m) ) 
     865        if( isNaN( m ) ) 
    835866            m = this.getMonth(); 
    836867        return this.constructor.strings.localeMonths[ m ]; 
     
    839870 
    840871    getLocaleMonthShortString: function( m ) { 
    841         if( isNaN(m) ) 
     872        if( isNaN( m ) ) 
    842873            m = this.getMonth(); 
    843874        return this.constructor.strings.localeShortMonths[ m ]; 
     
    865896         
    866897        return undefined; 
    867     } 
    868 }; 
     898    }; 
     899}