ConfigBaseInfoController.js 7.16 KB
Newer Older
lepdou committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
application_module.controller("ConfigBaseInfoController",
                              ['$rootScope', '$scope', '$location', 'toastr', 'AppService', 'AppUtil',
                               function ($rootScope, $scope, $location, toastr, AppService, AppUtil) {


                                   var appId = AppUtil.parseParams($location.$$url).appid;
                                   var pageContext = {
                                       appId: appId,
                                       env: '',
                                       clusterName: 'default'
                                   };

                                   $rootScope.pageContext = pageContext;

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

                                   AppService.load_nav_tree($rootScope.pageContext.appId).then(function (result) {
                                       var navTree = [];
lepdou committed
19 20
                                       var nodes = AppUtil.collectData(result);

lepdou committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
                                       nodes.forEach(function (item) {
                                           var node = {};
                                           //first nav
                                           node.text = item.env;
                                           var clusterNodes = [];
                                           //如果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 = [];

                                                   clusterNode.text = item.name;
                                                   parentNode.push(node.text);
                                                   clusterNode.tags = parentNode;
                                                   clusterNodes.push(clusterNode);
                                               });
                                           }
                                           node.nodes = clusterNodes;
                                           navTree.push(node);
                                       });
lepdou committed
45

lepdou committed
46
                                       $('#treeview').treeview({
lepdou committed
47
                                                                   color: "#797979",
lepdou committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61
                                                                   showBorder: true,
                                                                   data: navTree,
                                                                   levels: 99,
                                                                   onNodeSelected: function (event, data) {
                                                                       if (!data.tags){//first nav node
                                                                           $rootScope.pageContext.env = data.text;
                                                                           $rootScope.pageContext.clusterName = 'default';
                                                                       }else {//second cluster node
                                                                           $rootScope.pageContext.env = data.tags[0];
                                                                           $rootScope.pageContext.clusterName = data.text;
                                                                       }
                                                                       $rootScope.refreshNamespaces();
                                                                   }
                                                               });
lepdou committed
62

lepdou committed
63 64 65 66 67
                                   }, function (result) {
                                       toastr.error(AppUtil.errorMsg(result), "加载导航出错");
                                   });

                                   ////// app info //////
lepdou committed
68
                                   
lepdou committed
69
                                   AppService.load($rootScope.pageContext.appId).then(function (result) {
lepdou committed
70
                                       $scope.appBaseInfo = result;
lepdou committed
71 72 73 74 75 76
                                   },function (result) {
                                       toastr.error(AppUtil.errorMsg(result), "加载App信息出错");
                                   });


                                   ////// 补缺失的环境 //////
lepdou committed
77 78 79 80 81 82 83
                                   $scope.missEnvs = [];
                                   AppService.find_miss_envs($rootScope.pageContext.appId).then(function (result) {
                                       $scope.missEnvs = AppUtil.collectData(result);
                                   },function (result) {
                                       console.log(AppUtil.errorMsg(result));
                                   });

lepdou committed
84
                                   $scope.selectedEnvs = [];
lepdou committed
85 86 87 88 89 90 91 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 117 118 119
                                   $scope.toggleSelection = function toggleSelection(env) {
                                       var idx = $scope.selectedEnvs.indexOf(env);

                                       // is currently selected
                                       if (idx > -1) {
                                           $scope.selectedEnvs.splice(idx, 1);
                                       }

                                       // is newly selected
                                       else {
                                           $scope.selectedEnvs.push(env);
                                       }
                                   };

                                   $scope.createEnvs = function () {
                                       var count = 0;
                                       $scope.selectedEnvs.forEach(function (env) {
                                           AppService.create(env, $scope.appBaseInfo).then(function (result) {
                                               toastr.success(env, '创建成功');
                                               count ++;
                                               if (count == $scope.selectedEnvs.length){
                                                   location.reload(true);
                                               }
                                           }, function (result) {
                                               toastr.error(AppUtil.errorMsg(result), '创建失败:' + env);
                                               count ++;
                                               if (count == $scope.selectedEnvs){
                                                   $route.reload();
                                               }
                                           });
                                       });
                                   };

                               }]);