popup.js 2.55 KB
/**
 * @author Administrator
 */

define( 'popup', function( require, exports ) { 
	var Popup = (function(){
		
		var config = {
			width : 300,
			height : 200,
			x : 0,
			y : 0,
			time : 0,
			c_height : 0,
			determine : function(){},
			cancel : function(){},
			data : null,
			win : $(window),
			isfooter : true,
			isheader : true,
			isrolling : false
		};
		
		
		var views = {
			settle : function(data){
				config = $.extend(config, data);
				if(config.time > 0){isclose()};
				if(config.isrolling){
					config.win = $(document);
				};
				
				config.y = (config.win.height() - config.height) / 2;
				config.x = (config.win.width() - config.width) / 2;
				
				if(config.isheader){
					config.c_height = config.height - 40;
				}else{
					config.c_height = config.height;
				};
				
				this.show();
			},
			isclose : function(){
				var time = setTimeout(function () { 
					clearTimeout(time);
			 		$('.n-popup .cancel').click();
			    }, config.time);
			},
			show : function(){
				$("body .n-popup").remove();//清理原有的	
				var content = "";
				
				if(config.url){
					content = '<iframe src="'+config.url+'" frameborder="no" border="0" allowTransparency="true" ></iframe>';
				}else if(config.content){
					content = config.content;
				};
				var btns = '<a href="#" class="save" >确定</a><a href="#" class="cancel" >取消</a>';
				
				var view =  '<div class="n-popup" style="'+(config.isrolling ? "position:absolute" : "")+'">'
							+'<div class="background" style="'+(config.isrolling ? "position:fixed" : "")+'"></div>'
							+'<div class="n-popup-cont" style="width:'+config.width+'px;height:'+config.height+'px;'
							+'left:'+config.x+'px;top:'+config.y+'px;" >'+(! config.isheader ? '' : '<div class="n-popup-cont-header">'
							+'<p>'+config.title+'</p><a href="#" class="cancel colse"></a></div>')+'<div class="n-popup-cont-content"'
							+' style="height:'+config.c_height+'px;'+(!config.isheader ? 'top:6px;': 'top:47px;')+'" >'+content+'</div>'
					   		+'<div class="n-popup-cont-footer" '+(config.isfooter ? '' : 'style="display: none;"')+'  >'
					   		+btns+'</div></div></div>';
					   		
				$('body').append(view);
				this.events();
			},
			events : function(){
				$('.n-popup .cancel').click(function(){
					config.cancel();
					$('.n-popup').remove();
					return false;
				});
				$('.n-popup .save').click(function(){
					config.determine();
					$('.n-popup').remove();
					return false;
				});
			}
		};
		
		
		this.start = function(data){
			views.settle(data);
		};
		
		return this;
	})();
	
	return Popup;
});