﻿//EasyUI Popup类EasyPopup.js（继承自弹出层基类EasyLayer）
var easyPopup = function(olayer,sclass,closecls,scolor,tcolor,dragdrop,width,height,zindex){
	sclass = sclass||'easypopup';
	scolor = scolor||'#808080';
	tcolor = tcolor||'#ffffff';
	width = isNaN(width)?300:width;
	height = isNaN(height)?200:height;
	easyLayer.call(this,olayer,zindex,width,height);//继承弹出层
	this.closecls = closecls||'closebtn';//关闭按钮样式名，通过此样式名设定关闭动作
	this.fixsize = false;//是否固定大小
	this.autofullsize = false;//如果未设置autofullsize，则超过屏幕大小则强制适应屏幕尺寸
	this.dstimes = 0;//微调尺寸正在进行的次数
	this.onclick = null;//事件：popup层被单击时
	this.init(sclass,closecls,scolor,tcolor,dragdrop,width,height);
};
easyPopup.prototype = {
	autosize:function(){
		var dc = this.dcontent,dcbd = this.dcbd;
		if(dcbd.scrollLeft<=0&&dcbd.scrollTop<=0){return;}//调用此方法前，先设置sl和st，无需更改尺寸了
		var dw = Math.max(dcbd.offsetWidth,dcbd.clientWidth,dcbd.scrollWidth)+1;
		var dh = Math.max(dcbd.offsetHeight,dcbd.clientHeight,dcbd.scrollHeight)+1;
		var idw = dw,idh = dh,rs = easyUI.getWindowSize();
		if(!this.autofullsize){
			var xdw = rs.width;
			var xdh = rs.height;
			idw = Math.min(dw,(xdw-100)),idh = Math.min(dh,(xdh-150));//如果未设置autofullsize，则超过屏幕大小则强制适应屏幕尺寸
		}
		if(dcbd){
			dcbd.style.overflow = 'auto';
			dcbd.style.margin = '5px';
			dcbd.style.width = idw+'px';
			dcbd.style.height = idh+'px';
		}
		idw += 10;
		idh += 80;
		this.layer.style.width = idw+'px';
		this.layer.style.height = idh+'px';
		this.dshadow.style.width = idw+'px';
		this.dshadow.style.height = idh+'px';
		dc.style.width = idw+'px';
		dc.style.height = idh+'px';
		var xdde = document.documentElement,xdb = document.body,dbox = this.layer;
		x = Math.max(xdde.scrollLeft,xdb.scrollLeft)+Math.floor((rs.width-idw)/2);
		y = Math.max(xdde.scrollTop,xdb.scrollTop)+Math.floor((rs.height-idh)/2);
		dbox.style.left = x+'px';
		dbox.style.top = y+'px';
		this.mask.resetsize();
	},
	detectsize:function(){//检测微调尺寸
		var dc = this.dcontent,dcbd = this.dcbd,bnext = (!this.isopen)?false:true;
		dcbd.scrollLeft = 100;dcbd.scrollTop = 100;//检测内容显示是否完全(或是否存在滚动条)
		if(dcbd.scrollLeft==0&&dcbd.scrollTop==0){bnext = false;}else{bnext = true;}
		if(!this.autofullsize){//适应屏幕尺寸时
			var msie = /msie/i.test(navigator.appVersion);
			var rs = easyUI.getWindowSize();
			var dw = (msie)?dc.offsetWidth:Math.min(dc.offsetWidth,dc.clientWidth);
			var dh = (msie)?dc.offsetHeight:Math.min(dc.offsetHeight,dc.clientHeight);
			var xdw = rs.width;
			var xdh = rs.height;
			if(dw>=(xdw-100)&&dh>=(xdh-150)){dcbd.scrollLeft=0;dcbd.scrollTop=0;return;}
			if(dw>=(xdw-100)&&dcbd.scrollTop==0){dcbd.scrollLeft=0;dcbd.scrollTop=0;return;}
			if(dh>=(xdh-150)&&dcbd.scrollLeft==0){dcbd.scrollLeft=0;dcbd.scrollTop=0;return;}
		}
		if(!bnext){
			if(this.dstimes<10){//给十次机会先 ^_^
				this.dstimes++;
				window.setTimeout(arguments.callee.bind(this),200);
				return;
			}else{
				this.dstimes = 0;
				return;
			}
		}
		this.autosize();
		window.setTimeout(arguments.callee.bind(this),10);
	},
	disable:function(){
		if(!this.mask){return;}
		this.mask.resetsize();
		this.mask.clearcount();
		this.mask.open(0,0);
	},
	enable:function(){
		if(!this.mask){return;}
		this.mask.clearcount();
		this.mask.close();
	},
	hideShadow:function(){
		this.dshadow.style.display = 'none';
	},
	setShadow:function(opacity,rx,ry,cb){//设置阴影
		opacity = (!isNaN(opacity))?opacity:30;
		rx = (!isNaN(rx))?rx:10;
		ry = (!isNaN(ry))?ry:10;
		var cw = parseInt(easyUI.getStyle(this.dcontent,'width')),ch = parseInt(easyUI.getStyle(this.dcontent,'height'));
		var icw = Math.max(this.dcontent.offsetWidth,cw),ich = Math.max(this.dcontent.offsetHeight,ch);
		this.dshadow.style.cssText = 'position:absolute;left:'+rx+'px;top:'+ry+'px;width:'+icw+'px;height:'+ich+'px;overflow:hidden;background:#000;-moz-opacity:'+(opacity/100)+';opacity:'+(opacity/100)+';filter:alpha(opacity='+opacity+');z-index:-1;';
	},
	setTitle:function(stitle){//设置标题
		var dt = this.dchd;
		if(!dt){return;}
		var dts = dt.getElementsByTagName('strong')[0];
		if(!dts){return;}
		stitle = stitle?stitle.replace(/(?:(<)|>)/ig,function(s,a){return a?'&lt;':'&gt;';}):'easy popup';
		dts.innerHTML = '<nobr>'+stitle+'</nobr>';
	},
	setContent:function(scontent){//设置内容
		var dc = this.dcbd;
		if(!dc){return;}
		if(this.fixsize){dc.innerHTML = scontent;return;}
		dc.style.visibility = 'hidden';//更改DOM前隐藏
		dc.innerHTML = '';
		dc.style.width = '200px';
		dc.style.height = '50px';
		dc.innerHTML = '<nobr>'+scontent+'</nobr>';
		dc.scrollLeft = 100;dc.scrollTop = 100;
		this.autosize();//重绘尺寸
		dc.innerHTML = scontent;
		dc.style.visibility = 'visible';
		this.detectsize();//微调尺寸
	},
	setStatus:function(sst){
		var df = this.dcft;
		if(!df){return;}
		df.innerHTML = sst||'';
	},
	popup:function(msg,stitle,status){
		msg = msg||'';
		stitle = stitle||'内容窗口：';
		this.setTitle(stitle);
		this.setContent(msg);
		this.setStatus(status);
		this.open();
	},
	init:function(sclass,closecls,scolor,tcolor,dragdrop,width,height){
		var stmp = '<div class="popcontent">';
		stmp += '<div class="pophd"><strong style="display:block;width:90%;overflow:hidden;margin:0 5px;font-size:12px;background:'+scolor+';color:'+tcolor+';text-align:left;cursor:default;">Easy Popup - EasyUI</strong></div>';
		stmp += '<div class="popbd" style="margin:5px;height:'+(height-80)+'px;overflow:auto;">欢迎使用EasyUI系列控件，当前控件为Easy Popup Layer。</div>';
		stmp += '<div class="popft" style="position:absolute;left:2px;bottom:-2px;_bottom:2px;width:100%;height:32px;overflow:hidden;background:#F0EFE6;text-align:left;border-top:1px solid #D5D4CB;"></div>';
		stmp += '<div class="'+this.closecls+'" style="position:absolute;top:5px;right:5px;padding:0;margin:0;color:'+tcolor+';font-size:15px;line-height:15px;cursor:pointer;">&#215;</div>';
		stmp += '</div>';
		stmp += '<div class="popshadow">&nbsp;</div>';
		stmp += '<div class="popmask">&nbsp;</div>';
		this.layer.innerHTML = stmp;
		this.dcontent = this.layer.childNodes[0];
		this.dshadow = this.layer.childNodes[1];
		this.dmask = this.layer.childNodes[2];
		this.dchd = easyUI.getElementsBy('class','pophd','div',this.dcontent)[0];
		this.dcbd = easyUI.getElementsBy('class','popbd','div',this.dcontent)[0];
		this.dcft = easyUI.getElementsBy('class','popft','div',this.dcontent)[0];
		this.layer.className = sclass;
		this.layer.style.overflow = 'visible';
		this.dcontent.style.cssText = 'background:#fff;border:2px solid '+scolor+';width:'+width+'px;height:'+height+'px;overflow:hidden;';
		this.dshadow.style.cssText = 'position:absolute;left:0;top:0;z-index:-1;display:none;';
		this.dchd.style.cssText = 'background:'+scolor+';color:#fff;height:25px;line-height:25px;padding:0;margin:0;text-align:left;overflow:hidden;zoom:1;';
		if(dragdrop){//设置拖拽
			var da = this.dchd||this.dcontent;
			var xddpp = new easyDragDrop(da,this.layer,null,true);
		}
		this.mask = new easyMask(this.dmask,width,height,2,'#000',30,false);
		this.mask.resetsize = function(){
			this.mask.layer.style.width = this.dcontent.offsetWidth+'px';
			this.mask.layer.style.height = this.dcontent.offsetHeight+'px';
		}.bind(this);
		//单击事件onclick应用
		var foooc = function(e){
			var dtarget = easyUI.getTarget(e),stag = dtarget.tagName,sclass = dtarget.className;
			if(sclass=='popmask'||dtarget.parentNode.className=='popmask'){easyUI.stopEvent(e);return;}
			if(sclass==this.closecls){
				easyUI.stopEvent(e);
				this.close();
				return;
			}
			if(this.onclick&&this.onclick.constructor==Function){this.onclick(e);}
			if(this.dcbd.scrollHeight>this.dcbd.offsetHeight){this.autosize();}
		};
		this.layer.onclick = foooc.bind(this);
		//重写继承自easyLayer的open方法
		var fooopold = this.open;
		this.open = function(x,y){
			fooopold.call(this,x,y);
			if(!this.fixsize){this.detectsize();}
		};
	}
};