Apq.js

 

/* Apq
 *
 * 2006-04-16    黄宗银
 * 
*/


if!window.Apq )
{
    window.Apq 
= {
        
"__key""namespace",
        
"__type""Apq"
    }
;
}


if!Apq.Class )
{
    
/// window --------------------------------------------------------------------------------------------------------------------------------
    /// 简写且增强的 document.getElementById 
    window.$ = function(){
        
var ary = [];
        
forvar i = 0; i < arguments.length; i++ )
        
{
            
var o = arguments[i];
            
iftypeof o == 'string' )
            
{
                o 
= document.getElementById(o);
            }

            
if( arguments.length == 1 )
            
{
                
return o;
            }

            ary.push(o);
        }

        
return ary;
    }
;

    window.$o 
= function(){
        
var ary = [];
        
forvar i = 0; i < arguments.length; i++ )
        
{
            
var o = arguments[i];
            
iftypeof o == 'string' && o.Trim() != "" )
            
{
                
var ps = o.split(".");
                o 
= this;
                
forvar j = 0; j < ps.length; j++ )
                
{
                    
iftypeof o[ps[j]] != "undefined" && typeof o[ps[j]] != "unkown" )
                    
{
                        o 
= o[ps[j]];
                    }

                    
else
                    
{
                        o 
= null;
                        
break;
                    }

                }

            }

            
if( arguments.length == 1 )
            
{
                
return o;
            }

            ary.push(o);
        }

        
return ary;
    }
;
    
    
/// document ------------------------------------------------------------------------------------------------------------------------------
    document.__ref = function ( ns, dir, ext ){
        dir 
= dir || "";
        ext 
= ext || "js";
        document.write( '
<script type="text/jscript" src="' );
        document.write( dir );
        document.write( ns );
        document.write( '.' + ext + '
"></\script>' );
    }
;

    
/// <fn>回调函数</fn>
    document.addBehavior = function( o, ns, fn, dir ){
        dir 
= dir || "";
        
if( ns.toLowerCase() == "webservice" )
        
{
            dir 
= dir || Apq$Config.Apq;
        }

        
if( fn )
        
{
            o.onreadystatechange 
= function(){
                document.__addBehavior( fn, o );
            }
;
        }

        o.addBehavior( dir 
+ ns + ".htc" );
    }
;

    document.__addBehavior 
= function( fn, o ){
        
if( o.readyState == "complete" )
        
{
            fn.call( o );
        }

    }
;

    
/// 创建元素
    document.CreateElement = function( sTag ){
        
var he = document.createElement( sTag );
        he.id 
= he.uniqueID;
        
return he;
    }
;

    
/// 添加/修改 属性
    document.setNamedItem = function( he, name, value ){
        
var ha = document.createAttribute( name );
        ha.value 
= value;
        he.attributes.setNamedItem( ha );
        
return ha;
    }
;

    
/// 添加子级元素
    /// <sWhere="beforeEnd"></sWhere>
    document.insertAdjacentElement = function( he, sWhere, oc ){
        he.insertAdjacentElement( sWhere 
|| "beforeEnd", oc );
    }
;

    
/// 添加 HTML 到标签内
    /// <sWhere="beforeEnd"></sWhere>
    document.insertAdjacentHTML = function( he, sWhere, sHTML ){
        he.insertAdjacentHTML( sWhere 
|| "beforeEnd", sHTML );
    }
;

    
/// 添加子元素 ne 到 he 的已有子元素 hc 之前
    /// <hc>为 null 时添加到所有子元素之前</hc>
    document.insertBefore = function( he, ne, hc ){
        
return he.insertBefore( ne, hc );
    }
;

    
/// 获取指定Cookie名的值
    /// <name>Cookie名[ASP.NET_SessionId]</name>
    document.getCookie = function( name ){
        
var aryCookie = document.cookie.split( /|=/ );
        
forvar i = 0; i < aryCookie.length; i++ )
        
{
            
if( aryCookie[i++== name )
            
{
                
return unescape(aryCookie[i])
            }

        }

        
return null;
    }
;

    
/// 设置Cookie
    /// <name>Cookie名</name>
    /// <value>值</value>
    /// <expires>期满</expires>
    /// <domain>访问域</domain>
    /// <path>目录</path>
    /// <secure>保护/安全</secure>
    document.setCookie = function( name, value, path, expires, domain, secure ){
        
var strname = name + "=" + escape(value) + " ;path = " + (path || "/");
        
var strexpires = expires ? " ;expires = " + expires.toGMTString() : "";
        
var strdomain = domain ? " ;domain = " + domain : "";
        
var strsecure = secure ? ";secure" : "";
        document.cookie 
= strname + strexpires + strdomain + strsecure;
    }
;

    
/// 删除指定的Cookie
    /// <name>Cookie名</name>
    document.delCookie = function( name ){
        
var exp = new Date();
        exp.setTime( exp.getTime() 
- 1 );
        document.cookie 
= name + "=Deleted; expires=" + exp.toGMTString();
    }
;

    
/// location --------------------------------------------------------------------------------------------------------------
    location.getQueryString = function( str ){
        
var QueryString = new ActiveXObject( "Scripting.Dictionary" );
        
var index = str.indexOf( '?' );
        
if( index == -1 )
        
{
            
return QueryString;
        }


        
var s = str.substr( index + 1 );
        
var ary = s.split( "&" );
        
forvar i = 0; i < ary.length; i++ )
        
{
            
var index = ary[i].indexOf( "=" );
            
if( index != -1 )
            
{
                QueryString.Item( ary[i].substr( 
0, index ) ) = ary[i].substr( index+1 );
            }

        }

        
return QueryString;
    }
;

    location.BuildSearch 
= function( QueryString ){
        
var ary = [];
        
var vbary = QueryString.Keys();
        
forvar i = 0; i < QueryString.Count; i++ )
        
{
            ary.push( vbary.getItem(i) 
+ "=" + QueryString.Item( vbary.getItem(i) ) );
        }

        
return ary.join( "&" );
    }
