2007年1月21日星期日

(js) Javascript runtime environment.

/*<![CDATA[*/
/** javascript runtime environment.
 * @author 闲耘 (HoToo)
 * @author
http://www.xianyun.org
 * @create 2006-6-26
 */


/** Browser object.
 * @update 2006-6-17
 *              2006-8-16 add isNetscape.
 * @p.s. thanks robin pan and meizz.
 */

window.Browser = {
    isIE : (navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0),
    isFirefox : navigator.userAgent.indexOf('Firefox') >= 0,
    isOpera : navigator.userAgent.indexOf('Opera') >= 0,
    isNetscape : navigator .userAgent.indexOf('Netscape') >= 0
};
 
/** window.classpath.
 * @description using classpath to find classes.
 * @update 2006-7-29
 */
window.classpath = function (){
    var _sc = document.getElementsByTagName('script');
    var _cp = _sc[_sc.length - 1].getAttribute('classpath');
    return (_cp == null) ? 'classes/' : _cp + '/';
}();
 
/** window._package()
 * @description create the object for name space.
 * @param packageName, string, like Java, using dot (.) replace solidus (/) with directory.
 * @demo _package(' cn.hotoo.Obj') will create these object: cn, cn.hotoo, cn.hotoo.Obj
 * @update <<2006-7-26
 */
/*
window._package = function(packageName){
    var ps = packageName.split('.');
    for (var i = 0; i < ps.length; i++){
        var _ps = ps[0];
        for (var j = 1; j <= i; j++){
            _ps += '.' + ps[j];
        };
        try{
            if (typeof(eval(_ps)) == 'function'){continue;}; // object is existed.
            if (typeof(eval(_ps)) == 'undefined'){ // object.property == 'undefined'
                eval(_ps + ' = new Function();');
            };
        }catch(e){
            eval(_ps + ' = new Function();'); // non object.
        };
    };
};
*/

/** from AtlasRuntime.js
 * @create 2006-8-3
 */
window._package = function(packageName){
    var oRoot = window;
    var ps = packageName.split('.');
    for (var i = 0; i < ps.length; i++){
        var curr = ps[i];
        if (!oRoot[curr]){
            oRoot[curr] = new Object();
        };
        oRoot = oRoot[curr];
    };
};
 
/** window._import()
 * @description dynamic load Javascript file at runtime.
 * @param uri, string, like Java package name and class name. eg: 'org.xianyun.Obj'.
 * @param classpath, string, if need a temporary classpath, set it;
 *     else, set null or non-string object to using default classpath.
 * @param fCallback, function, when load javascript file successful, execute it.
 * @p.s. the best version. 2006-7-26
 */
/*
window._import = function(uri, classpath, fCallback){
    var path = (typeof(classpath) == 'string') ? classpath : this.classpath;
    var id = 'script_' + path.replace (/\//g, '_') + uri.replace(/\./g, '_');
    var callback = function(script){
        if (Browser.isIE){
            script.onreadystatechange = function(){
                if (script.readyState == 'complete' || script.readyState == 'loaded'){
                    if(typeof(fCallback) == 'function'){fCallback();};
                };
            };
        }else if(Browser.isFirefox ){
            script.onload = function(){
                if(typeof(fCallback) == 'function'){fCallback();};
            };
            script.onerror = function(){
                //alert('load error.');
            };
        }else{ // Opera 9.0 passed.
            if(typeof(fCallback) == 'function'){fCallback();};
        };
    };
    if (document.getElementById(id) != null){ // script tag is created.
        try{
            fCallback();
        }catch(e){ // scripts is not success loaded.
            callback(document.getElementById(id));
        };
        return;
    };
    var _url = path + uri.replace(/\./g, '/') + '.js';
    var script = document.createElement('script');
    script.setAttribute('language', 'javascript');
    script.setAttribute('type', 'text/javascript');
    script.setAttribute('id', id);
    script.setAttribute('src', _url);
    //document.body.appendChild(script);
    document.getElementsByTagName('head')[0].appendChild(script);
    callback(script);
};
*/

/** window._import()
 * @description dynamic, batch or not load scripts at runtime.
 * @param uris, array or string, if is array may like this:
 * uris = [
 *     ['packagesName.className ', 'classpath/'], // %currentDirectory%/classpath/, must end with solidus (/).
 *     ['cn.hotoo.tools.Counter', null], // null or non-string, using default classpath.
 *     ['cn.hotoo.UI.Window.HTWindow ', ''] // current directory, do no start with solidus (/), if not, using root directory.
 * ];
 * @param classpath, string, the common classpath in this action of batch import.
 * @param fCallback, function, after loaded all scripts, do this action.
 * @p.s. excessive design. 2006-7-27
 */
window._import = function(uris, classpath, fCallback){
    var __import = function(uri, classpath, fCallback){
        var path = (typeof(classpath) == 'string') ? classpath : this.classpath;
        var id = 'script_' + path.replace(/\//g, '_') + uri.replace(/\./g, '_');
        var callback = function(script){
            if (Browser.isIE){
                script.onreadystatechange = function(){
                    if (script.readyState == 'complete' || script.readyState == 'loaded'){
                        if(typeof(fCallback) == 'function'){fCallback();};
                    };
                };
            }else if(Browser.isFirefox || Browser.isNetscape){
                script.onload = function(){
                    if(typeof(fCallback) == 'function'){fCallback();};
                };
            }else{ // Opera 8.0/9.0 passed.
                if(typeof(fCallback) == 'function'){fCallback();};
            };
        };
        if (document.getElementById(id) != null){ // script tag is created.
            try{
                fCallback();
            }catch (e){ // scripts is not success loaded.
                callback(document.getElementById(id));
            };
            return ;
        };
        var _url = path + uri.replace(/\./g, '/') + '.js';
        var script = document.createElement('script');
        script.setAttribute('language', 'javascript');
        script.setAttribute('type', 'text/javascript');
        script.setAttribute('id', id);
        script.setAttribute ('src', _url);
        //document.body.appendChild(script);
        document.getElementsByTagName('head')[0].appendChild(script);
        callback(script);
    };
    if (typeof(uris) == 'string'){
        __import(uris, classpath, fCallback);
    }else if ( typeof(uris) == 'object'){
        if (uris.length <= 0){return;};
        var len = uris.length - 1;
        if (typeof(uris[len][1]) == 'string'){ // using temporary classpath.
            imp = '__import("' + uris[len][0] + '", "' + uris[len][1] + '", fCallback)';
        }else if (typeof(classpath) == 'string'){ // using common classpath.
            imp = '__import("' + uris[len][0] + '", "' + classpath + '", fCallback)';
        }else{ // using default classpath.
            imp = '__import("' + uris[len][0] + '", null, fCallback)';
        };
        for (var i = len - 1; i >= 0; i--){
            if (typeof(uris[i][1]) == 'string'){
                imp = '__import("' + uris[i][0] + '", "' + uris[i][1] + '", function(){' + imp + ';})';
            }else if ( typeof(classpath) == 'string'){
                imp = '__import("' + uris[i][0] + '", "' + classpath + '", function(){' + imp + ';})';
            } else{
                imp = '__import("' + uris[i][0] + '", null, function(){' + imp + ';})';
            };
        };
        eval(imp);
    };
};
/*]]>*/

没有评论:

发表评论