gray-release-rules-modal-directive.js 7.69 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
directive_module.directive('rulesmodal', rulesModalDirective);

function rulesModalDirective(toastr, AppUtil, EventManager, InstanceService) {
    return {
        restrict: 'E',
        templateUrl: '../../views/component/gray-release-rules-modal.html',
        transclude: true,
        replace: true,
        scope: {
            appId: '=',
            env: '=',
            cluster: '='
        },
        link: function (scope) {

            scope.completeEditBtnDisable = false;

            scope.batchAddIPs = batchAddIPs;
            scope.addRules = addRules;
            scope.removeRule = removeRule;
            scope.completeEditItem = completeEditItem;
            scope.cancelEditItem = cancelEditItem;
            scope.initSelectIps = initSelectIps;

            EventManager.subscribe(EventManager.EventType.EDIT_GRAY_RELEASE_RULES,
                                   function (context) {
                                       var branch = context.branch;
                                       scope.branch = branch;

                                       if (branch.editingRuleItem.clientIpList && branch.editingRuleItem.clientIpList[0] == '*'){
                                           branch.editingRuleItem.ApplyToAllInstances = true;
                                       }else {
                                           branch.editingRuleItem.ApplyToAllInstances = false;
                                       }


                                       $('.rules-ip-selector').select2({
                                                                           placeholder: "从实例列表中选择",
                                                                           allowClear: true
                                                                       });

                                       AppUtil.showModal('#rulesModal');
                                   });

            $('.rules-ip-selector').on('select2:select', function () {
                addRules(scope.branch);
            });

            function addRules(branch) {
                var newRules, selector = $('.rules-ip-selector');

                newRules = selector.select2('data');

                var parsedIPs = [];
                newRules.forEach(function (rule) {
                    parsedIPs.push(rule.text);
                });

                selector.select2("val", "");
                addRuleItemIP(branch, parsedIPs);
                scope.$apply();
            }

            function batchAddIPs(branch, newIPs) {
                if (!newIPs) {
                    return;
                }
                addRuleItemIP(branch, newIPs.split(','));
            }

            function addRuleItemIP(branch, newIps) {
                var oldIPs = branch.editingRuleItem.draftIpList;
                if (newIps && newIps.length > 0) {
                    newIps.forEach(function (IP) {
                        if (!AppUtil.checkIPV4(IP)) {
                            toastr.error("不合法的IP地址:" + IP);
                        } else if (oldIPs.indexOf(IP) < 0) {
                            oldIPs.push(IP);
                        }
                    })
                }
                //remove IP:all
                oldIPs.forEach(function (IP, index) {
                    if (IP == "*") {
                        oldIPs.splice(index, 1);
                    }
                });

            }

            function removeRule(ruleItem, IP) {

                ruleItem.draftIpList.forEach(function (existedRule, index) {
                    if (existedRule == IP) {
                        ruleItem.draftIpList.splice(index, 1);
                    }
                })

            }

            function completeEditItem(branch) {
                scope.completeEditBtnDisable = true;

                if (!branch.editingRuleItem.clientAppId) {
                    toastr.error("灰度的AppId不能为空");
                    scope.completeEditBtnDisable = false;
                    return;
                }

                if (branch.editingRuleItem.isNew && branch.rules && branch.rules.ruleItems) {
                    var errorRuleItem = false;
                    branch.rules.ruleItems.forEach(function (ruleItem) {
                        if (ruleItem.clientAppId == branch.editingRuleItem.clientAppId) {
                            toastr.error("已经存在AppId=" + branch.editingRuleItem.clientAppId + "的规则");
                            errorRuleItem = true;
                        }
                    });
                    if (errorRuleItem) {
                        scope.completeEditBtnDisable = false;
                        return;
                    }
                }

                if (!branch.editingRuleItem.ApplyToAllInstances) {
                    if (branch.editingRuleItem.draftIpList.length == 0) {
                        toastr.error("IP列表不能为空");
                        scope.completeEditBtnDisable = false;
                        return;
                    } else {
                        branch.editingRuleItem.clientIpList = branch.editingRuleItem.draftIpList;
                    }
                } else {
                    branch.editingRuleItem.clientIpList = ['*'];
                }

                if (!branch.rules) {
                    branch.rules = {
                        appId: scope.appId,
                        clusterName: scope.cluster,
                        namespaceName: branch.baseInfo.namespaceName,
                        branchName: branch.baseInfo.clusterName
                    };
                }

                if (!branch.rules.ruleItems) {
                    branch.rules.ruleItems = [];
                }

                if (branch.editingRuleItem.isNew) {
                    branch.rules.ruleItems.push(branch.editingRuleItem);
                }

                branch.editingRuleItem = undefined;
                scope.toAddIPs = '';

                AppUtil.hideModal('#rulesModal');

                EventManager.emit(EventManager.EventType.UPDATE_GRAY_RELEASE_RULES,
                                  {
                                      branch: branch
                                  }, branch.baseInfo.namespaceName);
                scope.completeEditBtnDisable = false;
            }

            function cancelEditItem(branch) {
                branch.editingRuleItem.isEdit = false;
                branch.editingRuleItem = undefined;
                scope.toAddIPs = '';
                AppUtil.hideModal('#rulesModal');
            }

            $('#rulesModal').on('shown.bs.modal', function (e) {
                initSelectIps();
            });

            function initSelectIps() {
                scope.selectIps = [];
                if (!scope.branch.parentNamespace.isPublic ||
                    scope.branch.parentNamespace.isLinkedNamespace) {

                    scope.branch.editingRuleItem.clientAppId = scope.branch.baseInfo.appId;
                }

                if (!scope.branch.editingRuleItem.clientAppId) {
                    return;
                }
                InstanceService.findInstancesByNamespace(scope.appId,
                                                         scope.env,
                                                         scope.cluster,
                                                         scope.branch.baseInfo.namespaceName,
                                                         scope.branch.editingRuleItem.clientAppId,
                                                         0,
                                                         2000)
                    .then(function (result) {
                        scope.selectIps = result.content;
                    });
            }

        }
    }
}