;
    
    
/// 计算当前 location 的 QueryString
    location.QueryString = location.getQueryString( location.search );
    
    
/// Object --------------------------------------------------------------------------------------------------------------------------------
    Object.prototype.CopyFrom = function( o, c ){
        
return Object.Copy( o, this, c );
    }
;

    Object.prototype.CopyTo 
= function( o, c ){
        
return Object.Copy( this, o, c );
    }
;

    Object.Copy 
= function( s, d, c ){
        d 
= d || {};
        
forvar p in s )
        
{
            
iftypeof( s[p] ) != "unknown" )
            
{
                Object.Set( d, p, s[p], c, 
false );
            }

        }

        
return d;
    }
;
    
    Object.Get 
= function( o, p ){
        
return o == null ? $o(p) : o[p];
    }
;
    
    Object.Set 
= function( o, p, v, c, e ){
        
if( o == null )
        
{
            
throw new Error( -1"参数 [o] 不能为 null" );
        }

        c 
= c == null ? true : c;
        
if!&& typeof o[p] != "undefined" )
        
{
            
return;
        }

        
        
var oe = "old": o[p], "now": v };
        o[p] 
= v;
        
        e 
= e == null ? true : e;
        
if( e && oe.old != oe.now )
        
{
            
// 引发属性已改变事件
            o.fireEvent( p+"Changed", oe );
        }

    }
