ConfigBaseInfoController.js 13.5 KB
Newer Older
L
lepdou 已提交
1
application_module.controller("ConfigBaseInfoController",
C
czd890 已提交
2 3 4 5 6
    ['$rootScope', '$scope', '$window', '$location', '$translate', 'toastr', 'EventManager', 'UserService',
        'AppService',
        'FavoriteService',
        'PermissionService',
        'AppUtil', ConfigBaseInfoController]);
7

C
czd890 已提交
8 9 10 11
function ConfigBaseInfoController($rootScope, $scope, $window, $location, $translate, toastr, EventManager, UserService, AppService,
    FavoriteService,
    PermissionService,
    AppUtil) {
12

L
lepdou 已提交
13 14
    var urlParams = AppUtil.parseParams($location.$$url);
    var appId = urlParams.appid;
15

16 17
    if (!appId) {
        $window.location.href = '/index.html';
L
lepdou 已提交
18
        return;
19 20
    }

21
    initPage();
22

23 24 25 26 27
    function initPage() {
        $rootScope.hideTip = JSON.parse(localStorage.getItem("hideTip"));

        //load session storage to recovery scene
        var scene = JSON.parse(sessionStorage.getItem(appId));
L
lepdou 已提交
28

29 30
        $rootScope.pageContext = {
            appId: appId,
L
lepdou 已提交
31 32
            env: urlParams.env ? urlParams.env : (scene ? scene.env : ''),
            clusterName: urlParams.cluster ? urlParams.cluster : (scene ? scene.cluster : 'default')
33 34
        };

L
lepdou 已提交
35 36 37 38
        //storage page context to session storage
        sessionStorage.setItem(
            $rootScope.pageContext.appId,
            JSON.stringify({
C
czd890 已提交
39 40 41
                env: $rootScope.pageContext.env,
                cluster: $rootScope.pageContext.clusterName
            }));
L
lepdou 已提交
42

43 44
        UserService.load_user().then(function (result) {
            $rootScope.pageContext.userId = result.userId;
45
            loadAppInfo();
46
            handleFavorite();
L
abtest  
lepdou 已提交
47
        }, function (result) {
48
            toastr.error(AppUtil.errorMsg(result),  $translate.instant('Config.GetUserInfoFailed'));
49
        });
50 51

        handlePermission();
52 53
    }

54
    function loadAppInfo() {
L
lepdou 已提交
55

56 57 58
        $scope.notFoundApp = true;
        AppService.load($rootScope.pageContext.appId).then(function (result) {
            $scope.notFoundApp = false;
59

60 61
            $scope.appBaseInfo = result;
            $scope.appBaseInfo.orgInfo = result.orgName + '(' + result.orgId + ')';
62

63
            loadNavTree();
64
            recordVisitApp();
L
lepdou 已提交
65
            findMissEnvs();
66

67 68 69 70
            $(".J_appFound").removeClass("hidden");
        }, function (result) {
            $(".J_appNotFound").removeClass("hidden");
        });
L
lepdou 已提交
71 72 73 74 75 76
    }

    $scope.createAppInMissEnv = function () {
        var count = 0;
        $scope.missEnvs.forEach(function (env) {
            AppService.create_remote(env, $scope.appBaseInfo).then(function (result) {
C
czd890 已提交
77
                toastr.success(env, $translate.instant('Common.Created'));
L
lepdou 已提交
78 79 80 81 82
                count++;
                if (count == $scope.missEnvs.length) {
                    location.reload(true);
                }
            }, function (result) {
C
czd890 已提交
83
                toastr.error(AppUtil.errorMsg(result), `${$translate.instant('Common.CreateFailed')}:${env}`);
L
lepdou 已提交
84 85 86 87 88 89 90
                count++;
                if (count == $scope.missEnvs.length) {
                    location.reload(true);
                }
            });
        });
    };
91

92 93 94
    $scope.createMissingNamespaces = function () {
        AppService.create_missing_namespaces($rootScope.pageContext.appId, $rootScope.pageContext.env,
            $rootScope.pageContext.clusterName).then(function (result) {
C
czd890 已提交
95
                toastr.success($translate.instant('Common.Created'));
96 97
                location.reload(true);
            }, function (result) {
C
czd890 已提交
98
                toastr.error(AppUtil.errorMsg(result), $translate.instant('Common.CreateFailed'));
99
            }
C
czd890 已提交
100
            );
101 102
    };

L
lepdou 已提交
103
    function findMissEnvs() {
104 105 106
        $scope.missEnvs = [];
        AppService.find_miss_envs($rootScope.pageContext.appId).then(function (result) {
            $scope.missEnvs = AppUtil.collectData(result);
107

N
nobodyiam 已提交
108
            if ($scope.missEnvs.length > 0) {
C
czd890 已提交
109
                toastr.warning($translate.instant('Config.ProjectMissEnvInfos'));
N
nobodyiam 已提交
110 111
            }

112 113
            $scope.findMissingNamespaces();
        });
114
    }
115 116 117 118 119 120 121 122

    EventManager.subscribe(EventManager.EventType.CHANGE_ENV_CLUSTER, function () {
        $scope.findMissingNamespaces();
    });

    $scope.findMissingNamespaces = function () {
        $scope.missingNamespaces = [];
        // only check missing private namespaces when app exists in current env
123
        if ($rootScope.pageContext.env && $scope.missEnvs.indexOf($rootScope.pageContext.env) === -1) {
C
czd890 已提交
124 125 126 127 128 129 130
            AppService.find_missing_namespaces($rootScope.pageContext.appId, $rootScope.pageContext.env,
                $rootScope.pageContext.clusterName).then(function (result) {
                    $scope.missingNamespaces = AppUtil.collectData(result);
                    if ($scope.missingNamespaces.length > 0) {
                        toastr.warning($translate.instant('Config.ProjectMissNamespaceInfos'));
                    }
                });
131 132 133
        }
    };

134 135 136 137 138 139
    function recordVisitApp() {
        //save user recent visited apps
        var VISITED_APPS_STORAGE_KEY = "VisitedAppsV2";
        var visitedAppsObject = JSON.parse(localStorage.getItem(VISITED_APPS_STORAGE_KEY));
        var hasSaved = false;

140
        if (!visitedAppsObject) {
141
            visitedAppsObject = {};
142 143 144
        }

        if (!visitedAppsObject[$rootScope.pageContext.userId]) {
L
abtest  
lepdou 已提交
145
            visitedAppsObject[$rootScope.pageContext.userId] = [];
146 147 148 149 150 151 152 153 154 155
        }

        var visitedApps = visitedAppsObject[$rootScope.pageContext.userId];
        if (visitedApps && visitedApps.length > 0) {
            visitedApps.forEach(function (app) {
                if (app == appId) {
                    hasSaved = true;
                    return;
                }
            });
156 157 158 159 160 161 162 163 164 165 166
        }

        var currentUserVisitedApps = visitedAppsObject[$rootScope.pageContext.userId];
        if (!hasSaved) {
            //if queue's length bigger than 6 will remove oldest app
            if (currentUserVisitedApps.length >= 6) {
                currentUserVisitedApps.splice(0, 1);
            }
            currentUserVisitedApps.push($rootScope.pageContext.appId);

            localStorage.setItem(VISITED_APPS_STORAGE_KEY,
C
czd890 已提交
167
                JSON.stringify(visitedAppsObject));
168 169 170 171
        }

    }

172
    function loadNavTree() {
L
lepdou 已提交
173

174 175 176 177 178
        AppService.load_nav_tree($rootScope.pageContext.appId).then(function (result) {
            var navTree = [];
            var nodes = AppUtil.collectData(result);

            if (!nodes || nodes.length == 0) {
C
czd890 已提交
179
                toastr.error($translate.instant('Config.SystemError'));
180 181 182
                return;
            }
            //default first env if session storage is empty
183 184
            if (!$rootScope.pageContext.env) {
                $rootScope.pageContext.env = nodes[0].env;
185
            }
L
abtest  
lepdou 已提交
186 187

            EventManager.emit(EventManager.EventType.REFRESH_NAMESPACE);
188

189
            nodes.forEach(function (env) {
190 191 192 193 194
                if (!env.clusters || env.clusters.length == 0) {
                    return;
                }
                var node = {};
                node.text = env.env;
L
lepdou 已提交
195

196 197 198 199
                var clusterNodes = [];

                //如果env下面只有一个default集群则不显示集群列表
                if (env.clusters && env.clusters.length == 1 && env.clusters[0].name
C
czd890 已提交
200
                    == 'default') {
201
                    if ($rootScope.pageContext.env == env.env) {
202 203 204 205
                        node.state = {};
                        node.state.selected = true;
                    }
                    node.selectable = true;
L
lepdou 已提交
206

207 208 209
                } else {
                    node.selectable = false;
                    //cluster list
210
                    env.clusters.forEach(function (cluster) {
211 212 213 214
                        var clusterNode = {},
                            parentNode = [];

                        //default selection from session storage or first env & first cluster
215
                        if ($rootScope.pageContext.env == env.env && $rootScope.pageContext.clusterName
C
czd890 已提交
216
                            == cluster.name) {
217 218 219 220 221 222
                            clusterNode.state = {};
                            clusterNode.state.selected = true;
                        }

                        clusterNode.text = cluster.name;
                        parentNode.push(node.text);
C
czd890 已提交
223
                        clusterNode.tags = [$translate.instant('Common.Cluster')];
224 225
                        clusterNode.parentNode = parentNode;
                        clusterNodes.push(clusterNode);
L
lepdou 已提交
226

227 228 229 230 231 232 233 234
                    });
                }
                node.nodes = clusterNodes;
                navTree.push(node);
            });

            //init treeview
            $('#treeview').treeview({
C
czd890 已提交
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
                color: "#797979",
                showBorder: true,
                data: navTree,
                levels: 99,
                expandIcon: '',
                collapseIcon: '',
                showTags: true,
                onNodeSelected: function (event, data) {
                    if (!data.parentNode) {//first nav node
                        $rootScope.pageContext.env = data.text;
                        $rootScope.pageContext.clusterName =
                            'default';
                    } else {//second cluster node
                        $rootScope.pageContext.env =
                            data.parentNode[0];
                        $rootScope.pageContext.clusterName =
                            data.text;
                    }
                    //storage scene
                    sessionStorage.setItem(
                        $rootScope.pageContext.appId,
                        JSON.stringify({
                            env: $rootScope.pageContext.env,
                            cluster: $rootScope.pageContext.clusterName
                        }));

                    $window.location.href = "/config.html#/appid="
                        + $rootScope.pageContext.appId
                        + "&env=" + $rootScope.pageContext.env
                        + "&cluster=" + $rootScope.pageContext.clusterName;

                    EventManager.emit(EventManager.EventType.REFRESH_NAMESPACE);
                    EventManager.emit(EventManager.EventType.CHANGE_ENV_CLUSTER);
                    $rootScope.showSideBar = false;
                }
            });
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291

            var envMapClusters = {};
            navTree.forEach(function (node) {
                if (node.nodes && node.nodes.length > 0) {

                    var clusterNames = [];
                    node.nodes.forEach(function (cluster) {
                        if (cluster.text != 'default') {
                            clusterNames.push(cluster.text);
                        }

                    });

                    envMapClusters[node.text] = clusterNames.join(",");

                }
            });

            $rootScope.envMapClusters = envMapClusters;

        }, function (result) {
C
czd890 已提交
292
            toastr.error(AppUtil.errorMsg(result), $translate.instant('Config.SystemError'));
293
        });
L
lepdou 已提交
294

295 296
    }

297 298 299
    function handleFavorite() {

        FavoriteService.findFavorites($rootScope.pageContext.userId,
C
czd890 已提交
300
            $rootScope.pageContext.appId)
301 302 303
            .then(function (result) {
                if (result && result.length) {
                    $scope.favoriteId = result[0].id;
304
                }
305

306 307
            });

308 309 310 311 312 313 314 315 316
        $scope.addFavorite = function () {
            var favorite = {
                userId: $rootScope.pageContext.userId,
                appId: $rootScope.pageContext.appId
            };

            FavoriteService.addFavorite(favorite)
                .then(function (result) {
                    $scope.favoriteId = result.id;
C
czd890 已提交
317
                    toastr.success($translate.instant('Config.FavoriteSuccessfully'));
318
                }, function (result) {
C
czd890 已提交
319
                    toastr.error(AppUtil.errorMsg(result), $translate.instant('Config.FavoriteFailed'));
320 321 322 323 324 325 326
                })
        };

        $scope.deleteFavorite = function () {
            FavoriteService.deleteFavorite($scope.favoriteId)
                .then(function (result) {
                    $scope.favoriteId = 0;
C
czd890 已提交
327
                    toastr.success($translate.instant('Config.CancelledFavorite'));
328
                }, function (result) {
329
                    toastr.error(AppUtil.errorMsg(result), $translate.instant('Config.CancelFavoriteFailed'));
330 331 332 333 334 335 336 337 338
                })
        };
    }

    function handlePermission() {
        //permission
        PermissionService.has_create_namespace_permission(appId).then(function (result) {
            $scope.hasCreateNamespacePermission = result.hasPermission;
        }, function (result) {
339

340
        });
341

342 343 344
        PermissionService.has_create_cluster_permission(appId).then(function (result) {
            $scope.hasCreateClusterPermission = result.hasPermission;
        }, function (result) {
345

346
        });
347

348

349 350 351
        PermissionService.has_assign_user_permission(appId).then(function (result) {
            $scope.hasAssignUserPermission = result.hasPermission;
        }, function (result) {
352

353
        });
354

355 356 357
        $scope.showMasterPermissionTips = function () {
            $("#masterNoPermissionDialog").modal('show');
        };
358 359
    }

360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
    var VIEW_MODE_SWITCH_WIDTH = 1156;
    if (window.innerWidth <= VIEW_MODE_SWITCH_WIDTH) {
        $rootScope.viewMode = 2;
        $rootScope.showSideBar = false;
    } else {
        $rootScope.viewMode = 1;
    }

    $rootScope.adaptScreenSize = function () {
        if (window.innerWidth <= VIEW_MODE_SWITCH_WIDTH) {
            $rootScope.viewMode = 2;
        } else {
            $rootScope.viewMode = 1;
            $rootScope.showSideBar = false;
        }

    };

L
lepdou 已提交
378 379
    $(window).resize(function () {
        $scope.$apply(function () {
380 381 382 383
            $rootScope.adaptScreenSize();
        });
    });

384
}
L
lepdou 已提交
385