DiffConfigController.js 3.5 KB
Newer Older
1
diff_item_module.controller("DiffItemController",
2 3 4 5 6 7 8 9 10 11 12 13 14 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
    ['$scope', '$location', '$window', '$translate', 'toastr', 'AppService', 'AppUtil', 'ConfigService',
        function ($scope, $location, $window, $translate, toastr, AppService, AppUtil, ConfigService) {

            var params = AppUtil.parseParams($location.$$url);
            $scope.pageContext = {
                appId: params.appid,
                env: params.env,
                clusterName: params.clusterName,
                namespaceName: params.namespaceName
            };
            var sourceItems = [];

            $scope.diff = diff;
            $scope.syncBtnDisabled = false;
            $scope.showCommentDiff = false;

            $scope.collectSelectedClusters = collectSelectedClusters;

            $scope.syncItemNextStep = syncItemNextStep;
            $scope.backToAppHomePage = backToAppHomePage;
            $scope.switchSelect = switchSelect;

            $scope.showText = showText;

            $scope.itemsKeyedByKey = {};

            $scope.syncData = {
                syncToNamespaces: [],
                syncItems: []
            };

            function diff() {
                $scope.syncData = parseSyncSourceData();
                if ($scope.syncData.syncToNamespaces.length < 2) {
                    toastr.warning($translate.instant('Config.Diff.PleaseChooseTwoCluster'));
                    return;
                }
                $scope.syncData.syncToNamespaces.forEach(function (namespace) {
                    ConfigService.find_items(namespace.appId,
                        namespace.env,
                        namespace.clusterName,
                        namespace.namespaceName).then(function (result) {
                            result.forEach(function (item) {
                                var itemsKeyedByClusterName = $scope.itemsKeyedByKey[item.key] || {};
                                itemsKeyedByClusterName[namespace.env + ':' + namespace.clusterName + ':' + namespace.namespaceName] = item;
                                $scope.itemsKeyedByKey[item.key] = itemsKeyedByClusterName;
                            });
                        });
                });
                $scope.syncItemNextStep(1);
52 53
            }

54
            var selectedClusters = [];
55

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
            function collectSelectedClusters(data) {
                selectedClusters = data;
            }

            function parseSyncSourceData() {
                var syncData = {
                    syncToNamespaces: [],
                    syncItems: []
                };
                var namespaceName = $scope.pageContext.namespaceName;
                selectedClusters.forEach(function (cluster) {
                    if (cluster.checked) {
                        cluster.clusterName = cluster.name;
                        cluster.namespaceName = namespaceName;
                        syncData.syncToNamespaces.push(cluster);
                    }
                });

                return syncData;
            }

            ////// flow control ///////
78

79 80 81 82
            $scope.syncItemStep = 1;
            function syncItemNextStep(offset) {
                $scope.syncItemStep += offset;
            }
83

84 85 86
            function backToAppHomePage() {
                $window.location.href = '/config.html?#appid=' + $scope.pageContext.appId;
            }
87

88 89 90
            function switchSelect(o) {
                o.checked = !o.checked;
            }
91

92 93 94 95 96
            function showText(text) {
                $scope.text = text;
                AppUtil.showModal('#showTextModal');
            }
        }]);