;

    
/// 属性操作器 ----------------------------------------------------------------------------------------------------------------------------
    /// 添加
    /// <rw>按位从右到左:
    ///        1: r
    ///        2: w
    /// </rw>
    Object.prototype.pAdd = function( p, rw ){
        
if( rw & 1 )
        
{
            
this[p+"_get"= function(){
                
return Object.Get( this, p );
            }
;
        }

        
if( rw & 2 )
        
{
            
this[p+"_set"= function( v, c ){
                Object.Set( 
this, p, v, c, true );
            }
;
        }

    }
;
    
    
/// 移除
    Object.prototype.pRemove = function( p, rw ){
        
if( rw & 1 )
        
{
            
delete this[p+"_get"];
        }

        
if( rw & 2 )
        
{
            
delete this[p+"_set"];
        }

    }
;

    
/// 系统"关键字"定义 ----------------------------------------------------------------------------------------------------------------------
    /// namespace
    Apq.namespace = function( ns ){
        
var nss = ns.split( "." );
        
var root = window;
        
forvar i = 0; i < nss.length; i++ )
        
{
            
iftypeof( root[nss[i]] ) == "undefined" )
            
{
                root[nss[i]] 
= {
                    
"__key""namespace",
                    
"__type": (root.__type ? (root.__type+".") : ""+ nss[i]
                }
;
            }

            root 
= root[nss[i]];
        }

        
return root;
    }
;

    
// 应用该框架必须引用的文件
    Apq.__need = [ "prototype""Apq" ];

    
/// using
    /// <g>是否将该空间定义保存到主框架</g>
    Apq.using = function( ns, g, dir, ext ){
        
if( Apq.__need.Contains( ns ) || $o(ns) )
        
{
            
return;
        }

        
if( Apq$jsContainer[ns] )
        
{
            __eval( window, Apq$jsContainer[ns] );
            
return;
        }


        dir 
= dir || "";
        ext 
= ext || "js";
        
if/^Apq\..+/.test( ns ) )
        
{
            dir 
= Apq$Config.Apq;
        }


        
if!window.Apq$jsxh && !MsXml_Test() )
        
{
            
return;
        }

        Apq$jsxh.open( 
"GET", dir + ns + "." + ext, false );
        Apq$jsxh.send();
        
if( Apq$jsxh.status == 200 )
        
{
            Apq$jsContainer[ns] 
= Apq$jsxh.responseText;
            __eval( window, Apq$jsContainer[ns] );
            
return;
        }

        
throw new Error( Apq$jsxh.status, Apq$jsxh.statusText );
    }
;

    
/// Class
    /// <__type>类全名</__type>
    /// <ihs>(单)继承列表</ihs>
    /// <__abstract>是否抽象类</__abstract>
    /// <__sealed>是否密封类</__sealed>
    Apq.Class = function( __type, ihs, __abstract, __sealed ){
        
var fn = function(){
            
// 调用构造函数
            // 直接调用
            ifthis.ctor == fn.prototype.ctor )
            
{
                
if( fn.__abstract )
                
{
                    
throw new Error( -1"抽象类不能创建实例" );
                }

                
this.ctor.apply( this, arguments );
            }

            
// 由派生类调用
            else
            
{
                fn.prototype.ctor.apply( 
this, arguments );
            }

        }
;

        
/// 关键语句,不能删除
        fn.prototype.constructor = fn;

        
// 设置类信息
        Object.Set( fn.prototype, "__key""class"falsefalse );
        Object.Set( fn.prototype, 
"__type", __type, falsefalse );
        Object.Set( fn, 
"__key""class"falsefalse );
        Object.Set( fn, 
"__type", __type, falsefalse );
        Object.Set( fn, 
"__abstract", __abstract || falsefalsefalse );
        Object.Set( fn, 
"__sealed", __sealed || falsefalsefalse );

        
// 提供默认构造函数和析构函数
        fn.prototype.ctor = function(){
            
if( fn.__base )
            
{
                fn.__base.prototype.ctor.apply( 
this, arguments );
            }

        }
;
        fn.prototype.Finalize 
= function(){
            
if( fn.__base )
            
{
                fn.__base.prototype.Finalize.call( 
this );
            }

        }
;
        
// 提供基类初始化函数
        fn.prototype.base = function(){
            
if( fn.__base )
            
{
                fn.__base.prototype.ctor.apply( 
this, arguments );
            }

            
else
            
{
                Debug.writeln( '未找到类 
"' + fn.__type + '" 的基类,此次基类初始化已忽略' );
            }

        }
;

        
// 继承列表
        var base;
        fn.__ifs 
= ihs;
        
if( ihs && ihs.length )
        
{
            
// 分离基类
            base = ihs[0].__key == "class" ? ihs.shift() : null;

            
// 实现接口
            if( fn.__ifs )
            
{
                
forvar i = 0; i < fn.__ifs.length; i++ )
                
{
                    fn.prototype.CopyFrom( fn.__ifs[i], 
false );
                }

            }

        }

        
