ConfigBaseInfoController.js 10.6 KB
Newer Older
L
lepdou 已提交
1
application_module.controller("ConfigBaseInfoController",
L
lepdou 已提交
2 3 4 5
                              ['$rootScope', '$scope', '$location', 'toastr', 'AppService', 'PermissionService',
                               'AppUtil',
                               function ($rootScope, $scope, $location, toastr, AppService, PermissionService,
                                         AppUtil) {
L
lepdou 已提交
6 7

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

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

L
lepdou 已提交
12 13
                                   var pageContext = {
                                       appId: appId,
L
update  
lepdou 已提交
14 15
                                       env: scene ? scene.env : '',
                                       clusterName: scene ? scene.cluster : 'default'
L
lepdou 已提交
16 17 18 19 20 21 22 23
                                   };

                                   $rootScope.pageContext = pageContext;

                                   ////// load cluster nav tree //////

                                   AppService.load_nav_tree($rootScope.pageContext.appId).then(function (result) {
                                       var navTree = [];
L
lepdou 已提交
24 25
                                       var nodes = AppUtil.collectData(result);

26 27
                                       if (!nodes || nodes.length == 0) {
                                           toastr.error("加载环境信息出错");
L
lepdou 已提交
28 29
                                           return;
                                       }
30 31 32 33
                                       //default first env if session storage is empty
                                       if (!pageContext.env) {
                                           pageContext.env = nodes[0].env;
                                       }
L
lepdou 已提交
34 35 36 37 38
                                       $rootScope.refreshNamespaces();

                                       nodes.forEach(function (env, envIdx) {
                                           if (!env.clusters || env.clusters.length == 0) {
                                               return;
39
                                           }
L
lepdou 已提交
40
                                           var node = {};
L
lepdou 已提交
41
                                           node.text = env.env;
L
lepdou 已提交
42
                                           var clusterNodes = [];
43

L
lepdou 已提交
44
                                           //如果env下面只有一个default集群则不显示集群列表
L
lepdou 已提交
45
                                           if (env.clusters && env.clusters.length == 1 && env.clusters[0].name
46 47
                                                                                           == 'default') {
                                               if (envIdx == 0) {
L
lepdou 已提交
48 49 50
                                                   node.state = {};
                                                   node.state.selected = true;
                                               }
L
lepdou 已提交
51
                                               node.selectable = true;
L
lepdou 已提交
52
                                           } else {
L
lepdou 已提交
53
                                               node.selectable = false;
54
                                               //cluster list
L
lepdou 已提交
55
                                               env.clusters.forEach(function (cluster, clusterIdx) {
L
lepdou 已提交
56 57 58
                                                   var clusterNode = {},
                                                       parentNode = [];

59
                                                   //default selection from session storage or first env & first cluster
L
update  
lepdou 已提交
60
                                                   if (pageContext.env == env.env && pageContext.clusterName == cluster.name) {
L
lepdou 已提交
61 62 63 64 65
                                                       clusterNode.state = {};
                                                       clusterNode.state.selected = true;
                                                   }

                                                   clusterNode.text = cluster.name;
L
lepdou 已提交
66
                                                   parentNode.push(node.text);
L
lepdou 已提交
67 68
                                                   clusterNode.tags = ['集群'];
                                                   clusterNode.parentNode = parentNode;
L
lepdou 已提交
69 70 71 72 73 74
                                                   clusterNodes.push(clusterNode);
                                               });
                                           }
                                           node.nodes = clusterNodes;
                                           navTree.push(node);
                                       });
L
lepdou 已提交
75

L
lepdou 已提交
76
                                       $('#treeview').treeview({
L
lepdou 已提交
77
                                                                   color: "#797979",
L
lepdou 已提交
78 79 80
                                                                   showBorder: true,
                                                                   data: navTree,
                                                                   levels: 99,
L
lepdou 已提交
81 82
                                                                   expandIcon: '',
                                                                   collapseIcon: '',
L
lepdou 已提交
83
                                                                   showTags: true,
L
lepdou 已提交
84
                                                                   onNodeSelected: function (event, data) {
L
lepdou 已提交
85
                                                                       if (!data.parentNode) {//first nav node
L
lepdou 已提交
86
                                                                           $rootScope.pageContext.env = data.text;
L
lepdou 已提交
87 88 89
                                                                           $rootScope.pageContext.clusterName =
                                                                               'default';
                                                                       } else {//second cluster node
90 91
                                                                           $rootScope.pageContext.env =
                                                                               data.parentNode[0];
L
lepdou 已提交
92 93
                                                                           $rootScope.pageContext.clusterName =
                                                                               data.text;
L
lepdou 已提交
94
                                                                       }
L
update  
lepdou 已提交
95
                                                                       //storage scene
96
                                                                       sessionStorage.setItem(
L
update  
lepdou 已提交
97 98 99 100 101
                                                                           $rootScope.pageContext.appId,
                                                                           JSON.stringify({
                                                                               env: $rootScope.pageContext.env,
                                                                               cluster: $rootScope.pageContext.clusterName
                                                                           }));
102

L
lepdou 已提交
103 104 105
                                                                       $rootScope.refreshNamespaces();
                                                                   }
                                                               });
L
lepdou 已提交
106

L
lepdou 已提交
107 108 109 110 111
                                   }, function (result) {
                                       toastr.error(AppUtil.errorMsg(result), "加载导航出错");
                                   });

                                   ////// app info //////
L
lepdou 已提交
112

L
lepdou 已提交
113
                                   AppService.load($rootScope.pageContext.appId).then(function (result) {
L
lepdou 已提交
114
                                       $scope.appBaseInfo = result;
J
Jason Song 已提交
115
                                       $scope.appBaseInfo.orgInfo = result.orgName + '(' + result.orgId + ')';
L
lepdou 已提交
116
                                   }, function (result) {
L
lepdou 已提交
117 118 119 120
                                       toastr.error(AppUtil.errorMsg(result), "加载App信息出错");
                                   });

                                   ////// 补缺失的环境 //////
L
lepdou 已提交
121 122 123
                                   $scope.missEnvs = [];
                                   AppService.find_miss_envs($rootScope.pageContext.appId).then(function (result) {
                                       $scope.missEnvs = AppUtil.collectData(result);
L
lepdou 已提交
124
                                   }, function (result) {
L
lepdou 已提交
125 126 127
                                       console.log(AppUtil.errorMsg(result));
                                   });

L
lepdou 已提交
128
                                   $scope.createAppInMissEnv = function () {
L
lepdou 已提交
129
                                       var count = 0;
L
lepdou 已提交
130 131
                                       $scope.missEnvs.forEach(function (env) {
                                           AppService.create_remote(env, $scope.appBaseInfo).then(function (result) {
L
lepdou 已提交
132
                                               toastr.success(env, '创建成功');
L
lepdou 已提交
133 134
                                               count++;
                                               if (count == $scope.missEnvs.length) {
L
lepdou 已提交
135 136 137 138
                                                   location.reload(true);
                                               }
                                           }, function (result) {
                                               toastr.error(AppUtil.errorMsg(result), '创建失败:' + env);
L
lepdou 已提交
139 140 141
                                               count++;
                                               if (count == $scope.missEnvs.length) {
                                                   location.reload(true);
L
lepdou 已提交
142 143 144 145 146
                                               }
                                           });
                                       });
                                   };

L
lepdou 已提交
147 148 149 150
                                   //permission
                                   PermissionService.has_create_namespace_permission(appId).then(function (result) {
                                       $scope.hasCreateNamespacePermission = result.hasPermission;
                                   }, function (result) {
151

L
lepdou 已提交
152
                                   });
153

L
lepdou 已提交
154 155 156 157 158
                                   PermissionService.has_create_cluster_permission(appId).then(function (result) {
                                       $scope.hasCreateClusterPermission = result.hasPermission;
                                   }, function (result) {

                                   });
159

L
lepdou 已提交
160 161 162 163 164 165
                                   PermissionService.has_assign_user_permission(appId).then(function (result) {
                                       $scope.hasAssignUserPermission = result.hasPermission;
                                   }, function (result) {

                                   });

L
lepdou 已提交
166 167
                               }]);