username@email.com
2025-04-27 15eb82df2d6ec539e9d4245bfe08d531e8eb6379
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
//树形控件
; RoadUI.Tree = function (options) {
    var defaults = {
        srcElement: null,
        id: "",
        showline: true,
        showico: true,
        showroot: true, //是否显示根节点
        animation: 0, //显示速度
        path: "", //json路径
        refreshpath: "", //刷新加载路径
        focusnode: null, //当前焦点节点
        onclick: null,
        ondblclick: null,
        afterInit: null,//初始化树形列表后运行方法
    };
    this.opts = $.extend(defaults, options);
    this.$srcObj = null;
    if (this.opts.id.isNullOrEmpty() && this.opts.srcElement == null) {
        throw "要初始化为Tree的对象或对象ID为空!";
    }
    else {
        this.$srcObj = this.opts.srcElement != null ? $(this.opts.srcElement) : $("#" + this.opts.id);
        if (this.$srcObj == null || this.$srcObj.size() === 0) {
            throw "要初始化为Tree的对象为空!";
        }
    }
    if (this.opts.path.isNullOrEmpty()) {
        throw "json路径无效!";
    }
    if (!this.opts.id) {
        this.opts.id = this.$srcObj.attr("id");
    }
    var instance = this;
    var $rootObj = this.$srcObj;
 
    this.init = function () {
        instance.$srcObj.html('');
        $.ajax({
            type: "get", url: this.opts.path, dataType: "json", async: true, cache: false,
            success: function (json) {
                instance.addNodes(json, $rootObj, false, true);
                if ($.isFunction(instance.opts.afterInit))
                    instance.opts.afterInit(json);
            },
            error: function (obj, msg) { throw "json加载错误!" + msg; }
        });
    };
    //添加节点 isRefresh:是否二次加载 isInit:是否初始化
    this.addNodes = function (json, $node, isRefresh, isInit) {
        if ($node == undefined || $node == null || $node.size() === 0) {
            return;
        }
        json = json.childs || json;
        if (json.length == 0) {
            return;
        }
        var $preDiv = $(">ul>li", $node);
 
        var i = 0;
        for (i = 0; i < json.length; i++) {
            var jsoni = json[i];
            var id = jsoni.id || RoadUI.Core.newid(false);
            var title = jsoni.title || " ";
            var ico = jsoni.ico || "";
            var hasChilds = jsoni.childs && jsoni.childs.length > 0;
            var hasChildsInDb = jsoni.hasChilds && parseInt(jsoni.hasChilds) > 0;
            var isLast = i == json.length - 1;
            var istop = isInit && i == 0;
            var isroot = istop && this.opts.showroot && json.length === 1; //是否为根节点
 
            json[i].id = id;
 
            var $div = $('<div id="roadui_tree_' + id + '" class="tree_div"></div>');
            var $ul = $('<ul class="tree_ul"></ul>');
            $div.data("json", jsoni);
            if (isroot) {
                $div.attr("isroot", "1");
                $ul.css("padding-left", "0");
            }
 
            var jian = 1;
            if (this.opts.showico) jian++;
            if (this.opts.showline) jian++;
            for (var j = 0; j < $preDiv.size() - jian; j++) {
                $ul.append($preDiv.eq(j).clone());
            }
 
            if ("1" !== $node.attr("isroot") && !isroot && !isInit) {
                var lineClass = this.opts.showline && $node.next().size() > 0 ? "tree_line_conn" : "tree_empty";
                var $line = $('<li class="tree_li"><span class="' + lineClass + '"></span></li>');
                $ul.append($line);
            }
 
            if (this.opts.showline && !isroot) {
                var line1Class = "";
                if (hasChilds) {
                    if (istop) {
                        line1Class = isLast ? "tree_minus_bottom_noprev" : "tree_minus_top_noprev";
                    }
                    else {
                        line1Class = isLast ? "tree_minus_bottom" : "tree_minus_center";
                    }
                }
                else if (hasChildsInDb) {
 
                    if (istop) {
                        line1Class = isLast ? "tree_plus_bottom_noprev" : "tree_plus_top_noprev";
 
                    }
                    else {
                        line1Class = isLast ? "tree_plus_bottom" : "tree_plus_center";
                    }
                }
                else {
                    if (istop) {
                        line1Class = isLast ? "tree_line_bottom" : "tree_line_top";
                    }
                    else {
                        line1Class = isLast ? "tree_line_bottom" : "tree_line_center";
                    }
                }
                var $line1 = $('<li class="tree_li"><span class="' + line1Class + '"></span></li>');
                if (line1Class !== "tree_line_bottom" && line1Class !== "tree_line_center") {
                    $line1.bind("click", function () { instance.toggleNode($(this).parent()); });
                }
                $ul.append($line1);
            }
 
            if (this.opts.showico || isroot) {
                var icoClass = "tree_leaf";
                if (isroot) {
                    icoClass = "tree_root";
                }
                else {
                    if (hasChilds) {
                        icoClass = "tree_open";
                    }
                    else if (hasChildsInDb) {
                        icoClass = "tree_close";
                    }
                }
                var $ico = $('<li class="tree_li"><span class="' + icoClass + '"' + (ico.trim().length > 0 ? ' style="background:url(' + ico + ') no-repeat left center;"' : '') + '></span></li>');
                if (icoClass !== "tree_leaf") {
                    $ico.bind("click", function () { instance.toggleNode($(this).parent()); return false; });
                }
                $ul.append($ico);
            }
 
            var $title = $('<li class="tree_li"><span class="tree_title">' + title + '</span></li>');
            $ul.append($title);
            if ($.isFunction(this.opts.onclick)) {
 
                $title.bind("click", function () {
                    var jid = $(this).parent().parent().data("json");
                    instance.opts.onclick(jid);
                });
            }
            if ($.isFunction(this.opts.ondblclick)) {
 
                $title.bind("dblclick", function () {
                    var jid = $(this).parent().parent().data("json");
                    instance.opts.ondblclick(jid);
                });
            }
 
            $ul.bind("mouseover", function () {
                $(this).removeClass().addClass("tree_ul_over");
            }).bind("mouseout", function () {
                if (instance.focusnode == null || instance.focusnode.size() === 0 || instance.focusnode.get(0) !== $(this).get(0)) {
                    $(this).removeClass().addClass("tree_ul");
                }
            }).bind("click", function () {
                if (instance.focusnode != null && instance.focusnode.size() > 0) {
                    instance.focusnode.removeClass().addClass("tree_ul");
                }
                instance.focusnode = $(this);
                $(this).removeClass().addClass("tree_ul_over");
            });
 
            $div.append($ul);
            $node.append($div);
        }
        var j = 0;
        for (j = 0; j < json.length; j++) {
            var jsonj = json[j];
            if (jsonj.childs && jsonj.childs.length > 0) {
                this.addNodes(jsonj, $("#roadui_tree_" + jsonj.id, $rootObj), isRefresh, false);
            }
        }
    };
 
    this.toggleNode = function ($ul, isRefresh) {
        if (!$ul || $ul.size() === 0)
            return;
        var $div = $ul.parent();
        if ($div.size() === 0)
            return;
        var $childNodes = $(">div", $div);
        //从数据库加载
        if (isRefresh || $childNodes.size() === 0) {
            toggleLoading($div, true);
            var id = "";
            var jsondata = $div.data("json");
            if (jsondata && jsondata.id)
                id = jsondata.id;
            var url = instance.opts.refreshpath;
            if (url.indexOf('?') >= 0)
                url += "&refreshid=" + id;
            else
                url += "?refreshid=" + id;
            $.ajax({
                type: "get", url: url, dataType: "json", async: true, cache: false,
                success: function (json) {
                    if ($childNodes.length > 0)
                        $childNodes.remove();
                    instance.addNodes(json, $div, true, false);
                    toggleLoading($div, false);
                    if (json.length > 0)
                        toggleIco($ul, true);
                },
                error: function (obj, msg) { throw "json加载错误!" + msg; }
            });
        }
        else {
            if ($childNodes.eq(0).is(":hidden")) {
                $childNodes.show(this.opts.animation || 0);
                toggleIco($ul, true);
            }
            else {
                $childNodes.hide(this.opts.animation || 0);
                toggleIco($ul, false);
            }
        }
    };
    var toggleIco = function ($ul, isShow) {
        var $spans = $(">li>span", $ul);
        if (isShow) {
            $spans.filter(".tree_close").removeClass().addClass("tree_open");
            $spans.filter(".tree_plus_bottom").removeClass().addClass("tree_minus_bottom");
            $spans.filter(".tree_plus_center").removeClass().addClass("tree_minus_center");
            $spans.filter(".tree_plus_top").removeClass().addClass("tree_minus_top");
            $spans.filter(".tree_plus_top_noprev").removeClass().addClass("tree_minus_top_noprev");
            $spans.filter(".tree_plus_bottom_noprev").removeClass().addClass("tree_minus_bottom_noprev");
            if ($(">li", $ul).length > 0) {
                $spans.filter(".tree_leaf").removeClass().addClass("tree_open").parent().bind("click", function () { instance.toggleNode($(this).parent()); });
                $spans.filter(".tree_line_bottom").removeClass().addClass("tree_minus_bottom").parent().bind("click", function () { instance.toggleNode($(this).parent()); });
                $spans.filter(".tree_line_center").removeClass().addClass("tree_minus_center").parent().bind("click", function () { instance.toggleNode($(this).parent()); });
                $spans.filter(".tree_line_top").removeClass().addClass("tree_minus_top").parent().bind("click", function () { instance.toggleNode($(this).parent()); });
            }
        }
        else {
            $spans.filter(".tree_open").removeClass().addClass("tree_close");
            $spans.filter(".tree_minus_bottom").removeClass().addClass("tree_plus_bottom");
            $spans.filter(".tree_minus_center").removeClass().addClass("tree_plus_center");
            $spans.filter(".tree_minus_top").removeClass().addClass("tree_plus_top");
            $spans.filter(".tree_minus_top_noprev").removeClass().addClass("tree_plus_top_noprev");
            $spans.filter(".tree_minus_bottom_noprev").removeClass().addClass("tree_plus_bottom_noprev");
        }
 
    };
    this.refresh = function (divOrId) {
        if (typeof (divOrId) == "string") {
            var $div = $("#roadui_tree_" + divOrId);
            if ($div.size() === 0) {
                return;
            }
            this.toggleNode($div.children("ul"), true);
        }
        else {
            this.toggleNode($(divOrId), true);
        }
    };
    var toggleLoading = function ($div, isLoading) {
        if (isLoading) {
            var $ico = $(".tree_close", $(">ul", $div));
            $ico.removeClass().addClass("tree_loading");
        }
        else {
            var $ico = $(".tree_loading", $(">ul", $div));
            $ico.removeClass().addClass("tree_open");
        }
    };
    this.init();
}