// 设置基类
        Apq.Class.base_set( fn, base );
        
return fn;
    }
;

    
/// 设置基类
    Apq.Class.base_set = function( cls, base ){
        cls.__base 
= base;
        
if!base )
        
{
            
return cls;
        }

        
        
if( base.__sealed )
        
{
            
throw new Error( -1"不能继承密封类: " + base.__type );
        }

        
forvar p in base.prototype )
        
{
            
iftypeof( cls.prototype[p] ) == "undefined" && p.indexOf("__") )
            
{
                cls.prototype[p] 
= base.prototype[p];
            }

        }

        cls.CopyFrom( base, 
false );
        
return cls;
    }
;

    
/// 获取基类列表
    Apq.Class.bases_get = function( cls ){
        
var ary = [];
        
forvar cb = cls.__base; cb; cb = cb.__base )
        
{
            ary.push( cb );
        }

        
return ary;
    }
;

    
/// 获取运行时类层次列表
    Apq.Class.Runtime_get = function( o ){
        
var ary = Apq.Class.bases_get( o.constructor );
        ary.unshift( o.constructor );
        
return ary;
    }
;
    
    
/// 根据运行时对象 o 获取指定类 cls 的运行时直接派生类,无派生类则返回 null
    Apq.Class.Runtime_child = function( o, cls ){
        
forvar cb = o.constructor; cb; cb = cb.__base )
        
{
            
if( Apq.Class.Equals( cb.__base, cls ) )
            
{
                
return cb;
            }

        }

        
return null;
    }
;

    
/// 类是否相同
    Apq.Class.Equals = function( c1, c2 ){
        
var t1 = String.ChangeFirstUpper( Apq.GetTypeName( c1 ) );
        
var t2 = String.ChangeFirstUpper( Apq.GetTypeName( c2 ) );
        
return t1 == t2;
    }
;

    
/// interface
    /// <__type>接口全名</__type>
    /// <__ifs>继承列表</__ifs>
    /// <fns>方法名列表</fns>
    Apq.interface = function( __type, __ifs, fns ){
        
var it = {
            
"__key""interface",
            
"__type": __type
        }
;
        
// 定义方法
        forvar i = 0; i < fns.length; i++ )
        
{
            it[fns[i]] 
= Apq.Function.abstract;
        }

        
// 继承接口
        it.__ifs = __ifs;
        
if( it.__ifs )
        
{
            
forvar i = 0; i < it.__ifs.length; i++ )
            
{
                it.CopyFrom( it.__ifs[i], 
false );
            }

        }

        
return it;
    }
;
    
    
/// delegate
    /// <fn>需要扩展为委托的函数</fn>
    Apq.delegate = function( fn ){
        
var d = fn || function(){
            
return d._delegate.Invoke.apply( d._delegate, arguments );
        }
;
        d.__key 
= "delegate";
        d.__type 
= "Apq.delegate";
        d._delegate 
= new Apq.delegate._delegate();
        d._delegate.add.bind( d, 
"add", d._delegate );
        d._delegate.remove.bind( d, 
"remove", d._delegate );
        d._delegate.InvokeAll.bind( d, 
"InvokeAll", d._delegate );
        
return d;
    }
;
    
    Apq.delegate._delegate 
= Apq.Class( "Apq.delegate" );
    
    Apq.delegate._delegate.prototype.ctor 
= function(){
        
this.methods = [];
        
this.contexts = [];
        
this.Enable = true;    // 用于 启用/禁用 代理
    }
;
    
    Apq.delegate._delegate.prototype.add 
= function( f, o ){
        
iftypeof f == "string" )
        
{
            f 
= Object.Get( o, f );
        }

        
