index.js 7.31 KB
$(document).ready(function(){
	$.disableBack();
	//$.beforeUnload();
	var layout=$(document.body).children(".rs-base").children(".rs-layout");
	layout.layout({
		fit:true
	});
	var north=layout.layout("panel","north");
	var west=layout.layout("panel","west");
	var center=layout.layout("panel","center");
	var menu=layout.children(".rs-menu");//$(document.body).children(".rs-menu");
	var menuSearch=north.find(".menu-search input");
	var userMore=north.find(".more");
	var userWindow=$(document.body).children(".rs-user");
	var btnMenu=west.find(".btn-menu");
	var btnHome=west.find(".btn-crm");
	var btnExit=west.find(".btn-exit");
	var tabs=$('<div class="rs-tabs"></div>').appendTo(center).tabs({
		fit : true,
		border : false,
		plain:true,
//		tabHeight:22,
		tabWidth:150,
		onBeforeClose: function(title,index){
			var tab=tabs.tabs("getTab",index);
//			var editors=tab.find(".ckeditor");
//			for (var i = 0; i < editors.length; i++) {
//				var editor = $(editors[i]);
//				
//			}
			return true;
		},
		onAdd:function(title,index){
			$(this).tabs('getTab',index).addClass('rs-box');
		}
	}).tabs("add", {
		title : '首页',
		href : './home'
	});
	initMenu();
	initSearchMenu();
	initUser();
	/*事件绑定*/
	layout.bind("_resize.menu",function(){
		menu.height(west.height());
	});
	btnMenu.bind("click.menu",function(){
		if(btnMenu.hasClass("shrink")){
			menuShrink();
		}else{
			menuExpand();
		}
	});
	btnHome.bind("click.home",function(){
		tabs.tabs("select",0);
	});
	btnExit.bind("click.exit",function(){
		$.messager.confirm(resource.msg_title, resource.onExit,function(r){
			if (r){
				window.location.href="./logout";
			}
		});
	});
//	menu.bind("mouseleave.menu",function(){
//		menuShrink();
//	});
	$(document).bind("click.menu",function(e){ //收菜单
		var obj=$(e.target);
//		if(obj.closest(".pl-home-page")){ //
//			return;
//		}
		var m=obj.closest(".rs-menu,.pl-home-module");
		if(!obj.hasClass("btn-menu")&&m&&m.length==0){
			menuShrink();
		}
	});
	function initMenu() {
		menu.rs_load().css({
			height:west.height(),
			top:north.height()
		}).rs_load("show");
		menuLoad('./menu/select.json');
	}
	function initSearchMenu(){
		menuSearch.combobox({
			prompt:resource.index.menuSearch,
			valueField:'id',
			textField: 'text',
			editable:true,
			panelWidth:260,
			panelHeight:'auto',
			panelMaxHeight:200,
			hasDownArrow:false,
			url:'./menu/select/search.json',
			loadFilter: loadFilterUtils,
			filter: function(q, row){
				var opts = $(this).combobox('options');
				//添加判断子菜单是否匹配
				var isFind=row[opts.textField].indexOf(q) >= 0;
				if(!isFind){
					for (var i = 0; i < row.child.length; i++) {
						if(row.child[i].indexOf(q) >= 0){
							isFind=true;
							break;
						}
					}
				}
				return isFind;
			},
			formatter: function(row){
				var opts = $(this).combobox('options');
				//生成子菜单内容
				row.child=[];
				var html=row[opts.textField];
				appendChild(row.children,1);
				return html;
				function appendChild(items,c){
					if(items){
						var t=c+1;
						for (var i = 0; i < items.length; i++) {
							if(items[i]&&items[i].text){
								row.child.push(items[i].text);
								html+=('<div class="nav-select">'+appendSpace(c)+items[i].text+'</div>');
								appendChild(items[i].children,t);
							}
						}
					}
				}
				function appendSpace(c){
					var space="";
					for (var i = 0; i < c; i++) {
						space+=("<i class='out'></i>");
					}
					return space;
				}
			},
			onHidePanel:function(){
				var t=menuSearch.combobox("getValue");
				if(t){
					data=menuSearch.combobox("getData");
					if(data){
						for (var i = 0; i < data.length; i++) {
							if(data[i]&&data[i].id==t&&data[i].url){
								showTabUtils({text:formatMenuName(data[i].text),url:data[i].url});
								break;
							}
						}
					}
				}
			}
		});
		menuSearch.combo('panel').parent().addClass("menu-search-combo");
		menuSearch.parent().find(".textbox-text").bind("focus.menuSearch",function(){
			$(this).select();
		});
	}
	function initUser(){
		userWindow.window({
			title:'用户信息'
		}).tabs({
			fit:true,
			tabPosition:'left'
		});
		var pwdPanel=userWindow.tabs("getTab",0)
		userWindow.tabs('update', {
			tab: pwdPanel,
			options: {
				title: '密码修改'
			}
		});
		var pwdForm=pwdPanel.find("form.password");
		var lblOldPwd=pwdForm.find("div.oldpwd");
		var txtOldPwd=pwdForm.find("input.oldpwd").textbox({
			required:true,
			invalidMessage:'请输入旧密码',
			type:'password'
		});
		var lblNewPwd=pwdForm.find("div.newpwd");
		var txtNewPwd=pwdForm.find("input.newpwd").textbox({
			required:true,
			invalidMessage:'请输入新密码',
			type:'password'
		});
		var lblRepPwd=pwdForm.find("div.repeatpwd");
		var txtRepPwd=pwdForm.find("input.repeatpwd").textbox({
			required:true,
			invalidMessage:'请输入重复密码',
			type:'password'
		});
		var btnConfirm=pwdForm.find(".confirm");
		btnConfirm.bind("click.password",function(){
			var oldpwd=$.trim(txtOldPwd.textbox("getValue"));
			var newpwd=$.trim(txtNewPwd.textbox("getValue"));
			var reppwd=$.trim(txtRepPwd.textbox("getValue"));
			if(oldpwd==newpwd){ //cn.runsa.password.newPwdNotMatch
				$.messager.alert(resource.msg_title,resource.cn_runsa_password.newPwdNotMatch);
				return;
			}
			if(reppwd!=newpwd){//cn.runsa.password.repeatPwdNotMatch
				$.messager.alert(resource.msg_title,resource.cn_runsa_password.repeatPwdNotMatch);
				return;
			}
			if(newpwd.length<8){//cn.runsa.password.newpwdLenthError
				$.messager.alert(resource.msg_title,resource.cn_runsa_password.newpwdLenthError);
				return;
			}
			if(pwdForm.form("validate")){
				$.messager.progress();
				var data={
					oldPwd:oldpwd,
					newPwd:newpwd,
					repPwd:reppwd
				};
				$.post("./users/password/update.json",data,function(json){
					$.messager.progress('close');
					if(responseUtils(json)){
						$.messager.alert(resource.msg_title,'密码已修改,请重新登录.','info',function(){
							window.location.href="./logout";
						});
					}
				});
			}
		});
		userMore.bind("click.user",function(){
			userWindow.show().window("center").window("open");
		});
	}
	function formatMenuName(name){
		var i=name.lastIndexOf("(");
		return i>0?name.substring(0,i):name;
	}
});
var animateSpeed=500;
function menuShrink(){
	var layout=$(document.body).children(".rs-base").children(".rs-layout");
	var west=layout.layout("panel","west");
	var btnMenu=west.find(".btn-menu");
	var menu=layout.children(".rs-menu");
	btnMenu.removeClass("shrink");
	menu.animate({ 
		left:-menu.width()+west.width()
	}, animateSpeed);
}
function menuExpand(){
	var layout=$(document.body).children(".rs-base").children(".rs-layout");
	var west=layout.layout("panel","west");
	var btnMenu=west.find(".btn-menu");
	var menu=layout.children(".rs-menu");
	btnMenu.addClass("shrink");
	menu.animate({ 
		left:west.width()
	}, animateSpeed);
}
function menuLoad(url){
	var layout=$(document.body).children(".rs-base").children(".rs-layout");
	var menu=layout.children(".rs-menu");
	menu.tree({
		url:url,
		lines:false,
		checkbox:false,
		onClick: function(node){			
			if(node.state=="open"){
				$(this).tree("collapse",node.target);
			}else{
				$(this).tree("expand",node.target);
				showTabUtils(node);
			}
			if(node.url&&node.url!=""){
				menuShrink();
			}
		},
		onLoadSuccess:function(node, data){
			$(this).rs_load("hide");
		},
		onSelect : showTabUtils
	});
}