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

L
abtest  
lepdou 已提交
8 9
function ConfigBaseInfoController($rootScope, $scope, $location, toastr, EventManager, UserService, AppService,
                                  FavoriteService,
10 11
                                  PermissionService,
                                  AppUtil) {
12 13 14

    var appId = AppUtil.parseParams($location.$$url).appid;

15
    initPage();
16

17 18 19 20 21 22 23 24 25 26 27 28 29
    function initPage() {
        $rootScope.hideTip = JSON.parse(localStorage.getItem("hideTip"));

        //load session storage to recovery scene
        var scene = JSON.parse(sessionStorage.getItem(appId));
        $rootScope.pageContext = {
            appId: appId,
            env: scene ? scene.env : '',
            clusterName: scene ? scene.cluster : 'default'
        };

        UserService.load_user().then(function (result) {
            $rootScope.pageContext.userId = result.userId;
30
            loadAppInfo();
31
            handleFavorite();
L
abtest  
lepdou 已提交
32
        }, function (result) {
33
            toastr.error(AppUtil.errorMsg(result), "获取用户登录信息失败");
34
        });
35 36

        handlePermission();
37 38
    }

39 40 41 42
    function loadAppInfo() {
        $scope.notFoundApp = true;
        AppService.load($rootScope.pageContext.appId).then(function (result) {
            $scope.notFoundApp = false;
43

44 45
            $scope.appBaseInfo = result;
            $scope.appBaseInfo.orgInfo = result.orgName + '(' + result.orgId + ')';
46

47
            loadNavTree();
48
            recordVisitApp();
49

50 51 52 53
            $(".J_appFound").removeClass("hidden");
        }, function (result) {
            $(".J_appNotFound").removeClass("hidden");
        });
54

55 56 57 58 59
        ////// 补缺失的环境 //////
        $scope.missEnvs = [];
        AppService.find_miss_envs($rootScope.pageContext.appId).then(function (result) {
            $scope.missEnvs = AppUtil.collectData(result);
        }, function (result) {
60

61
        });
62

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        $scope.createAppInMissEnv = function () {
            var count = 0;
            $scope.missEnvs.forEach(function (env) {
                AppService.create_remote(env, $scope.appBaseInfo).then(function (result) {
                    toastr.success(env, '创建成功');
                    count++;
                    if (count == $scope.missEnvs.length) {
                        location.reload(true);
                    }
                }, function (result) {
                    toastr.error(AppUtil.errorMsg(result), '创建失败:' + env);
                    count++;
                    if (count == $scope.missEnvs.length) {
                        location.reload(true);
                    }
                });
            });
        };
    }
82

83 84 85 86 87 88
    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;

89
        if (!visitedAppsObject) {
90
            visitedAppsObject = {};
91 92 93
        }

        if (!visitedAppsObject[$rootScope.pageContext.userId]) {
L
abtest  
lepdou 已提交
94
            visitedAppsObject[$rootScope.pageContext.userId] = [];
95 96 97 98 99 100 101 102 103 104
        }

        var visitedApps = visitedAppsObject[$rootScope.pageContext.userId];
        if (visitedApps && visitedApps.length > 0) {
            visitedApps.forEach(function (app) {
                if (app == appId) {
                    hasSaved = true;
                    return;
                }
            });
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
        }

        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,
                                 JSON.stringify(visitedAppsObject));
        }

    }