iftypeof f == "function" )
        
{
            
this.methods.push( f );
            
this.contexts.push( o );
        }

        
else
        
{
            Debug.writeln( 
"未找到方法,不能增加委托,此次操作将被忽略" );
        }

    }
;

    
/// <f>方法或成员名</f>
    Apq.delegate._delegate.prototype.remove = function( f, o ){
        
iftypeof f == "string" )
        
{
            f 
= Object.Get( o, f );
        }

        
iftypeof f == "function" )
        
{
            
forvar i = this.methods.length - 1; i >= 0; i-- )
            
{
                
ifthis.methods[i] == f && this.contexts[i] == o )
                
{
                    
this.methods.RemoveAt( i );
                    
this.contexts.RemoveAt( i );
                }

            }

        }

    }
;
    
    
/// 普通调用
    Apq.delegate._delegate.prototype.Invoke = function(){
        
ifthis.Enable )
        
{
            
forvar i = 0; i < this.methods.length; i++ )
            
{
                
var v = this.methods[i].apply( this.contexts[i], arguments );
                
if( i == this.methods.length - 1 )
                
{
                    
return v;
                }

            }

        }

    }
;
    
    
/// 引发事件
    Apq.delegate._delegate.prototype.Fire = function(){
        
ifthis.Enable )
        
{
            
forvar i = this.methods.length-1; i >= 0; i-- )
            
{
                
var v = this.methods[i].apply( this.contexts[i], arguments );
                
if!i )
                
{
                    
return v;
                }

            }

        }

    }
;
    Apq.delegate._delegate.prototype.InvokeAll 
= function(){
        
ifthis.Enable )
        
{
            
forvar i = 0; i < this.methods.length; i++ )
            
{
                Apq.setTimeout( 
0this.methods[i], arguments, this.contexts[i] );
            }

        }

    }
;
    Apq.delegate._delegate.prototype.Equals 
= function( di ){
        
if( Apq.GetTypeName( di ) == this.__type )
        
{
            
forvar i = 0; i < this.methods.length; i++ )
            
{
                
ifthis.methods[i] != di.methods[i] || this.contexts[i] != di.contexts[i] )
                
{
                    
return false;
                }

            }

            
return true;
        }

        
return false;
    }
;

    Apq.setTimeout 
= function( t, fn, args, o ){
        
var f = function(){
            
if( args )
            
{
                fn.apply( o, args );
            }

            
else
            
{
                fn.call( o );
            }

        }
;
        
return setTimeout( f, t );
    }
;

    Object.prototype.attachEvent 
= function( n, fn, oe ){
        
if!this.Events )
        
{
            
this.Events = {};
        }

        
if!this.Events[n] )
        
{
            
var d = Apq.delegate( 
                
function()
                
{
                    
return d._delegate.Fire.apply( d._delegate, arguments );
                }

             );
            d.__key 
= "event";
            d.__type 
= "Apq.event";
            
this.Events[n] = d;
        }

        
if( fn )
        
{
            
this.Events[n].add( fn, oe );
        }

    }
;
    
    Object.prototype.detachEvent 
= function( n, fn, oe ){
        
ifthis.Events && this.Events[n] )
        
{
            
this.Events[n].remove( fn, oe );
        }

    }
;

    Object.prototype.fireEvent 
= function( n, e ){
        
ifthis.Events && this.Events[n] )
        
{
            
return this.Events[n]( this, e );
        }

    }
;

    
/// 运算符 --------------------------------------------------------------------------------------------------------------------------------
    /// 等于
    Apq.Equals = function( o1, o2 ){
        
if( o1 == o2 )
        
{
            
return true;
        }

        
if( o1 == null )
        
{
            
var t = o1;
            o1 
= o2;
            o2 
= t;
        }

        
if( o1.Equals )
        
{
            
return o1.Equals( o2 );
        }

        
return false;
    }
