UNPKG

ng-error-message

Version:

Displays error messages when a form control is invalid avoiding the long list of tags for each error

183 lines (175 loc) 5.02 kB
import { Injectable, Pipe, NgModule, defineInjectable, inject } from '@angular/core'; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * Abstract class * @abstract */ class NgErrorMessageLoader { } /** * Load the json file */ class NgErrorMessageLoaderService { /** * @param {?} _http * @param {?} _dictURl */ constructor(_http, _dictURl) { this._http = _http; this._dictURl = _dictURl; } /** * Gets the dictionary json file * @return {?} */ getDictionary() { if (!/.json$/.test(this._dictURl)) { throw `${this._dictURl} must be a json file`; } return this._http.get(this._dictURl); } } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * Service for serving the errors dictionary */ class NgErrorMessageService { /** * @param {?} _loader */ constructor(_loader) { this._loader = _loader; /** * Contains the dictionary of errors */ this._errors = {}; } /** * @return {?} */ get errors() { return this._errors; } /** * Loads the dictionary * @return {?} */ load() { this._loader.getDictionary().subscribe((/** * @param {?} dict * @return {?} */ dict => { this._errors = dict; })); } } NgErrorMessageService.decorators = [ { type: Injectable, args: [{ providedIn: 'root' },] } ]; /** @nocollapse */ NgErrorMessageService.ctorParameters = () => [ { type: NgErrorMessageLoader } ]; /** @nocollapse */ NgErrorMessageService.ngInjectableDef = defineInjectable({ factory: function NgErrorMessageService_Factory() { return new NgErrorMessageService(inject(NgErrorMessageLoader)); }, token: NgErrorMessageService, providedIn: "root" }); /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ class NgErrorMessagePipe { /** * @param {?} _errorMessageSrv */ constructor(_errorMessageSrv) { this._errorMessageSrv = _errorMessageSrv; /** * Saves the untransformed message */ this._cachedCleanMessage = null; /** * Saves the transformed message */ this._cachedTransformedMessage = null; } /** * @param {?} errors * @param {?=} args * @return {?} */ transform(errors, args = {}) { if (!errors || typeof errors !== 'object' || Array.isArray(errors) || typeof args !== 'object' || Array.isArray(args)) return null; /** @type {?} */ const errKey = Object.keys(errors)[0]; // The first found error /** @type {?} */ const errArgs = args[errKey]; /** @type {?} */ let msg = errKey !== undefined && this._errorMessageSrv.errors[errKey] || null; if (this._cachedCleanMessage === msg) { return this._cachedTransformedMessage; } this._cachedCleanMessage = msg; if (msg) { for (let p in errArgs) { msg = msg.replace(new RegExp("{{\\s?" + p + "\\s?}}", 'g'), errArgs[p]); } } this._cachedTransformedMessage = msg; return msg; } } NgErrorMessagePipe.decorators = [ { type: Pipe, args: [{ name: 'errorMessage', pure: false },] } ]; /** @nocollapse */ NgErrorMessagePipe.ctorParameters = () => [ { type: NgErrorMessageService } ]; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ class NgErrorMessageModule { /** * starts the services * @param {?} jsonProvider * @return {?} */ static forRoot(jsonProvider) { return { ngModule: NgErrorMessageModule, providers: [ jsonProvider, NgErrorMessageService ] }; } } NgErrorMessageModule.decorators = [ { type: NgModule, args: [{ declarations: [NgErrorMessagePipe], exports: [NgErrorMessagePipe] },] } ]; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ export { NgErrorMessageService, NgErrorMessageLoader, NgErrorMessageLoaderService, NgErrorMessageModule, NgErrorMessagePipe as ɵa }; //# sourceMappingURL=ng-error-message.js.map