app.js 6.71 KB
var App = function() {

	return {
		/** date-box 格式化日期 */
		formatDate : function(date) {
			return date.getFullYear() + "-" + (date.getMonth() + 1) + "-"
					+ date.getDate();
		},

		formatDateTime : function(date) {
			return date.getFullYear() + "-" + (date.getMonth() + 1) + "-"
					+ date.getDate() + " " + date.getHours() + ":"
					+ date.getMinutes() + ":" + date.getSeconds();
		},

		/** GridView 格式化Bool列 */
		formatBool : function(value, row, index) {
			if (value == 1) {
				return "是";
			} else if (value == 0) {
				return "否";
			}
			return value;
		},
		
		/**GridView单元格鼠标悬停提示*/
		formatCellTooltip : function (value, row, index) {
			return "<span title='" + value + "'>" + value + "</span>";
		},

		/** 判断字符串是否为null或空字符串 */
		isBlank : function(data) {
			return data == null || data == '';
		},

		/** 打开标签页 */
		openTab : function(title, url) {
			if ($('#tabs').tabs('exists', title)) {
				$("#tabs").tabs('select', title);
			} else {
				// 右键关闭会传过来null
				if (url == null) {
					return;
				}
				var content = '<iframe style="width:100%;height:100%;" scrolling="auto" frameborder="0" src="/iframeMenu?url='
						+ url + '"></iframe>';
				parent.$("#tabs").tabs('add', {
					title : title,
					content : content,
					closable : true
				});
			}
		},
		
		
		/**转页面打开标签页*/
		openTurnTab : function(title, url) {
			if (parent.$('#tabs').tabs('exists', title)) {
				parent.$("#tabs").tabs('select', title);
			} else {
				// 右键关闭会传过来null
				if (url == null) {
					return;
				}
				url = encodeURI(encodeURI(url));
				var content = '<iframe style="width:100%;height:100%;" scrolling="auto" frameborder="0" src="/iframeTurnMenu?url='
						+ url + '"></iframe>';
				parent.$("#tabs").tabs('add', {
					title : title,
					content : content,
					closable : true
				});
			}
		},
		
		/** 初始化省市级联控件 */
		initAddress : function() {
			if ($('#combobox-province') == null) {
				return;
			}

			$('#combobox-province').combobox(
					{
						onShowPanel : function() {
							$('#combobox-province').combobox('reload',
									'/pub/area?level=0');
						}
					});

			$('#combobox-city').combobox(
					{
						onShowPanel : function() {
							var province = $('#combobox-province').combobox(
									'getValue');
							if (App.isBlank(province)) {
								$('#combobox-city').combobox('hidePanel');
								$.messager.alert('消息', '请选择省份', 'warning');
							} else {
								$('#combobox-city').combobox('reload',
										'/pub/area?pCode=' + province);
							}
						}
					});

			$('#combobox-area')
					.combobox(
							{
								onShowPanel : function() {
									var city = $('#combobox-city').combobox(
											'getValue');
									if (App.isBlank(city)) {
										$('#combobox-area').combobox(
												'hidePanel');
										$.messager.alert('消息', '请选择城市',
												'warning');
									} else {
										$('#combobox-area').combobox('reload',
												'/pub/area?pCode=' + city);
									}
								}
							});
		},

		/** 关闭新增/修改窗口 */
		closeAddDialog : function() {
			$('#add-dialog').dialog('close');
			$('#add-form').form('clear');
		},

		/** 关闭新增/修改窗口 */
		closeDialog : function(dialog, form) {
			$('#' + dialog).dialog('close');
			if (form != null && form != '') {
				$('#' + form).form('clear');
			}
		},
		
		/**
		 * 将form封装成DataGrid的查询参数
		 * @formId 表单Id
		 */
		dataGridQueryParams : function(formId) {
			var params = new Object();
			$.each($('#' + formId).serializeArray(), function(i, item) {
				params[item.name] = item.value;
			});
			return params;
		},
		
		/**
		 * 在页面中任何嵌套层次的窗口中获取顶层窗口
		 * 
		 * @return 当前页面的顶层窗口对象
		 */
		getTopWinow : function() {
			var p = window;
			while (p != p.parent) {
				p = p.parent;
			}
			return p;
		},
		
		/**
		 * 单选按钮组默认选中
		 * @param name 按钮组名称
		 * @param selectValue 选中项的值
		 */
		selectRadio : function(name, selectValue) {
			$('[name="' + name + '"]:radio').each(function() {
				if (this.value == selectValue) {
					this.checked = "checked";
				}
			});
		}
	}

}();

(function($) {
	
	//回车事件:查询
	document.onkeydown = function(event) {
		if(event.keyCode == 13) {
			var buttons = $('#search-form').find($('.easyui-linkbutton'));
			if (buttons.length > 0) {
				buttons[0].click();
			}
		}
	}
	
	/** 确认密码验证 */
	$.extend($.fn.validatebox.defaults.rules, {
		equals : {
			validator : function(value, param) {
				return value == $(param[0]).val();
			},
			message : '两次密码输入应相同'
		}
	});

	$.ajaxSetup({
		complete : function(request, textStatus) {
			// 通过XMLHttpRequest取得响应头,sessionstatus,
			var sessionstatus = request.getResponseHeader("sessionstatus");
			if (sessionstatus == 'timeout') {
				// 如果超时就处理 ,指定要跳转的页面
				var top = App.getTopWinow(); // 获取当前页面的顶层窗口对象
				top.location.href = _ctx + "/pub/login"; // 跳转到登陆页面
			}
			
			//访问频率
			var timerInterval = request.getResponseHeader('TimeInterval');
			if (timerInterval != null && timerInterval != '') {
				$.messager.alert('提示', '操作间隔为' + timerInterval + '秒,请不要频繁操作');
			}
		}
	});
	
	/**jQuery插件,将form表单序列化成JSON对象*/
	$.fn.serializeJson = function() {
		var json = {};
		$(this.serializeArray()).each(function() {
			json[this.name] = this.value;
		});
		return json;
	};
	
	// 对Date的扩展,将 Date 转化为指定格式的String
	// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, 
	// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
	// 例子: 
	// (new Date()).format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
	// (new Date()).format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
	Date.prototype.format = function (fmt) {
	    var o = {
	        "M+": this.getMonth() + 1, //月份 
	        "d+": this.getDate(), //日 
	        "h+": this.getHours(), //小时 
	        "m+": this.getMinutes(), //分 
	        "s+": this.getSeconds(), //秒 
	        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
	        "S": this.getMilliseconds() //毫秒 
	    };
	    if (/(y+)/.test(fmt)) {
	    	fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	    }
		for ( var k in o) {
			if (new RegExp("(" + k + ")").test(fmt)) {
				fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
			}
		}
		return fmt;
	}
})(jQuery);