;

    
/// is
    Apq.is = function( o, cls ){
        
if( o != null )
        
{
            
// 递归搜索基类及接口
            if( Apq.Class.Equals( o, cls ) )
            
{
                
return true;
            }

            
if( o.__base )
            
{
                
if( Apq.is( o.__base, cls ) )
                
{
                    
return true;
                }

            }

            
if( o.__ifs )
            
{
                
forvar i = 0; i < o.__ifs.length; i++ )
                
{
                    
if( Apq.is( o.__ifs[i], cls ) )
                    
{
                        
return true;
                    }

                }

            }

        }

        
return cls == null;
    }
;

    
/// 函数构造器 ----------------------------------------------------------------------------------------------------------------------------
    /// <prt>需要增加方法的对象,一般为原型</prt>
    Apq.Function = function( prt, n, fn, v ){
        
var rfn = function(){
            
// 虚函数 且 由派生类对象调用
            if( v && this.__type != prt.__type )    // this.__type 为运行时对象的类名
            {
                
var c = Apq.Class.Runtime_child( this, prt );    // 查找 prt 的运行时直接派生类
                // 未找到 prt 的直接派生类
                if!c )
                
{
                    
throw new Error( -1"类层次结构异常.请检查类定义: " + this.__type );
                }

                
// 已找到 prt 的直接派生类
                if( c.prototype[n] )
                
{
                    
return c.prototype[n].apply( this, arguments );
                }

                
// 默认采用对象的同名成员运行
                return this[n].apply( this, arguments );
            }

            
// 非虚函数 或 由本类对象调用
            return fn.apply( this, arguments );
        }
;
        rfn.__mn 
= n;
        prt[n] 
= rfn;
        
return rfn;
    }
;

    
/// 抽象方法
    Apq.Function.abstract = function(){
        
throw new Error( -1, '抽象方法应由子类实现.' );
    }
;

    
/// toJSON --------------------------------------------------------------------------------------------------------------------------------
    /// 用字符串表示对象
    Apq.toJSON = function( o ) {
        
if( o.toJSON )
        
{
            Array.Apply( arguments, 
"RemoveAt", [ 0 ] );
            
return o.toJSON.apply( o, arguments );
        }

        
var strClassName = Apq.GetTypeName( o ).toLowerCase();
        
if( strClassName == "undefined" || strClassName == "null" )
        
{
            
return strClassName;
        }

        
if( strClassName == "system.xml.xmldocument" || strClassName == "system.xml.xmlnode" )
        
{
            
// Xml 相关类
            return o.xml;
        }

        
return (new Error( -1"未知对象类型,系统无法表示" )).toJSON();
    }
;
    
    Array.prototype.toJSON 
= function(){
        
var a = [];
        
forvar i = 0; i < this.length; i++ )
        
{
            a.push( Apq.toJSON(
this[i]) );
        }

        
return "" + a.join( "" ) + " ]";
    }
;

    
/// 模拟反射 ------------------------------------------------------------------------------------------------------------------------------
    /// 获取类全名
    Apq.GetTypeName = function( o ){
        
iftypeof o == "undefined" )
        
{
            
return "undefined";
        }

        
if( o == null )
        
{
            
return "null";
        }

        
if( o.__type )
        
{
            
return o.__type;
        }

        
iftypeof o != "object" )
        
{
            
// boolean, number, string, function
            return typeof o;
        }

        
switch( o.constructor )
        
{
            
case Array:
                
return "Array";
            
case Boolean:
                
return "Boolean";
            
case Date:
                
return "Date";
            
case Enumerator:
                
return "Enumerator";
            
case Error:
                
return "Error";
            
case Function:
                
return "Function";
            
case Number:
                
return "Number";
            
case RegExp:
                
return "RegExp";
            
case String:
                
return "String";
            
case VBArray:
                
return "VBArray";
        }

        
// Xml 相关类
        if( o.documentElement )
        
{
            
return "System.Xml.XmlDocument";
        }

        
if( o.xml )
        
{
            
return "System.Xml.XmlNode";
        }

        
return "object";
    }
