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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 3× 2× 6× 3× 3× 3× 3× 3× 3× 1× 1× 3× 1× 2× 3× 2× 2× 1× 6× 3× 2× 1× 1× 1× 2× 3× 9× 3× 3× 3× 3× 3× 3× 3× 3× 3× 3× 3× 3× 1× 1× 3× 1× 1× 3× 3× 1× 1× 1× 13× 3× 3× 3× 3× 1× 3× 3× 3× 3× 1× 1× 1× 1× 3× 1× | /* * @license * Copyright Hôpitaux Universitaires de Genève. All Rights Reserved. * * Use of this source code is governed by an Apache-2.0 license that can be * found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE */ import { FocusMonitor } from '@angular/cdk/a11y'; import { coerceBooleanProperty } from '@angular/cdk/coercion'; import { ChangeDetectionStrategy, ChangeDetectorRef, Component, DoCheck, ElementRef, EventEmitter, HostBinding, Input, OnChanges, OnDestroy, Optional, Output, Self, ViewChild } from '@angular/core'; import { ControlValueAccessor, FormControl, FormGroupDirective, NgControl, NgForm, ValidationErrors, Validator, ValidatorFn } from '@angular/forms'; import { _MatInputMixinBase, CanUpdateErrorState, ErrorStateMatcher, MatFormFieldControl } from '@angular/material'; import { Subject } from 'rxjs'; import { DejaTextMetricsService } from '../../common/core/text-metrics/text-metrics.service'; import { DejaChildValidatorDirective } from '../../common/core/validation/child-validator.directive'; export const createCounterRangeValidator = (maxValue: number, minValue: number) => { return (c: FormControl) => { const err = { rangeError: { given: c.value, max: maxValue, min: minValue } }; return ((maxValue && c.value > maxValue) || (minValue && c.value < minValue)) ? err : null; }; }; @Component({ changeDetection: ChangeDetectionStrategy.OnPush, providers: [{ provide: MatFormFieldControl, useExisting: DejaNumericStepperComponent }], selector: 'deja-numeric-stepper', styles: [require('./numeric-stepper.component.scss')], template: require('./numeric-stepper.component.html') }) export class DejaNumericStepperComponent extends _MatInputMixinBase implements CanUpdateErrorState, DoCheck, OnChanges, OnDestroy, ControlValueAccessor, MatFormFieldControl<number>, Validator { public static nextId = 0; @HostBinding() public id = `deja-numeric-stepper-${DejaNumericStepperComponent.nextId++}`; @HostBinding('class.floating') public get shouldLabelFloat() { return this.focused || !this.empty || this.alwaysDisplayUnit; } @HostBinding('attr.aria-describedby') public describedBy = ''; public controlType = 'deja-numeric-stepper'; public errorState = false; public size = 0; public stateChanges = new Subject<void>(); public focused = false; /** Max value of stepper */ @Input() public max: number; /** Min value of stepper */ @Input() public min: number; /** Step for stepper : default 1 */ @Input() public step = 1; /** Unit of stepper */ @Input() public unit: string; @ViewChild(DejaChildValidatorDirective) public set inputValidatorDirective(value: DejaChildValidatorDirective) { Iif (value) { value.parentControl = this.ngControl; } } /** * Placeholder of the input */ @Input() public get placeholder() { return this._placeholder; } public set placeholder(plh) { this._placeholder = plh; this.stateChanges.next(); } private _placeholder: string; /** unit always visible */ @Input() public get alwaysDisplayUnit() { return this._alwaysDisplayUnit; } public set alwaysDisplayUnit(value) { this._alwaysDisplayUnit = coerceBooleanProperty(value); } private _alwaysDisplayUnit: boolean; public get empty(): boolean { return !this._value && this._value !== 0; } private _value: number; /** Function for min / max validation */ public validateFn: ValidatorFn; /** Output to get the event when the value is modified (no validation) */ @Output() public textChange: EventEmitter<number> = new EventEmitter<number>(); /** Allow to disabled the component */ @Input() public set disabled(value) { const disabled = coerceBooleanProperty(value); this._disabled = disabled || null; this.changeDetectorRef.markForCheck(); } @Input() public get required() { return this._required; } public set required(req) { this._required = coerceBooleanProperty(req); this.stateChanges.next(); } private _required = false; /** * Get disable value */ public get disabled() { return this.ngControl ? this.ngControl.disabled : this._disabled; } @HostBinding('attr.disabled') private _disabled: boolean = null; // NgModel implementation protected onTouchedCallback: () => void = () => { }; protected onChangeCallback: (_: any) => void = () => { }; protected onValidatorChangeCallback: () => void = () => { }; constructor( public dejaTextMetricsService: DejaTextMetricsService, private elementRef: ElementRef, private changeDetectorRef: ChangeDetectorRef, @Self() @Optional() public ngControl: NgControl, private fm: FocusMonitor, @Optional() _parentForm: NgForm, @Optional() _parentFormGroup: FormGroupDirective, _defaultErrorStateMatcher: ErrorStateMatcher, ) { super(_defaultErrorStateMatcher, _parentForm, _parentFormGroup, ngControl); Iif (this.ngControl) { this.ngControl.valueAccessor = this; } this.fm.monitor(elementRef.nativeElement, true).subscribe((origin) => { this.focused = !!origin; if (!this.focused) { this.onTouchedCallback(); } this.stateChanges.next(); }); } public ngOnChanges(changes: any) { if (changes.min || changes.max) { this.validateFn = createCounterRangeValidator(this.max, this.min); if (this.ngControl && this.ngControl.control) { this.ngControl.control.setValidators(this.validateFn); } } } public ngDoCheck() { Iif (this.ngControl) { // We need to re-evaluate this on every change detection cycle, because there are some // error triggers that we can't subscribe to (e.g. parent form submissions). This means // that whatever logic is in here has to be super lean or we risk destroying the performance. this.updateErrorState(); } } public validate(c: FormControl): ValidationErrors { return this.validateFn(c); } public ngOnDestroy() { this.stateChanges.complete(); this.fm.stopMonitoring(this.elementRef.nativeElement); } public setDescribedByIds(ids: string[]) { this.describedBy = ids.join(' '); } public onContainerClick(event: MouseEvent) { if ((event.target as Element).tagName.toLowerCase() !== 'input') { this.elementRef.nativeElement.querySelector('input').focus(); } } // ************* ControlValueAccessor Implementation ************** public get value() { return this._value; } public set value(val: number) { this.writeValue(val); this.onChangeCallback(val); this.onTouchedCallback(); this.stateChanges.next(); } public writeValue(value: number) { this._value = isNaN(value) ? null : +value; this.checkSize(value); this.changeDetectorRef.markForCheck(); this.textChange.emit(value); } public registerOnChange(fn: any) { this.onChangeCallback = fn; } public registerOnTouched(fn: any) { this.onTouchedCallback = fn; } public setDisabledState(isDisabled: boolean) { this.disabled = isDisabled; } // ************* End of ControlValueAccessor Implementation ************** public checkSize(value?: number) { this.size = this.dejaTextMetricsService.getTextWidth((value || this.value || 0).toString(), this.elementRef.nativeElement); } } |