NamespaceController.js 5.2 KB
Newer Older
L
lepdou 已提交
1
namespace_module.controller("LinkNamespaceController",
L
lepdou 已提交
2 3 4 5 6
                              ['$scope', '$location', '$window', 'toastr', 'AppService', 'AppUtil', 'NamespaceService',
                               function ($scope, $location, $window, toastr, AppService, AppUtil, NamespaceService) {

                                   var params = AppUtil.parseParams($location.$$url);
                                   $scope.appId = params.appid;
L
lepdou 已提交
7 8 9
                                   $scope.type = params.type;

                                   $scope.step = 1;
L
lepdou 已提交
10 11 12 13 14 15 16 17

                                   NamespaceService.find_public_namespaces().then(function (result) {
                                       var publicNamespaces = [];
                                       result.forEach(function (item) {
                                            var namespace = {};
                                           namespace.id = item.name;
                                           namespace.text = item.name;
                                           publicNamespaces.push(namespace);
18
                                       });
L
lepdou 已提交
19 20 21 22 23 24 25
                                       $('#namespaces').select2({
                                                                    width: '250px',
                                                                    data: publicNamespaces
                                                                });
                                   }, function (result) {
                                       toastr.error(AppUtil.errorMsg(result), "load public namespace error");
                                   });
L
lepdou 已提交
26 27 28 29 30 31

                                   $scope.appNamespace = {
                                       appId:$scope.appId,
                                       name:'',
                                       comment:''
                                   };
L
lepdou 已提交
32 33 34 35 36

                                   var selectedClusters = [];
                                   $scope.collectSelectedClusters = function (data) {
                                        selectedClusters = data;
                                   };
L
lepdou 已提交
37 38 39 40 41 42 43 44 45 46 47
                                   $scope.createNamespace = function () {
                                       if ($scope.type == 'link'){
                                           if (selectedClusters.length == 0){
                                               toastr.warning("请选择集群");
                                               return;
                                           }

                                           if ($scope.namespaceType == 1){
                                               $scope.namespaceName = $('#namespaces').select2('data')[0].id;
                                           }

48
                                           var namespaceCreationModels = [];
L
lepdou 已提交
49
                                           selectedClusters.forEach(function (cluster) {
50 51 52 53 54 55 56 57 58 59 60 61
                                               namespaceCreationModels.push({
                                                                                env: cluster.env,
                                                                                namespace: {
                                                                                    appId: $scope.appId,
                                                                                    clusterName: cluster.clusterName,
                                                                                    namespaceName: $scope.namespaceName
                                                                                }
                                                                            });
                                           });
                                           NamespaceService.createNamespace($scope.appId, namespaceCreationModels)
                                               .then(function (result) {
                                                   toastr.success("创建成功");
L
step 2  
lepdou 已提交
62
                                                   $scope.step = 2;
L
lepdou 已提交
63
                                               }, function (result) {
64
                                                   toastr.error(AppUtil.errorMsg(result));
L
lepdou 已提交
65 66 67 68
                                               });
                                       }else {
                                           NamespaceService.createAppNamespace($scope.appId, $scope.appNamespace).then(function (result) {
                                               $scope.step = 2;
L
lepdou 已提交
69
                                           }, function (result) {
L
lepdou 已提交
70
                                               toastr.error(AppUtil.errorMsg(result), "创建失败");
L
lepdou 已提交
71
                                           });
L
lepdou 已提交
72 73
                                       }

L
lepdou 已提交
74 75 76 77 78 79 80
                                   };

                                   $scope.namespaceType = 1;
                                   $scope.selectNamespaceType = function (type) {
                                       $scope.namespaceType = type;
                                   };

L
lepdou 已提交
81
                                   $scope.back = function () {
82
                                       $window.location.href = '/config.html?#appid=' + $scope.appId;
L
lepdou 已提交
83
                                   };
L
lepdou 已提交
84 85
                               }]);