;

    
/// Apq.VAR -------------------------------------------------------------------------------------------------------------------------------
    /// 提供变量常规检验功能
    Apq.VAR = {};
    Apq.VAR.Check 
= function( op, o1, o2 ){
        
if( op == "==" )
        
{
            
return Apq.Equals( o1, o2 );
        }

        
return eval( "o1 " + op + " o2" );
    }
;

    
/// Apq.Argument --------------------------------------------------------------------------------------------------------------------------
    /// 提供参数常规检验功能
    Apq.Argument = {};
    Apq.Argument.CheckNull 
= function( n, v ){
        
if( Apq.VAR.Check( "==", v, null ) )
        
{
            
throw new Error( 1, String.Format( "参数 [{0}] 不能为 null", n ) );
        }

    }
;

    
/// <n>参数名</n>
    /// <a>参数</a>
    /// <cls>期望类型<cls>
    Apq.Argument.CheckType = function( n, v, cls ){
        
if!Apq.is( v, cls ) )
        
{
            
throw new Error( 2, String.Format( "参数 [{0}] 类型不匹配,期望类型为: {1}", n, Apq.GetTypeName(cls) ) );
        }

    }
;

    
/// 对象属性遍历器 ------------------------------------------------------------------------------------------------------------------------
    /// 可以 获取指定位置的遍历项,修改当前遍历项
    Apq.Enumerator = Apq.Class( "Apq.Enumerator" );
    
/// <o>要遍历的 Object </o>
    /// <k>跳过列表</k>
    /// <f>是否包含方法</f>
    Apq.Enumerator.prototype.ctor = function( o, k, f ){
        Apq.Argument.CheckNull( 
"o", o );
        
this.o = o;        // 当前遍历的对象
        this.a = [];    // 属性值列表
        this.n = [];    // 属性名列表
        forvar p in o )
        
{
            
if( k && k.Contains( p ) )
            
{
                
continue;
            }

            
if!&& typeof o[p] == "function" )
            
{
                
continue;
            }

            
this.a.push( o[p] );
            
this.n.push( p );
        }

        
this.moveFirst();
    }
;

    Apq.Enumerator.prototype.GetPosition 
= function( i ){
        
if( i >= 0 && i < this.a.length )
        
{
            
return this.a[i];
        }

        
return null;
    }
;

    Apq.Enumerator.prototype.SetPosition 
= function( i ){
        
if( i == null )
        
{
            i 
= this.i;
        }

        
if( i < 0 || i > this.a.length )
        
{
            
throw new Error( 1"已超出集合范围." );
        }

        
this.i = i;
    }
;

    Apq.Enumerator.prototype.GetItem 
= function(){
        
return this.GetPosition( this.i );
    }
;

    Apq.Enumerator.prototype.SetItem 
= function( m ){
        
this.a[this.i] = this.o[this.n[this.i]] = m;
    }
;

    Apq.Enumerator.prototype.moveFirst 
= function(){
        
this.SetPosition( 0 );
    }
;

    Apq.Enumerator.prototype.moveNext 
= function(){
        
this.SetPosition( this.i + 1 );
    }
;

    Apq.Enumerator.prototype.item 
= Apq.Enumerator.prototype.GetItem;

    Apq.Enumerator.prototype.atEnd 
= function(){
        
return this.i >= this.a.length;
    }
;

    Apq.Enumerator.prototype.ItemName 
= function(){
        
return this.n[this.i];
    }
;

    
/// Apq.XmlHttp ---------------------------------------------------------------------------------------------------------------------------
    /// Apq.XmlHttp 是核心部分,故在此定义
    /// 该类应仅用于获取(各种)数据,操作 Xml 数据请使用 <xml/>
    /// 注意:由于采用池管理,故不能直接使用 new 来创建实例,请使用 Apq.XmlHttp.Pool.New()
    Apq.XmlHttp = Apq.Class( "Apq.XmlHttp" );

    Apq.XmlHttp.prototype.create 
= function(){
        
this.XMLHTTP = new ActiveXObject( "MsXml2.XMLHTTP.6.0" );
    }


    
