hjg
2023-10-14 f6c2f15c37aef4675dda01fb5ec17cde4e141c3f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
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');    
}