window.I = {}
|
var basic=getpath();
|
var modelview=function() {
|
var self=this;
|
self.filter=ko.observable();
|
self.menus=ko.observableArray();
|
self.mtype=ko.observable(window.mtype);
|
self.mtypetitle=ko.observable($('#mtypetitle').val());
|
self.menu=ko.observable({
|
id:ko.observable(0),
|
title:ko.observable(),
|
icon:ko.observable(),
|
url:ko.observable(),
|
valid:ko.observable(false),
|
trget:ko.observable(),
|
pmenu:ko.observable(),
|
pmenutitle:ko.observable(),
|
mtype:ko.observable(window.mtype)
|
});
|
self.pmenus=ko.observableArray();
|
self.mtypes=ko.observableArray();
|
self.validchg=function(item)
|
{
|
//修改菜单有效无效
|
jQuery.post(basic+'/admin/bmenucon/validchg.htm', {id:item.id(),vld:item.valid(),mtype:window.mtype}, function(data, textStatus, xhr) {
|
if(data==null||data==item.valid())
|
return;
|
item.valid(data);
|
var lft=item.lft();
|
var rgt=item.rgt();
|
if(data==false)
|
{
|
jQuery.each(self.menus(), function(index, val) {
|
if(val.lft()>lft&&val.lft()<rgt)
|
{
|
val.valid(data);
|
}
|
});
|
}else{
|
jQuery.each(self.menus(), function(index, val) {
|
if(val.lft()<lft&&val.rgt()>rgt)
|
{
|
val.valid(data);
|
}
|
});
|
}
|
},'json');
|
}
|
self.uporder=function(item)
|
{
|
var tts=self.menus();
|
var indx=tts.indexOf(item);
|
var mtype=self.mtype();
|
//向上
|
jQuery.post(basic+'/admin/bmenucon/uporder.htm', {id:item.id(),indx:indx,mtype:mtype}, function(data, textStatus, xhr) {
|
if(data==null)
|
return;
|
if(data.code==1)
|
{
|
alert('不能向上移动了!');
|
return;
|
}
|
getmenus(0,15,md.filter(),0/*0或1,是否包含pages*/,window.mtype,tts[0].id());
|
},'json');
|
}
|
self.downorder=function(item)
|
{
|
var tts=self.menus();
|
var indx=tts.indexOf(item);
|
//向上
|
jQuery.post(basic+'/admin/bmenucon/downorder.htm', {id:item.id(),indx:indx,length:tts.length,mtype:window.mtype}, function(data, textStatus, xhr) {
|
if(data==null)
|
return;
|
if(data.code==1)
|
{
|
alert('不能向下移动了!');
|
return;
|
}
|
getmenus(0,15,md.filter(),0/*0或1,是否包含pages*/,window.mtype,tts[0].id());
|
},'json');
|
}
|
self.delmenu=function(item)
|
{
|
if(!confirm('删除会删掉所有子菜单,请问是否确认删除?'))
|
return;
|
jQuery.post(basic+'/admin/bmenucon/delmenu.htm', {id:item.id,mtype:window.mtype}, function(data, textStatus, xhr) {
|
if(data.code==1)
|
{
|
alert(data.errmsg);
|
return;
|
}else
|
{
|
self.menus.remove(item);
|
}
|
},'json');
|
|
}
|
self.editmenu=function(item)
|
{
|
var tt=item.pmenu();
|
//根据item,初始化menu
|
if(tt<0)
|
{
|
jQuery.post(basic+'/admin/bmenucon/pmenu.htm', {id: item.id}, function(data, textStatus, xhr) {
|
if(data==null)
|
return;
|
if(data.menu!=null)
|
{
|
item.pmenu(data.menu.id);
|
item.pmenutitle(data.menu.title)
|
}else{
|
item.pmenu(0);
|
item.pmenutitle();
|
}
|
},'json');
|
}
|
if(item.mtype==null)
|
item.mtype=ko.observable(window.mtype);
|
self.menu(item);
|
$('#modal1').modal('show');
|
}
|
self.psel=function(item)
|
{
|
//选中上级菜单
|
$('#modal2').modal('hide');
|
$('#modal1').modal('show');
|
self.menu().pmenu(item.id);
|
self.menu().pmenutitle(item.title);
|
$('#ptitle').text(item.title);
|
$('#pid').val(item.id);
|
}
|
|
}
|
jQuery(document).ready(function($) {
|
window.mtype=$('#mtype').val();
|
var md=window.md=new modelview();
|
ko.applyBindings(md);
|
md.mtypes.push({id:md.mtype(),title:md.mtypetitle()});
|
var pagesize=15;
|
getmenus(0,pagesize,null,1,window.mtype);
|
$('#searcha').click(function(){
|
var filter=md.filter();
|
getmenus(0,pagesize,filter,1,window.mtype);
|
});
|
$('#ctbtn').click(function(){
|
$('#modal1').modal('show');
|
md.menu({
|
id:ko.observable(0),
|
title:ko.observable(),
|
icon:ko.observable(),
|
url:ko.observable(),
|
valid:ko.observable(false),
|
trget:ko.observable(),
|
pmenu:ko.observable(),
|
pmenutitle:ko.observable(),
|
mtype:ko.observable(window.mtype)
|
});
|
});
|
var $fm1=$('#fm1');
|
$fm1.validate({
|
rules:{
|
title:{
|
required:true,
|
remote:{
|
url:basic+'/admin/bmenucon/check.htm',
|
type:'post',
|
data:{
|
title:function(){ return $('#title').val();},
|
id:function(){return md.menu().id();},
|
mtype:function(){return md.mtype();}
|
}
|
}
|
},
|
URL:{
|
required:true
|
}
|
},
|
messages:{
|
title:'名字不能为空且不能重复',
|
URL:'url不能为空'
|
},
|
errorPlacement:function(err,element)
|
{
|
err.appendTo($('#errmsg'));
|
}
|
});
|
|
$('#tjbtn').on('click',function(){
|
// var $t=$(this).button('loading');
|
if($fm1.valid()==true)
|
{
|
var tt=md.menu();
|
var obj=ko.mapping.toJS(tt);
|
obj.mtype=tt.mtype();
|
if(obj.id!=0){
|
obj.level=obj.level.split("-").length;
|
}
|
jQuery.post(basic+'/admin/bmenucon/mnsave.htm', obj, function(data, textStatus, xhr) {
|
// $t.button('reset');
|
$('#modal1').modal('hide');
|
if(data==null)
|
return;
|
if(data.code==0)
|
{
|
getmenus(0,15,md.filter(),1,window.mtype,data.nid);
|
}
|
},'json');
|
}else{
|
// $t.button('reset');
|
}
|
});
|
$('#selparent').click(function(){
|
var tid=0;
|
var mtype=0;
|
if(md.menu!=null)
|
{
|
tid=md.menu().id();
|
mtype=md.mtype();
|
}
|
$('#modal2').modal('show');
|
$('#modal1').modal('hide');
|
getnpmenus(0,10,tid,1,window.mtype);
|
});
|
|
/*
|
var obj={id:1,title:'menu',parent:{id:2,title:'mmmnnn'}};
|
var ttt=ko.mapping.fromJS(obj);
|
alert(ttt);
|
alert(ko.mapping.toJS(ttt));
|
*/
|
});
|
function getmenus(page,pagesize,filter,pages/*0或1,是否包含pages*/,mtype,tid){
|
jQuery.post(basic+'/admin/bmenucon/menus.htm', {page:page,pagesize:pagesize,filter:filter,pages:pages,mtype:mtype,tid:tid}, function(data, textStatus, xhr) {
|
if(data==null)
|
return;
|
var items=data.items;
|
window.md.menus.removeAll();
|
jQuery.each(items, function(index, val) {
|
var l=val.level;
|
val.level='';
|
for (var i = 1; i <l; i++) {
|
val.level+='-';
|
};
|
val.pmenu=-1;
|
val.pmenutitle='';
|
window.md.menus.push(ko.mapping.fromJS(val));
|
});
|
if(data.page!=null)
|
page=data.page;
|
var pages=data.pages;
|
if(pages!=null)
|
{
|
if(window.pages!=pages)
|
{
|
$('#pagdiv').unbind('page').empty();
|
$('#pagdiv').bootpag({total:pages,maxVisible:10,page:page+1}).on('page',function(event,num){
|
getmenus(num-1,pagesize,filter,pages,mtype,0);
|
});
|
}
|
if(pages<=1){
|
$('#pagdiv').empty();
|
}
|
}
|
},'json');
|
}
|
//获取非p的menu
|
function getnpmenus(page,pagesize,tid,pages,mtype){
|
jQuery.post(basic+'/admin/bmenucon/npmenus.htm', {page:page,pagesize:pagesize,tid:tid,pages:pages,mtype:mtype}, function(data, textStatus, xhr) {
|
if(data==null)
|
return;
|
var items=data.items;
|
window.md.pmenus.removeAll();
|
jQuery.each(items, function(index, val) {
|
var l=val.level;
|
val.level='';
|
for (var i = 1; i <l; i++) {
|
val.level+='-';
|
};
|
window.md.pmenus.push(val);
|
});
|
var pages=data.pages;
|
if(pages!=null)
|
{
|
if(window.pages2!=pages)
|
{
|
window.pages2=pages;
|
$('#pagdiv2').unbind('page').empty();
|
$('#pagdiv2').bootpag({total:pages,maxVisible:10,page:1}).on('page',function(event,num){
|
getnpmenus(num-1,pagesize,tid,0,mtype);
|
});
|
}
|
}
|
},'json');
|
}
|