﻿/*
 *  using OPS.Rumttime.Core.js
 *
 
 编写:谢月甲;最后更新:2006-05-06 00:32
 
 public fun::
 
 toOps()	//toXml()的m1版本,为向后兼容而预留
 toXml()	//toOps()的r1版本,
 load(opsXml)
 
 bind([container][,ops_hid])
 save()     //3.0 r1 新引入的接口,需要在bind()时,完成@ops_hid的绑定
 saveDefault()
 loadDefault()
 setParent(pso) //3.0 r1 新引入的接口,专为组件之间的整合而用,具体使用请参考DEMO
 ------------------------------------------------------------------------------
 有关OPS 3.0 的更新部份，请参考OPS.Runtime.Core.js里的介绍
 **/

var PsContainer = Class.create();
PsContainer.inherits(PsObject);
PsContainer.extend({
    initialize : function(entityName)
    {
       this.base.initialize.apply(this);
       this.undoAttribute = "ops:psundo";//"dtopsxml";  //当有这个属生,并不等于0的时候,则不处理该项
       this.context	= new PsContext();
       this.type = OPS.Type.PS;
       
       if(entityName != null && entityName.length > 0)
            this.entityName = entityName;
       
       this.defaultPso = null;
    },
    
    bind : function(container, ops_hid)
    {
		this.context	= new PsContext(container);
		
		if(ops_hid != null)
		{
            this.opsHid      = this.$(ops_hid);
            this.opsHid.setAttribute(PsItem.TypeAttri,"opsxml");
        }
		
		this.saveDefault();
    },
    
    saveDefault : function()		//与loadDefault配合使用
    {
		if(this.defaultPso != null)
			return;
		
		this.defaultPso = new PsObject();
		
		this.applyEvent("ontoxmlBefore");
		
	    this.defaultPso.context			= this.context;
	    this.defaultPso.undoAttribute   = this.undoAttribute;
	    this.defaultPso.buildItems();	//构建默认数据,并保存下来,
    },
    
    loadDefault : function()
    {
		if(this.defaultPso != null)
		{
			this.defaultPso.dataBind();
			
	        this.applyEvent("onload");
	    }
    },
    
    clear : function()
    {
        this.loadDefault();
    },
    
    setParent : function(pso)       //约束:一个组件,只能有一个父组件
    {
		if(pso != null)
			this._DoSetParent(pso);
    }
});

PsContainer.prototype.toString = function()
{
    return "[Object OPS.PsContainer]";
}

PsContainer.prototype._DoSetParent = function(pso)
{
    var type = pso.type;
    var my   = this;
    switch(type)
    {
        case OPS.Type.TC:
        pso.attachEvent("onload",function(){my.load();});
        pso.attachEvent("ontoxmlBefore",function(){my.save();});
        pso.attachEvent("onappendBefore",function(){my.save();});
        pso.attachEvent("onappend",function(){my.loadDefault();});
        pso.attachEvent("onremove",function(){my.load();});
        pso.attachEvent("onselectBefore",function(){my.save();});
        pso.attachEvent("onselect",function(){my.load();});
        pso.attachEvent("onclear",function(){my.loadDefault();});
        pso.attachEvent("ondisabledchange",function(val){my.disabled(val);})
        break;
        
        case OPS.Type.PS:
        pso.attachEvent("onload",function(){my.load();});
        pso.attachEvent("ontoxmlBefore",function(){my.save();});
        break;
    }
}
