add-exist-role-modal.ctrl.js 11.5 KB
Newer Older
eddie.woo's avatar
eddie.woo committed
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
commonModule.
controller('addExistRoleModalController', ['$scope', '$log', '$translate', 'uiGridConstants', '$location', '$timeout', '$interval', '$filter', 'SweetAlert', 'roleService', 'userService', 'permissionService',
    function ($scope, $log, $translate, uiGridConstants, $location, $timeout, $interval, $filter, SweetAlert, roleService, userService, permissionService) {

        $scope.searchValue = "";

        var modalSelector = '#addRoleModal';
        $scope.allowUncheck = false;

        var showWarning = function (resultMsg) {

            var errMsg = $translate.instant(resultMsg);
            SweetAlert.warning(errMsg);
        };

        var showSuccessMsg = function (resultMsg) {

            var errMsg = $translate.instant(resultMsg);
            SweetAlert.success(errMsg);
        };

        var showModal = function () {
            // $scope.selectedRoleList = [];
            // $scope.selectedRoleList = ['1a22c719-9751-4a7c-a3b5-6bf333567970', '468AAF7D-268B-4ADF-A331-FF762C950EC6'];

            $scope.originalRoleList = angular.copy($scope.selectedRoleList);

            getDataList();
            initPermissionByRoleIDList($scope.selectedRoleList);
            $(modalSelector).modal("show");
        };
        $scope.$watch('operateType', function (newValue, oldValue) {
            if (newValue) {
                reset();
                if (newValue == constant.Operation.Add) {
                    showModal();
                } else if (newValue == constant.Operation.Edit) {

                }
            }
        });

        var reset = function () {
            $scope.searchValue = '';
        };

        $(".addRoleModal").on("hidden.bs.modal", function () {
            $scope.operateType = null;
        });

        $scope.closeModal = function () {
            $scope.operateType = null;
        };

        $scope.Save = function () {
            //返回用户ID列表
            //console.log(JSON.stringify($scope.selectedRoleList));
            $scope.operateType = null;
            $scope.isUpdate = true;
            $(modalSelector).modal("hide");
        };


        var setSelectItems = function (roleData) {

            if ($scope.selectedRoleList && $scope.selectedRoleList.length > 0) {
                for (var i = 0; i < roleData.length; i++) {

                    if ($scope.selectedRoleList.indexOf(roleData[i].id) > -1) {
                        roleData[i].selected = true;
                        roleData[i].expanded = true;
                    }

                    if (roleData[i].subRoles && roleData[i].subRoles.length > 0) {
                        setSelectItems(roleData[i].subRoles);
                    }
                }
            }

            return roleData;
        };

        var getDataList = function () {

            roleService.getRoleListByServiceGroup().success(function (roleData) {
                if (roleData) {
                    var data = setSelectItems(roleData);
                    $scope.roleTreeViewData = data;
                }
            });
        };

        var initPermissionByRoleIDList = function (roleIDList)
        {
            permissionService.getDevTreePermissionsByRoleIDList(roleIDList, constant.serviceType.VAT).success(function (data) {
                if (data) {
                    $scope.permissionTreeViewData = data;
                }
            });

        };

        var loadPermissionByRoleID = function (roleID) {
            permissionService.getDevTreePermissionsByRoleID(roleID, constant.serviceType.VAT).success(function (data) {
                if (data) {
                    $scope.permissionTreeViewData = data.permissionDevTreeList;
                }
            });
        };

        $scope.searchOptions = {
            bindingOptions: {
                value: "searchValue"
            },
            placeholder: $translate.instant('Search'),
            width: 518,
            mode: "search",
            valueChangeEvent: "keyup"
        };

        var loadPermissionTree = function () {
            $scope.permissionTreeViewOptions = {
                bindingOptions: {
                    dataSource: 'permissionTreeViewData',
                    searchValue: 'searchValue'
                },
                dataStructure: "plain",
                selection: {
                    mode: "single"
                },
                loadPanel: {
                    enabled: true
                },
                scrolling: {
                    mode: "virtual"
                },
                keyExpr: "id",
                // parentIdExpr: "pLevel",

                showRowLines: true,
                showColumnLines: true,
                rowAlternationEnabled: true,
                showBorders: true,
                noDataText: $translate.instant('NoDataText'),
                selectAllText: $translate.instant('SelectAll')
            };
        };

        var loadRoleTree = function () {
            $scope.roleTreeViewOptions = {
                bindingOptions: {
                    dataSource: 'roleTreeViewData',
                    searchValue: 'searchValue'
                },
                selection: {
                    mode: "multiple"
                },

                loadPanel: {
                    enabled: true
                },
                scrolling: {
                    mode: "virtual"
                },
                keyExpr: "id",
166
                parentIdExpr: "parentID",
eddie.woo's avatar
eddie.woo committed
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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
                showRowLines: true,
                showColumnLines: true,
                expandAllEnabled: true,
                rowAlternationEnabled: true,
                expandedRowKeys: [1],
                showBorders: true,
                noDataText: $translate.instant('NoDataText'),
                selectAllText: $translate.instant('SelectAll'),
                displayExpr: 'name',  //显示的属性名称,默认为text
                itemsExpr: 'subRoles',  //子层的数组名称,默认为items
                //scrollDirection:'vertical',  //Accepted Values: 'vertical' | 'horizontal' | 'both'
                selectNodesRecursive: true,  //级联选择
                showCheckBoxesMode: 'normal', //Accepted Values: 'none' | 'normal' | 'selectAll'
                onItemSelectionChanged: function (obj, element, model, node) {

                    populateSelectedRole(obj.itemData);
                },
                onInitialized: function (e) {
                    $scope.widgetInstance = e.component;
                },
                //点击行,选中
                onItemClick: function (e) {

                    loadPermissionByRoleID(e.itemData.id);
                },
                onItemRendered: function (e) {

                    if ($scope.allowUncheck) { return;}

                    var originalRoleList = $scope.originalRoleList;

                    if (e && e.itemData && originalRoleList && originalRoleList.length>0) {
                    
                        if (originalRoleList.indexOf(e.itemData.id) > -1) {
                            e.itemElement.closest('li.dx-treeview-node').find('.dx-checkbox-checked').addClass("dx-state-disabled");
                        }
                        if (e.itemData.isRoleCategory && e.itemData.subRoles.length > 0)
                        {
                            var subRoles = e.itemData.subRoles;
                            var subRoleId = _.pluck(subRoles, 'id');
                            var hasCheckedAll = true;
                            for (var i = 0; i < originalRoleList.length; i++) {
                                var hasFind = _.find(subRoleId, function (item) {
                                    return item.id == originalRoleList[i].id;
                                });
                                if (!hasFind) {
                                    hasCheckedAll = false;
                                    break;
                                }
                            }
                            if (hasCheckedAll)
                            {
                                e.itemElement.closest('li.dx-treeview-node').find('.dx-checkbox-checked').addClass("dx-state-disabled");
                            }
                        } 
                    }
                }
            };

        };
        //构造选中的角色list
        var populateSelectedRole = function (itemData) {

            var itemIndex = -1;

            $.each($scope.selectedRoleList, function (index, item) {
                if (item.id === itemData.id) {
                    itemIndex = index;
                    return false;
                }
            });

            if (itemData.selected && itemIndex === -1) {
                if (itemData.isRoleCategory) {
                    //选择父级角色分类
                    itemData.subRoles.forEach(function (item) {
                        if ($scope.selectedRoleList.indexOf(item.id) < 0) {
                            $scope.selectedRoleList.push(item.id);
                        }
                    });
                } else {
                    //选择叶子节点角色
                    $scope.selectedRoleList.push(itemData.id);
                }

            } else if (!itemData.selected) {
                //$scope.selectedRoleList.splice(itemIndex, 1);
                //$scope.selectedRoleList.forEach(function (item) {
                //    $scope.widgetInstance.selectItem(item);
                //});
                if (!itemData.isRoleCategory) {
                    //叶子节点取消选中情况
                    $.each($scope.selectedRoleList, function (index, item) {
                        if (item === itemData.id) {
                            $scope.selectedRoleList.splice(index, 1);
                            return false;
                        }
                    });
                } else {

                    var cloneSelectedRoleList = angular.copy($scope.selectedRoleList);
                    var removeIndexList = [];

                    $.each(cloneSelectedRoleList, function (index, item) {
                        itemData.subRoles.forEach(function (x) {
                            if (item === x.id) {
                                removeIndexList.push(index);
                            }
                        });
                    });

                    //从后往前删除
                    for (var i = removeIndexList.length - 1; i >= 0; i--) {
                        $scope.selectedRoleList.splice(removeIndexList[i], 1);
                    }

                    $scope.selectedRoleList.forEach(function (item) {
                        $scope.widgetInstance.selectItem(item);
                    });

                    if (!$scope.allowUncheck) {
                        $scope.originalRoleList.forEach(function (item) {
                            $scope.widgetInstance.selectItem(item);
                        });
                    }

                }
            }

            $scope.selectedItemList = [];
            $scope.roleTreeViewData.forEach(function (item) {
                var subRoles = _.filter(item.subRoles, function (item) {
                    return $scope.selectedRoleList.indexOf(item.id) > -1;
                });

                subRoles.forEach(function (row) {
                    $scope.selectedItemList.push(row);
                });
            });


        };

        (function initialize() {
            $log.debug('addExistUserModalController.ctor()...');

            loadRoleTree();
            loadPermissionTree();
        
            //  showModal();
        }) ();

}
]);