-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathtimepicker.component.ts
418 lines (355 loc) · 10.7 KB
/
timepicker.component.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
EventEmitter,
Inject,
Input,
LOCALE_ID,
OnChanges,
OnInit,
Output,
SimpleChanges,
ViewChild,
} from '@angular/core';
import { Observable, Subject } from 'rxjs';
import { convertToBoolProperty, NbBooleanInput } from '../helpers';
import { NbPortalDirective } from '../cdk/overlay/mapping';
import { NbPlatform } from '../cdk/platform/platform-service';
import { NbDateService, NbDayPeriod } from '../calendar-kit/services/date.service';
import { range, rangeFromTo } from '../calendar-kit/helpers';
import { NbCalendarTimeModelService } from '../calendar-kit/services/calendar-time-model.service';
import {
NB_DEFAULT_TIMEPICKER_LOCALIZATION_CONFIG,
NB_TIME_PICKER_CONFIG,
NbSelectedTimePayload,
NbTimePickerConfig,
} from './model';
interface NbTimePartOption {
value: number;
text: string;
}
/**
* The TimePicker components itself.
* Provides a proxy to `TimePicker` options as well as custom picker options.
*/
@Component({
selector: 'nb-timepicker',
templateUrl: './timepicker.component.html',
styleUrls: ['./timepicker.component.scss'],
exportAs: 'nbTimepicker',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class NbTimePickerComponent<D> implements OnChanges {
protected blur$: Subject<void> = new Subject<void>();
fullTimeOptions: D[];
hoursColumnOptions: NbTimePartOption[];
minutesColumnOptions: NbTimePartOption[];
secondsColumnOptions: NbTimePartOption[];
readonly dayPeriodColumnOptions = [NbDayPeriod.AM, NbDayPeriod.PM];
hostRef: ElementRef;
isAM = true;
timepickerFormatChange$: Subject<void> = new Subject();
/**
* Emits when timepicker looses focus.
*/
get blur(): Observable<void> {
return this.blur$.asObservable();
}
/**
* Defines time format string.
* */
@Input()
get timeFormat(): string {
return this._timeFormat;
}
set timeFormat(timeFormat: string) {
this._timeFormat = timeFormat;
}
protected _timeFormat: string;
computedTimeFormat: string = this.setupTimeFormat();
/**
* Defines 12 hours format .
* */
@Input()
get twelveHoursFormat(): boolean {
return this._twelveHoursFormat;
}
set twelveHoursFormat(value: boolean) {
this._twelveHoursFormat = convertToBoolProperty(value);
}
protected _twelveHoursFormat: boolean;
static ngAcceptInputType_twelveHoursFormat: NbBooleanInput;
/**
* Defines should show am/pm label if twelveHoursFormat enabled.
* */
@Input()
get showAmPmLabel(): boolean {
return this._showAmPmLabel;
}
set showAmPmLabel(value: boolean) {
this._showAmPmLabel = convertToBoolProperty(value);
}
protected _showAmPmLabel: boolean = true;
static ngAcceptInputType_showAmPmLabel: NbBooleanInput;
/**
* Show seconds in timepicker.
* Ignored when singleColumn is true
* */
@Input()
get withSeconds(): boolean {
return this._withSeconds;
}
set withSeconds(value: boolean) {
this._withSeconds = convertToBoolProperty(value);
}
protected _withSeconds: boolean;
static ngAcceptInputType_withSeconds: NbBooleanInput;
/**
* Show timepicker values in one column with 60 minutes step by default.
* */
@Input()
get singleColumn(): boolean {
return this._singleColumn;
}
set singleColumn(value: boolean) {
this._singleColumn = convertToBoolProperty(value);
}
_singleColumn: boolean;
static ngAcceptInputType_singleColumn: NbBooleanInput;
/**
* Defines minutes offset for options, when timepicker is in single column mode.
* By default it’s 60 minutes: '12:00, 13:00: 14:00, 15:00...'
* */
@Input()
set step(step: number) {
this._step = step;
}
get step(): number {
return this._step;
}
protected _step: number;
/**
* Date which will be rendered as selected.
* */
@Input()
set date(date: D) {
this._date = date;
this.isAM = this.dateService.getDayPeriod(this.date) === NbDayPeriod.AM;
this.buildColumnOptions();
this.cd.markForCheck();
}
get date(): D {
return this._date;
}
_date: D;
/**
* Minimum available date for selection.
* */
@Input() min: D;
/**
* Maximum available date for selection.
* */
@Input() max: D;
/**
* In timepicker value should be always true
* In calendar-with-time.component should set to false
* @docs-private
*/
@Input() showFooter: boolean = true;
@Input() applyButtonText: string;
@Input() hoursText: string;
@Input() minutesText: string;
@Input() secondsText: string;
@Input() ampmText: string;
@Input() currentTimeButtonText: string;
/**
* Emits date when selected.
* */
@Output() onSelectTime: EventEmitter<NbSelectedTimePayload<D>> = new EventEmitter<NbSelectedTimePayload<D>>();
@ViewChild(NbPortalDirective, { static: true }) portal: NbPortalDirective;
constructor(
@Inject(NB_TIME_PICKER_CONFIG) protected config: NbTimePickerConfig,
protected platformService: NbPlatform,
@Inject(LOCALE_ID) locale: string,
public cd: ChangeDetectorRef,
protected calendarTimeModelService: NbCalendarTimeModelService<D>,
protected dateService: NbDateService<D>,
) {
this.initFromConfig(this.config);
}
ngOnChanges({ step, twelveHoursFormat, withSeconds, singleColumn }: SimpleChanges): void {
const nextTimeFormat = this.setupTimeFormat();
if (nextTimeFormat !== this.computedTimeFormat) {
this.computedTimeFormat = nextTimeFormat;
this.timepickerFormatChange$.next();
}
const isConfigChanged = step || twelveHoursFormat || withSeconds || singleColumn;
if (isConfigChanged || !this.fullTimeOptions) {
this.buildColumnOptions();
}
}
setHost(hostRef: ElementRef): void {
this.hostRef = hostRef;
}
attach(hostRef: ElementRef): void {
this.hostRef = hostRef;
}
setCurrentTime(): void {
this.date = this.dateService.today();
this.onSelectTime.emit({
time: this.date,
save: true,
});
}
setHour(value: number): void {
this.updateValue(this.dateService.setHours(this.date, value));
}
setMinute(value: number): void {
this.updateValue(this.dateService.setMinutes(this.date, value));
}
setSecond(value: number): void {
this.updateValue(this.dateService.setSeconds(this.date, value));
}
selectFullTime(value: D): void {
this.updateValue(value);
}
changeDayPeriod(dayPeriodToSet: NbDayPeriod): void {
if (this.dateService.getDayPeriod(this.date) === dayPeriodToSet) {
return;
}
// Subtract hours when switching to AM (before midday, 0-11 in 24-hour) from PM (after midday, 12-24 in 24-hour),
// otherwise add hours because switching to PM from AM.
const direction = dayPeriodToSet === NbDayPeriod.AM ? -1 : 1;
const increment = direction * this.dateService.HOURS_IN_DAY_PERIOD;
this.updateValue(this.dateService.addHours(this.date, increment));
}
updateValue(date: D): void {
this.onSelectTime.emit({ time: date });
}
saveValue(): void {
this.onSelectTime.emit({
time: this.date,
save: true,
});
}
trackByTimeValues(index, item: NbTimePartOption): number {
return item.value;
}
trackBySingleColumnValue(index, item: D) {
return this.dateService.valueOf(item);
}
trackByDayPeriod(index, item: NbDayPeriod): string {
return item;
}
showSeconds(): boolean {
return this.withSeconds && !this.singleColumn;
}
isSelectedHour(val: number): boolean {
if (this.date) {
return this.dateService.getHours(this.date) === val;
}
return false;
}
isSelectedMinute(val: number): boolean {
if (this.date) {
return this.dateService.getMinutes(this.date) === val;
}
return false;
}
isSelectedSecond(val: number): boolean {
if (this.date) {
return this.dateService.getSeconds(this.date) === val;
}
return false;
}
isSelectedDayPeriod(dayPeriod: NbDayPeriod): boolean {
if (this.date) {
return dayPeriod === this.dateService.getDayPeriod(this.date);
}
return false;
}
getFullTimeString(item: D): string {
return this.dateService.format(item, this.computedTimeFormat).toUpperCase();
}
isSelectedFullTimeValue(value: D): boolean {
if (this.date) {
return this.dateService.isSameHourAndMinute(value, this.date);
}
return false;
}
getValidatorConfig() {
return { min: this.min, max: this.max };
}
protected buildColumnOptions(): void {
this.fullTimeOptions = this.singleColumn ? this.calendarTimeModelService.getHoursRange(this.step) : [];
this.hoursColumnOptions = this.generateHours();
this.minutesColumnOptions = this.generateMinutesOrSeconds();
this.secondsColumnOptions = this.showSeconds() ? this.generateMinutesOrSeconds() : [];
}
/**
* @docs-private
*/
isFirefox(): boolean {
return this.platformService.FIREFOX;
}
protected generateHours(): NbTimePartOption[] {
if (!this.twelveHoursFormat) {
return range(24, (v: number) => {
return { value: v, text: this.calendarTimeModelService.paddToTwoSymbols(v) };
});
}
if (this.isAM) {
return range(12, (v: number) => {
const text = v === 0 ? 12 : v;
return { value: v, text: this.calendarTimeModelService.paddToTwoSymbols(text) };
});
}
return rangeFromTo(12, 24, (v: number) => {
const text = v === 12 ? 12 : v - 12;
return { value: v, text: this.calendarTimeModelService.paddToTwoSymbols(text) };
});
}
protected generateMinutesOrSeconds(): NbTimePartOption[] {
return range(60, (v: number) => {
return { value: v, text: this.calendarTimeModelService.paddToTwoSymbols(v) };
});
}
protected setupTimeFormat(): string {
if (!this.timeFormat) {
return this.config.format || this.buildTimeFormat();
}
return this.timeFormat;
}
/**
* @docs-private
*/
buildTimeFormat(): string {
if (this.twelveHoursFormat) {
return `${
this.withSeconds && !this.singleColumn
? this.dateService.getTwelveHoursFormatWithSeconds()
: this.dateService.getTwelveHoursFormat()
}`;
} else {
return `${
this.withSeconds && !this.singleColumn
? this.dateService.getTwentyFourHoursFormatWithSeconds()
: this.dateService.getTwentyFourHoursFormat()
}`;
}
}
protected initFromConfig(config: NbTimePickerConfig) {
if (config) {
this.twelveHoursFormat = config.twelveHoursFormat;
} else {
this.twelveHoursFormat = this.dateService.getLocaleTimeFormat().includes('h');
}
const localeConfig = { ...NB_DEFAULT_TIMEPICKER_LOCALIZATION_CONFIG, ...(config?.localization ?? {}) };
this.hoursText = localeConfig.hoursText;
this.minutesText = localeConfig.minutesText;
this.secondsText = localeConfig.secondsText;
this.ampmText = localeConfig.ampmText;
}
}