]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/fm-editor/fm-editor.component.ts
lp1857911 allow override of IDL class label for display in the FM editor
[Evergreen.git] / Open-ILS / src / eg2 / src / app / share / fm-editor / fm-editor.component.ts
1 import {Component, OnInit, Input, ViewChild,
2     Output, EventEmitter, TemplateRef} from '@angular/core';
3 import {NgForm} from '@angular/forms';
4 import {IdlService, IdlObject} from '@eg/core/idl.service';
5 import {Observable} from 'rxjs';
6 import {map} from 'rxjs/operators';
7 import {AuthService} from '@eg/core/auth.service';
8 import {PcrudService} from '@eg/core/pcrud.service';
9 import {OrgService} from '@eg/core/org.service';
10 import {DialogComponent} from '@eg/share/dialog/dialog.component';
11 import {ToastService} from '@eg/share/toast/toast.service';
12 import {StringComponent} from '@eg/share/string/string.component';
13 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
14 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
15 import {FormatService} from '@eg/core/format.service';
16 import {TranslateComponent} from '@eg/share/translate/translate.component';
17 import {FmRecordEditorActionComponent} from './fm-editor-action.component';
18 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
19 import {Directive, HostBinding} from '@angular/core';
20 import {AbstractControl, NG_VALIDATORS, ValidationErrors, Validator, Validators} from '@angular/forms';
21
22 interface CustomFieldTemplate {
23     template: TemplateRef<any>;
24
25     // Allow the caller to pass in a free-form context blob to
26     // be addedto the caller's custom template context, along
27     // with our stock context.
28     context?: {[fields: string]: any};
29 }
30
31 export interface CustomFieldContext {
32     // Current create/edit/view record
33     record: IdlObject;
34
35     // IDL field definition blob
36     field: any;
37
38     // additional context values passed via CustomFieldTemplate
39     [fields: string]: any;
40 }
41
42 // Collection of extra options that may be applied to fields
43 // for controling non-default behaviour.
44 export interface FmFieldOptions {
45
46     // Render the field as a combobox using these values, regardless
47     // of the field's datatype.
48     customValues?: ComboboxEntry[];
49
50     // Provide / override the "selector" value for the linked class.
51     // This is the field the combobox will search for typeahead.  If no
52     // field is defined, the "selector" field is used.  If no "selector"
53     // field exists, the combobox will pre-load all linked values so
54     // the user can click to navigate.
55     linkedSearchField?: string;
56
57     // When true for combobox fields, pre-fetch the combobox data
58     // so the user can click or type to find values.
59     preloadLinkedValues?: boolean;
60
61     // Additional search conditions to include when constructing
62     // the query for a linked field's combobox
63     linkedSearchConditions?: {[field: string]: string};
64
65     // Directly override the required state of the field.
66     // This only has an affect if the value is true.
67     isRequired?: boolean;
68
69     // If this function is defined, the function will be called
70     // at render time to see if the field should be marked are required.
71     // This supersedes all other isRequired specifiers.
72     isRequiredOverride?: (field: string, record: IdlObject) => boolean;
73
74     // Directly apply the readonly status of the field.
75     // This only has an affect if the value is true.
76     isReadonly?: boolean;
77
78     // If this function is defined, the function will be called
79     // at render time to see if the field should be marked readonly.
80     // This supersedes all other isReadonly specifiers.
81     isReadonlyOverride?: (field: string, record: IdlObject) => boolean;
82
83     // Render the field using this custom template instead of chosing
84     // from the default set of form inputs.
85     customTemplate?: CustomFieldTemplate;
86
87     // help text to display via a popover
88     helpText?: StringComponent;
89
90     // minimum and maximum permitted values for int fields
91     min?: number;
92     max?: number;
93 }
94
95 @Component({
96   selector: 'eg-fm-record-editor',
97   templateUrl: './fm-editor.component.html',
98   /* align checkboxes when not using class="form-check" */
99   styles: ['input[type="checkbox"] {margin-left: 0px;}']
100 })
101 export class FmRecordEditorComponent
102     extends DialogComponent implements OnInit {
103
104     // IDL class hint (e.g. "aou")
105     @Input() idlClass: string;
106
107     // Show datetime fields in this particular timezone
108     timezone: string = this.format.wsOrgTimezone;
109
110     // Permissions extracted from the permacrud defs in the IDL
111     // for the current IDL class
112     modePerms: {[mode: string]: string};
113
114     // Collection of FmFieldOptions for specifying non-default
115     // behaviour for each field (by field name).
116     @Input() fieldOptions: {[fieldName: string]: FmFieldOptions} = {};
117
118     // This is used to set default values when making a new record
119     @Input() defaultNewRecord: IdlObject;
120
121     // list of fields that should not be displayed
122     @Input() hiddenFieldsList: string[] = [];
123     @Input() hiddenFields: string; // comma-separated string version
124
125     // list of fields that should always be read-only
126     @Input() readonlyFieldsList: string[] = [];
127     @Input() readonlyFields: string; // comma-separated string version
128
129     // list of required fields; this supplements what the IDL considers
130     // required
131     @Input() requiredFieldsList: string[] = [];
132     @Input() requiredFields: string; // comma-separated string version
133
134     // list of timestamp fields that should display with a timepicker
135     @Input() datetimeFieldsList: string[] = [];
136     @Input() datetimeFields: string; // comma-separated string version
137
138     // list of org_unit fields where a default value may be applied by
139     // the org-select if no value is present.
140     @Input() orgDefaultAllowedList: string[] = [];
141     @Input() orgDefaultAllowed: string; // comma-separated string version
142
143     // IDL record display label.  Defaults to the IDL label.
144     @Input() recordLabel: string;
145
146     // When true at the component level, pre-fetch the combobox data
147     // for all combobox fields.  See also FmFieldOptions.
148     @Input() preloadLinkedValues: boolean;
149
150     // Display within a modal dialog window or inline in the page.
151     @Input() displayMode: 'dialog' | 'inline' = 'dialog';
152
153     // Hide the top 'Record Editor: ...' banner.  Primarily useful
154     // for displayMode === 'inline'
155     @Input() hideBanner: boolean;
156
157     // do not close dialog on error saving record
158     @Input() remainOpenOnError: false;
159
160     // if date fields need to be in a specific order (e.g.
161     // start date before end date), specify them in a comma-
162     // separated list here.
163     @Input() dateFieldOrderList: '';
164
165     // Emit the modified object when the save action completes.
166     @Output() recordSaved = new EventEmitter<IdlObject>();
167
168     // Emit the modified object when the save action completes.
169     @Output() recordDeleted = new EventEmitter<IdlObject>();
170
171     // Emit the original object when the save action is canceled.
172     @Output() recordCanceled = new EventEmitter<IdlObject>();
173
174     // Emit an error message when the save action fails.
175     @Output() recordError = new EventEmitter<string>();
176
177     @ViewChild('translator', { static: true }) private translator: TranslateComponent;
178     @ViewChild('successStr', { static: true }) successStr: StringComponent;
179     @ViewChild('failStr', { static: true }) failStr: StringComponent;
180     @ViewChild('confirmDel', { static: true }) confirmDel: ConfirmDialogComponent;
181     @ViewChild('fmEditForm', { static: false}) fmEditForm: NgForm;
182
183     // IDL info for the the selected IDL class
184     idlDef: any;
185
186     // Can we edit the primary key?
187     pkeyIsEditable = false;
188
189     // List of IDL field definitions.  This is a subset of the full
190     // list of fields on the IDL, since some are hidden, virtual, etc.
191     fields: any[];
192
193     // DOM id prefix to prevent id collisions.
194     idPrefix: string;
195
196     // mode: 'create' for creating a new record,
197     //       'update' for editing an existing record
198     //       'view' for viewing an existing record without editing
199     @Input() mode: 'create' | 'update' | 'view' = 'create';
200
201     // custom function for munging the record before it gets saved;
202     // will get passed mode and the record itself
203     @Input() preSave: Function;
204
205     // recordId and record getters and setters.
206     // Note that setting the this.recordId to NULL does not clear the
207     // current value of this.record and vice versa.  Only viable data
208     // is actionable.  This allows the caller to use both @Input()'s
209     // without each clobbering the other.
210
211     // Record ID to view/update.
212     _recordId: any = null;
213     @Input() set recordId(id: any) {
214         if (id) {
215             if (id !== this._recordId) {
216                 this._recordId = id;
217                 this._record = null; // force re-fetch
218                 this.handleRecordChange();
219             }
220         } else {
221             this._recordId = null;
222         }
223     }
224
225     get recordId(): any {
226         return this._recordId;
227     }
228
229     // IDL record we are editing
230     _record: IdlObject = null;
231     @Input() set record(r: IdlObject) {
232         if (r) {
233             if (!this.idl.pkeyMatches(this.record, r)) {
234                 this._record = r;
235                 this._recordId = null; // avoid mismatch
236                 this.handleRecordChange();
237             }
238         } else {
239             this._record = null;
240         }
241     }
242
243     get record(): IdlObject {
244         return this._record;
245     }
246
247     actions: FmRecordEditorActionComponent[] = [];
248
249     initDone: boolean;
250
251     // Comma-separated list of field names defining the order in which
252     // fields should be rendered in the form.  Any fields not represented
253     // will be rendered alphabetically by label after the named fields.
254     @Input() fieldOrder: string;
255
256     // When true, show a delete button and support delete operations.
257     @Input() showDelete: boolean;
258
259     constructor(
260       private modal: NgbModal, // required for passing to parent
261       private idl: IdlService,
262       private auth: AuthService,
263       private toast: ToastService,
264       private format: FormatService,
265       private org: OrgService,
266       private pcrud: PcrudService) {
267       super(modal);
268     }
269
270     // Avoid fetching data on init since that may lead to unnecessary
271     // data retrieval.
272     ngOnInit() {
273
274         // In case the caller sets the value to null / undef.
275         if (!this.fieldOptions) { this.fieldOptions = {}; }
276
277         this.listifyInputs();
278         this.idlDef = this.idl.classes[this.idlClass];
279         this.recordLabel = this.recordLabel || this.idlDef.label;
280
281         // Add some randomness to the generated DOM IDs to ensure against clobbering
282         this.idPrefix = 'fm-editor-' + Math.floor(Math.random() * 100000);
283
284         if (this.isDialog()) {
285             this.onOpen$.subscribe(() => this.initRecord());
286         } else {
287             this.initRecord();
288         }
289         this.initDone = true;
290     }
291
292     // If the record ID changes after ngOnInit has been called
293     // and we're using displayMode=inline, force the data to
294     // resync in real time
295     handleRecordChange() {
296         if (this.initDone && !this.isDialog()) {
297             this.initRecord();
298         }
299     }
300
301     open(args?: NgbModalOptions): Observable<any> {
302         if (!args) {
303             args = {};
304         }
305         // ensure we don't hang on to our copy of the record
306         // if the user dismisses the dialog
307         args.beforeDismiss = () => {
308             this.record = undefined;
309             return true;
310         };
311         return super.open(args);
312     }
313
314     isDialog(): boolean {
315         return this.displayMode === 'dialog';
316     }
317
318     isDirty(): boolean {
319         return this.fmEditForm ? this.fmEditForm.dirty : false;
320     }
321
322     // DEPRECATED: This is a duplicate of this.record = abc;
323     setRecord(record: IdlObject) {
324         console.warn('fm-editor:setRecord() is deprecated. ' +
325             'Use editor.record = abc or [record]="abc" instead');
326         this.record = record; // this calls the setter
327     }
328
329     // Translate comma-separated string versions of various inputs
330     // to arrays.
331     private listifyInputs() {
332         if (this.hiddenFields) {
333             this.hiddenFieldsList = this.hiddenFields.split(/,/);
334         }
335         if (this.readonlyFields) {
336             this.readonlyFieldsList = this.readonlyFields.split(/,/);
337         }
338         if (this.requiredFields) {
339             this.requiredFieldsList = this.requiredFields.split(/,/);
340         }
341         if (this.datetimeFields) {
342             this.datetimeFieldsList = this.datetimeFields.split(/,/);
343         }
344         if (this.orgDefaultAllowed) {
345             this.orgDefaultAllowedList = this.orgDefaultAllowed.split(/,/);
346         }
347     }
348
349     private initRecord(): Promise<any> {
350
351         const pc = this.idlDef.permacrud || {};
352         this.modePerms = {
353             view:   pc.retrieve ? pc.retrieve.perms : [],
354             create: pc.create ? pc.create.perms : [],
355             update: pc.update ? pc.update.perms : [],
356         };
357
358         this.pkeyIsEditable = !('pkey_sequence' in this.idlDef);
359
360         if (this.mode === 'update' || this.mode === 'view') {
361
362             let promise;
363             if (this.record && this.recordId === null) {
364                 promise = Promise.resolve(this.record);
365             } else if (this.recordId) {
366                 promise =
367                     this.pcrud.retrieve(this.idlClass, this.recordId).toPromise();
368             } else {
369                 // Not enough data yet to fetch anything
370                 return Promise.resolve();
371             }
372
373             return promise.then(rec => {
374
375                 if (!rec) {
376                     return Promise.reject(`No '${this.idlClass}'
377                         record found with id ${this.recordId}`);
378                 }
379
380                 // Set this._record (not this.record) to avoid loop in initRecord()
381                 this._record = rec;
382                 this.convertDatatypesToJs();
383                 return this.getFieldList();
384             });
385         }
386
387         // In 'create' mode.
388         //
389         // Create a new record from the stub record provided by the
390         // caller or a new from-scratch record
391         if (!this.record) {
392             // NOTE: Set this._record (not this.record) to avoid
393             // loop in initRecord()
394             if (this.defaultNewRecord) {
395                 // Clone to avoid polluting the stub record
396                 this._record = this.idl.clone(this.defaultNewRecord);
397             } else {
398                 this._record = this.idl.create(this.idlClass);
399             }
400         }
401         this._recordId = null; // avoid future confusion
402
403         return this.getFieldList();
404     }
405
406     // Modifies the FM record in place, replacing IDL-compatible values
407     // with native JS values.
408     private convertDatatypesToJs() {
409         this.idlDef.fields.forEach(field => {
410             if (field.datatype === 'bool') {
411                 if (this.record[field.name]() === 't') {
412                     this.record[field.name](true);
413                 } else if (this.record[field.name]() === 'f') {
414                     this.record[field.name](false);
415                 }
416             }
417         });
418     }
419
420     // Modifies the provided FM record in place, replacing JS values
421     // with IDL-compatible values.
422     convertDatatypesToIdl(rec: IdlObject) {
423         const fields = this.idlDef.fields.filter(f => !f.virtual);
424
425         fields.forEach(field => {
426             if (field.datatype === 'bool') {
427                 if (rec[field.name]() === true) {
428                     rec[field.name]('t');
429                 // } else if (rec[field.name]() === false) {
430                 } else { // TODO: some bools can be NULL
431                     rec[field.name]('f');
432                 }
433             } else if (field.datatype === 'org_unit') {
434                 const org = rec[field.name]();
435                 if (org && typeof org === 'object') {
436                     rec[field.name](org.id());
437                 }
438             }
439         });
440     }
441
442     private flattenLinkedValues(field: any, list: IdlObject[]): ComboboxEntry[] {
443         const class_ = field.class;
444         const fieldOptions = this.fieldOptions[field.name] || {};
445         const idField = this.idl.classes[class_].pkey;
446
447         const selector = fieldOptions.linkedSearchField
448             || this.idl.getClassSelector(class_) || idField;
449
450         return list.map(item => {
451             if (item !== undefined) {
452                 return {id: item[idField](), label: this.getFmRecordLabel(field, selector, item)};
453             }
454         });
455     }
456
457     private getFmRecordLabel(field: any, selector: string, fm: IdlObject): string {
458         // for now, need to keep in sync with getFmRecordLabel in combobox
459         // alternatively, have fm-edit not wire-up the combobox's data source for it
460         switch (field.class) {
461             case 'acmc':
462                 return fm.course_number() + ': ' + fm.name();
463                 break;
464             case 'acqf':
465                 return fm.code() + ' (' + fm.year() + ')'
466                        + ' (' + this.getOrgShortname(fm.org()) + ')';
467                 break;
468             case 'acpl':
469                 return fm.name() + ' (' + this.getOrgShortname(fm.owning_lib()) + ')';
470                 break;
471             default:
472                 // no equivalent of idlIncludeLibraryInLabel yet
473                 return fm[selector]();
474         }
475     }
476     getOrgShortname(ou: any) {
477         if (typeof ou === 'object') {
478             return ou.shortname();
479         } else {
480             return this.org.get(ou).shortname();
481         }
482     }
483
484     private getFieldList(): Promise<any> {
485
486         const fields = this.idlDef.fields.filter(f =>
487             !f.virtual && !this.hiddenFieldsList.includes(f.name));
488
489         // Wait for all network calls to complete
490         return Promise.all(
491             fields.map(field => this.constructOneField(field))
492
493         ).then(() => {
494             const order = this.fieldOrder ? this.fieldOrder.split(/,/) : [];
495             this.fields = this.idl.sortIdlFields(fields, order);
496         });
497     }
498
499     private constructOneField(field: any): Promise<any> {
500
501         let promise = null;
502         const fieldOptions = this.fieldOptions[field.name] || {};
503
504         if (this.mode === 'view') {
505             field.readOnly = true;
506         } else if (fieldOptions.isReadonlyOverride) {
507             field.readOnly =
508                 !fieldOptions.isReadonlyOverride(field.name, this.record);
509         } else {
510             field.readOnly = fieldOptions.isReadonly === true
511                 || this.readonlyFieldsList.includes(field.name);
512         }
513
514         if (fieldOptions.isRequiredOverride) {
515             field.isRequired = () => {
516                 return fieldOptions.isRequiredOverride(field.name, this.record);
517             };
518         } else {
519             field.isRequired = () => {
520                 return field.required
521                     || fieldOptions.isRequired
522                     || this.requiredFieldsList.includes(field.name);
523             };
524         }
525
526         if (fieldOptions.customTemplate) {
527             field.template = fieldOptions.customTemplate.template;
528             field.context = fieldOptions.customTemplate.context;
529         } else if (fieldOptions.customValues) {
530
531             field.linkedValues = fieldOptions.customValues;
532
533         } else if (field.datatype === 'link' && field.readOnly) {
534
535             // no need to fetch all possible values for read-only fields
536             const idToFetch = this.record[field.name]();
537
538             if (idToFetch) {
539
540                 // If the linked class defines a selector field, fetch the
541                 // linked data so we can display the data within the selector
542                 // field.  Otherwise, avoid the network lookup and let the
543                 // bare value (usually an ID) be displayed.
544                 const selector = fieldOptions.linkedSearchField ||
545                     this.idl.getClassSelector(field.class);
546
547                 if (selector && selector !== field.name) {
548                     promise = this.pcrud.retrieve(field.class, idToFetch)
549                         .toPromise().then(list => {
550                             field.linkedValues =
551                                 this.flattenLinkedValues(field, Array(list));
552                         });
553                 } else {
554                     // No selector, display the raw id/key value.
555                     field.linkedValues = [{id: idToFetch, name: idToFetch}];
556                 }
557             }
558
559         } else if (field.datatype === 'link') {
560
561             if (fieldOptions.linkedSearchConditions) {
562                 field.idlBaseQuery = fieldOptions.linkedSearchConditions;
563             }
564             field.selector = fieldOptions.linkedSearchField ||
565                              this.idl.getClassSelector(field.class);
566
567         } else if (field.datatype === 'timestamp') {
568             field.datetime = this.datetimeFieldsList.includes(field.name);
569         } else if (field.datatype === 'org_unit') {
570             field.orgDefaultAllowed =
571                 this.orgDefaultAllowedList.includes(field.name);
572         }
573
574         if (fieldOptions.helpText) {
575             field.helpText = fieldOptions.helpText;
576             field.helpText.current().then(help => field.helpTextValue = help);
577         }
578
579         if (fieldOptions.min) {
580             field.min = Number(fieldOptions.min);
581         }
582         if (fieldOptions.max) {
583             field.max = Number(fieldOptions.max);
584         }
585
586         return promise || Promise.resolve();
587     }
588
589     // Returns a context object to be inserted into a custom
590     // field template.
591     customTemplateFieldContext(fieldDef: any): CustomFieldContext {
592         return Object.assign(
593             {   record : this.record,
594                 field: fieldDef // from this.fields
595             },  fieldDef.context || {}
596         );
597     }
598
599     save() {
600         const recToSave = this.idl.clone(this.record);
601         if (this.preSave) {
602             this.preSave(this.mode, recToSave);
603         }
604         this.convertDatatypesToIdl(recToSave);
605         this.pcrud[this.mode]([recToSave]).toPromise().then(
606             result => {
607                 this.recordSaved.emit(result);
608                 if (this.fmEditForm) {
609                     this.fmEditForm.form.markAsPristine();
610                 }
611                 this.successStr.current().then(msg => this.toast.success(msg));
612                 if (this.isDialog()) { this.record = undefined; this.close(result); }
613             },
614             error => {
615                 this.recordError.emit(error);
616                 this.failStr.current().then(msg => this.toast.warning(msg));
617                 if (this.isDialog() && !this.remainOpenOnError) { this.error(error); }
618             }
619         );
620     }
621
622     remove() {
623         this.confirmDel.open().subscribe(confirmed => {
624             if (!confirmed) { return; }
625             const recToRemove = this.idl.clone(this.record);
626             this.pcrud.remove(recToRemove).toPromise().then(
627                 result => {
628                     this.recordDeleted.emit(result);
629                     this.successStr.current().then(msg => this.toast.success(msg));
630                     if (this.isDialog()) { this.close(result); }
631                 },
632                 error => {
633                     this.recordError.emit(error);
634                     this.failStr.current().then(msg => this.toast.warning(msg));
635                     if (this.isDialog() && !this.remainOpenOnError) { this.error(error); }
636                 }
637             );
638         });
639     }
640
641     cancel() {
642         this.recordCanceled.emit(this.record);
643         this.record = undefined;
644         this.close();
645     }
646
647     closeEditor() {
648         this.record = undefined;
649         this.close();
650     }
651
652     // Returns a string describing the type of input to display
653     // for a given field.  This helps cut down on the if/else
654     // nesti-ness in the template.  Each field will match
655     // exactly one type.
656     inputType(field: any): string {
657
658         if (field.template) {
659             return 'template';
660         }
661
662         if ( field.datatype === 'timestamp' && field.datetime ) {
663             return 'timestamp-timepicker';
664         }
665
666         // Some widgets handle readOnly for us.
667         if (   field.datatype === 'timestamp'
668             || field.datatype === 'org_unit'
669             || field.datatype === 'bool') {
670             return field.datatype;
671         }
672
673         if (field.readOnly) {
674             if (field.datatype === 'money') {
675                 return 'readonly-money';
676             }
677
678             if (field.datatype === 'link' && field.class === 'au') {
679                 return 'readonly-au';
680             }
681
682             if (field.datatype === 'link' || field.linkedValues) {
683                 return 'readonly-list';
684             }
685
686             return 'readonly';
687         }
688
689         if (field.datatype === 'id' && !this.pkeyIsEditable) {
690             return 'readonly';
691         }
692
693         if (   field.datatype === 'int'
694             || field.datatype === 'float'
695             || field.datatype === 'money') {
696             return field.datatype;
697         }
698
699         if (field.datatype === 'link') {
700             return 'link';
701         }
702
703         if (field.linkedValues) {
704             return 'list';
705         }
706
707         // datatype == text / interval / editable-pkey
708         return 'text';
709     }
710
711     openTranslator(field: string) {
712         this.translator.fieldName = field;
713         this.translator.idlObject = this.record;
714
715         this.translator.open().subscribe(
716             newValue => {
717                 if (newValue) {
718                     this.record[field](newValue);
719                 }
720             }
721         );
722     }
723 }
724
725 // https://stackoverflow.com/a/57812865
726 @Directive({
727     selector: 'input[type=number][egMin][formControlName],input[type=number][egMin][formControl],input[type=number][egMin][ngModel]',
728     providers: [{ provide: NG_VALIDATORS, useExisting: MinValidatorDirective, multi: true }]
729 })
730 export class MinValidatorDirective implements Validator {
731     @HostBinding('attr.egMin') @Input() egMin: number;
732
733     constructor() { }
734
735     validate(control: AbstractControl): ValidationErrors | null {
736         const validator = Validators.min(this.egMin);
737         return validator(control);
738     }
739 }
740 @Directive({
741     selector: 'input[type=number][egMax][formControlName],input[type=number][egMax][formControl],input[type=number][egMax][ngModel]',
742     providers: [{ provide: NG_VALIDATORS, useExisting: MaxValidatorDirective, multi: true }]
743 })
744 export class MaxValidatorDirective implements Validator {
745     @HostBinding('attr.egMax') @Input() egMax: number;
746
747     constructor() { }
748
749     validate(control: AbstractControl): ValidationErrors | null {
750         const validator = Validators.max(this.egMax);
751         return validator(control);
752     }
753 }