File size: 6,567 Bytes
7e79d67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
 * Copyright 2016 Cnlyml
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

define(function (require, exports, module) {
    require("js/jquery-2.1.1.js");
    require("plugins/zTree/css/zTreeStyle/zTreeStyle.css");
    require("plugins/zTree/js/jquery.ztree.all.min.js");
    require("plugins/touchspin/jquery.bootstrap-touchspin.min.css");
    require("plugins/touchspin/jquery.bootstrap-touchspin.min.js");

    var zTree;
    var addNum = 0;

    var settings = {
        async: {
            enable: true,
            url: Global.adminPath + "/account/module/getModule",
            autoParam:["id=moduleID"]
        },
        data: {
            key: {
                name : "moduleName"
            }
        },
        simpleData: {
            enable: true,
            idKey: "id"
        },
        callback: {
            onClick: zTreeOnClick,
            beforeRemove: beforeRemove
        },
        edit: {
            drag: {
                isCopy:false
            },
            enable:true,
            showRenameBtn:false,
            showRemoveBtn:false
        }
    };

    module.exports = {
        init: function() {
            initZtree();
            initElement();
        },

        editPermission: function(a) {
            console.log($(a).parent().parent().find("td").html());
            setInputValue("permissionCode", $(a).parent().parent().find("td").eq(0).html());
            setInputValue("permissionDesc", $(a).parent().parent().find("td").eq(1).html());

            setInputValue("permissionID", $(a).parent().parent().find("input").eq(0).html());
        }
    };

    function initZtree() {
       zTree = $.fn.zTree.init($("#moduleZtree"), settings);
    }

    function initElement() {
        $(".touchspin1").TouchSpin({
            min: 1,
            max: 100,
            step: 1,
            buttondown_class: 'btn btn-white',
            buttonup_class: 'btn btn-white'
        });

        $("#deleteModuleBtn").click(function() {
            
        });

        $("#addModuleBtn").click(function () {

            if(addNum > 0) {
                ZH.Pop.msg(5, "您有一个新建的节点未保存,请保存或删除该节点后再进行添加");
                return;
            }

            var nodes = zTree.getSelectedNodes();
            if(nodes == null || nodes.length < 1) {
                var node = zTree.addNodes(null, -1, {"moduleName":"新模块", "permissions":[]});
                zTree.selectNode(node, false);
            }else {
                var node = nodes[0];
                if(!node.isParent && node.getParentNode() == null) {
                    ZH.Pop.confirm("您是需要添加最顶级模块还是在该选中模块下建立子模块呢?<br><br>确定添加顶级模块,取消在该选中模块下建立子模块", function() {
                        var f = zTree.addNodes(null, -1, {"moduleName":"新模块", "permissions":[]});
                        zTree.selectNode(f, false);
                        layer.closeAll();
                    }, function() {
                        var f = zTree.addNodes(node, -1, {"moduleName":"新模块", "parentID": node.id, "permissions":[]});
                        zTree.selectNode(f, false);
                        layer.closeAll();
                    });
                }else {
                    var f = zTree.addNodes(node, -1, {"moduleName":"新模块", "parentID": node.id, "permissions":[]});
                    zTree.selectNode(f, false);
                }
            }

            addNum++;
        })
    }

    function beforeRemove(treeId, treeNode) {
        return true;
    }

    function zTreeOnClick(event, treeId, treeNode) {
        setInputValue("moduleName", treeNode.moduleName);
        setInputValue("moduleUrl", treeNode.moduleUrl);
        setInputValue("moduleSort", treeNode.moduleSort);
        setInputValue("description", treeNode.description);
        setInputValue("moduleCode", treeNode.moduleCode);

        setInputValue("parentModuleName", treeNode.isParent ? "" : treeNode.getParentNode() == null ? "" : treeNode.getParentNode().moduleName);
        setInputValue("parentID", treeNode.isParent ? "" : treeNode.getParentNode() == null ? "" : treeNode.getParentNode().id);

        /**
         * 判断权限列表
         */
        if(treeNode.isParent) {
            $("#permissionListMessage").css("display","block").val("寂寞很无奈~_~");
            $("#permissionFormMessage").css("display","block").val("该模块不能添加权限");
            $(".permissionList").css("display","none");
            $(".permissionForm").css("display","none");
        }else {
            $("#permissionListMessage").css("display","none");
            $("#permissionFormMessage").css("display","none");
            $(".permissionList").css("display","block");
            $(".permissionForm").css("display","block");

            var permissions = treeNode.permissions;
            if(permissions.length < 1) {
                $("#permissionListMessage").css("display","block").val("寂寞很无奈~_~");
                $(".permissionList").css("display","none");
            }else {
                var html = "";
                for(var i = 0; i < permissions.length; i++) {
                    html += "<tr>";
                    html += '<input type="hidden" name="permissions['+i+'].id" value="'+permissions[i].id+'">';
                    html += '<td>'+permissions[i].permissionCode+'</td>';
                    html += '<td>'+permissions[i].permissionDesc+'</td>';
                    html += '<td><a href="javascript:void(0);" onclick="module.editPermission(this)" class="btn btn-primary btn-xs">修改权限</a>&nbsp;&nbsp;<a href="javascript:;" class="btn btn-danger btn-xs">删除权限</a></td>';
                    html += "</tr>";
                }

                $(".permissionListBody").html(html);
            }
        }
    }

    function setInputValue(name, value) {
        $("input[name = '"+name+"']").val(value);
    }

});