wangtengyu
2018-12-07 f459412e0dac4ed94106da043b4c6f8576bfe496
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
/**
 * 
 * 商品分类选择器
 * 
 * @author 
 * @date 2015-06-17
 * 
 * $.showCategorySelecter
 * $.ajaxCategorySelecter
 * 
 */
(function($){
    
    function getFont(treeId, node) {
        return node.font ? node.font : {};
    }
    
    function onBodyDown(event)
    {
        if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
            hideMenu();
        }
    }
    
    var nodeMap = new Array();
       
       var hashSet = new Array();
    
       $.getRelationNodes = function(nodes, id)
       {
           var node = nodeMap[id];
           
           if(node != null && node.pId != 0 && hashSet[node.pId] == undefined){
               var pnode = nodeMap[node.pId];
               nodes.push(pnode);
               hashSet[pnode.id] = 0;
               return $.getRelationNodes(nodes, node.pId);
           }
       };
       
       //未完成
       $.search = function(setting, zTree, zNodes, keyword, treeDemo_id)
       {
           for(var i = 0; i < zNodes.length; i++)
           {
               zNodes[i].font = {};
           }
           
           if(keyword == null || keyword.length == 0){
               $.fn.zTree.init($("#"+treeDemo_id), setting, zNodes);
               var zTree = $.fn.zTree.getZTreeObj(treeDemo_id);
               zTree.refresh();
               return;
           }
           
           hashSet = new Array();
           
           var nodes = new Array();
           
           for(var i = 0; i < zNodes.length; i++)
           {
               var node = zNodes[i];
               if(node != null && node.name_pinyin.indexOf(keyword) != -1 && hashSet[node.pId] == undefined)
               {
                   node.font = {'color': 'red'};
                   nodes.push(node);
                   hashSet[node.id] = 0;
                   $.getRelationNodes(nodes, node.id);
               }
           }
           
           $.fn.zTree.init($("#"+treeDemo_id), setting, nodes);
           var zTree = $.fn.zTree.getZTreeObj(treeDemo_id);
           zTree.refresh();
           zTree.expandAll(true);
       };
 
       /**
     * 获取分类选择面板
     * @param catNameObj 分类名称对象
     * @param catIdObj 分类ID称对象
     * @param selectedId 被选择分类编号
     * @param lang_cat_expand_all 全部展开的语言项设置
     * @param lang_cat_collect_all 全部收缩的语言项设置
     * @param lang_cat_expand 展开的语言项设置
     * @param lang_cat_collect 收缩的语言项设置
     */
    $.showCategorySelecter = function(catNameObj, catIdObj, zNodes, selectedId, lang_cat_expand_all, lang_cat_collect_all, lang_cat_expand, lang_cat_collect)
    {
        
        if(lang_cat_expand_all == undefined || lang_cat_expand_all.length == 0){
            lang_cat_expand_all = "全部展开";
        }
        if(lang_cat_collect_all == undefined || lang_cat_collect_all.length == 0){
            lang_cat_collect_all = "全部收缩";
        }
        if(lang_cat_expand == undefined || lang_cat_expand.length == 0){
            lang_cat_expand = "展开";
        }
        if(lang_cat_collect == undefined || lang_cat_collect.length == 0){
            lang_cat_collect = "收缩";
        }
        
        var setting = 
          {
            view: 
            {
                fontCss: getFont,
                nameIsHTML: true,
                showIcon: true,
                dblClickExpand: true
            },
            data: 
            {
                simpleData: 
                {
                    enable: true
                }
            },
            callback: 
            {
                onClick: function(){}
            }
        };
        
        catNameObj = $(catNameObj);
        catIdObj = $(catIdObj);
        
        var catId = catIdObj.attr("id");
        
        var menuContent_id = "menuContent_"+catId;
        var btn_expand_all_id = "btn_expand_all_"+catId;
        var btn_expand_id = "btn_expand_"+catId;
        var btn_collect_id = "btn_collect_"+catId;
        var treeDemo_id = "treeDemo_"+catId;
        
        //生成DIV
        var menuHtml = "<div id='"+menuContent_id+"' class='menuContent' style='display: none; position: absolute; background-color: white; border: 1px black solid;'>"
            +"<div style='text-align: center;'>"
            +"<input type='button' id='"+btn_expand_all_id+"' value='"+lang_cat_expand_all+"' class='button expand_all'>"
            +"<input type='button' id='"+btn_expand_id+"' value='"+lang_cat_expand+"' class='button expand'>"
            +"<input type='button' id='"+btn_collect_id+"' value='"+lang_cat_collect+"' class='button collect'>"
            +"</div>"
            +"<ul id='"+treeDemo_id+"' class='ztree' style='margin-top:0; width:250px; height: 300px; overflow: auto;'></ul>"
            +"</div>";
        catIdObj.after(menuHtml);
        
        setting.callback.onClick = function()
        {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo_"+catId);
               var nodes = zTree.getSelectedNodes();
               
               if(nodes.length == 0)
               {
                   return;
               }
               
               catIdObj.attr("value", nodes[0].id);
               catNameObj.attr("value", nodes[0].name);
               catNameObj.attr("nowvalue", nodes[0].name);
        };
           
           for(var i = 0; i < zNodes.length; i++)
           {
            var node = zNodes[i];
            if(node != null)
            {
                nodeMap[node.id] = node;
            }
           }
        
        $.fn.zTree.init($("#"+treeDemo_id), setting, zNodes);
        
        var zTree = $.fn.zTree.getZTreeObj(treeDemo_id);
        
        if(selectedId != undefined && selectedId != null && selectedId.length > 0){
            var selectNode = zTree.getNodeByParam("id", selectedId);
            if(selectNode != null)
            {
                zTree.selectNode(selectNode, true);
            }
        }
        
        
        catNameObj.focus(function(){
               var offset = $(catNameObj).offset();
               $("#"+menuContent_id).css({left: offset.left + "px", top: offset.top + $(this).outerHeight() + "px"}).slideDown("fast");
               $("body").bind("mousedown", function(event){
                   if($("#"+menuContent_id).is(":hidden")){
                       return;
                   }
                   if (!(event.target.id == "menuBtn" || event.target.id == menuContent_id || $(event.target).parents("#"+menuContent_id).length>0)) {
                       $("#"+menuContent_id).fadeOut("fast");
                   }
               });
               if(catIdObj.val().length != 0)
               {
                   var zTree = $.fn.zTree.getZTreeObj(treeDemo_id);
                   zTree.selectNode(zTree.getNodesByParam("id", catIdObj.val()), true);
               }
        });
        
        // 失去焦点检查为空则自动补全
        catNameObj.blur(function(){
            if($(this).val().length == 0)
            {
                $(this).val($(this).attr("nowvalue"));
            }
        });
        
        catNameObj.keyup(function(){
            $.search(setting, zTree, zNodes, catNameObj.val(), treeDemo_id);
            return false;
            
        });
        
        // 展开/收缩全部
        var expand = false;
        $("#"+btn_expand_all_id).click(function(){
            zTree.expandAll(!expand);
            
            if(expand)
            {
                $(this).val(lang_cat_expand_all);
            }
            else
            {
                $(this).val(lang_cat_collect_all);
            }
            
            expand = !expand;
        });
        
        // 展开
        $("#"+btn_expand_id).click(function(){
            var nodes = zTree.getSelectedNodes();
            if(nodes.length > 0){
                zTree.expandNode(nodes[0], true, true);
            }else{
                zTree.expandAll(true);
            }
        });
        // 收缩
        $("#"+btn_collect_id).click(function(){
            var nodes = zTree.getSelectedNodes();
            if(nodes.length > 0){
                zTree.expandNode(nodes[0], false, true);
            }else{
                zTree.expandAll(false);
            }
        });
    };
    
    
    /**
     * ajax获取分类选择面板
     * @param catNameObj 分类名称对象
     * @param catIdObj 分类ID称对象
     * @param selectedId 被选择分类编号
     * @param lang_cat_expand_all 全部展开的语言项设置
     * @param lang_cat_collect_all 全部收缩的语言项设置
     * @param lang_cat_expand 展开的语言项设置
     * @param lang_cat_collect 收缩的语言项设置
     */
    $.ajaxCategorySelecter = function(catNameObj, catIdObj, selectedId, lang_cat_expand_all, lang_cat_collect_all, lang_cat_expand, lang_cat_collect){
        var url = "goods.php?act=ajax_category";
        $.get(url, {}, function(data){
            data = $.parseJSON(data);
            $.showCategorySelecter(catNameObj, catIdObj, data, selectedId);
        }, "text");
    };
        
    $.ajaxCategorySelecter1 = function(catNameObj, catIdObj, selectedId, lang_cat_expand_all, lang_cat_collect_all, lang_cat_expand, lang_cat_collect){
        var url = "virtual_goods.php?act=ajax_category";
        $.get(url, {}, function(data){
            data = $.parseJSON(data);
            $.showCategorySelecter(catNameObj, catIdObj, data, selectedId);
        }, "text");
    };
        
})(jQuery);