rs.load.js 2.96 KB
/**
 * rs_load - Runsa Plugin
 * 
 * author:yijun
 * 
 * Dependencies:
 *   
 */
(function($){
	function initWrap(container){
		var opts = $.data(container, 'rs_load').options;
		var cc=$(container);
		var ghost,center,icon,label;
		
		if(cc.is(".rs-load")){
			ghost=cc.children(".load-ghost");
		}else{
			cc.addClass("rs-load");
			ghost=$('<div class="load-ghost"></div>').appendTo(cc);
		}
		if(opts.cls){
			cc.addClass(opts.cls);
		}
		if(opts.position){
			cc.css("position",opts.position);
		}
		ghost.children().remove();
		if($(document.body).find(".rs-size-calc").length==0){
			$(document.body).append('<div class="rs-size-calc"></div>');
		}
		center=$('<div></div>').appendTo(ghost);
		if(opts.location=="center"){
			center.addClass("load-center");
		}else if(opts.location=="topleft"){
			center.addClass("load-topleft");
		}
		icon=$('<div class="load-icon icon-loading"></div>').appendTo(center);
		if(opts.iconCls){
			icon.addClass(opts.iconCls);
		}
		if(opts.label){
			label=$('<div class="load-label"></div>').appendTo(center);
			label.html(opts.label);
			if(opts.labelCls){
				label.addClass(opts.labelCls);
			}
		}else{
			center.height(icon.height());
		}
	};
	function resize(container){
		var cc=$(container);
		var opts = $.data(container, 'rs_load').options;
		var ghost=cc.children(".load-ghost");
		var center=ghost.children(".load-center");
		var label=center.children(".load-label");
		var labelGhost=$(document.body).find(".rs-size-calc");
		labelGhost.css({
			fontFamily:label.css("font-family"),
			fontSize:label.css("font-size"),
			fontWeight:label.css("font-weight")
		})
		labelGhost.html(opts.label);
		
		var top,left;
		top=Math.floor(center.outerHeight()/2);
		left=Math.floor(labelGhost.outerWidth()/2);
		center.css({
			marginTop:-top,
			marginLeft:-left
		});
	}
	function show(container){
		var cc=$(container);
		var ghost=cc.children(".load-ghost");
		ghost.show();
		resize(container);
	}
	function hide(container){
		var cc=$(container);
		var ghost=cc.children(".load-ghost");
		setTimeout(function(){
			ghost.hide();
		},200);
	}
	function destroy(container){
		$.removeData(container, 'rs_load');
	}
	$.fn.rs_load = function(options, param){
		if (typeof options == 'string') {
			return $.fn.rs_load.methods[options](this, param);
		}
		options = options || {};
		return this.each(function(){
			var state = $.data(this, 'rs_load');
			if (state) {
				$.extend(state.options, options);
			} else {
				var opts=$.extend({},$.fn.rs_load.defaults,options);
				$.data(this, 'rs_load', {options:opts});
			}
			initWrap(this);
		});
	};
	$.fn.rs_load.methods={
		show:function(jq){
			return jq.each(function() {
				show(this);
			});
		},
		hide:function(jq){
			return jq.each(function() {
				hide(this);
			});
		},
		destroy:function(jq){
			return jq.each(function() {
				destroy(this);
			});
		}
	};
	$.fn.rs_load.defaults={
		location:'center',
		cls:null,
		iconCls:null,
		label:'Loading,please wait...',
		labelCls:null,
		position:null
	};
})(jQuery);