AppConfigController.js 12.6 KB
Newer Older
L
lepdou 已提交
1
application_module.controller("AppConfigController",
L
lepdou 已提交
2 3
                              ['$scope', '$location', 'toastr', 'AppService', 'ConfigService',
                               function ($scope, $location, toastr, AppService, ConfigService) {
L
lepdou 已提交
4

L
lepdou 已提交
5
                                   var appId = $location.$$url.split("=")[1];
6
                                   var currentUser = 'test_user';
L
lepdou 已提交
7 8
                                   var pageContext = {
                                       appId: appId,
9
                                       env: '',
L
lepdou 已提交
10 11 12
                                       clusterName: 'default'
                                   };

L
lepdou 已提交
13 14 15
                                   $scope.pageContext = pageContext;

                                   ///////////// load cluster nav tree /////////
L
lepdou 已提交
16

L
lepdou 已提交
17
                                   AppService.load_nav_tree($scope.pageContext.appId).then(function (result) {
L
lepdou 已提交
18 19 20 21 22 23 24
                                       var navTree = [];
                                       var nodes = result.nodes;
                                       nodes.forEach(function (item) {
                                           var node = {};
                                           //first nav
                                           node.text = item.env;
                                           var clusterNodes = [];
25 26 27 28 29 30 31 32 33
                                           //如果env下面只有一个default集群则不显示集群列表
                                           if (item.clusters && item.clusters.length == 1 && item.clusters[0].name == 'default'){
                                               node.selectable = true;
                                           }else {
                                               node.selectable = false;
                                               //second nav
                                               item.clusters.forEach(function (item) {
                                                   var clusterNode = {},
                                                       parentNode = [];
L
lepdou 已提交
34

35 36 37 38 39 40
                                                   clusterNode.text = item.name;
                                                   parentNode.push(node.text);
                                                   clusterNode.tags = parentNode;
                                                   clusterNodes.push(clusterNode);
                                               });
                                           }
L
lepdou 已提交
41 42 43 44 45 46 47
                                           node.nodes = clusterNodes;
                                           navTree.push(node);
                                       });
                                       $('#treeview').treeview({
                                                                   color: "#428bca",
                                                                   showBorder: true,
                                                                   data: navTree,
L
lepdou 已提交
48 49
                                                                   levels: 99,
                                                                   onNodeSelected: function (event, data) {
50 51 52 53 54 55 56
                                                                       if (!data.tags){//first nav node
                                                                           $scope.pageContext.env = data.text;
                                                                           $scope.pageContext.clusterName = 'default';
                                                                       }else {//second cluster node
                                                                           $scope.pageContext.env = data.tags[0];
                                                                           $scope.pageContext.clusterName = data.text;
                                                                       }
L
lepdou 已提交
57 58
                                                                       refreshNamespaces();
                                                                   }
L
lepdou 已提交
59 60
                                                               });
                                   }, function (result) {
61
                                       toastr.error(result.status + result.data.message, "加载导航出错");
L
lepdou 已提交
62 63
                                   });

L
lepdou 已提交
64 65 66 67 68 69 70 71 72 73 74
                                   /////////// namespace ////////////

                                   var namespace_view_type = {
                                       TEXT:'text',
                                       TABLE: 'table',
                                       LOG: 'log'
                                   };

                                   refreshNamespaces();

                                   function refreshNamespaces(viewType) {
75 76 77
                                       if ($scope.pageContext.env == ''){
                                           return;
                                       }
L
lepdou 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
                                       ConfigService.load_all_namespaces($scope.pageContext.appId, $scope.pageContext.env,
                                                                         $scope.pageContext.clusterName, viewType).then(
                                           function (result) {
                                               $scope.namespaces = result;

                                               //初始化视图
                                               if ($scope.namespaces) {
                                                   $scope.namespaces.forEach(function (item) {
                                                       item.isModify = false;
                                                       if (!viewType){//default text view
                                                           $scope.switchView(item, namespace_view_type.TEXT);
                                                       }else if (viewType == namespace_view_type.TABLE){
                                                           item.viewType = namespace_view_type.TABLE;
                                                       }


                                                       item.isTextEditing = false;
                                                   })
                                               }

                                           }, function (result) {
99
                                               toastr.error(result.status + result.data.message, "加载配置信息出错");
L
lepdou 已提交
100 101 102 103 104 105
                                           });
                                   }

                                   ////////////global view oper /////////////

                                   $scope.switchView = function (namespace, viewType) {
L
lepdou 已提交
106

L
lepdou 已提交
107 108 109
                                       if (namespace_view_type.TEXT == viewType) {
                                           namespace.text = parseModel2Text(namespace);
                                       } else if (namespace_view_type.TABLE == viewType) {
L
lepdou 已提交
110

L
lepdou 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
                                       }
                                       namespace.viewType = viewType;
                                   };
                                   
                                   //把表格内容解析成文本
                                   function parseModel2Text(namespace) {
                                       if (!namespace.items) {
                                           return "无配置信息";
                                       }
                                       var result = "";
                                       namespace.items.forEach(function (item) {
                                           if (item.item.key) {
                                               result +=
                                                   item.item.key + " = " + item.item.value + "\n";
                                           } else {
                                               result += item.item.comment + "\n";
L
lepdou 已提交
127 128
                                           }

L
lepdou 已提交
129
                                       });
130

L
lepdou 已提交
131 132 133 134 135
                                       return result;
                                   }

                                   ////////// text view oper /////////

L
lepdou 已提交
136 137 138 139 140 141
                                   $scope.draft = {};
                                   //保存草稿
                                   $scope.saveDraft = function (namespace) {
                                       $scope.draft = namespace;
                                   };

142
                                   $scope.commitComment = '';
L
lepdou 已提交
143
                                   //更新配置
L
lepdou 已提交
144
                                   $scope.commitChange = function () {
L
lepdou 已提交
145 146
                                       ConfigService.modify_items($scope.pageContext.appId, $scope.pageContext.env, $scope.pageContext.clusterName,
                                                                  $scope.draft.namespace.namespaceName, $scope.draft.text,
147
                                                                  $scope.draft.namespace.id, $scope.commitComment, currentUser).then(
L
lepdou 已提交
148
                                           function (result) {
L
lepdou 已提交
149
                                               toastr.success("更新成功");
L
lepdou 已提交
150 151 152
                                               //refresh all namespace items
                                               refreshNamespaces();

L
lepdou 已提交
153 154
                                               $scope.draft.backupText = '';//清空备份文本
                                               $scope.toggleTextEditStatus($scope.draft);
L
lepdou 已提交
155

L
lepdou 已提交
156
                                           }, function (result) {
157
                                               toastr.error(result.status + result.data.message, "更新失败");
L
lepdou 已提交
158 159 160 161 162

                                           }
                                       );
                                   };

L
lepdou 已提交
163 164 165 166 167 168 169 170 171 172 173
                                   //文本编辑框状态切换
                                   $scope.toggleTextEditStatus = function (namespace) {
                                       namespace.isTextEditing = !namespace.isTextEditing;
                                       if (namespace.isTextEditing){//切换为编辑状态,保存一下原来值
                                           $scope.draft.backupText = namespace.text;
                                       }else {
                                           if ($scope.draft.backupText){//取消编辑,则复原
                                               namespace.text = $scope.draft.backupText;
                                           }
                                       }
                                   };
L
lepdou 已提交
174 175 176 177

                                   ////////// table view oper /////////

                                   //查看旧值
L
lepdou 已提交
178 179 180 181 182 183 184 185
                                   $scope.queryOldValue = function (key, oldValue) {
                                       $scope.queryKey = key;
                                       if (oldValue == '') {
                                           $scope.OldValue = key + "是新添加的key";
                                       } else {
                                           $scope.OldValue = oldValue;
                                       }
                                   };
L
lepdou 已提交
186 187 188 189 190 191
                                   
                                   /////// release ///////
                                   var releaseNamespace = {};
                                   
                                   $scope.prepareReleaseNamespace = function (namespace) {
                                       releaseNamespace = namespace;        
L
lepdou 已提交
192
                                   };
L
lepdou 已提交
193 194 195 196 197 198 199 200 201 202
                                   $scope.releaseComment = '';
                                   $scope.release = function () {
                                       ConfigService.release($scope.pageContext.appId, $scope.pageContext.env, 
                                                             $scope.pageContext.clusterName,
                                                             releaseNamespace.namespace.namespaceName, currentUser,
                                                             $scope.releaseComment).then(
                                           function (result) {
                                               toastr.success("发布成功");
                                               //refresh all namespace items
                                               refreshNamespaces();
L
lepdou 已提交
203

L
lepdou 已提交
204
                                           }, function (result) {
205
                                               toastr.error(result.status + result.data.message,  "发布失败");
L
lepdou 已提交
206

L
lepdou 已提交
207 208
                                           }
                                       );    
L
lepdou 已提交
209 210
                                   }

L
lepdou 已提交
211 212
                               }]);