121
    function loadNavTree() {
L
lepdou 已提交
122

123 124 125 126 127 128 129 130 131
        AppService.load_nav_tree($rootScope.pageContext.appId).then(function (result) {
            var navTree = [];
            var nodes = AppUtil.collectData(result);

            if (!nodes || nodes.length == 0) {
                toastr.error("系统出错,请重试或联系系统负责人");
                return;
            }
            //default first env if session storage is empty
132 133
            if (!$rootScope.pageContext.env) {
                $rootScope.pageContext.env = nodes[0].env;
134
            }
L
abtest  
lepdou 已提交
135 136

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

138
            nodes.forEach(function (env) {
139 140 141 142 143
                if (!env.clusters || env.clusters.length == 0) {
                    return;
                }
                var node = {};
                node.text = env.env;
L
lepdou 已提交
144

145 146 147 148 149
                var clusterNodes = [];

                //如果env下面只有一个default集群则不显示集群列表
                if (env.clusters && env.clusters.length == 1 && env.clusters[0].name
                                                                == 'default') {
150
                    if ($rootScope.pageContext.env == env.env) {
151 152 153 154
                        node.state = {};
                        node.state.selected = true;
                    }
                    node.selectable = true;
L
lepdou 已提交
155

156 157 158
                } else {
                    node.selectable = false;
                    //cluster list
159
                    env.clusters.forEach(function (cluster) {
160 161 162 163
                        var clusterNode = {},
                            parentNode = [];

                        //default selection from session storage or first env & first cluster
164
                        if ($rootScope.pageContext.env == env.env && $rootScope.pageContext.clusterName
165
                                                                     == cluster.name) {
166 167 168 169 170 171 172 173 174
                            clusterNode.state = {};
                            clusterNode.state.selected = true;
                        }

                        clusterNode.text = cluster.name;
                        parentNode.push(node.text);
                        clusterNode.tags = ['集群'];
                        clusterNode.parentNode = parentNode;
                        clusterNodes.push(clusterNode);
L
lepdou 已提交
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
                    });
                }
                node.nodes = clusterNodes;
                navTree.push(node);
            });

            //init treeview
            $('#treeview').treeview({
                                        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
                                                               }));

L
abtest  
lepdou 已提交
210
                                            EventManager.emit(EventManager.EventType.REFRESH_NAMESPACE);
211
                                            $rootScope.showSideBar = false;
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
                                        }
                                    });

            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) {
            toastr.error(AppUtil.errorMsg(result), "系统出错,请重试或联系系统负责人");
        });
L
lepdou 已提交
237

238 239
    }

240 241 242
    function handleFavorite() {

        FavoriteService.findFavorites($rootScope.pageContext.userId,
243
                                      $rootScope.pageContext.appId)
244 245 246
            .then(function (result) {
                if (result && result.length) {
                    $scope.favoriteId = result[0].id;
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
        $scope.addFavorite = function () {
            var favorite = {
                userId: $rootScope.pageContext.userId,
                appId: $rootScope.pageContext.appId
            };

            FavoriteService.addFavorite(favorite)
                .then(function (result) {
                    $scope.favoriteId = result.id;
                    toastr.success("收藏成功");
                }, function (result) {
                    toastr.error(AppUtil.errorMsg(result), "收藏失败");
                })
        };

        $scope.deleteFavorite = function () {
            FavoriteService.deleteFavorite($scope.favoriteId)
                .then(function (result) {
                    $scope.favoriteId = 0;
                    toastr.success("取消收藏成功");
                }, function (result) {
                    toastr.error(AppUtil.errorMsg(result), "取消收藏失败");
                })
        };
    }

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

283
        });
284

285 286 287
        PermissionService.has_create_cluster_permission(appId).then(function (result) {
            $scope.hasCreateClusterPermission = result.hasPermission;
        }, function (result) {
288

289
        });
290

291 292 293
        PermissionService.has_assign_user_permission(appId).then(function (result) {
            $scope.hasAssignUserPermission = result.hasPermission;
        }, function (result) {
294

295
        });
296

297 298 299
        $scope.showMasterPermissionTips = function () {
            $("#masterNoPermissionDialog").modal('show');
        };
300 301
    }

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
    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;
        }

    };

    $(window).resize(function(){
        $scope.$apply(function(){
            $rootScope.adaptScreenSize();
        });
    });

327
}
L
lepdou 已提交
328