rs.utils.js 5.1 KB
/**
 * Runsa Function
 * 
 * author:yijun
 * 
 * Dependencies:
 *   
 */
(function($) {
	/*
	$.fn.yourPluginName = function(options) {
		// 各种属性和参数

		var options = $.extend(defaults, options);

		this.each(function() {
			// 插件的实现代码

		});
	};
	*/
	$.appendZero = function(num, n) {
		return (Array(n).join(0) + num).slice(-n);
	}
	/**
	 * px to int
	 */
	$.parseCssInt = function(a) {
		return parseInt(a.replace("px", ""));
	}
	$.JSON = function(a, b, c) {
		var url = a || "./json";
		var data;
		var callback;
		for (var i = 0; i < arguments.length; i++) {
			if (typeof (arguments[i]) == "string") {
				url = arguments[i];
			} else if (typeof (arguments[i]) == "object") {
				data = arguments[i];
			} else if (typeof (arguments[i]) == "function") {
				callback = arguments[i];
			}
		}
		$.ajax({
			url : url,
			data : JSON.stringify(data),
			type : "POST",
			contentType : "application/json; charset=utf-8",
			success : callback
		});
	}
	$.getJSONP = function(a, b, c) {
		var url = a || ".jsonp";
		var data;
		var callback;
		for (var i = 0; i < arguments.length; i++) {
			if (typeof (arguments[i]) == "string") {
				url = arguments[i];
			} else if (typeof (arguments[i]) == "object") {
				data = arguments[i];
			} else if (typeof (arguments[i]) == "function") {
				callback = arguments[i];
			}
		}

		$.ajax({
			url : url,
			data : data,
			dataType : 'jsonp',
			success : callback
		});
	};
	/*检验返回格式*/
	$.response=function(result) {
		var success=false;
		var data=(typeof result == 'string')?eval('(' + result + ')'):result;
		if(data.status){
			success=data && data.status == "success";
			if (!success) {
				$.messager.alert(resource.msg_title,resourceUtils(data.message));
			}
		}
		return success;
	}
	/*加载过滤 */
	$.loadFilterUtils=function(data,format){
		var val;
		var success=responseUtils(data);
		var control=$(this);
		var cls=control.attr("class");
		if(format && typeof (format[i]) == "function"){
			format.call(this, data.list);
		}
		if(cls.indexOf("datagrid")>-1){
			return success?{rows:data.list,total:data.total}:{rows:[],total:0};
		}else if(cls.indexOf("tree")>-1){//tree
			return data.list;
		}else{
			return data.list;
		}
	}
	/**
	 * enable request thread
	 */
	var threadList = {};
	var threadSeed = 1;
	var threadName = "taskThread";
	$.taskThread = function(a, b) { // 任务线程
		var options = a;
		var id = b;
		if (typeof options == 'string') {
			if (id) {
				$.taskThread.methods[options](id);
			}
		} else {
			var obj = {};
			var opts = $.extend({}, $.taskThread.defaults, options);
			if (!opts.id) {
				opts.id = threadName + (threadSeed++);
			}
			$.data(document, opts.id, opts);
			threadList[opts.id] = null;
			$.taskThread.methods["start"](opts.id);
		}

	};
	$.taskThread.methods = {
		start : function(id) {
			var opts = $.data(document, id);
			if (opts) {
				if (threadList[opts.id]) {
					clearInterval(threadList[opts.id]);
				}
				threadList[opts.id] = setInterval(function() {
					var params = opts.onBegin.call(document, opts.params);
					if (opts.dataType == "jsonp") {
						$.JSONP(opts.url, params, opts.onSuccess);
					} else {
						$.ajax({
							url : opts.url,
							data : params,
							type : opts.type,
							dataType : opts.dataType,
							success : opts.onSuccess
						});
					}
					opts.onEnd.call(document, opts.id);
				}, opts.delay);
			}
		},
		stop : function(id) {
			var opts = $.data(document, id);
			if (opts && threadList[opts.id]) {
				clearInterval(threadList[opts.id]);
			}
		}
	};
	$.taskThread.defaults = {
		id : null,
		delay : 60000,
		start : true,
		url : null,
		type : "post",
		dataType : "jsonp",
		params : {},
		onBegin : function(p) {
			return p;
		},
		onSuccess : function(o) {
		},
		onEnd : function() {
		}
	}
	$.encodeParamURI = function(a, b) {
		var url;
		var data;
		for (var i = 0; i < arguments.length; i++) {
			if (typeof (arguments[i]) == "string") {
				url = arguments[i];
			} else if (typeof (arguments[i]) == "object") {
				data = arguments[i];
			}
		}
		var existsParam = url && url.indexOf("?") > -1;

		for (var key in data) {
			if(data[key]!=null){
				var link = existsParam ? "&" : "?";
				url += (link + encodeURIComponent(key) + "=" + encodeURIComponent(data[key]));
				if (!existsParam) {
					existsParam = true;
				}
			}
		}
		return url;
	}
	/* 禁用后退 */
	$.disableBack=function(){
		$(document).bind("keydown",function(e){
			if(e.keyCode==8){
				var t=e.target.type || e.target.getAttribute('type');
				var readonly = e.target.getAttribute('readonly');  
				if(t&&(t=="password" || t=="text" || t=="textarea")&&!(readonly=="readonly")){
					return true;
				}
//				if (e&&e.stopPropagation){
//				    e.stopPropagation(); 
//				}else{
//				    window.event.cancelBubble = true; 
//				}
				return false;
			}
			return true;
		});
	}
	$.beforeUnload = function(a) {
		// 各种属性和参数
		var message = a || "";

		window.onbeforeunload = function(e) { // 绑定刷新等事件
			if (e && e.preventDefault) {
				e.preventDefault();
			} else if (window.event) {
				window.event.returnValue = message;
			}
			return message;
		}
	};
})(jQuery);