item-modal-directive.js 5.8 KB
Newer Older
lepdou committed
1 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
directive_module.directive('itemmodal', itemModalDirective);

function itemModalDirective(toastr, AppUtil, EventManager, ConfigService) {
    return {
        restrict: 'E',
        templateUrl: '../../views/component/item-modal.html',
        transclude: true,
        replace: true,
        scope: {
            appId: '=',
            env: '=',
            cluster: '=',
            toOperationNamespace: '=',
            item: '='
        },
        link: function (scope) {


            var TABLE_VIEW_OPER_TYPE = {
                CREATE: 'create',
                UPDATE: 'update'
            };

            scope.doItem = doItem;
            scope.collectSelectedClusters = collectSelectedClusters;

            function doItem() {

                if (!scope.item.value) {
                    scope.item.value = "";
                }

                if (scope.item.tableViewOperType == TABLE_VIEW_OPER_TYPE.CREATE) {

                    //check key unique
                    var hasRepeatKey = false;
                    scope.toOperationNamespace.items.forEach(function (item) {
                        if (!item.isDeleted && scope.item.key == item.item.key) {
                            toastr.error("key=" + scope.item.key + " 已存在");
                            hasRepeatKey = true;
                        }
                    });
                    if (hasRepeatKey) {
                        return;
                    }

                    scope.item.addItemBtnDisabled = true;

                    if (scope.toOperationNamespace.isBranch) {
                        ConfigService.create_item(scope.appId,
                                                  scope.env,
                                                  scope.toOperationNamespace.baseInfo.clusterName,
                                                  scope.toOperationNamespace.baseInfo.namespaceName,
                                                  scope.item).then(
                            function (result) {
56 57 58
                                toastr.success("添加成功,如需生效请发布");
                                scope.item.addItemBtnDisabled = false;
                                AppUtil.hideModal('#itemModal');
lepdou committed
59 60 61 62
                                EventManager.emit(EventManager.EventType.REFRESH_NAMESPACE,
                                                  {
                                                      namespace: scope.toOperationNamespace
                                                  });
63 64


lepdou committed
65 66
                            }, function (result) {
                                toastr.error(AppUtil.errorMsg(result), "添加失败");
67
                                scope.item.addItemBtnDisabled = false;
lepdou committed
68 69
                            });
                    } else {
70 71 72 73 74
                        if (selectedClusters.length == 0) {
                            toastr.error("请选择集群");
                            return;
                        }

lepdou committed
75 76 77 78 79 80 81
                        selectedClusters.forEach(function (cluster) {
                            ConfigService.create_item(scope.appId,
                                                      cluster.env,
                                                      cluster.name,
                                                      scope.toOperationNamespace.baseInfo.namespaceName,
                                                      scope.item).then(
                                function (result) {
82 83
                                    scope.item.addItemBtnDisabled = false;
                                    AppUtil.hideModal('#itemModal');
lepdou committed
84 85 86 87 88 89 90 91 92 93 94
                                    toastr.success(cluster.env + " , " + scope.item.key, "添加成功,如需生效请发布");
                                    if (cluster.env == scope.env &&
                                        cluster.name == scope.cluster) {

                                        EventManager.emit(EventManager.EventType.REFRESH_NAMESPACE,
                                                          {
                                                              namespace: scope.toOperationNamespace
                                                          });
                                    }
                                }, function (result) {
                                    toastr.error(AppUtil.errorMsg(result), "添加失败");
95
                                    scope.item.addItemBtnDisabled = false;
lepdou committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
                                });
                        });
                    }


                } else {

                    if (!scope.item.comment) {
                        scope.item.comment = "";
                    }

                    ConfigService.update_item(scope.appId,
                                              scope.env,
                                              scope.toOperationNamespace.baseInfo.clusterName,
                                              scope.toOperationNamespace.baseInfo.namespaceName,
                                              scope.item).then(
                        function (result) {
                            EventManager.emit(EventManager.EventType.REFRESH_NAMESPACE,
                                              {
                                                  namespace: scope.toOperationNamespace
                                              });
                            
                            AppUtil.hideModal('#itemModal');

                            toastr.success("更新成功, 如需生效请发布");
                        }, function (result) {
                            toastr.error(AppUtil.errorMsg(result), "更新失败");
                        });
                }

            }

            var selectedClusters = [];
            function collectSelectedClusters(data) {
                selectedClusters = data;
            }
        }
    }
}