/
timepicker.ts
313 lines (278 loc) · 12.3 KB
/
timepicker.ts
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import {
ChangeDetectorRef,
Component,
forwardRef,
Input,
OnChanges,
SimpleChanges,
ViewEncapsulation
} from '@angular/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
import {isInteger, isNumber, padNumber, toInteger} from '../util/util';
import {NgbTime} from './ngb-time';
import {NgbTimepickerConfig} from './timepicker-config';
import {NgbTimeAdapter} from './ngb-time-adapter';
const NGB_TIMEPICKER_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => NgbTimepicker),
multi: true
};
/**
* A directive that helps with wth picking hours, minutes and seconds.
*/
@Component({
selector: 'ngb-timepicker',
encapsulation: ViewEncapsulation.None,
styleUrls: ['./timepicker.scss'],
template: `
<fieldset [disabled]="disabled" [class.disabled]="disabled">
<div class="ngb-tp">
<div class="ngb-tp-input-container ngb-tp-hour">
<button *ngIf="spinners" tabindex="-1" type="button" (click)="changeHour(hourStep)"
class="btn btn-link" [class.btn-sm]="isSmallSize" [class.btn-lg]="isLargeSize" [class.disabled]="disabled"
[disabled]="disabled">
<span class="chevron ngb-tp-chevron"></span>
<span class="sr-only" i18n="@@ngb.timepicker.increment-hours">Increment hours</span>
</button>
<input type="text" class="ngb-tp-input form-control" [class.form-control-sm]="isSmallSize"
[class.form-control-lg]="isLargeSize"
maxlength="2" inputmode="numeric" placeholder="HH" i18n-placeholder="@@ngb.timepicker.HH"
[value]="formatHour(model?.hour)" (change)="updateHour($event.target.value)"
[readOnly]="readonlyInputs" [disabled]="disabled" aria-label="Hours" i18n-aria-label="@@ngb.timepicker.hours"
(keydown)="_filter($event)"
(keydown.ArrowUp)="changeHour(hourStep); $event.preventDefault()"
(keydown.ArrowDown)="changeHour(-hourStep); $event.preventDefault()">
<button *ngIf="spinners" tabindex="-1" type="button" (click)="changeHour(-hourStep)"
class="btn btn-link" [class.btn-sm]="isSmallSize" [class.btn-lg]="isLargeSize" [class.disabled]="disabled"
[disabled]="disabled">
<span class="chevron ngb-tp-chevron bottom"></span>
<span class="sr-only" i18n="@@ngb.timepicker.decrement-hours">Decrement hours</span>
</button>
</div>
<div class="ngb-tp-spacer">:</div>
<div class="ngb-tp-input-container ngb-tp-minute">
<button *ngIf="spinners" tabindex="-1" type="button" (click)="changeMinute(minuteStep)"
class="btn btn-link" [class.btn-sm]="isSmallSize" [class.btn-lg]="isLargeSize" [class.disabled]="disabled"
[disabled]="disabled">
<span class="chevron ngb-tp-chevron"></span>
<span class="sr-only" i18n="@@ngb.timepicker.increment-minutes">Increment minutes</span>
</button>
<input type="text" class="ngb-tp-input form-control" [class.form-control-sm]="isSmallSize" [class.form-control-lg]="isLargeSize"
maxlength="2" inputmode="numeric" placeholder="MM" i18n-placeholder="@@ngb.timepicker.MM"
[value]="formatMinSec(model?.minute)" (change)="updateMinute($event.target.value)"
[readOnly]="readonlyInputs" [disabled]="disabled" aria-label="Minutes" i18n-aria-label="@@ngb.timepicker.minutes"
(keydown)="_filter($event)"
(keydown.ArrowUp)="changeMinute(minuteStep); $event.preventDefault()"
(keydown.ArrowDown)="changeMinute(-minuteStep); $event.preventDefault()">
<button *ngIf="spinners" tabindex="-1" type="button" (click)="changeMinute(-minuteStep)"
class="btn btn-link" [class.btn-sm]="isSmallSize" [class.btn-lg]="isLargeSize" [class.disabled]="disabled"
[disabled]="disabled">
<span class="chevron ngb-tp-chevron bottom"></span>
<span class="sr-only" i18n="@@ngb.timepicker.decrement-minutes">Decrement minutes</span>
</button>
</div>
<div *ngIf="seconds" class="ngb-tp-spacer">:</div>
<div *ngIf="seconds" class="ngb-tp-input-container ngb-tp-second">
<button *ngIf="spinners" tabindex="-1" type="button" (click)="changeSecond(secondStep)"
class="btn btn-link" [class.btn-sm]="isSmallSize" [class.btn-lg]="isLargeSize" [class.disabled]="disabled"
[disabled]="disabled">
<span class="chevron ngb-tp-chevron"></span>
<span class="sr-only" i18n="@@ngb.timepicker.increment-seconds">Increment seconds</span>
</button>
<input type="text" class="ngb-tp-input form-control" [class.form-control-sm]="isSmallSize" [class.form-control-lg]="isLargeSize"
maxlength="2" inputmode="numeric" placeholder="SS" i18n-placeholder="@@ngb.timepicker.SS"
[value]="formatMinSec(model?.second)" (change)="updateSecond($event.target.value)"
[readOnly]="readonlyInputs" [disabled]="disabled" aria-label="Seconds" i18n-aria-label="@@ngb.timepicker.seconds"
(keydown)="_filter($event)"
(keydown.ArrowUp)="changeSecond(secondStep); $event.preventDefault()"
(keydown.ArrowDown)="changeSecond(-secondStep); $event.preventDefault()">
<button *ngIf="spinners" tabindex="-1" type="button" (click)="changeSecond(-secondStep)"
class="btn btn-link" [class.btn-sm]="isSmallSize" [class.btn-lg]="isLargeSize" [class.disabled]="disabled"
[disabled]="disabled">
<span class="chevron ngb-tp-chevron bottom"></span>
<span class="sr-only" i18n="@@ngb.timepicker.decrement-seconds">Decrement seconds</span>
</button>
</div>
<div *ngIf="meridian" class="ngb-tp-spacer"></div>
<div *ngIf="meridian" class="ngb-tp-meridian">
<button type="button" class="btn btn-outline-primary" [class.btn-sm]="isSmallSize" [class.btn-lg]="isLargeSize"
[disabled]="disabled" [class.disabled]="disabled"
(click)="toggleMeridian()">
<ng-container *ngIf="model?.hour >= 12; else am" i18n="@@ngb.timepicker.PM">PM</ng-container>
<ng-template #am i18n="@@ngb.timepicker.AM">AM</ng-template>
</button>
</div>
</div>
</fieldset>
`,
providers: [NGB_TIMEPICKER_VALUE_ACCESSOR]
})
export class NgbTimepicker implements ControlValueAccessor,
OnChanges {
disabled: boolean;
model: NgbTime;
private _hourStep: number;
private _minuteStep: number;
private _secondStep: number;
/**
* Whether to display 12H or 24H mode.
*/
@Input() meridian: boolean;
/**
* If `true`, the spinners above and below inputs are visible.
*/
@Input() spinners: boolean;
/**
* If `true`, it is possible to select seconds.
*/
@Input() seconds: boolean;
/**
* The number of hours to add/subtract when clicking hour spinners.
*/
@Input()
set hourStep(step: number) {
this._hourStep = isInteger(step) ? step : this._config.hourStep;
}
get hourStep(): number { return this._hourStep; }
/**
* The number of minutes to add/subtract when clicking minute spinners.
*/
@Input()
set minuteStep(step: number) {
this._minuteStep = isInteger(step) ? step : this._config.minuteStep;
}
get minuteStep(): number { return this._minuteStep; }
/**
* The number of seconds to add/subtract when clicking second spinners.
*/
@Input()
set secondStep(step: number) {
this._secondStep = isInteger(step) ? step : this._config.secondStep;
}
get secondStep(): number { return this._secondStep; }
/**
* If `true`, the timepicker is readonly and can't be changed.
*/
@Input() readonlyInputs: boolean;
/**
* The size of inputs and buttons.
*/
@Input() size: 'small' | 'medium' | 'large';
constructor(
private readonly _config: NgbTimepickerConfig, private _ngbTimeAdapter: NgbTimeAdapter<any>,
private _cd: ChangeDetectorRef) {
this.meridian = _config.meridian;
this.spinners = _config.spinners;
this.seconds = _config.seconds;
this.hourStep = _config.hourStep;
this.minuteStep = _config.minuteStep;
this.secondStep = _config.secondStep;
this.disabled = _config.disabled;
this.readonlyInputs = _config.readonlyInputs;
this.size = _config.size;
}
onChange = (_: any) => {};
onTouched = () => {};
writeValue(value) {
const structValue = this._ngbTimeAdapter.fromModel(value);
this.model = structValue ? new NgbTime(structValue.hour, structValue.minute, structValue.second) : new NgbTime();
if (!this.seconds && (!structValue || !isNumber(structValue.second))) {
this.model.second = 0;
}
this._cd.markForCheck();
}
registerOnChange(fn: (value: any) => any): void { this.onChange = fn; }
registerOnTouched(fn: () => any): void { this.onTouched = fn; }
setDisabledState(isDisabled: boolean) { this.disabled = isDisabled; }
changeHour(step: number) {
this.model.changeHour(step);
this.propagateModelChange();
}
changeMinute(step: number) {
this.model.changeMinute(step);
this.propagateModelChange();
}
changeSecond(step: number) {
this.model.changeSecond(step);
this.propagateModelChange();
}
updateHour(newVal: string) {
const isPM = this.model.hour >= 12;
const enteredHour = toInteger(newVal);
if (this.meridian && (isPM && enteredHour < 12 || !isPM && enteredHour === 12)) {
this.model.updateHour(enteredHour + 12);
} else {
this.model.updateHour(enteredHour);
}
this.propagateModelChange();
}
updateMinute(newVal: string) {
this.model.updateMinute(toInteger(newVal));
this.propagateModelChange();
}
updateSecond(newVal: string) {
this.model.updateSecond(toInteger(newVal));
this.propagateModelChange();
}
toggleMeridian() {
if (this.meridian) {
this.changeHour(12);
}
}
formatHour(value: number) {
if (isNumber(value)) {
if (this.meridian) {
return padNumber(value % 12 === 0 ? 12 : value % 12);
} else {
return padNumber(value % 24);
}
} else {
return padNumber(NaN);
}
}
formatMinSec(value: number) { return padNumber(value); }
get isSmallSize(): boolean { return this.size === 'small'; }
get isLargeSize(): boolean { return this.size === 'large'; }
ngOnChanges(changes: SimpleChanges): void {
if (changes['seconds'] && !this.seconds && this.model && !isNumber(this.model.second)) {
this.model.second = 0;
this.propagateModelChange(false);
}
}
_filter(e: KeyboardEvent) {
// tslint:disable-next-line
const keyCode = e.keyCode;
const ctrlKey = e.ctrlKey;
const metaKey = e.metaKey;
const shiftKey = e.shiftKey;
if (
// Allow: Delete, Backspace, Tab, Escape, Enter
[46, 8, 9, 27, 13].indexOf(keyCode) !== -1 || (keyCode === 65 && ctrlKey === true) || // Allow: Ctrl+A
(keyCode === 67 && ctrlKey === true) || // Allow: Ctrl+C
(keyCode === 86 && ctrlKey === true) || // Allow: Ctrl+V
(keyCode === 88 && ctrlKey === true) || // Allow: Ctrl+X
(keyCode === 65 && metaKey === true) || // Cmd+A (Mac)
(keyCode === 67 && metaKey === true) || // Cmd+C (Mac)
(keyCode === 86 && metaKey === true) || // Cmd+V (Mac)
(keyCode === 88 && metaKey === true) || // Cmd+X (Mac)
(keyCode >= 35 && keyCode <= 39) // Home, End, Left, Right
) {
return; // let it happen, don't do anything
}
// Ensure that it is a number and stop the keypress
if ((shiftKey || keyCode < 48 || keyCode > 57) && (keyCode < 96 || keyCode > 105)) {
e.preventDefault();
}
}
private propagateModelChange(touched = true) {
if (touched) {
this.onTouched();
}
if (this.model.isValid(this.seconds)) {
this.onChange(
this._ngbTimeAdapter.toModel({hour: this.model.hour, minute: this.model.minute, second: this.model.second}));
} else {
this.onChange(this._ngbTimeAdapter.toModel(null));
}
}
}