/// 使用完毕,返还到池
    Apq.XmlHttp.prototype.Restore = function(){
        
this.leisure = true;    // 可分配
    }
;

    
/// 根据 XMLHTTP 的 responseXML 创建其对应的 <xml/> 对象
    Apq.XmlHttp.prototype.ToXml = function( hx ){
        hx 
= hx || document.CreateElement( "xml" );
        hx.async 
= false;
        hx.loadXML( 
this.XMLHTTP.responseXML.xml );
        
return hx;
    }
;

    
/// Apq.XmlHttp.Pool ----------------------------------------------------------------------------------------------------------------------
    /// 使用"池"来管理 Apq.XmlHttp 实例,尽量减少客户端创建对象的次数[* 未能实现自动管理,即需要手动返还实例 *]
    Apq.XmlHttp.Pool = [];

    Apq.XmlHttp.Pool.create 
= function(){
        
var xh = new Apq.XmlHttp();
        xh.create();
        
this.push( xh );
        xh.index 
= this.lastIndexOf( xh );
        
return xh;
    }
;

    
/// 请求 XMLHTTP 对象
    /// <n=1>请求的数量<n>
    /// <fa=false>是否强制返回数组,仅当 n == 1 时该参数才有意义</fa>
    /// <return>返回 数组 或 单个对象(n==1)</return>
    Apq.XmlHttp.Pool.New = function( n, fa ){
        n 
= n || 1;

        
if( n == 1 )
        
{
            
var tmp;
            
forvar i = this.length - 1; i >= 0; i-- )
            
{
                
ifthis[i] && this[i].leisure )
                
{
                    
this[i].leisure = false;
                    tmp 
= this[i];
                    
forvar eor = new Apq.Enumerator( tmp ); !eor.atEnd(); eor.moveNext() )
                    
{
                        
var p = eor.ItemName();
                        
switch( p )
                        
{
                            
case "index":
                            
case "Restore":
                            
case "leisure":
                                
continue;

                            
case "XMLHTTP":
                                eor.item().onreadystatechange 
= Function.Empty;
                                
break;

                            
default:
                                tmp[p] 
= null;
                                
break;
                        }

                    }

                    
break;
                }

            }


            
if!tmp )
            
{
                tmp 
= this.create();
            }


            
if( fa )
            
{
                
var ary = [];
                ary.push( tmp );
                
return ary;
            }

            
else
            
{
                
return tmp;
            }

        }


        
var ary = [];
        
forvar i = 0; i < n; i++ )
        
{
            ary.push( 
this.New( 1 ) );
        }

        
return ary;
    }
;

    
/// Apq.Convert ---------------------------------------------------------------------------------------------------------------------------
    Apq.Convert = {
        ToBoolean: 
function( o ){
            
if!o )
            
{
                
return false;
            }


            
switch( Apq.GetTypeName( o ).toLowerCase() )
            
{
                
case "number":
                    
return isNaN( o ) ? false : o != 0;

                
case "string":
                    
return o.toLowerCase() == "true";

                
default:    // array, bool, date, object, regexp
                    return true;
            }

        }
,

        ToDate: 
function( o ){
            
switch( Apq.GetTypeName( o ).toLowerCase() )
            
{
                
case "date":
                    
return o;

                
case "number":
                    
return new Date( o );

                
case "string":
                    
// yyyy-MM-dd[( |T)HH:mm:ss.fffffff]
                    var ds = o.split( /[:T \.\+-]/ );
                    
var ns = [];
                    
var n = ds.length < 7 ? ds.length : 7;
                    
forvar i = 0; i < n; i++ )
                    
{
                        ns.push( parseInt(ds[i], 
10) );
                    }

                    
return new Date( ns[0], ns[1]-1, ns[2], ns[3|| null, ns[4|| null, ns[5|| null, ns[6]/10000 || null );

                
default:    // array, bool, object, regexp
                    return null;
            }

        }

    }
;
}

posted @ 2006-06-14 10:32  Amwpfiqvy  阅读(1748)  评论(0编辑  收藏  举报