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

L
lepdou 已提交
9
                                   var namespace_view_type = {
L
lepdou 已提交
10
                                       TEXT: 'text',
L
lepdou 已提交
11 12 13 14
                                       TABLE: 'table',
                                       LOG: 'log'
                                   };

L
lepdou 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
                                   var TABLE_VIEW_OPER_TYPE = {
                                       RETRIEVE: 'retrieve',
                                       CREATE: 'create',
                                       UPDATE: 'update'
                                   };


                                   $rootScope.refreshNamespaces = refreshNamespaces;
                                   
                                   $scope.commitChange = commitChange;
                                   
                                   $scope.prepareReleaseNamespace = prepareReleaseNamespace;
                                   
                                   $scope.release = release;
                                   
                                   $scope.retrieveItem = retrieveItem;
                                   
                                   $scope.preDeleteItem = preDeleteItem;
                                   
                                   $scope.deleteItem = deleteItem;
                                   
                                   $scope.editItem = editItem;
                                   
                                   $scope.createItem = createItem;
                                   
                                   $scope.doItem = doItem;
                                   

                                   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) {

                                       });
                                   
                                   UserService.load_user().then(function (result) {
                                       $scope.currentUser = result.userId;
                                   });

                                   function refreshNamespaces(viewType) {
L
lepdou 已提交
59
                                       if ($rootScope.pageContext.env == '') {
60 61
                                           return;
                                       }
L
lepdou 已提交
62 63 64 65
                                       ConfigService.load_all_namespaces($rootScope.pageContext.appId,
                                                                         $rootScope.pageContext.env,
                                                                         $rootScope.pageContext.clusterName,
                                                                         viewType).then(
L
lepdou 已提交
66 67 68
                                           function (result) {
                                               $scope.namespaces = result;

L
lepdou 已提交
69 70
                                               setInterval(function () {
                                                   $('[data-tooltip="tooltip"]').tooltip();
71 72 73 74 75 76 77 78 79 80
                                                   $('.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 已提交
81
                                               }, 2500);
L
lepdou 已提交
82 83

                                           }, function (result) {
L
lepdou 已提交
84
                                               toastr.error(AppUtil.errorMsg(result), "加载配置信息出错");
L
lepdou 已提交
85
                                           });
L
lepdou 已提交
86
                                   };
L
lepdou 已提交
87

L
lepdou 已提交
88 89 90 91 92 93 94
                                   function commitChange(namespace) {
                                       var model = {
                                           configText: namespace.editText,
                                           namespaceId: namespace.namespace.id,
                                           format: namespace.format
                                       };
                                       ConfigService.modify_items($rootScope.pageContext.appId, $rootScope.pageContext.env,
L
lepdou 已提交
95 96
                                                                  $rootScope.pageContext.clusterName,
                                                                  namespace.namespace.namespaceName,
L
lepdou 已提交
97
                                                                  model).then(
L
lepdou 已提交
98
                                           function (result) {
L
lepdou 已提交
99
                                               toastr.success("更新成功, 如需生效请发布");
L
lepdou 已提交
100
                                               //refresh all namespace items
L
lepdou 已提交
101
                                               $rootScope.refreshNamespaces();
L
lepdou 已提交
102

L
lepdou 已提交
103
                                           }, function (result) {
L
lepdou 已提交
104
                                               toastr.error(AppUtil.errorMsg(result), "更新失败");
L
lepdou 已提交
105 106
                                           }
                                       );
L
lepdou 已提交
107
                                   }
L
lepdou 已提交
108
                                   var releaseModal = $('#releaseModal');
109
                                   $scope.toReleaseNamespace = {};
L
lepdou 已提交
110
                                   function prepareReleaseNamespace(namespace) {
111
                                       if (!namespace.hasReleasePermission) {
L
lepdou 已提交
112 113
                                           $('#releaseNoPermissionDialog').modal('show');
                                           return;
114 115 116 117
                                       } else if (namespace.lockOwner && $scope.currentUser == namespace.lockOwner){
                                           //自己修改不能自己发布
                                           $('#releaseDenyDialog').modal('show');
                                       } else {
L
lepdou 已提交
118 119
                                           $('#releaseModal').modal('show');
                                       }
120 121
                                       $scope.releaseTitle = new Date().Format("yyyy-MM-dd hh:mm:ss");
                                       $scope.toReleaseNamespace = namespace;
L
lepdou 已提交
122
                                   }
123

L
lepdou 已提交
124
                                   $scope.releaseComment = '';
L
lepdou 已提交
125
                                   function release() {
L
lepdou 已提交
126 127
                                       ConfigService.release($rootScope.pageContext.appId, $rootScope.pageContext.env,
                                                             $rootScope.pageContext.clusterName,
128
                                                             $scope.toReleaseNamespace.namespace.namespaceName,
L
lepdou 已提交
129
                                                             $scope.releaseTitle,
L
lepdou 已提交
130 131
                                                             $scope.releaseComment).then(
                                           function (result) {
L
lepdou 已提交
132
                                               releaseModal.modal('hide');
L
lepdou 已提交
133 134
                                               toastr.success("发布成功");
                                               //refresh all namespace items
L
lepdou 已提交
135
                                               $rootScope.refreshNamespaces();
L
lepdou 已提交
136

L
lepdou 已提交
137
                                           }, function (result) {
L
lepdou 已提交
138
                                               toastr.error(AppUtil.errorMsg(result), "发布失败");
L
lepdou 已提交
139

L
lepdou 已提交
140
                                           }
L
lepdou 已提交
141
                                       );
L
lepdou 已提交
142 143
                                   }

L
lepdou 已提交
144 145 146


                                   $scope.tableViewOperType = '', $scope.item = {};
L
bugfix  
lepdou 已提交
147
                                   var toOperationNamespace;
L
lepdou 已提交
148 149

                                   //查看配置
L
lepdou 已提交
150
                                   function retrieveItem(namespace, item, oldValue) {
L
lepdou 已提交
151 152 153
                                       switchTableViewOperType(TABLE_VIEW_OPER_TYPE.RETRIEVE);
                                       $scope.item = item;
                                       $scope.item.oldValue = oldValue;
L
bugfix  
lepdou 已提交
154
                                       toOperationNamespace = namespace;
L
lepdou 已提交
155
                                       $scope.hasModifyPermission = namespace.hasModifyPermission;
L
lepdou 已提交
156
                                   }
L
lepdou 已提交
157

L
bugfix  
lepdou 已提交
158
                                   var toDeleteItemId = 0;
L
lepdou 已提交
159
                                   function preDeleteItem(namespace, itemId) {
160 161 162 163
                                       if (!lockCheck(namespace)){
                                           return;
                                       }

L
bugfix  
lepdou 已提交
164
                                       toOperationNamespace = namespace;
165
                                       toDeleteItemId = itemId;
166 167

                                       $("#deleteConfirmDialog").modal("show");
L
lepdou 已提交
168
                                   }
169

L
lepdou 已提交
170
                                   function deleteItem() {
L
lepdou 已提交
171 172 173
                                       ConfigService.delete_item($rootScope.pageContext.appId,
                                                                 $rootScope.pageContext.env,
                                                                 $rootScope.pageContext.clusterName,
L
bugfix  
lepdou 已提交
174
                                                                 toOperationNamespace.namespace.namespaceName,
L
lepdou 已提交
175
                                                                 toDeleteItemId).then(
176 177 178 179 180 181
                                           function (result) {
                                               toastr.success("删除成功!");
                                               $rootScope.refreshNamespaces();
                                           }, function (result) {
                                               toastr.error(AppUtil.errorMsg(result), "删除失败");
                                           });
L
lepdou 已提交
182
                                   }
183

L
lepdou 已提交
184
                                   //修改配置
L
lepdou 已提交
185
                                   function editItem(namespace, item) {
186 187 188
                                       if (!lockCheck(namespace)){
                                           return;
                                       }
L
lepdou 已提交
189 190
                                       switchTableViewOperType(TABLE_VIEW_OPER_TYPE.UPDATE);
                                       $scope.item = item;
L
bugfix  
lepdou 已提交
191
                                       toOperationNamespace = namespace;
192 193

                                       $("#itemModal").modal("show");
L
lepdou 已提交
194
                                   }
L
lepdou 已提交
195 196

                                   //新增配置
L
lepdou 已提交
197
                                   function createItem(namespace) {
198 199 200 201
                                       if (!lockCheck(namespace)){
                                           return;
                                       }

L
lepdou 已提交
202 203
                                       switchTableViewOperType(TABLE_VIEW_OPER_TYPE.CREATE);
                                       $scope.item = {};
L
bugfix  
lepdou 已提交
204
                                       toOperationNamespace = namespace;
205
                                       $('#itemModal').modal('show');
L
lepdou 已提交
206
                                   }
L
lepdou 已提交
207 208 209 210 211 212 213 214 215 216 217

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

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

                                   var itemModal = $("#itemModal");
L
lepdou 已提交
218
                                   function doItem() {
L
lepdou 已提交
219 220 221 222

                                       if (selectedClusters.length == 0) {
                                           toastr.error("请选择集群");
                                       } else {
L
lepdou 已提交
223 224 225
                                           if (!$scope.item.value) {
                                               $scope.item.value = "";
                                           }
L
lepdou 已提交
226 227
                                           selectedClusters.forEach(function (cluster) {
                                               if ($scope.tableViewOperType == TABLE_VIEW_OPER_TYPE.CREATE) {
L
bugfix  
lepdou 已提交
228 229 230 231 232 233 234 235 236 237 238 239
                                                   //check key unique
                                                   var hasRepeatKey = false;
                                                   toOperationNamespace.items.forEach(function (item) {
                                                      if ($scope.item.key == item.item.key){
                                                          toastr.error("key=" + $scope.item.key + " 已存在");
                                                          hasRepeatKey = true;
                                                          return;
                                                          }
                                                   });
                                                   if (hasRepeatKey){
                                                       return;
                                                   }
L
lepdou 已提交
240 241 242 243

                                                   ConfigService.create_item($rootScope.pageContext.appId,
                                                                             cluster.env,
                                                                             cluster.name,
L
bugfix  
lepdou 已提交
244
                                                                             toOperationNamespace.namespace.namespaceName,
L
lepdou 已提交
245 246
                                                                             $scope.item).then(
                                                       function (result) {
L
lepdou 已提交
247 248
                                                           toastr.success(cluster.env + " , " + $scope.item.key,
                                                                          "添加成功");
L
lepdou 已提交
249 250 251
                                                           itemModal.modal('hide');
                                                           $rootScope.refreshNamespaces(namespace_view_type.TABLE);
                                                       }, function (result) {
L
lepdou 已提交
252
                                                           toastr.error(AppUtil.errorMsg(result), "添加失败");
L
lepdou 已提交
253 254 255
                                                       });

                                               } else if ($scope.tableViewOperType == TABLE_VIEW_OPER_TYPE.UPDATE) {
L
lepdou 已提交
256

257
                                                   if (!$scope.item.comment) {
L
lepdou 已提交
258 259
                                                       $scope.item.comment = "";
                                                   }
L
lepdou 已提交
260 261 262
                                                   ConfigService.update_item($rootScope.pageContext.appId,
                                                                             cluster.env,
                                                                             cluster.name,
L
bugfix  
lepdou 已提交
263
                                                                             toOperationNamespace.namespace.namespaceName,
L
lepdou 已提交
264 265
                                                                             $scope.item).then(
                                                       function (result) {
L
lepdou 已提交
266
                                                           toastr.success("更新成功, 如需生效请发布");
L
lepdou 已提交
267 268 269
                                                           itemModal.modal('hide');
                                                           $rootScope.refreshNamespaces(namespace_view_type.TABLE);
                                                       }, function (result) {
L
lepdou 已提交
270
                                                           toastr.error(AppUtil.errorMsg(result), "更新失败");
L
lepdou 已提交
271 272 273 274 275
                                                       });
                                               }
                                           });
                                       }

L
lepdou 已提交
276
                                   }
L
lepdou 已提交
277

L
lepdou 已提交
278 279 280 281
                                   function lockCheck(namespace) {
                                       if (namespace.lockOwner && scope.currentUser != namespace.lockOwner) {
                                           scope.lockOwner = namespace.lockOwner;
                                           $('#namespaceLockedDialog').modal('show');
282 283
                                           return false;
                                       }
L
lepdou 已提交
284 285
                                       return true;
                                   }
286

L
lepdou 已提交
287
                                   $('.config-item-container').removeClass('hide');
L
lepdou 已提交
288 289
                               }]);