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

function controller($rootScope, $scope, toastr, AppUtil, EventManager, ConfigService,
L
lepdou 已提交
7
                    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 32 33

        initRole();
        initUser();
        initPublishInfo();
    }

    function initRole() {
L
abtest  
lepdou 已提交
34 35 36 37 38 39 40 41 42 43
        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 已提交
44
    }
L
abtest  
lepdou 已提交
45

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

    }

L
lepdou 已提交
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
    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 已提交
89 90
    EventManager.subscribe(EventManager.EventType.REFRESH_NAMESPACE,
                           function (context) {
L
lepdou 已提交
91
                               if (context.namespace) {
L
abtest  
lepdou 已提交
92
                                   refreshSingleNamespace(context.namespace);
L
lepdou 已提交
93
                               } else {
L
abtest  
lepdou 已提交
94
                                   refreshAllNamespaces();
Z
zhangle 已提交
95 96
                               }

L
abtest  
lepdou 已提交
97 98 99 100 101 102 103 104
                           });

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

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

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

L
lepdou 已提交
112
                initPublishInfo();
L
abtest  
lepdou 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
            }, function (result) {
                toastr.error(AppUtil.errorMsg(result), "加载配置信息出错");
            });
    }

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

        ConfigService.load_namespace($rootScope.pageContext.appId,
                                     $rootScope.pageContext.env,
                                     $rootScope.pageContext.clusterName,
                                     namespace.baseInfo.namespaceName).then(
            function (result) {

                $scope.namespaces.forEach(function (namespace, index) {
L
lepdou 已提交
130
                    if (namespace.baseInfo.namespaceName == result.baseInfo.namespaceName) {
N
nobodyiam 已提交
131 132 133
                        result.showNamespaceBody = true;
                        result.initialized = true;
                        result.show = namespace.show;
L
abtest  
lepdou 已提交
134
                        $scope.namespaces[index] = result;
L
lepdou 已提交
135 136 137 138
                    }
                });

                initPublishInfo();
L
abtest  
lepdou 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

            }, function (result) {
                toastr.error(AppUtil.errorMsg(result), "加载配置信息出错");
            });
    }

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

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

    var toDeleteItemId = 0;

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

159
        $scope.config = item;
L
abtest  
lepdou 已提交
160
        $scope.toOperationNamespace = namespace;
161
        toDeleteItemId = item.id;
L
abtest  
lepdou 已提交
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

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

    function deleteItem() {
        ConfigService.delete_item($rootScope.pageContext.appId,
                                  $rootScope.pageContext.env,
                                  $rootScope.pageContext.clusterName,
                                  $scope.toOperationNamespace.baseInfo.namespaceName,
                                  toDeleteItemId).then(
            function (result) {
                toastr.success("删除成功!");
                EventManager.emit(EventManager.EventType.REFRESH_NAMESPACE,
                                  {
                                      namespace: $scope.toOperationNamespace
                                  });
            }, function (result) {
                toastr.error(AppUtil.errorMsg(result), "删除失败");
            });
    }

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

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

191
        if (namespace.isBranch || namespace.isLinkedNamespace) {
L
abtest  
lepdou 已提交
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
            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,
                                            $rootScope.pageContext.env,
                                            $rootScope.pageContext.clusterName,
                                            toCreateBranchNamespace.baseInfo.namespaceName)
            .then(function (result) {
                toastr.success("创建灰度成功");
                EventManager.emit(EventManager.EventType.REFRESH_NAMESPACE,
                                  {
                                      namespace: toCreateBranchNamespace
                                  });
            }, function (result) {
                toastr.error(AppUtil.errorMsg(result), "创建灰度失败");
            })

    }

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

    function deleteBranch() {
        NamespaceBranchService.deleteBranch($rootScope.pageContext.appId,
                                            $rootScope.pageContext.env,
                                            $rootScope.pageContext.clusterName,
                                            $scope.toDeleteBranch.baseInfo.namespaceName,
                                            $scope.toDeleteBranch.baseInfo.clusterName
            )
            .then(function (result) {
                toastr.success("删除成功");
                EventManager.emit(EventManager.EventType.REFRESH_NAMESPACE,
                                  {
                                      namespace: $scope.toDeleteBranch.parentNamespace
                                  });
            }, function (result) {
                toastr.error(AppUtil.errorMsg(result), "删除分支失败");
            })

    }

L
lepdou 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    EventManager.subscribe(EventManager.EventType.EMERGENCY_PUBLISH,
                           function (context) {
                               AppUtil.showModal("#emergencyPublishAlertDialog");
                               $scope.emergencyPublishContext = context;
                           });

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

            EventManager.emit(EventManager.EventType.MERGE_AND_PUBLISH_NAMESPACE,
                              {
                                  branch: $scope.emergencyPublishContext.namespace,
                                  isEmergencyPublish: true
                              });
        } else {
            EventManager.emit(EventManager.EventType.PUBLISH_NAMESPACE,
                              {
                                  namespace: $scope.emergencyPublishContext.namespace,
                                  isEmergencyPublish: true
                              });
        }

    }

L
lepdou 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
    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='
                          + clusterName;

                namespaceTips.push("<a target='_blank' href=\'" + url + "\'>AppId = " + appId + ", 集群 = " + clusterName
                                   + ", Namespace = " + namespace.namespaceName + "</a>");
            });

            $scope.deleteNamespaceContext.detailReason =
                "以下应用已关联此公共Namespace,必须先删除全部已关联的Namespace才能删除公共Namespace。<br>"
                + namespaceTips.join("<br>");

            AppUtil.showModal('#deleteNamespaceDenyForPublicNamespaceDialog');
        }

    });


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

L
lepdou 已提交
376

L
abtest  
lepdou 已提交
377 378
}

L
lepdou 已提交
379