common.js 5.5 KB
/**
 * 
 */
Date.prototype.dateAdd = function(interval, number) {
	var d = this;
	var k = {
		'y' : 'FullYear',
		'q' : 'Month',
		'm' : 'Month',
		'w' : 'Date',
		'd' : 'Date',
		'h' : 'Hours',
		'n' : 'Minutes',
		's' : 'Seconds',
		'ms' : 'MilliSeconds'
	};
	var n = {
		'q' : 3,
		'w' : 7
	};
	eval('d.set' + k[interval] + '(d.get' + k[interval] + '()+'
			+ ((n[interval] || 1) * number) + ')');
	return d;
}
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;
}
function initFormCheckBox() {
	var boxs = $('.checkboxInit');
	if (boxs) {
		for (var i = 0; i < boxs.length; i++) {
			var dataVal = "," + $(boxs[i]).attr('data-value') + ",";
			var checkBoxs = $('.' + $(boxs[i]).attr('m-item'));
			for (var j = 0; j < checkBoxs.length; j++) {
				if (dataVal.indexOf("," + $(checkBoxs[j]).val() + ",") > -1) {
					checkBoxs[j].checked = 'true';
				}
			}
		}
	}
}

function initFormRadioBox() {
	var boxs = $('.radioInit');
	if (boxs) {
		for (var i = 0; i < boxs.length; i++) {
			var dataVal = "," + $(boxs[i]).attr('data-value') + ",";
			var radios = $('.' + $(boxs[i]).attr('m-item'));
			for (var j = 0; j < radios.length; j++) {
				if (dataVal.indexOf("," + $(radios[j]).val() + ",") > -1) {
					radios[j].checked = 'true';
				}
			}
		}
	}
}
function logout(){
	var box = bootbox.confirm({
		size: "small",
	    message: "是否确定要登出系统?",
	    callback: function (result) {
	    	if(result){
	    		$.post(basePath+"/logout", {}, function(data) {
	    			window.location.href=basePath;
	    		}, "json");
	    	}
	    }
	});
	box.css({
	  'top': '55%',
	  'margin-top': function () {
	    return -(box.height() / 2);
	  }
	});	
}

function goLogin(){
	$.post(basePath+"/saveCurrentUrl",{beforeLogin:window.location.href},function(result){
		window.location.href=basePath+"/login";
	});
}
function addViewNumber(tableName,colName,idName,idVal){
	var par = {};
	par.tableName = tableName;
	par.colName = colName;
	par.idName = idName;
	par.id = idVal;
	$.post(basePath+"/common/addViewNumber",par,function(result){
		if($('#likenumber')){
			$('#likenumber').text(parseInt($('#likenumber').text(),10)+1)
		}
	});
}

function initSelectVal() {
	var sels = $("select");
	for (var i = 0; i < sels.length; i++) {
		var val = $(sels[i]).attr('value');
		$(sels[i]).val(val);
	}
}
function initSelectVal2() {
	var selItems = $('.itemForm');
	for (var i = 0; i < selItems.length; i++) {
		$(selItems[i]).val($(selItems[i]).attr('item-value'));
	}
}
function setSelectMenu() {
	var boxs = $('.menuBox');
	var menuIds = [];
	for (var i = 0; i < boxs.length; i++) {
		var item = $('#jqxTree').jqxTree('getItem', boxs[i]);
		if (item.checked) {
			menuIds.push(item.value);
		}
	}
	$('#roleMenu').val(menuIds.join(","));

}
function initFormMultiSelect() {
	var boxs = $('.multiSelect');
	if (boxs) {
		for (var i = 0; i < boxs.length; i++) {
			$(boxs[i]).multiselect({
				selectAllText : ' 全部',
				selectAllValue : '全部',
				filterPlaceholder : '搜索',
				nonSelectedText : '请选择',
				nSelectedText : '选中',
				allSelectedText : '全部',
				maxHeight: 200,
				includeSelectAllOption : true,
				enableCaseInsensitiveFiltering : true
			});
			var val = $(boxs[i]).attr('value');
			if(val){
				val = val.replace('[', ',');
				val = val.replace(']', ',');
				val = "," + val + ",";
				$(boxs[i]).find('option').each(function() {
					if (val.indexOf(',' + this.value + ',') >-1||val.indexOf(', ' + this.value + ',') >-1){
						this.selected = true;
					}
				});
				$(boxs[i]).multiselect("refresh");
			}
		}
	}
}


function alert(message,method){
	var box = bootbox.alert({ 
	  size: "small",
	  message: message,
	  callback:function(){
		  if(method!=undefined){
			  method();
		  }
	  }
	}); 
	
	box.css({
	  'top': '55%',
	  'margin-top': function () {
	    return -(box.height() / 2);
	  }
	});	
}

function popWindow(option) {
	id = 'popWindow';
	if (option.id) {
		id = option.id;
	}
	var frame = $('#' + id).find('iframe');
	if(option.title){
		$('#' + id).find('.popwinTitleText').html(option.title);
		
	}
	frame[0].src = option.url;
	if(option.width){
		option.width = option.width.replace('px','');
		frame[0].width = option.width-6;
		$('#' + id).find('.modal-content').css('width',option.width);
	}else{
		frame[0].width = 600-6;
		$('#' + id).find('.modal-content').css('width',600);
	}
	if(option.height){
		option.width = option.height.replace('px','');
		frame[0].height = option.height-36;
		$('#' + id).find('.modal-content').css('height',option.height);
	}else{
		frame[0].height = 500-26;
		$('#' + id).find('.modal-content').css('height',500);
	}
	$('#'+id).modal();
}

$(document).ready(function() {
	initSelectVal();
	initSelectVal2();
	initFormCheckBox();
	initFormRadioBox();
	initFormMultiSelect();
});