popup.js 2.64 KB
/**
 * @author Administrator
 */
var Popup = (function(){
	var p_width = 600;
	var p_height = 400;
	var p_x = 0;
	var p_y = 0;
	var conentHeight = 0;
	var formFun = null;
	var cancelFun = null;
	var callData = null;
	var onStartFun = null;
	
	/**
	 * 初始化
	 */
	function initialize (data){
		
		$('body .n-popup').remove();//清理原有的
		var content = '';
		if(data.url){
			content = '<iframe src="'+data.url+'" frameborder="no" border="0" allowTransparency="true" ></iframe>';
		}else if(data.content){
			content = data.content;
		}else{
			content = '暂无内容';
		};
		
		var text1 = "确定";
		var text2 = "取消";
		var _btnView = '';
		if(data.btns){
			if(data.btns.length == 1){
				text1 = data.btns[0];
				_btnView = '<a href="#" class="save" >'+text1+'</a>';
			}else{
				text1 = data.btns[0];
				text2 = data.btns[1];
				_btnView = '<a href="#" class="save" >'+text1+'</a><a href="#" class="cancel" >'+text2+'</a>';
			};
		}else{
			_btnView = '<a href="#" class="save" >'+text1+'</a>';
		};
		
		var view =  '<div class="n-popup"><div class="background"></div><div class="n-popup-cont" style="width:'+p_width+'px;height:'+p_height+'px;left:'+p_x+'px;top:'+p_y+'px;" >'
				   +'<div class="n-popup-cont-header"><p>'+data.title+'</p><a href="#" class="colse cancel"></a></div>'
				   +'<div class="n-popup-cont-content" style="height:'+conentHeight+'px;" >'+content+'</div>'
				   +'<div class="n-popup-cont-footer" '+(data.isfooterBar ? '' : 'style="display: none;"')+'  >'+_btnView+'</div></div></div>';
		$('body').append(view);
		eventListeners();
	};
	
	/**
	 * 事件监听区
	 */
	function eventListeners (){
		$('.n-popup .cancel').click(function(){
			if(cancelFun){
				cancelFun(callData);
			};
			$('.n-popup').remove();
			return false;
		});
		
		$('.n-popup .save').click(function(){
			if(formFun){
				formFun(callData);
			};
			
			return false;
		});
	};
	
	/**
	 * 参数设置
	 */
  this.start = function(obj){
		if(obj.width){
			p_width = obj.width;
		};
		if(obj.height){
			p_height = obj.height;
		};
		p_y = ($(window).height() - p_height) / 2;
		p_x = ($(window).width() - p_width) / 2;
		
		if(obj.isfooterBar){
			conentHeight = p_height - 96;
		}else{
			conentHeight = p_height - 41;
		};
		
		if(obj.saveFun){
			formFun = obj.saveFun;
		};
		if(obj.cancelFun){
			cancelFun = obj.cancelFun;
		};
		if(obj.onStartCallback && jQuery.isFunction(obj.onStartCallback)){
			onStartFun = obj.onStartCallback;
			onStartFun();
		}
		//拼装页面
		initialize(obj);
	};
	
	/**
	 * 设置数据
	 */
	this.setData = function(data){
		callData = data;
	};
	
	this.colse = function(){
		$('.n-popup').remove();
	};
	
	return this;
})();