ConfigNamespaceController.js 22.1 KB
Newer Older
L
lepdou 已提交
1
application_module.controller("ConfigNamespaceController",
L
lepdou 已提交
2
                              ['$rootScope', '$scope', '$location', 'toastr', 'AppUtil', 'ConfigService', 'PermissionService',
L
lepdou 已提交
3 4
                               'CommitService',
                               function ($rootScope, $scope, $location, toastr, AppUtil, ConfigService, PermissionService, CommitService) {
5

L
lepdou 已提交
6
                                   var namespace_view_type = {
L
lepdou 已提交
7
                                       TEXT: 'text',
L
lepdou 已提交
8 9 10 11
                                       TABLE: 'table',
                                       LOG: 'log'
                                   };

L
lepdou 已提交
12
                                   $rootScope.refreshNamespaces = function (viewType) {
L
lepdou 已提交
13
                                       if ($rootScope.pageContext.env == '') {
14 15
                                           return;
                                       }
L
lepdou 已提交
16 17 18 19
                                       ConfigService.load_all_namespaces($rootScope.pageContext.appId,
                                                                         $rootScope.pageContext.env,
                                                                         $rootScope.pageContext.clusterName,
                                                                         viewType).then(
L
lepdou 已提交
20 21 22 23 24
                                           function (result) {
                                               $scope.namespaces = result;

                                               //初始化视图
                                               if ($scope.namespaces) {
L
lepdou 已提交
25 26

                                                   $scope.namespaces.forEach(function (namespace) {
L
lepdou 已提交
27
                                                       if (!viewType) {//default text view
L
lepdou 已提交
28
                                                           $scope.switchView(namespace, namespace_view_type.TABLE);
L
lepdou 已提交
29
                                                       } else if (viewType == namespace_view_type.TABLE) {
L
lepdou 已提交
30
                                                           namespace.viewType = namespace_view_type.TABLE;
L
lepdou 已提交
31 32
                                                       }

L
lepdou 已提交
33
                                                       namespace.isTextEditing = false;
L
lepdou 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
                                                       
                                                       //permission
                                                       PermissionService.has_modify_namespace_permission($rootScope.pageContext.appId, namespace.namespace.namespaceName)
                                                           .then(function (result) {
                                                               namespace.hasModifyPermission = result.hasPermission;     
                                                           }, function (result) {
                                                               
                                                           });

                                                       PermissionService.has_release_namespace_permission($rootScope.pageContext.appId, namespace.namespace.namespaceName)
                                                           .then(function (result) {
                                                               namespace.hasReleasePermission = result.hasPermission;
                                                           }, function (result) {

                                                           });
L
lepdou 已提交
49
                                                   });
L
lepdou 已提交
50
                                               }
L
lepdou 已提交
51 52
                                               setInterval(function () {
                                                   $('[data-tooltip="tooltip"]').tooltip();
53 54 55 56 57 58 59 60 61 62
                                                   $('.namespace-view-table').bind('mousewheel DOMMouseScroll',
                                                                                   function (e) {
                                                                                       var e0 = e.originalEvent,
                                                                                           delta = e0.wheelDelta
                                                                                                   || -e0.detail;

                                                                                       this.scrollTop +=
                                                                                           ( delta < 0 ? 1 : -1 ) * 30;
                                                                                       e.preventDefault();
                                                                                   });
L
lepdou 已提交
63
                                               }, 2500);
L
lepdou 已提交
64 65

                                           }, function (result) {
L
lepdou 已提交
66
                                               toastr.error(AppUtil.errorMsg(result), "加载配置信息出错");
L
lepdou 已提交
67
                                           });
L
lepdou 已提交
68
                                   };
L
lepdou 已提交
69

L
lepdou 已提交
70 71 72 73 74 75 76 77 78 79 80
                                   
                                   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 已提交
81
                                   $scope.switchView = function (namespace, viewType) {
L
lepdou 已提交
82
                                       namespace.viewType = viewType;
L
lepdou 已提交
83 84 85
                                       if (namespace_view_type.TEXT == viewType) {
                                           namespace.text = parseModel2Text(namespace);
                                       } else if (namespace_view_type.TABLE == viewType) {
L
lepdou 已提交
86

L
lepdou 已提交
87 88
                                       } else {
                                           $scope.loadCommitHistory(namespace);
L
lepdou 已提交
89 90
                                       }
                                   };
L
lepdou 已提交
91

L
lepdou 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
                                   $scope.loadCommitHistory = function(namespace) {
                                       if (!namespace.commits){
                                           namespace.commits = [];
                                           namespace.commitPage = 0;
                                       }
                                       CommitService.find_commits($rootScope.pageContext.appId,
                                                                  $rootScope.pageContext.env,
                                                                  $rootScope.pageContext.clusterName,
                                                                  namespace.namespace.namespaceName,
                                                                  namespace.commitPage)
                                           .then(function (result) {
                                               if (result.length == 0){
                                                   namespace.hasLoadAllCommit = true;
                                               }
                                               for (var i = 0; i < result.length; i++) {
                                                   //to json
                                                   result[i].changeSets = JSON.parse(result[i].changeSets);
                                                   namespace.commits.push(result[i]);
                                               }
                                               namespace.commitPage += 1;
                                           }, function (result) {
                                               toastr.error(AppUtil.errorMsg(result), "加载提交历史记录出错");
                                           });
                                   }

L
lepdou 已提交
117
                                   var MAX_ROW_SIZE = 30;
118
                                   var APPEND_ROW_SIZE = 8;
L
lepdou 已提交
119
                                   //把表格内容解析成文本
L
lepdou 已提交
120
                                   function parseModel2Text(namespace, modeType) {
L
lepdou 已提交
121

L
lepdou 已提交
122 123 124 125
                                       if (!namespace.items) {
                                           return "无配置信息";
                                       }
                                       var result = "";
L
lepdou 已提交
126
                                       var itemCnt = 0;
L
lepdou 已提交
127
                                       namespace.items.forEach(function (item) {
L
lepdou 已提交
128 129 130 131
                                           //deleted key
                                           if (!item.item.lastModifiedBy){
                                               return;
                                           }
L
lepdou 已提交
132
                                           if (item.item.key) {
J
Jason Song 已提交
133 134
                                               //use string \n to display as new line
                                               var itemValue = item.item.value.replace(/\n/g,"\\n");
L
lepdou 已提交
135

L
lepdou 已提交
136
                                               result +=
L
lepdou 已提交
137
                                                   item.item.key + " = " + itemValue + "\n";
L
lepdou 已提交
138 139
                                           } else {
                                               result += item.item.comment + "\n";
L
lepdou 已提交
140
                                           }
L
lepdou 已提交
141
                                           itemCnt++;
L
lepdou 已提交
142
                                       });
143

144 145
                                       namespace.itemCnt =
                                           itemCnt > MAX_ROW_SIZE ? MAX_ROW_SIZE : itemCnt + APPEND_ROW_SIZE;
L
lepdou 已提交
146 147 148
                                       return result;
                                   }

L
lepdou 已提交
149
                                   //更新配置
L
lepdou 已提交
150
                                   $scope.commitChange = function (namespace) {
L
lepdou 已提交
151 152
                                       ConfigService.modify_items($scope.pageContext.appId, $scope.pageContext.env,
                                                                  $scope.pageContext.clusterName,
L
lepdou 已提交
153 154 155
                                                                  namespace.namespace.namespaceName,
                                                                  namespace.editText,
                                                                  namespace.namespace.id).then(
L
lepdou 已提交
156
                                           function (result) {
L
lepdou 已提交
157
                                               toastr.success("更新成功");
L
lepdou 已提交
158
                                               //refresh all namespace items
L
lepdou 已提交
159
                                               $rootScope.refreshNamespaces();
L
lepdou 已提交
160

L
lepdou 已提交
161 162
                                               namespace.commited = true;
                                               $scope.toggleTextEditStatus(namespace);
L
lepdou 已提交
163

L
lepdou 已提交
164
                                           }, function (result) {
L
lepdou 已提交
165
                                               toastr.error(AppUtil.errorMsg(result), "更新失败");
L
lepdou 已提交
166 167 168 169
                                           }
                                       );
                                   };

L
lepdou 已提交
170 171 172
                                   //文本编辑框状态切换
                                   $scope.toggleTextEditStatus = function (namespace) {
                                       namespace.isTextEditing = !namespace.isTextEditing;
173
                                       if (namespace.isTextEditing) {//切换为编辑状态
L
lepdou 已提交
174
                                           namespace.commited = false;
175
                                           namespace.backupText = namespace.text;
L
lepdou 已提交
176 177
                                           namespace.editText = parseModel2Text(namespace, 'edit');

L
lepdou 已提交
178
                                       } else {
L
lepdou 已提交
179
                                           if (!namespace.commited) {//取消编辑,则复原
180
                                               namespace.text = namespace.backupText;
L
lepdou 已提交
181 182 183
                                           }
                                       }
                                   };
L
lepdou 已提交
184

L
lepdou 已提交
185
                                   var releaseModal = $('#releaseModal');
186
                                   $scope.toReleaseNamespace = {};
L
lepdou 已提交
187

L
lepdou 已提交
188
                                   $scope.prepareReleaseNamespace = function (namespace) {
L
lepdou 已提交
189 190 191 192 193 194
                                       if (!namespace.hasReleasePermission){
                                           $('#releaseNoPermissionDialog').modal('show');
                                           return;
                                       }else {
                                           $('#releaseModal').modal('show');
                                       }
195 196
                                       $scope.releaseTitle = new Date().Format("yyyy-MM-dd hh:mm:ss");
                                       $scope.toReleaseNamespace = namespace;
L
lepdou 已提交
197
                                   };
198

L
lepdou 已提交
199
                                   $scope.releaseComment = '';
200

L
lepdou 已提交
201
                                   $scope.release = function () {
L
lepdou 已提交
202 203
                                       ConfigService.release($rootScope.pageContext.appId, $rootScope.pageContext.env,
                                                             $rootScope.pageContext.clusterName,
204
                                                             $scope.toReleaseNamespace.namespace.namespaceName,
L
lepdou 已提交
205
                                                             $scope.releaseTitle,
L
lepdou 已提交
206 207
                                                             $scope.releaseComment).then(
                                           function (result) {
L
lepdou 已提交
208
                                               releaseModal.modal('hide');
L
lepdou 已提交
209 210
                                               toastr.success("发布成功");
                                               //refresh all namespace items
L
lepdou 已提交
211
                                               $rootScope.refreshNamespaces();
L
lepdou 已提交
212

L
lepdou 已提交
213
                                           }, function (result) {
L
lepdou 已提交
214
                                               toastr.error(AppUtil.errorMsg(result), "发布失败");
L
lepdou 已提交
215

L
lepdou 已提交
216
                                           }
L
lepdou 已提交
217 218 219 220 221 222 223 224 225 226 227 228
                                       );
                                   };

                                   var TABLE_VIEW_OPER_TYPE = {
                                       RETRIEVE: 'retrieve',
                                       CREATE: 'create',
                                       UPDATE: 'update'
                                   };

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

                                   //查看配置
L
lepdou 已提交
229
                                   $scope.retrieveItem = function (namespace, item, oldValue) {
L
lepdou 已提交
230 231 232
                                       switchTableViewOperType(TABLE_VIEW_OPER_TYPE.RETRIEVE);
                                       $scope.item = item;
                                       $scope.item.oldValue = oldValue;
L
lepdou 已提交
233
                                       toOperationNamespaceName = namespace.namespace.namespaceName;
L
lepdou 已提交
234
                                       $scope.hasModifyPermission = namespace.hasModifyPermission;
L
lepdou 已提交
235 236
                                   };

L
lepdou 已提交
237 238 239
                                   var toDeleteItemId = 0, toDeleteNamespace = {};
                                   $scope.preDeleteItem = function (namespace, itemId) {
                                       toDeleteNamespace = namespace;
240 241 242 243
                                       toDeleteItemId = itemId;
                                   };

                                   $scope.deleteItem = function () {
L
lepdou 已提交
244 245 246 247 248
                                       ConfigService.delete_item($rootScope.pageContext.appId,
                                                                 $rootScope.pageContext.env,
                                                                 $rootScope.pageContext.clusterName,
                                                                 toDeleteNamespace.namespace.namespaceName,
                                                                 toDeleteItemId).then(
249 250 251 252 253 254
                                           function (result) {
                                               toastr.success("删除成功!");
                                               $rootScope.refreshNamespaces();
                                           }, function (result) {
                                               toastr.error(AppUtil.errorMsg(result), "删除失败");
                                           });
255 256
                                   };

L
lepdou 已提交
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
                                   var toOperationNamespaceName = '';
                                   //修改配置
                                   $scope.editItem = function (namespace, item) {
                                       switchTableViewOperType(TABLE_VIEW_OPER_TYPE.UPDATE);
                                       $scope.item = item;
                                       toOperationNamespaceName = namespace.namespace.namespaceName;
                                   };

                                   //新增配置
                                   $scope.createItem = function (namespace) {
                                       switchTableViewOperType(TABLE_VIEW_OPER_TYPE.CREATE);
                                       $scope.item = {};
                                       toOperationNamespaceName = namespace.namespace.namespaceName;
                                   };

                                   $scope.switchToEdit = function () {
                                       switchTableViewOperType(TABLE_VIEW_OPER_TYPE.UPDATE);
                                   };

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

                                   function switchTableViewOperType(type) {
                                       $scope.tableViewOperType = type;
                                   }

                                   var itemModal = $("#itemModal");
                                   $scope.doItem = function () {

                                       if (selectedClusters.length == 0) {
                                           toastr.error("请选择集群");
                                       } else {
                                           selectedClusters.forEach(function (cluster) {
                                               if ($scope.tableViewOperType == TABLE_VIEW_OPER_TYPE.CREATE) {

                                                   ConfigService.create_item($rootScope.pageContext.appId,
                                                                             cluster.env,
                                                                             cluster.name,
                                                                             toOperationNamespaceName,
                                                                             $scope.item).then(
                                                       function (result) {
L
lepdou 已提交
300 301
                                                           toastr.success(cluster.env + " , " + $scope.item.key,
                                                                          "添加成功");
L
lepdou 已提交
302 303 304
                                                           itemModal.modal('hide');
                                                           $rootScope.refreshNamespaces(namespace_view_type.TABLE);
                                                       }, function (result) {
L
lepdou 已提交
305
                                                           toastr.error(AppUtil.errorMsg(result), "添加失败");
L
lepdou 已提交
306 307 308
                                                       });

                                               } else if ($scope.tableViewOperType == TABLE_VIEW_OPER_TYPE.UPDATE) {
L
lepdou 已提交
309 310 311 312 313 314
                                                   if (!$scope.item.value){
                                                       $scope.item.value = "";
                                                   }
                                                   if (!$scope.item.comment){
                                                       $scope.item.comment = "";
                                                   }
L
lepdou 已提交
315 316 317 318 319 320
                                                   ConfigService.update_item($rootScope.pageContext.appId,
                                                                             cluster.env,
                                                                             cluster.name,
                                                                             toOperationNamespaceName,
                                                                             $scope.item).then(
                                                       function (result) {
L
lepdou 已提交
321
                                                           toastr.success("更新成功, 如需生效请发布");
L
lepdou 已提交
322 323 324
                                                           itemModal.modal('hide');
                                                           $rootScope.refreshNamespaces(namespace_view_type.TABLE);
                                                       }, function (result) {
L
lepdou 已提交
325
                                                           toastr.error(AppUtil.errorMsg(result), "更新失败");
L
lepdou 已提交
326 327 328 329 330
                                                       });
                                               }
                                           });
                                       }

L
lepdou 已提交
331
                                   };
L
lepdou 已提交
332 333 334 335 336 337 338 339
                                   
                                   //permission
                                   PermissionService.has_assign_user_permission($rootScope.pageContext.appId)
                                       .then(function (result) {
                                           $scope.hasAssignUserPermission = result.hasPermission;
                                       }, function (result) {
                                           
                                       });
L
lepdou 已提交
340 341

                                   $('.config-item-container').removeClass('hide');
L
lepdou 已提交
342 343
                               }]);