SyncConfigController.js 7.6 KB
Newer Older
L
lepdou 已提交
1
sync_item_module.controller("SyncItemController",
L
lepdou 已提交
2 3
                              ['$scope', '$location', '$window', 'toastr', 'AppService', 'AppUtil', 'ConfigService',
                               function ($scope, $location, $window, toastr, AppService, AppUtil, ConfigService) {
L
lepdou 已提交
4 5 6 7 8 9 10 11 12 13 14

                                   var params = AppUtil.parseParams($location.$$url);
                                   $scope.pageContext = {
                                       appId: params.appid,
                                       env: params.env,
                                       clusterName: params.clusterName,
                                       namespaceName: params.namespaceName
                                   };
                                   
                                   ////// load env //////
                                   AppService.load_nav_tree($scope.pageContext.appId).then(function (result) {
L
lepdou 已提交
15
                                       $scope.namespaceIdentifers = [];
L
lepdou 已提交
16 17 18 19 20
                                       result.nodes.forEach(function (node) {
                                           var env = node.env;
                                           node.clusters.forEach(function (cluster) {
                                               cluster.env = env;
                                               cluster.checked = false;
L
lepdou 已提交
21 22 23 24
                                               if (env != $scope.pageContext.env || cluster.name != $scope.pageContext.clusterName){
                                                   $scope.namespaceIdentifers.push(cluster);
                                               }
                                           })
L
lepdou 已提交
25 26 27 28 29 30 31 32
                                       });
                                   }, function (result) {
                                       toastr.error(AppUtil.errorMsg(result), "加载环境出错");    
                                   });

                                   var envAllSelected = false;
                                   $scope.toggleEnvsCheckedStatus = function () {
                                       envAllSelected = !envAllSelected;
L
lepdou 已提交
33 34
                                       $scope.namespaceIdentifers.forEach(function (namespaceIdentifer) {
                                           namespaceIdentifer.checked = envAllSelected;
L
lepdou 已提交
35 36 37 38 39 40 41
                                       })
                                   };
                                   
                                   ////// load items //////
                                   ConfigService.find_items($scope.pageContext.appId, $scope.pageContext.env,
                                                            $scope.pageContext.clusterName, $scope.pageContext.namespaceName).then(function (result) {

L
lepdou 已提交
42 43 44 45 46 47
                                       $scope.sourceItems = [];
                                       result.forEach(function (item) {
                                           if (item.key){
                                               item.checked = false;
                                               $scope.sourceItems.push(item);
                                           }
L
lepdou 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61
                                       })
                                       
                                   }, function (result) {
                                       toastr.error(AppUtil.errorMsg(result), "加载配置出错");
                                   });

                                   var itemAllSelected = false;
                                   $scope.toggleItemsCheckedStatus = function () {
                                       itemAllSelected = !itemAllSelected;
                                       $scope.sourceItems.forEach(function (item) {
                                           item.checked = itemAllSelected;
                                       })       
                                   };

L
lepdou 已提交
62
                                   $scope.diff = function () {
63
                                       $scope.hasDiff = false;
L
lepdou 已提交
64
                                       ConfigService.diff($scope.pageContext.namespaceName, parseSyncSourceData()).then(function (result) {
65

L
lepdou 已提交
66
                                           $scope.diffs = result;
67 68 69 70 71
                                           result.forEach(function (diff) {
                                               if (!$scope.hasDiff) {
                                                   $scope.hasDiff = diff.diffs.createItems.length + diff.diffs.updateItems.length > 0;
                                               }
                                           });
L
lepdou 已提交
72 73 74 75 76 77 78 79 80
                                           $scope.syncItemNextStep(1);
                                       }, function (result) {
                                           toastr.error(AppUtil.errorMsg(result));
                                       });
                                   };
                                   
                                   $scope.syncItems = function () {
                                    ConfigService.sync_items($scope.pageContext.namespaceName, parseSyncSourceData()).then(function (result) {
                                        $scope.syncItemStep += 1;
L
lepdou 已提交
81
                                        $scope.syncSuccess = true;
L
lepdou 已提交
82
                                    }, function (result) {
L
lepdou 已提交
83
                                        $scope.syncSuccess = false;
L
lepdou 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
                                        toastr.error(AppUtil.errorMsg(result));
                                    });    
                                   };
                                   
                                   function parseSyncSourceData() {
                                       var sourceData = {
                                           syncToNamespaces: [],
                                           syncItems: []
                                       };
                                       var namespaceName = $scope.pageContext.namespaceName;
                                       $scope.namespaceIdentifers.forEach(function (namespaceIdentifer) {
                                           if (namespaceIdentifer.checked){
                                               namespaceIdentifer.clusterName = namespaceIdentifer.name;
                                               namespaceIdentifer.namespaceName = namespaceName;
                                               sourceData.syncToNamespaces.push(namespaceIdentifer);
                                           }
                                       });
                                       $scope.sourceItems.forEach(function (item) {
                                           if (item.checked) {
                                               sourceData.syncItems.push(item);
                                           }
                                       });
                                       return sourceData;
                                   }

L
lepdou 已提交
109 110 111 112 113 114 115
                                   ////// flow control ///////

                                   $scope.syncItemStep = 1;
                                   $scope.syncItemNextStep = function (offset) {
                                       $scope.syncItemStep += offset;
                                   };

L
lepdou 已提交
116
                                   $scope.backToAppHomePage = function () {
L
lepdou 已提交
117
                                       $window.location.href = '/views/config.html?#appid=' + $scope.pageContext.appId;
L
lepdou 已提交
118 119
                                   };

L
lepdou 已提交
120 121
                                   $scope.switchSelect = function (o) {
                                       o.checked = !o.checked;
L
lepdou 已提交
122 123 124
                                   }
                               }]);