/*! ng-w5c-validator v2.5.0 2017-06-05 */
!function () {
    var a = angular.module("w5c.validator", ["ng"]).provider("w5cValidator", [function () {
        var a = {
                required: "该选项不能为空",
                maxlength: "该选项输入值长度不能大于{maxlength}",
                minlength: "该选项输入值长度不能小于{minlength}",
                email: "输入邮件的格式不正确",
                repeat: "两次输入不一致",
                pattern: "该选项输入格式不正确",
                number: "必须输入数字",
                w5cuniquecheck: "该输入值已经存在，请重新输入",
                url: "输入URL格式不正确",
                max: "该选项输入值不能大于{max}",
                min: "该选项输入值不能小于{min}",
                customizer: "自定义验证不通过"
            },
            b = ["text", "password", "email", "number", "url", "tel", "hidden", ["textarea"], ["select"], ["select-multiple"], ["select-one"], "radio", "checkbox"],
            c = function (a) {
                return "FORM" === a[0].tagName || 11 == a[0].nodeType ? null : a && a.hasClass("form-group") ? a : c(a.parent())
            }, d = function () {
                this.elemTypes = b, this.rules = {}, this.isEmpty = function (a) {
                    return a ? a instanceof Array && 0 === a.length ? !0 : !1 : !0
                }, this.defaultShowError = function (a, b) {
                    var d = angular.element(a), e = c(d);
                    this.isEmpty(e) || e.hasClass("has-error") || e.addClass("has-error");
                    var f = d.next();
                    f && f.hasClass("w5c-error") || d.after('<span class="w5c-error">' + b[0] + "</span>")
                }, this.defaultRemoveError = function (a) {
                    var b = angular.element(a), d = c(b);
                    !this.isEmpty(d) && d.hasClass("has-error") && d.removeClass("has-error");
                    var e = b.next();
                    e.hasClass && e.hasClass("w5c-error") && e.remove()
                }, this.options = {blurTrig: !1, showError: !0, removeError: !0}
            };
        d.prototype = {
            constructor: d, config: function (a) {
                this.options = angular.extend(this.options, a)
            }, setRules: function (a) {
                this.rules = angular.extend(this.rules, a)
            }, getErrorMessage: function (b, c) {
                var d = null, e = c.name;
                switch (e && /\$\d+\$/i.test(e) && (e = e.replace(/\$\d+\$/i, "")), this.isEmpty(this.rules[e]) || this.isEmpty(this.rules[e][b]) || (d = this.rules[e][b]), b) {
                    case"maxlength":
                        return (d || a.maxlength).replace("{maxlength}", c.getAttribute("ng-maxlength"));
                    case"minlength":
                        return (d || a.minlength).replace("{minlength}", c.getAttribute("ng-minlength"));
                    case"max":
                        return (d || a.max).replace("{max}", c.getAttribute("max"));
                    case"min":
                        return (d || a.min).replace("{min}", c.getAttribute("min"));
                    default:
                        if (null !== d)return d;
                        if (null === a[b])throw new Error("该验证规则(" + b + ")默认错误信息没有设置！");
                        return a[b]
                }
            }, getErrorMessages: function (a, b) {
                var c = [];
                for (var d in b)if (b[d]) {
                    var e = this.getErrorMessage(d, a);
                    c.push(e)
                }
                return c
            }, showError: function (a, b, c) {
                var d = angular.extend({}, this.options, c);
                return angular.element(a).removeClass("valid").addClass("error"), d.showError !== !1 ? angular.isFunction(d.showError) ? d.showError(a, b) : d.showError === !0 ? this.defaultShowError(a, b) : void 0 : void 0
            }, removeError: function (a, b) {
                var c = angular.extend({}, this.options, b);
                return angular.element(a).removeClass("error").addClass("valid"), c.removeError !== !1 ? angular.isFunction(c.removeError) ? c.removeError(a) : c.removeError === !0 ? this.defaultRemoveError(a) : void 0 : void 0
            }
        };
        var e = new d;
        this.config = function (a) {
            e.config(a)
        }, this.setRules = function (a) {
            e.setRules(a)
        }, this.setDefaultRules = function (b) {
            a = angular.extend(a, b)
        }, this.$get = function () {
            return e
        }
    }]);
    "undefined" != typeof module && "undefined" != typeof exports && module.exports === exports && (module.exports = a.name)
}(), function () {
    function a(a, b, c, d, e, f) {
        var g = this, h = b[0];
        this.needBindKeydown = !1, this.formCtrl = null, this.submitSuccessFn = null, this.validElements = [], this.setValidate = function (i) {
            this.formCtrl = i;
            var j = function () {
                for (var b = [], c = 0; c < h.elements.length; c++) {
                    var e = h.elements[c].name;
                    if (e && g.validElements.indexOf(e) >= 0) {
                        var f = h[e];
                        if (i[e] && f && d.elemTypes.toString().indexOf(f.type) > -1 && !d.isEmpty(e)) {
                            if (i[e].$valid) {
                                d.removeError(f, g.options);
                                continue
                            }
                            var j = d.getErrorMessages(f, i[f.name].$error);
                            b.push(j[0]), d.removeError(f, g.options), d.showError(f, j, g.options), i[e].w5cError = !0
                        }
                    }
                }
                !d.isEmpty(b) && b.length > 0 ? i.$errors = b : i.$errors = [], a.$$phase || a.$apply(i.$errors)
            };
            i.doValidate = j, i.reset = function () {
                f(function () {
                    i.$setPristine();
                    for (var a = 0; a < h.elements.length; a++) {
                        var b = h.elements[a], c = angular.element(b);
                        d.removeError(c, g.options)
                    }
                    i.$errors = []
                })
            };
            var k = e(c.w5cSubmit);
            c.w5cSubmit && angular.isFunction(k) && (b.bind("submit", function (b) {
                j(), i.$valid && angular.isFunction(k) && a.$apply(function () {
                    k(a, {$event: b})
                })
            }), this.needBindKeydown = !0), this.needBindKeydown && b.bind("keydown keypress", function (c) {
                if (13 === c.which) {
                    var d = document.activeElement;
                    if (d.type && "textarea" !== d.type) {
                        var e = b.find("button");
                        e && e[0] && e[0].focus(), d.focus(), j(), c.preventDefault(), i.$valid && angular.isFunction(g.submitSuccessFn) && a.$apply(function () {
                            g.submitSuccessFn(a, {$event: c})
                        })
                    }
                }
            })
        }, this.doValidate = function (b, c) {
            var d = this;
            if (angular.isFunction(d.formCtrl.doValidate) && d.formCtrl.doValidate(), d.formCtrl.$valid && angular.isFunction(b)) a.$apply(function () {
                b()
            }); else if (!d.formCtrl.$valid && angular.isFunction(c)) {
                var e = d._getInvalidElements();
                a.$apply(function () {
                    c(d.formCtrl.$errors, e)
                })
            }
        }, this._getInvalidElements = function () {
            for (var a = [], c = 0; c < b[0].elements.length; c++) {
                var d = b[0].elements[c];
                this.validElements.indexOf(d.name) >= 0 && this.formCtrl[d.name] && this.formCtrl[d.name].$invalid && a.push(d)
            }
            return a
        }, this.removeElementValidation = function (a) {
            var b = this.validElements.indexOf(a);
            b >= 0 && (this.validElements.splice(b, 1), d.isEmpty(this.formCtrl.$errors) || this.doValidate(angular.noop))
        }, this.removeError = function (a) {
            this.formCtrl.$errors = [], this.formCtrl[a[0].name] && (this.formCtrl[a[0].name].w5cError = !1), d.removeError(a, this.options)
        }, this.initElement = function (b) {
            var c = angular.element(b), e = this;
            if (d.elemTypes.toString().indexOf(b.type) > -1 && !d.isEmpty(b.name) && !/^\d/.test(b.name)) {
                var h = c.attr("disabled");
                if (h && ("true" === h || "disabled" === h))return;
                if (!(this.validElements.indexOf(b.name) < 0))return;
                this.validElements.push(b.name);
                var i = this.formName + "." + b.name + ".$viewValue";
                a.$watch(i, function () {
                    e.removeError(c)
                }, !0), e.options && e.options.blurTrig && c.bind("blur", function () {
                    if (e.options.blurTrig) {
                        var a = this, b = angular.element(this);
                        f(function () {
                            if (g.formCtrl[a.name].$valid) d.removeError(b, g.options); else {
                                var c = d.getErrorMessages(a, g.formCtrl[a.name].$error);
                                d.showError(b, c, g.options), g.formCtrl[b[0].name] && (g.formCtrl[b[0].name].w5cError = !0)
                            }
                        }, 50)
                    }
                })
            }
        }, this._init = function () {
            if (this.formName = b.attr("name"), !this.formName)throw Error("form must has name when use w5cFormValidate");
            var e = a.$eval(c.w5cFormValidate);
            c.w5cFormValidate && a.$watch(c.w5cFormValidate, function (a) {
                a && (g.options = e = angular.extend({}, d.options, a))
            }, !0), this.options = angular.extend({}, d.options, e);
            for (var f = 0; f < b[0].elements.length; f++)this.initElement(b[0].elements[f])
        }, this._init()
    }

    angular.module("w5c.validator").directive("w5cFormValidate", [function () {
        return {
            require: ["w5cFormValidate", "^?form"],
            controller: ["$scope", "$element", "$attrs", "w5cValidator", "$parse", "$timeout", a],
            link: function (a, b, c, d) {
                var e = d[0], f = d[1];
                e.setValidate(f)
            }
        }
    }]).directive("w5cFormSubmit", ["$parse", function (a) {
        return {
            require: "^w5cFormValidate",
            scope: {w5cFormSubmit: "&", errorCallback: "&"},
            link: function (a, b, c, d) {
                b.bind("click", function (b) {
                    d.doValidate(function () {
                        a.w5cFormSubmit({$event: b})
                    }, function (b, c) {
                        a.errorCallback({$errors: b, $invalidElements: c})
                    })
                }), d.needBindKeydown = !0, d.submitSuccessFn = a.w5cFormSubmit
            }
        }
    }]).directive("w5cRepeat", ["$timeout", function (a) {
        "use strict";
        return {
            require: ["ngModel", "^w5cFormValidate"], link: function (b, c, d, e) {
                a(function () {
                    var a = c.inheritedData("$formController")[d.w5cRepeat], b = e[0], f = e[1];
                    b.$parsers.push(function (c) {
                        return c === a.$viewValue ? b.$setValidity("repeat", !0) : b.$setValidity("repeat", !1), c
                    }), a.$parsers.push(function (a) {
                        return b.$setValidity("repeat", a === b.$viewValue), a === b.$viewValue && f.removeError(c), a
                    })
                })
            }
        }
    }]).directive("w5cCustomizer", ["$timeout", function (a) {
        "use strict";
        return {
            require: ["^form", "?ngModel"], link: function (a, b, c, d) {
                var e = d[1], f = function () {
                    var b = a.$eval(c.w5cCustomizer);
                    b === !0 ? e.$setValidity("customizer", !0) : e.$setValidity("customizer", !1)
                }, g = d[0][c.associate];
                g && g.$viewChangeListeners.push(f), e.$viewChangeListeners.push(f), f()
            }
        }
    }]).directive("w5cUniqueCheck", ["$timeout", "$http", "w5cValidator", function (a, b, c) {
        return {
            require: ["ngModel", "?^w5cFormValidate", "?^form"], link: function (a, d, e, f) {
                var g = f[0], h = f[1], i = f[2], j = function () {
                    var f = a.$eval(e.w5cUniqueCheck), j = f.url, k = f.isExists;
                    b.get(j).success(function (a) {
                        var b = k === !1 ? "true" == a || 1 == a : !("true" == a || 1 == a);
                        if (g.$setValidity("w5cuniquecheck", b), !b) {
                            var e = c.getErrorMessage("w5cuniquecheck", d[0]);
                            c.showError(d[0], [e], h.options), i[d[0].name] && (i[d[0].name].w5cError = !0), i.$errors ? i.$errors.unshift(e) : i.$errors = [e]
                        }
                    })
                };
                g.$viewChangeListeners.push(function () {
                    i.$errors = [], g.$setValidity("w5cuniquecheck", !0), (!g.$invalid || g.$error.w5cuniquecheck) && g.$dirty && j()
                });
                var k = a.$eval(e.ngModel);
                if (k) {
                    if (g.$invalid && !g.$error.w5cuniquecheck)return;
                    j()
                }
            }
        }
    }]).directive("w5cDynamicName", [function () {
        return {
            restrict: "A", require: "?ngModel", link: function (a, b, c, d) {
                var e = a.$eval(c.w5cDynamicName) || c.w5cDynamicName;
                if (e) {
                    d.$name = e, b.attr("name", e);
                    var f = b.controller("form") || {$addControl: angular.noop};
                    f.$addControl(d)
                }
            }
        }
    }]).directive("w5cDynamicElement", ["$timeout", function (a) {
        return {
            restrict: "A", require: ["?ngModel", "?^w5cFormValidate", "?^form"], link: function (b, c, d, e) {
                var f = c[0].name, g = e[2];
                if (f) {
                    c.on("$destroy", function (a) {
                        b[g.$name] && e[1].removeElementValidation(f)
                    }), g[f] || g.$addControl(e[0]);
                    var h = !1;
                    e[2].$errors && e[2].$errors.length > 0 && (h = !0), e[1].initElement(c[0]), h && a(function () {
                        e[1].doValidate(angular.noop)
                    })
                }
            }
        }
    }])
}();