hjg
2024-10-30 8cf23534166c07e711aac2a25911ada317ba01f0
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
/*
*  the param is: url,data:json[[id,title,lft,rgt,pid]] 格式,
*/
(function($,window){
    jQuery.fn.mtree=function(options){
        var $owner=this,
            settings={url:'',data:'',collapse:true,folderitemcallback:function(id,object){return false;},itemcallback:function(id,object){return false;}};
            settings=jQuery.extend(settings, $owner.settings || {}, options || {});
            //todo:...
        $owner.settings=settings;
         var treeroot=jQuery('<ul class="mtree" lft="0" rgt="99999"></ul>');
 
        $owner.on('click',':checkbox.mtree_item',function(){
            var theurl=settings.url;
            var $tthis=$(this);
            if(!$tthis.is(':checked'))
                return;
            var id=$tthis.val();
            if(id==null||id=='')
                return;
            if(theurl!=null && theurl!='')
            {
                var pnode=$tthis.parent('li');
                var ulnode=pnode.children('ul');
                if(!ulnode.is('UL'))
                {
                    jQuery.getJSON(theurl, {id:id}, function(json, textStatus) {
                      //optional stuff to do after success
                      if(json==null||json=='')
                          return;
                      parsedata($tthis,json,settings.collapse);
                    });
                    
                }
            }
            var folderitemcallback=settings.folderitemcallback;
            folderitemcallback(id);
        });
        $owner.live('click','a.mtree_item_a',function(){
            var id=$(this).attr('rev')
            var itemcallback=settings.itemcallback;
            itemcallback(id);
        });
         function parsedata(treenode,data,collapse)
         {
             var plft=treenode.attr('lft');
             var prgt=treenode.attr('rgt');
             var pnode=treenode;
             var tnode=null;
             var valindex;
             for (valindex in data) {
                   var val=data[valindex];
                   var lft=val[2];
                   var rgt=val[3];
                   var id=val[0];
                   var title=val[1];
                   var pid=val[4];
                   var tempnode=jQuery('<li lft="'+lft+'" rgt="'+rgt+'"></li>');
                   if(rgt-lft>1)
                   {
                       var checkobj=null;
                       if(collapse==true||collapse=='true')
                           checkobj=jQuery('<input type="checkbox" class="mtree_item" id="item-'+id+'" value="'+id+'" checked="checked"/>');
                       else
                           checkobj=jQuery('<input type="checkbox" class="mtree_item" id="item-'+id+'" value="'+id+'" />');
                           var labelobj=jQuery('<label for="item-'+id+'">'+title+'</label>');    
                           checkobj.appendTo(tempnode);
                        labelobj.appendTo(tempnode);
                   }else{
                       var aobj=jQuery('<a href="javascript:void(0);" class="mtree_item_a" id="item-'+id+'" rev="'+id+'">'+title+'</a>')
                       aobj.appendTo(tempnode);
                   }                   
             
                var linode=tempnode;
                   while(true)
                   {
                       plft=pnode.attr('lft');
                       prgt=pnode.attr('rgt');
                       if(lft>plft&&lft<prgt)
                       {
                           if(pnode.is("UL"))
                           {
                               pnode.append(linode);
                           }else
                           {
                               var tnode=pnode.children("UL");
                               if(!tnode.is("UL"))
                                   tnode=jQuery('<ul lft="'+plft+'" rgt="'+prgt+'"></ul>');
                               tnode.append(linode);
                               pnode.append(tnode);    
                           }
                           pnode=linode;
                           break;
                       }else{
                           var tppnode=pnode.parent("li");
                           if(!tppnode.is("LI"))
                           {
                               tppnode=pnode.parent("ul");
                               if(!tppnode.is("UL"))
                                   break;
                           }
                           pnode=tppnode;
                       }
                   }
               }
         }
         var url=$owner.settings.url;
         var jsondata=null;
         if (url!=null&&url!='') {
             jQuery.getJSON('url', {}, function(json, textStatus) {
               //optional stuff to do after success
               jsondata=json;
             });
         }
         if($owner.settings.data!=null&&$owner.settings.data!='')
         {
             try{
                 jsondata=$owner.settings.data;
             }catch(e){alert(e);}
         }
         if(jsondata==null||jsondata=='')
             return null;
         parsedata(treeroot,jsondata,$owner.settings.collapse);
         $owner.append(treeroot);
         return {abort: function () {}};
    }
})(jQuery,window);