(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/common'), require('@angular/core'), require('rxjs'), require('rxjs/operators'), require('@angular/forms'), require('@angular/animations'), require('@angular/cdk/coercion'), require('@ng-tangram/components/core')) :
    typeof define === 'function' && define.amd ? define('@ng-tangram/components/forms', ['exports', '@angular/common', '@angular/core', 'rxjs', 'rxjs/operators', '@angular/forms', '@angular/animations', '@angular/cdk/coercion', '@ng-tangram/components/core'], factory) :
    (global = global || self, factory((global.nt = global.nt || {}, global.nt.components = global.nt.components || {}, global.nt.components.forms = {}), global.ng.common, global.ng.core, global.rxjs, global.rxjs.operators, global.ng.forms, global.ng.animations, global.ng.cdk.coercion, global.nt.components.core));
}(this, (function (exports, common, core, rxjs, operators, forms, animations, coercion, core$1) { 'use strict';

    var NtFormFieldControl = (function () {
        function NtFormFieldControl() {
        }
        NtFormFieldControl.prototype.focus = function () { };
        return NtFormFieldControl;
    }());

    var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
        var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
        if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
        else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
        return c > 3 && r && Object.defineProperty(target, key, r), r;
    };
    var NtFormLabelWidthDirective = (function () {
        function NtFormLabelWidthDirective() {
            this.widthChange = new core.EventEmitter();
        }
        Object.defineProperty(NtFormLabelWidthDirective.prototype, "width", {
            get: function () { return this._width || 120; },
            set: function (value) {
                this._width = coercion.coerceNumberProperty(value, 0);
                if (this._width > 0) {
                    this.widthChange.next(this._width);
                }
            },
            enumerable: true,
            configurable: true
        });
        NtFormLabelWidthDirective.prototype.ngOnDestroy = function () {
            this.widthChange.next();
            this.widthChange.complete();
        };
        __decorate([
            core.Input('ntFormLabelWidth')
        ], NtFormLabelWidthDirective.prototype, "width", null);
        NtFormLabelWidthDirective = __decorate([
            core.Directive({
                selector: 'form[ntFormLabelWidth]',
            })
        ], NtFormLabelWidthDirective);
        return NtFormLabelWidthDirective;
    }());

    var __decorate$1 = (this && this.__decorate) || function (decorators, target, key, desc) {
        var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
        if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
        else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
        return c > 3 && r && Object.defineProperty(target, key, r), r;
    };
    var NtFormOrientationDirective = (function () {
        function NtFormOrientationDirective() {
            this.orientationChange = new core.EventEmitter();
        }
        Object.defineProperty(NtFormOrientationDirective.prototype, "orientation", {
            get: function () { return this._orientation; },
            set: function (value) {
                this._orientation = value;
                this.orientationChange.next(this._orientation);
            },
            enumerable: true,
            configurable: true
        });
        NtFormOrientationDirective.prototype.ngOnDestroy = function () {
            this.orientationChange.next();
            this.orientationChange.complete();
        };
        __decorate$1([
            core.Input('ntFormOrientation')
        ], NtFormOrientationDirective.prototype, "orientation", null);
        NtFormOrientationDirective = __decorate$1([
            core.Directive({
                selector: 'form[ntFormOrientation]',
            })
        ], NtFormOrientationDirective);
        return NtFormOrientationDirective;
    }());

    var __decorate$2 = (this && this.__decorate) || function (decorators, target, key, desc) {
        var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
        if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
        else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
        return c > 3 && r && Object.defineProperty(target, key, r), r;
    };
    var __param = (this && this.__param) || function (paramIndex, decorator) {
        return function (target, key) { decorator(target, key, paramIndex); }
    };
    var NtFormFieldComponent = (function () {
        function NtFormFieldComponent(_ngZone, _changeDetectorRef, parentForm, parentFormGroup, _formLabelWidth, _formOrientation) {
            var _this = this;
            this._ngZone = _ngZone;
            this._changeDetectorRef = _changeDetectorRef;
            this._formLabelWidth = _formLabelWidth;
            this._formOrientation = _formOrientation;
            this._destroy = new rxjs.Subject();
            this._labelVisible = true;
            this._selfLabelWidth = false;
            this._selfOrientation = false;
            this._markVisible = true;
            this._ngForm = null;
            this._labelStyles = {};
            this._groupStyles = {};
            this._invalid = false;
            this.statusChanges = rxjs.defer(function () {
                if (_this.control && _this.ngControl) {
                    return _this.ngControl.statusChanges ? _this.ngControl.statusChanges : rxjs.of(null);
                }
                return _this._ngZone.onStable
                    .asObservable()
                    .pipe(operators.take(1), operators.switchMap(function () { return _this.statusChanges; }));
            });
            this._ngForm = parentForm || parentFormGroup;
            if (this._formLabelWidth) {
                this._formLabelWidth.widthChange.pipe(operators.takeUntil(this._destroy), operators.filter(function () { return !_this._selfLabelWidth; }))
                    .subscribe(function (width) {
                    _this._labelWidth = width;
                    _this._setHorizontalStyles();
                });
            }
            if (this._formOrientation) {
                this._formOrientation.orientationChange.pipe(operators.takeUntil(this._destroy), operators.filter(function () { return !_this._selfOrientation; }))
                    .subscribe(function (orientation) {
                    _this._orientation = orientation;
                    _this._setHorizontalStyles();
                });
            }
            this.statusChanges.pipe(operators.takeUntil(this._destroy)).subscribe(function () { return _this._validate(); });
        }
        Object.defineProperty(NtFormFieldComponent.prototype, "labelVisible", {
            get: function () { return this._labelVisible; },
            set: function (value) { this._labelVisible = coercion.coerceBooleanProperty(value); },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(NtFormFieldComponent.prototype, "labelWidth", {
            get: function () { return this._labelWidth || 120; },
            set: function (value) {
                this._labelWidth = coercion.coerceNumberProperty(value, 0);
                this._selfLabelWidth = this._labelWidth > 0;
                this._setHorizontalStyles();
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(NtFormFieldComponent.prototype, "orientation", {
            get: function () { return this._orientation; },
            set: function (value) {
                this._orientation = value;
                this._selfOrientation = true;
                this._setHorizontalStyles();
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(NtFormFieldComponent.prototype, "horizontal", {
            get: function () { return this.orientation === 'horizontal'; },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(NtFormFieldComponent.prototype, "markVisible", {
            get: function () { return this._markVisible; },
            set: function (value) {
                this._markVisible = coercion.coerceBooleanProperty(value);
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(NtFormFieldComponent.prototype, "required", {
            get: function () {
                if (this.ngControl &&
                    this.ngControl.control &&
                    this.ngControl.control.validator) {
                    var control = new forms.FormControl();
                    var validateResult = this.ngControl.control.validator(control);
                    return validateResult && validateResult.hasOwnProperty('required');
                }
                if (this.control) {
                    return !!this.control.required;
                }
                return false;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(NtFormFieldComponent.prototype, "errors", {
            get: function () {
                return this.control.ngControl ? this.control.ngControl.errors : null;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(NtFormFieldComponent.prototype, "ngControl", {
            get: function () { return this.control ? this.control.ngControl : null; },
            enumerable: true,
            configurable: true
        });
        NtFormFieldComponent.prototype.ngAfterViewInit = function () {
            var _this = this;
            if (this._ngForm && this.ngControl) {
                this._ngForm.ngSubmit.pipe(operators.takeUntil(this._destroy)).subscribe(function () { return _this._validate(); });
            }
        };
        NtFormFieldComponent.prototype.ngOnDestroy = function () {
            this._destroy.next();
            this._destroy.complete();
        };
        NtFormFieldComponent.prototype._validate = function () {
            if (this.ngControl) {
                this._invalid = !!this.ngControl.invalid;
                this._changeDetectorRef.markForCheck();
            }
        };
        NtFormFieldComponent.prototype._clearValidateMessage = function () {
            this._invalid = false;
        };
        NtFormFieldComponent.prototype._setHorizontalStyles = function () {
            if (this.labelWidth > 0 && this.horizontal) {
                this._labelStyles['width.px'] = this.labelWidth;
                this._groupStyles['margin-left.px'] = this.labelWidth;
            }
            else {
                delete this._labelStyles['width.px'];
                delete this._groupStyles['margin-left.px'];
            }
        };
        NtFormFieldComponent.ctorParameters = function () { return [
            { type: core.NgZone },
            { type: core.ChangeDetectorRef },
            { type: forms.NgForm, decorators: [{ type: core.Optional }] },
            { type: forms.FormGroupDirective, decorators: [{ type: core.Optional }] },
            { type: NtFormLabelWidthDirective, decorators: [{ type: core.Optional }] },
            { type: NtFormOrientationDirective, decorators: [{ type: core.Optional }] }
        ]; };
        __decorate$2([
            core.Input()
        ], NtFormFieldComponent.prototype, "label", void 0);
        __decorate$2([
            core.Input()
        ], NtFormFieldComponent.prototype, "labelVisible", null);
        __decorate$2([
            core.Input()
        ], NtFormFieldComponent.prototype, "labelWidth", null);
        __decorate$2([
            core.Input()
        ], NtFormFieldComponent.prototype, "messages", void 0);
        __decorate$2([
            core.Input()
        ], NtFormFieldComponent.prototype, "orientation", null);
        __decorate$2([
            core.Input()
        ], NtFormFieldComponent.prototype, "markVisible", null);
        __decorate$2([
            core.ContentChild(NtFormFieldControl)
        ], NtFormFieldComponent.prototype, "control", void 0);
        NtFormFieldComponent = __decorate$2([
            core.Component({
                selector: 'nt-form-field',
                template: "\n    <label class=\"nt-form-label\"\n      *ngIf=\"labelVisible\"\n      [ngStyle]=\"_labelStyles\"\n      [class.required]=\"markVisible && required\">\n      {{label}}\n    </label>\n    <div class=\"nt-form-group\" [ngStyle]=\"_groupStyles\">\n      <ng-content></ng-content>\n      <span class=\"form-error is-visible\" *ngIf=\"_invalid && errors\" [@fade]=\"_invalid\">\n        {{ errors | formError:label:messages }}\n      </span>\n    </div>\n  ",
                animations: [
                    animations.trigger('fade', [
                        animations.transition('* => false', core$1.fadeOut(.15)),
                        animations.transition('* => true', core$1.fadeIn(.15))
                    ])
                ],
                encapsulation: core.ViewEncapsulation.None,
                changeDetection: core.ChangeDetectionStrategy.OnPush,
                host: {
                    'class': 'nt-form-field',
                    '[class.nt-form-error]': '_invalid',
                    '[class.nt-form-horizontal]': 'horizontal'
                }
            }),
            __param(2, core.Optional()),
            __param(3, core.Optional()),
            __param(4, core.Optional()),
            __param(5, core.Optional())
        ], NtFormFieldComponent);
        return NtFormFieldComponent;
    }());

    var __decorate$3 = (this && this.__decorate) || function (decorators, target, key, desc) {
        var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
        if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
        else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
        return c > 3 && r && Object.defineProperty(target, key, r), r;
    };
    var __param$1 = (this && this.__param) || function (paramIndex, decorator) {
        return function (target, key) { decorator(target, key, paramIndex); }
    };
    var NtFormAutofocusDirective = (function () {
        function NtFormAutofocusDirective(form, formGroup) {
            this._destory = new rxjs.Subject();
            this.formContainer = form || formGroup;
        }
        NtFormAutofocusDirective.prototype.ngAfterContentInit = function () {
            var _this = this;
            if (this.formContainer) {
                this.formContainer.ngSubmit
                    .pipe(operators.takeUntil(this._destory))
                    .subscribe(function () {
                    var field = _this.fields.find(function (field) { return !!field.ngControl && !!field.ngControl.invalid; });
                    if (field && field.control) {
                        field.control.focus();
                    }
                });
            }
        };
        NtFormAutofocusDirective.prototype.ngOnDestroy = function () {
            this._destory.next();
            this._destory.complete();
        };
        NtFormAutofocusDirective.ctorParameters = function () { return [
            { type: forms.NgForm, decorators: [{ type: core.Optional }] },
            { type: forms.FormGroupDirective, decorators: [{ type: core.Optional }] }
        ]; };
        __decorate$3([
            core.ContentChildren(NtFormFieldComponent, { descendants: true })
        ], NtFormAutofocusDirective.prototype, "fields", void 0);
        NtFormAutofocusDirective = __decorate$3([
            core.Directive({
                selector: 'form[ntFormAutofocus]'
            }),
            __param$1(0, core.Optional()),
            __param$1(1, core.Optional())
        ], NtFormAutofocusDirective);
        return NtFormAutofocusDirective;
    }());

    var __assign = (this && this.__assign) || function () {
        __assign = Object.assign || function(t) {
            for (var s, i = 1, n = arguments.length; i < n; i++) {
                s = arguments[i];
                for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                    t[p] = s[p];
            }
            return t;
        };
        return __assign.apply(this, arguments);
    };
    var __decorate$4 = (this && this.__decorate) || function (decorators, target, key, desc) {
        var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
        if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
        else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
        return c > 3 && r && Object.defineProperty(target, key, r), r;
    };
    var DEFAULT_TEMPLATES = {
        "required": "请填写{0}",
        "email": "{0}不是邮箱格式",
        "min": "{0}必须输入大于{1}的数字",
        "max": "{0}必须输入小于{1}的数字",
        "pattern": "{0}不是合法的数据",
        "maxlength": "{0}长度为最大{1}",
        "minlength": "{0}长度为最小{1}"
    };
    var NtFormValidationTransformer = (function () {
        function NtFormValidationTransformer() {
        }
        NtFormValidationTransformer.prototype.transform = function (errors, label, messages) {
            if (!errors) {
                return '';
            }
            var templates = __assign(__assign({}, DEFAULT_TEMPLATES), messages);
            if (errors.hasOwnProperty('required')) {
                return this._format(templates['required'], label);
            }
            else if (errors.hasOwnProperty('email')) {
                return this._format(templates['email'], label);
            }
            else if (errors.hasOwnProperty('min')) {
                return this._format(templates['min'], label, errors.min.min);
            }
            else if (errors.hasOwnProperty('max')) {
                return this._format(templates['max'], label, errors.max.max);
            }
            else if (errors.hasOwnProperty('minlength')) {
                return this._format(templates['minlength'], label, errors.minlength.requiredLength);
            }
            else if (errors.hasOwnProperty('maxlength')) {
                return this._format(templates['maxlength'], label, errors.maxlength.requiredLength);
            }
            else if (errors.hasOwnProperty('pattern')) {
                return this._format(templates['pattern'], label);
            }
            else {
                return '';
            }
        };
        NtFormValidationTransformer.prototype._format = function (template) {
            var args = [];
            for (var _i = 1; _i < arguments.length; _i++) {
                args[_i - 1] = arguments[_i];
            }
            return template.replace(/\{(\d+)\}/g, function (match, number) {
                return typeof args[number] !== 'undefined' ? args[number] : match;
            });
        };
        NtFormValidationTransformer = __decorate$4([
            core.Injectable()
        ], NtFormValidationTransformer);
        return NtFormValidationTransformer;
    }());
    var NT_VALIDATION_TRANSFOMER = new core.InjectionToken('nt-validation-transformer');

    var __decorate$5 = (this && this.__decorate) || function (decorators, target, key, desc) {
        var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
        if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
        else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
        return c > 3 && r && Object.defineProperty(target, key, r), r;
    };
    var __param$2 = (this && this.__param) || function (paramIndex, decorator) {
        return function (target, key) { decorator(target, key, paramIndex); }
    };
    var __read = (this && this.__read) || function (o, n) {
        var m = typeof Symbol === "function" && o[Symbol.iterator];
        if (!m) return o;
        var i = m.call(o), r, ar = [], e;
        try {
            while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
        }
        catch (error) { e = { error: error }; }
        finally {
            try {
                if (r && !r.done && (m = i["return"])) m.call(i);
            }
            finally { if (e) throw e.error; }
        }
        return ar;
    };
    var __spread = (this && this.__spread) || function () {
        for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
        return ar;
    };
    var NtFormErrorPipe = (function () {
        function NtFormErrorPipe(_transformer) {
            this._transformer = _transformer;
        }
        NtFormErrorPipe.prototype.transform = function (value) {
            var _a;
            var args = [];
            for (var _i = 1; _i < arguments.length; _i++) {
                args[_i - 1] = arguments[_i];
            }
            return (_a = this._transformer).transform.apply(_a, __spread([value], args));
        };
        NtFormErrorPipe.ctorParameters = function () { return [
            { type: undefined, decorators: [{ type: core.Inject, args: [NT_VALIDATION_TRANSFOMER,] }] }
        ]; };
        NtFormErrorPipe = __decorate$5([
            core.Pipe({ name: 'formError' }),
            __param$2(0, core.Inject(NT_VALIDATION_TRANSFOMER))
        ], NtFormErrorPipe);
        return NtFormErrorPipe;
    }());

    var __decorate$6 = (this && this.__decorate) || function (decorators, target, key, desc) {
        var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
        if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
        else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
        return c > 3 && r && Object.defineProperty(target, key, r), r;
    };
    var NtFormsModule = (function () {
        function NtFormsModule() {
        }
        NtFormsModule_1 = NtFormsModule;
        NtFormsModule.forRoot = function (transformer) {
            return {
                ngModule: NtFormsModule_1,
                providers: [
                    { provide: NT_VALIDATION_TRANSFOMER, useClass: transformer || NtFormValidationTransformer }
                ]
            };
        };
        var NtFormsModule_1;
        NtFormsModule = NtFormsModule_1 = __decorate$6([
            core.NgModule({
                imports: [common.CommonModule],
                exports: [
                    NtFormFieldComponent,
                    NtFormAutofocusDirective,
                    NtFormLabelWidthDirective,
                    NtFormOrientationDirective,
                    NtFormErrorPipe
                ],
                declarations: [
                    NtFormFieldComponent,
                    NtFormAutofocusDirective,
                    NtFormLabelWidthDirective,
                    NtFormOrientationDirective,
                    NtFormErrorPipe
                ]
            })
        ], NtFormsModule);
        return NtFormsModule;
    }());

    exports.DEFAULT_TEMPLATES = DEFAULT_TEMPLATES;
    exports.NT_VALIDATION_TRANSFOMER = NT_VALIDATION_TRANSFOMER;
    exports.NtFormAutofocusDirective = NtFormAutofocusDirective;
    exports.NtFormErrorPipe = NtFormErrorPipe;
    exports.NtFormFieldComponent = NtFormFieldComponent;
    exports.NtFormFieldControl = NtFormFieldControl;
    exports.NtFormLabelWidthDirective = NtFormLabelWidthDirective;
    exports.NtFormOrientationDirective = NtFormOrientationDirective;
    exports.NtFormValidationTransformer = NtFormValidationTransformer;
    exports.NtFormsModule = NtFormsModule;

    Object.defineProperty(exports, '__esModule', { value: true });

})));
//# sourceMappingURL=forms.umd.js.map
