popup.js 3.99 KB
/**
 * @author Administrator
 */
var Popup = (function(){
	var p_width = 300;
	var p_height = 200;
	var p_x = 0;
	var p_y = 0;
	var p_time = 5000;
	var conentHeight = 0;
	var formFun = null;
	var cancelFun = null;
	var callData = 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" style="'+(data.isrolling ? "position:absolute" : "")+'"><div class="background" style="'+(data.isrolling ? "position:fixed" : "")+'"></div><div class="n-popup-cont" style="width:'+p_width+'px;height:'+p_height+'px;left:'+p_x+'px;top:'+p_y+'px;" >'
				   +(data.hideheaderBar ? '' : '<div class="n-popup-cont-header"><p>'+data.title+'</p><a href="#" class="cancel colse"></a></div>')
				   +'<div class="n-popup-cont-content" style="height:'+conentHeight+'px;'+(data.hideheaderBar ? 'top:0px;': 'top:47px;')+'" >'+content+'</div>'
				   +'<div class="n-popup-cont-footer" '+(data.hidefooterBar ? '' : 'style="display: none;"')+'  >'+_btnView+'</div></div></div>';
		/*var $view = $(view);
		if($view.find("img").size() > 0){
			$view.find("img").each(function(){
				$(this).lazyload({
					effect : "fadeIn",
					skip_invisible : false
				});		
			});
		}
		$('body').append($view);*/
		$('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.time){
			p_time = obj.time;	
		}
	  	if(obj.cancelclick){
			var time = setTimeout(function () { 
				clearTimeout(time);
			 	$('.n-popup .cancel').click();
			 }, p_time);
		};
		if(obj.saveclick){
			var time =  setTimeout(function () { 
			   clearTimeout(time);
			   $('.n-popup .save').click();
			}, p_time);
		};
		if(obj.width){
			p_width = obj.width;
		};
		if(obj.height){
			p_height = obj.height;
		};
		var vWin = $(window);
		if(obj.isrolling){
			vWin = $(document);
		};
		p_y = (vWin.height() - p_height) / 2;
		p_x = (vWin.width() - p_width) / 2;

		if(obj.hidefooterBar){
			conentHeight = p_height - 78;
		}else{
			conentHeight = p_height - 41;
		};
		if(obj.hideheaderBar){
			conentHeight = conentHeight + 47;
		};
		
		if(obj.saveFun){
			formFun = obj.saveFun;
		};
		if(obj.cancelFun){
			cancelFun = obj.cancelFun;
		};

		//拼装页面
		initialize(obj);
		if(!obj.verification){
			$('.n-popup .save').click(function(){
			colse();
			return false;
		});
			}
	};
	
	/**
	 * 设置数据
	 */
	this.setData = function(data){
		callData = data;
	};
	
	this.colse = function(){
		$('.n-popup').remove();
	};
	
	return this;
})();




function Alert(message, type, fun){
	var message; 
	if(type == 'error') {//错误
		message = '<p class="p-error"><span></span>'+message+'</p>';
	}
	else if(type == 'success') {//成功
		message = '<p class="p-succeed"><span></span>'+message+'</p>';
	}
	else {//警告
		message = '<p class="p-warning"><span></span>'+message+'</p>';
	}
	var fun;
	Popup.start({
		width:380,
		height:240,
		title   : '温馨提示',
		content : message,
		saveFun: fun,
		hidefooterBar:true,
		hideheaderBar:false,
		btns : ['确定']
	});
}