DiffConfigController.js 2.9 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  ['$scope', '$location', '$window', 'toastr', 'AppService', 'AppUtil', 'ConfigService',
      function ($scope, $location, $window, 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("请至少选择两个集群");
            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);
    }

    var selectedClusters = [];

    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);
71
            }
72
        });
73

74 75
        return syncData;
    }
76

77
    ////// 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');
    }
}]);