Administrator
2022-10-09 23f93897d618a5253a18b65b124196f95891f240
提交 | 用户 | 时间
58d006 1 window.I = {}
A 2 var basic=getpath();
3 var modelview=function() {
4     var self=this;
5     self.filter=ko.observable();
6     self.menus=ko.observableArray();
7     self.mtype=ko.observable(window.mtype);
8     self.mtypetitle=ko.observable($('#mtypetitle').val());
9     self.menu=ko.observable({
10         id:ko.observable(0),
11         title:ko.observable(),
12         icon:ko.observable(),
13         url:ko.observable(),
14         valid:ko.observable(false),
15         trget:ko.observable(),
16         pmenu:ko.observable(),
17         pmenutitle:ko.observable(),
18         mtype:ko.observable(window.mtype)
19     });
20     self.pmenus=ko.observableArray();
21     self.mtypes=ko.observableArray();
22     self.validchg=function(item)
23     {
24         //修改菜单有效无效
25         jQuery.post(basic+'/admin/bmenucon/validchg.htm', {id:item.id(),vld:item.valid(),mtype:window.mtype}, function(data, textStatus, xhr) {
26             if(data==null||data==item.valid())
27                 return;
28             item.valid(data);
29             var lft=item.lft();
30             var rgt=item.rgt();
31             if(data==false)
32             {
33                 jQuery.each(self.menus(), function(index, val) {
34                     if(val.lft()>lft&&val.lft()<rgt)
35                     {
36                         val.valid(data);
37                     }
38                 });
39             }else{
40                 jQuery.each(self.menus(), function(index, val) {
41                     if(val.lft()<lft&&val.rgt()>rgt)
42                     {
43                         val.valid(data);
44                     }
45                 });
46             }
47         },'json');
48     }
49     self.uporder=function(item)
50     {
51         var tts=self.menus();
52         var indx=tts.indexOf(item);
53         var mtype=self.mtype();
54         //向上
55         jQuery.post(basic+'/admin/bmenucon/uporder.htm', {id:item.id(),indx:indx,mtype:mtype}, function(data, textStatus, xhr) {
56             if(data==null)
57                 return;
58             if(data.code==1)
59             {
60                 alert('不能向上移动了!');
61                 return;
62             }    
63             getmenus(0,15,md.filter(),0/*0或1,是否包含pages*/,window.mtype,tts[0].id());
64         },'json');
65     }
66     self.downorder=function(item)
67     {
68         var tts=self.menus();
69         var indx=tts.indexOf(item);
70         //向上
71         jQuery.post(basic+'/admin/bmenucon/downorder.htm', {id:item.id(),indx:indx,length:tts.length,mtype:window.mtype}, function(data, textStatus, xhr) {
72             if(data==null)
73                 return;
74             if(data.code==1)
75             {
76                 alert('不能向下移动了!');
77                 return;
78             }    
79             getmenus(0,15,md.filter(),0/*0或1,是否包含pages*/,window.mtype,tts[0].id());
80         },'json');
81     }
82     self.delmenu=function(item)
83     {
84         if(!confirm('删除会删掉所有子菜单,请问是否确认删除?'))
85             return;
86         jQuery.post(basic+'/admin/bmenucon/delmenu.htm', {id:item.id,mtype:window.mtype}, function(data, textStatus, xhr) {
87           if(data.code==1)
88           {
89               alert(data.errmsg);
90               return;
91           }else
92           {
93               self.menus.remove(item);
94           }
95         },'json');
96         
97     }
98     self.editmenu=function(item)
99     {
100         var tt=item.pmenu();
101         //根据item,初始化menu
102         if(tt<0)
103         {
104             jQuery.post(basic+'/admin/bmenucon/pmenu.htm', {id: item.id}, function(data, textStatus, xhr) {
105                 if(data==null)
106                     return;
107                 if(data.menu!=null)
108                 {
109                     item.pmenu(data.menu.id);
110                     item.pmenutitle(data.menu.title)
111                 }else{
112                     item.pmenu(0);
113                     item.pmenutitle();
114                 }
115             },'json');
116         }
117         if(item.mtype==null)
118             item.mtype=ko.observable(window.mtype);
119         self.menu(item);
120         $('#modal1').modal('show');
121     }
122     self.psel=function(item)
123     {
124         //选中上级菜单
125         $('#modal2').modal('hide');
126         $('#modal1').modal('show');
127         self.menu().pmenu(item.id);
128         self.menu().pmenutitle(item.title);
129         $('#ptitle').text(item.title);
130         $('#pid').val(item.id);
131     }
132
133 }
134 jQuery(document).ready(function($) {
135     window.mtype=$('#mtype').val();
136     var md=window.md=new modelview();
137     ko.applyBindings(md);
138     md.mtypes.push({id:md.mtype(),title:md.mtypetitle()});
139     var pagesize=15;
140     getmenus(0,pagesize,null,1,window.mtype);
141     $('#searcha').click(function(){
142         var filter=md.filter();
143         getmenus(0,pagesize,filter,1,window.mtype);
144     });
145     $('#ctbtn').click(function(){
146         $('#modal1').modal('show');
147             md.menu({
148             id:ko.observable(0),
149             title:ko.observable(),
150             icon:ko.observable(),
151             url:ko.observable(),
152             valid:ko.observable(false),
153             trget:ko.observable(),
154             pmenu:ko.observable(),
155             pmenutitle:ko.observable(),
156             mtype:ko.observable(window.mtype)
157         });
158     });
159     var $fm1=$('#fm1');
160     $fm1.validate({
161         rules:{
162             title:{
163                 required:true,
164                 remote:{
165                     url:basic+'/admin/bmenucon/check.htm',
166                     type:'post',
167                     data:{
168                         title:function(){ return $('#title').val();},
169                         id:function(){return md.menu().id();},
170                         mtype:function(){return md.mtype();}
171                     }
172                 }
173             },
174             URL:{
175                 required:true
176             }
177         },
178         messages:{
179             title:'名字不能为空且不能重复',
180             URL:'url不能为空'
181         },
182         errorPlacement:function(err,element)
183                         {
184                             err.appendTo($('#errmsg'));
185                         }
186     });
187
188     $('#tjbtn').on('click',function(){
189         // var $t=$(this).button('loading');
190         if($fm1.valid()==true)
191         {
192             var tt=md.menu();
193             var obj=ko.mapping.toJS(tt);
194             obj.mtype=tt.mtype();
195             if(obj.id!=0){
196                 obj.level=obj.level.split("-").length;
197             }
198             jQuery.post(basic+'/admin/bmenucon/mnsave.htm', obj, function(data, textStatus, xhr) {
199                 // $t.button('reset');
200                 $('#modal1').modal('hide');
201                 if(data==null)
202                     return;
203                 if(data.code==0)
204                 {
205                     getmenus(0,15,md.filter(),1,window.mtype,data.nid);    
206                 }
207             },'json');
208         }else{
209             // $t.button('reset');
210         }
211     });
212     $('#selparent').click(function(){
213         var tid=0;
214         var mtype=0;
215         if(md.menu!=null)
216         {
217             tid=md.menu().id();
218             mtype=md.mtype();
219         }
220         $('#modal2').modal('show');
221         $('#modal1').modal('hide');
222         getnpmenus(0,10,tid,1,window.mtype);
223     });
224
225     /*
226     var obj={id:1,title:'menu',parent:{id:2,title:'mmmnnn'}};
227     var ttt=ko.mapping.fromJS(obj);
228     alert(ttt);
229     alert(ko.mapping.toJS(ttt));
230     */
231 });
232 function getmenus(page,pagesize,filter,pages/*0或1,是否包含pages*/,mtype,tid){
233     jQuery.post(basic+'/admin/bmenucon/menus.htm', {page:page,pagesize:pagesize,filter:filter,pages:pages,mtype:mtype,tid:tid}, function(data, textStatus, xhr) {
234         if(data==null)
235             return;
236         var items=data.items;
237         window.md.menus.removeAll();
238         jQuery.each(items, function(index, val) {
239             var l=val.level;
240             val.level='';
241             for (var i =  1; i <l; i++) {
242                 val.level+='-';
243             };
244             val.pmenu=-1;
245             val.pmenutitle='';
246             window.md.menus.push(ko.mapping.fromJS(val));
247         });
248         if(data.page!=null)
249             page=data.page;
250         var pages=data.pages;
251         if(pages!=null)
252         {
253             if(window.pages!=pages)
254             {
255                 $('#pagdiv').unbind('page').empty();
256                 $('#pagdiv').bootpag({total:pages,maxVisible:10,page:page+1}).on('page',function(event,num){
257                       getmenus(num-1,pagesize,filter,pages,mtype,0);
258                 });
259             }
260             if(pages<=1){
261                 $('#pagdiv').empty();
262             }
263         }
264     },'json');
265 }
266 //获取非p的menu
267 function getnpmenus(page,pagesize,tid,pages,mtype){
268     jQuery.post(basic+'/admin/bmenucon/npmenus.htm', {page:page,pagesize:pagesize,tid:tid,pages:pages,mtype:mtype}, function(data, textStatus, xhr) {
269         if(data==null)
270             return;
271         var items=data.items;
272         window.md.pmenus.removeAll();
273         jQuery.each(items, function(index, val) {
274             var l=val.level;
275             val.level='';
276             for (var i =  1; i <l; i++) {
277                 val.level+='-';
278             };
279             window.md.pmenus.push(val);
280         });
281         var pages=data.pages;
282         if(pages!=null)
283         {
284             if(window.pages2!=pages)
285             {
286                 window.pages2=pages;
287                 $('#pagdiv2').unbind('page').empty();
288                 $('#pagdiv2').bootpag({total:pages,maxVisible:10,page:1}).on('page',function(event,num){
289                       getnpmenus(num-1,pagesize,tid,0,mtype);
290                 });
291             }
292         }
293     },'json');    
294 }