ConfigNamespaceController.js 12.7 KB
Newer Older
L
lepdou 已提交
1
application_module.controller("ConfigNamespaceController",
2 3 4
    ['$rootScope', '$scope', '$translate', 'toastr', 'AppUtil', 'EventManager', 'ConfigService',
        'PermissionService', 'UserService', 'NamespaceBranchService', 'NamespaceService',
        controller]);
L
abtest  
lepdou 已提交
5

6 7
function controller($rootScope, $scope, $translate, toastr, AppUtil, EventManager, ConfigService,
    PermissionService, UserService, NamespaceBranchService, NamespaceService) {
L
abtest  
lepdou 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21

    $scope.rollback = rollback;
    $scope.preDeleteItem = preDeleteItem;
    $scope.deleteItem = deleteItem;
    $scope.editItem = editItem;
    $scope.createItem = createItem;
    $scope.closeTip = closeTip;
    $scope.showText = showText;
    $scope.createBranch = createBranch;
    $scope.preCreateBranch = preCreateBranch;
    $scope.preDeleteBranch = preDeleteBranch;
    $scope.deleteBranch = deleteBranch;
    $scope.showNoModifyPermissionDialog = showNoModifyPermissionDialog;
    $scope.lockCheck = lockCheck;
L
lepdou 已提交
22
    $scope.emergencyPublish = emergencyPublish;
L
abtest  
lepdou 已提交
23 24 25 26

    init();

    function init() {
L
lepdou 已提交
27 28 29 30 31
        initRole();
        initUser();
        initPublishInfo();
    }
    function initRole() {
L
abtest  
lepdou 已提交
32 33 34 35 36 37 38 39 40 41
        PermissionService.get_app_role_users($rootScope.pageContext.appId)
            .then(function (result) {
                var masterUsers = '';
                result.masterUsers.forEach(function (user) {
                    masterUsers += user.userId + ',';
                });
                $scope.masterUsers = masterUsers.substring(0, masterUsers.length - 1);
            }, function (result) {

            });
L
lepdou 已提交
42
    }
L
abtest  
lepdou 已提交
43

L
lepdou 已提交
44
    function initUser() {
L
abtest  
lepdou 已提交
45 46 47 48 49 50
        UserService.load_user().then(function (result) {
            $scope.currentUser = result.userId;
        });

    }

L
lepdou 已提交
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
    function initPublishInfo() {
        NamespaceService.getNamespacePublishInfo($rootScope.pageContext.appId)
            .then(function (result) {
                if (!result) {
                    return;
                }
                $scope.hasNotPublishNamespace = false;
                var namespacePublishInfo = [];

                Object.keys(result).forEach(function (env) {
                    if (env.indexOf("$") >= 0) {
                        return;
                    }

                    var envPublishInfo = result[env];
                    Object.keys(envPublishInfo).forEach(function (cluster) {

                        var clusterPublishInfo = envPublishInfo[cluster];
                        if (clusterPublishInfo) {
                            $scope.hasNotPublishNamespace = true;

                            if (Object.keys(envPublishInfo).length > 1) {
                                namespacePublishInfo.push("[" + env + ", " + cluster + "]");
                            } else {
                                namespacePublishInfo.push("[" + env + "]");
                            }

                        }
                    })
                });

                $scope.namespacePublishInfo = namespacePublishInfo;
            });

    }

L
abtest  
lepdou 已提交
87
    EventManager.subscribe(EventManager.EventType.REFRESH_NAMESPACE,
88 89 90 91 92 93
        function (context) {
            if (context.namespace) {
                refreshSingleNamespace(context.namespace);
            } else {
                refreshAllNamespaces();
            }
Z
zhangle 已提交
94

95
        });
L
abtest  
lepdou 已提交
96 97 98 99 100 101 102

    function refreshAllNamespaces() {
        if ($rootScope.pageContext.env == '') {
            return;
        }

        ConfigService.load_all_namespaces($rootScope.pageContext.appId,
103 104 105
            $rootScope.pageContext.env,
            $rootScope.pageContext.clusterName).then(
                function (result) {
L
abtest  
lepdou 已提交
106

107 108
                    $scope.namespaces = result;
                    $('.config-item-container').removeClass('hide');
L
abtest  
lepdou 已提交
109

110 111 112 113
                    initPublishInfo();
                }, function (result) {
                    toastr.error(AppUtil.errorMsg(result), $translate.instant('Config.LoadingAllNamespaceError'));
                });
L
abtest  
lepdou 已提交
114 115 116 117 118 119 120 121
    }

    function refreshSingleNamespace(namespace) {
        if ($rootScope.pageContext.env == '') {
            return;
        }

        ConfigService.load_namespace($rootScope.pageContext.appId,
122 123 124 125 126 127 128 129 130 131 132 133 134
            $rootScope.pageContext.env,
            $rootScope.pageContext.clusterName,
            namespace.baseInfo.namespaceName).then(
                function (result) {

                    $scope.namespaces.forEach(function (namespace, index) {
                        if (namespace.baseInfo.namespaceName == result.baseInfo.namespaceName) {
                            result.showNamespaceBody = true;
                            result.initialized = true;
                            result.show = namespace.show;
                            $scope.namespaces[index] = result;
                        }
                    });
L
lepdou 已提交
135

136
                    initPublishInfo();
L
abtest  
lepdou 已提交
137

138 139 140
                }, function (result) {
                    toastr.error(AppUtil.errorMsg(result), $translate.instant('Config.LoadingAllNamespaceError'));
                });
L
abtest  
lepdou 已提交
141 142 143 144 145 146 147 148 149 150 151
    }

    function rollback() {
        EventManager.emit(EventManager.EventType.ROLLBACK_NAMESPACE);
    }

    $scope.tableViewOperType = '', $scope.item = {};
    $scope.toOperationNamespace;

    var toDeleteItemId = 0;

152
    function preDeleteItem(namespace, item) {
L
abtest  
lepdou 已提交
153 154 155 156
        if (!lockCheck(namespace)) {
            return;
        }

157
        $scope.config = item;
L
abtest  
lepdou 已提交
158
        $scope.toOperationNamespace = namespace;
159
        toDeleteItemId = item.id;
L
abtest  
lepdou 已提交
160 161 162 163 164 165

        $("#deleteConfirmDialog").modal("show");
    }

    function deleteItem() {
        ConfigService.delete_item($rootScope.pageContext.appId,
166 167 168 169 170 171 172 173 174 175 176 177 178
            $rootScope.pageContext.env,
            $scope.toOperationNamespace.baseInfo.clusterName,
            $scope.toOperationNamespace.baseInfo.namespaceName,
            toDeleteItemId).then(
                function (result) {
                    toastr.success($translate.instant('Config.Deleted'));
                    EventManager.emit(EventManager.EventType.REFRESH_NAMESPACE,
                        {
                            namespace: $scope.toOperationNamespace
                        });
                }, function (result) {
                    toastr.error(AppUtil.errorMsg(result), $translate.instant('Config.DeleteFailed'));
                });
L
abtest  
lepdou 已提交
179 180 181 182 183 184 185 186 187 188
    }

    //修改配置
    function editItem(namespace, toEditItem) {
        if (!lockCheck(namespace)) {
            return;
        }

        $scope.item = _.clone(toEditItem);

189
        if (namespace.isBranch || namespace.isLinkedNamespace) {
L
abtest  
lepdou 已提交
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
            var existedItem = false;
            namespace.items.forEach(function (item) {
                if (!item.isDeleted && item.item.key == toEditItem.key) {
                    existedItem = true;
                }
            });
            if (!existedItem) {
                $scope.item.lineNum = 0;
                $scope.item.tableViewOperType = 'create';
            } else {
                $scope.item.tableViewOperType = 'update';
            }

        } else {
            $scope.item.tableViewOperType = 'update';
        }

        $scope.toOperationNamespace = namespace;

        AppUtil.showModal('#itemModal');
    }

    //新增配置
    function createItem(namespace) {
        if (!lockCheck(namespace)) {
            return;
        }

        $scope.item = {
            tableViewOperType: 'create'
        };

        $scope.toOperationNamespace = namespace;
        AppUtil.showModal('#itemModal');
    }

    var selectedClusters = [];
    $scope.collectSelectedClusters = function (data) {
        selectedClusters = data;
    };

    function lockCheck(namespace) {
        if (namespace.lockOwner && $scope.currentUser != namespace.lockOwner) {
            $scope.lockOwner = namespace.lockOwner;
            $('#namespaceLockedDialog').modal('show');
            return false;
        }
        return true;
    }

    function closeTip(clusterName) {
        var hideTip = JSON.parse(localStorage.getItem("hideTip"));
        if (!hideTip) {
            hideTip = {};
            hideTip[$rootScope.pageContext.appId] = {};
        }

        if (!hideTip[$rootScope.pageContext.appId]) {
            hideTip[$rootScope.pageContext.appId] = {};
        }

        hideTip[$rootScope.pageContext.appId][clusterName] = true;

        $rootScope.hideTip = hideTip;

        localStorage.setItem("hideTip", JSON.stringify(hideTip));

    }

    function showText(text) {
        $scope.text = text;
        $('#showTextModal').modal('show');
    }

    function showNoModifyPermissionDialog() {
        $("#modifyNoPermissionDialog").modal('show');
    }

    var toCreateBranchNamespace = {};

    function preCreateBranch(namespace) {
        toCreateBranchNamespace = namespace;
        AppUtil.showModal("#createBranchTips");
    }

    function createBranch() {
        NamespaceBranchService.createBranch($rootScope.pageContext.appId,
277 278 279
            $rootScope.pageContext.env,
            $rootScope.pageContext.clusterName,
            toCreateBranchNamespace.baseInfo.namespaceName)
L
abtest  
lepdou 已提交
280
            .then(function (result) {
281
                toastr.success($translate.instant('Config.GrayscaleCreated'));
L
abtest  
lepdou 已提交
282
                EventManager.emit(EventManager.EventType.REFRESH_NAMESPACE,
283 284 285
                    {
                        namespace: toCreateBranchNamespace
                    });
L
abtest  
lepdou 已提交
286
            }, function (result) {
287
                toastr.error(AppUtil.errorMsg(result), $translate.instant('Config.GrayscaleCreateFailed'));
L
abtest  
lepdou 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300
            })

    }

    function preDeleteBranch(branch) {
        //normal delete
        branch.branchStatus = 0;
        $scope.toDeleteBranch = branch;
        AppUtil.showModal('#deleteBranchDialog');
    }

    function deleteBranch() {
        NamespaceBranchService.deleteBranch($rootScope.pageContext.appId,
301 302 303 304 305
            $rootScope.pageContext.env,
            $rootScope.pageContext.clusterName,
            $scope.toDeleteBranch.baseInfo.namespaceName,
            $scope.toDeleteBranch.baseInfo.clusterName
        )
L
abtest  
lepdou 已提交
306
            .then(function (result) {
307
                toastr.success($translate.instant('Config.BranchDeleted'));
L
abtest  
lepdou 已提交
308
                EventManager.emit(EventManager.EventType.REFRESH_NAMESPACE,
309 310 311
                    {
                        namespace: $scope.toDeleteBranch.parentNamespace
                    });
L
abtest  
lepdou 已提交
312
            }, function (result) {
313
                toastr.error(AppUtil.errorMsg(result), $translate.instant('Config.BranchDeleteFailed'));
L
abtest  
lepdou 已提交
314 315 316 317
            })

    }

L
lepdou 已提交
318
    EventManager.subscribe(EventManager.EventType.EMERGENCY_PUBLISH,
319 320 321 322
        function (context) {
            AppUtil.showModal("#emergencyPublishAlertDialog");
            $scope.emergencyPublishContext = context;
        });
L
lepdou 已提交
323 324 325 326 327

    function emergencyPublish() {
        if ($scope.emergencyPublishContext.mergeAndPublish) {

            EventManager.emit(EventManager.EventType.MERGE_AND_PUBLISH_NAMESPACE,
328 329 330 331
                {
                    branch: $scope.emergencyPublishContext.namespace,
                    isEmergencyPublish: true
                });
L
lepdou 已提交
332 333
        } else {
            EventManager.emit(EventManager.EventType.PUBLISH_NAMESPACE,
334 335 336 337
                {
                    namespace: $scope.emergencyPublishContext.namespace,
                    isEmergencyPublish: true
                });
L
lepdou 已提交
338 339 340 341
        }

    }

L
lepdou 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354 355
    EventManager.subscribe(EventManager.EventType.DELETE_NAMESPACE_FAILED, function (context) {
        $scope.deleteNamespaceContext = context;

        if (context.reason == 'master_instance') {
            AppUtil.showModal('#deleteNamespaceDenyForMasterInstanceDialog');
        } else if (context.reason == 'branch_instance') {
            AppUtil.showModal('#deleteNamespaceDenyForBranchInstanceDialog');
        } else if (context.reason == 'public_namespace') {
            var otherAppAssociatedNamespaces = context.otherAppAssociatedNamespaces;
            var namespaceTips = [];
            otherAppAssociatedNamespaces.forEach(function (namespace) {
                var appId = namespace.appId;
                var clusterName = namespace.clusterName;
                var url = '/config.html?#/appid=' + appId + '&env=' + $scope.pageContext.env + '&cluster='
356
                    + clusterName;
L
lepdou 已提交
357

358 359
                namespaceTips.push("<a target='_blank' href=\'" + url + "\'>AppId = " + appId + ", Cluster = " + clusterName
                    + ", Namespace = " + namespace.namespaceName + "</a>");
L
lepdou 已提交
360 361
            });

362
            $scope.deleteNamespaceContext.detailReason = $translate.instant('Config.DeleteNamespaceFailedTips') + "<br>" + namespaceTips.join("<br>");
L
lepdou 已提交
363 364 365 366 367 368

            AppUtil.showModal('#deleteNamespaceDenyForPublicNamespaceDialog');
        }

    });

369 370 371 372 373
    EventManager.subscribe(EventManager.EventType.SYNTAX_CHECK_TEXT_FAILED, function (context) {
        $scope.syntaxCheckContext = context;

        AppUtil.showModal('#syntaxCheckFailedDialog');
    });
L
lepdou 已提交
374

L
abtest  
lepdou 已提交
375
    new Clipboard('.clipboard');
376

L
lepdou 已提交
377

L
abtest  
lepdou 已提交
378 379
}

L
lepdou 已提交
380