-
Notifications
You must be signed in to change notification settings - Fork 7
/
datechooser.ts
554 lines (505 loc) · 17.5 KB
/
datechooser.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*
* Philip Crotwell
* University of South Carolina, 2019
* http://www.seis.sc.edu
*/
import {DateTime, Duration, Interval} from "luxon";
import {isoToDateTime, isDef} from "./util";
export const HOURMIN_ELEMENT = 'sp-hourmin';
export const DATETIME_ELEMENT = 'sp-datetime';
export const TIMERANGE_ELEMENT = 'sp-timerange';
export const hourMinRegEx = /^([0-1]?[0-9]):([0-5]?[0-9])$/;
export const HOUR_MIN_24 = "HH:mm";
export const START_LABEL = "startlabel";
export const DEFAULT_START_LABEL = "Start:";
export const END_LABEL = "endlabel";
export const DEFAULT_END_LABEL = "End:";
export const DUR_LABEL = "durLabel";
export const DEFAULT_DUR_LABEL = "Dur:";
/**
* Hour and Minute chooser.
* Use as '<sp-hourmin></sp-hourmin>'
*/
export class HourMinChooser extends HTMLElement {
_time: DateTime;
updateCallback: (time: DateTime) => void;
popupDiv: HTMLDivElement;
constructor() {
super();
const mythis = this;
this._time = DateTime.utc().set({second: 0, millisecond: 0});
const attr_date_time = this.getAttribute("date-time");
if (attr_date_time) {
this._time = isoToDateTime(attr_date_time);
this._time.set({second: 0, millisecond: 0}); // only hour and min?
}
this.updateCallback = function(time: DateTime) {};
const shadow = this.attachShadow({mode: 'open'});
// style
const style = document.createElement('style');
style.textContent = `
.hourminpopup {
position: absolute;
top: 17px;
box-shadow: 0 5px 15px -5px rgba(0,0,0,.5);
background-color: white;
z-index: 10;
}
.hidden {
visibility: hidden;
}
.shown {
visibility: visible;
}
.popupDivRel {
position: relative;
}
input.hourMin {
width: 3em;
}
`;
shadow.appendChild(style);
const wrapper = document.createElement('span');
document.addEventListener('click', e => {
//console.log("mouseleave wrapper");
mythis.hide();
});
const popupDiv = document.createElement('div');
this.popupDiv = popupDiv;
popupDiv.setAttribute("class", "hourminpopup hidden");
const hourDiv = popupDiv.appendChild(document.createElement('div'));
const hour_label = hourDiv.appendChild(document.createElement('label'));
hour_label.textContent = "Hour:";
const hour_slider = hourDiv.appendChild(document.createElement('input'));
hour_slider.setAttribute("type", "range");
hour_slider.setAttribute("min", "0");
hour_slider.setAttribute("max", "23");
hour_slider.value = `${this.time.hour}`;
hour_slider.setAttribute("class", "hourSlider");
hour_slider.oninput = function(e: Event) {
if (e.target !== null) {
const target = e.target as HTMLInputElement;
const hour = Number.parseInt(target.value);
if (!Number.isNaN(hour)) {
mythis.time = mythis.time.set({hour: hour});
}
}
};
const minDiv = popupDiv.appendChild(document.createElement('div'));
const min_label = minDiv.appendChild(document.createElement('label'));
min_label.textContent = "Min:";
const min_slider = minDiv.appendChild(document.createElement('input'));
min_slider.setAttribute("type", "range");
min_slider.setAttribute("min", "0");
min_slider.setAttribute("max", "59");
min_slider.value = `${this.time.minute}`;
min_slider.setAttribute("class", "minSlider");
min_slider.oninput = function(e: Event) {
if (e.target !== null) {
const target = e.target as HTMLInputElement;
const min = Number.parseInt(target.value);
if (!Number.isNaN(min)) {
mythis.time = mythis.time.set({minute: min});
}
}
};
const ntextSpan = wrapper.appendChild(document.createElement('span'));
const relDiv = ntextSpan.appendChild(document.createElement('span'));
relDiv.setAttribute("class", "popupDivRel");
relDiv.appendChild(popupDiv);
const ntext = ntextSpan.appendChild(document.createElement('input'));
ntext.setAttribute('type','text');
ntext.setAttribute('name','hourMin');
ntext.setAttribute('class','hourMin');
ntext.value = this.time.toFormat(HOUR_MIN_24);
ntext.onchange = function(e: Event) {
let val = ntext.value;
if (val === null ) {
val = mythis.time.toFormat(HOUR_MIN_24);
ntext.value = val;
}
const match = hourMinRegEx.exec(val);
if (match) {
//ntext.style("background-color", null);
const h = match[1];
const m = match[2];
const newTime = mythis.time.set({hour: parseInt(h), minute: parseInt(m)});
if (newTime !== mythis.time) {
mythis.time = newTime;
}
mythis.hide();
} else {
ntext.value = mythis.time.toFormat(HOUR_MIN_24);
}
};
ntext.onclick = function(e) {
e.stopPropagation();
mythis.showHide();
};
shadow.appendChild(wrapper);
}
/**
* Shows or hides the popup based on current visibility style
*/
showHide(): void {
if (this.popupDiv.getAttribute("class")?.includes("hidden")) {
this.show();
} else {
this.hide();
}
}
hide(): void {
if ( ! this.popupDiv.getAttribute("class")?.includes("hidden")) {
this.popupDiv.setAttribute("class", "hourminpopup hidden");
}
}
show(): void {
this.popupDiv.setAttribute("class", "hourminpopup visible");
this._adjustPopupPosition();
}
/** @private */
xxx_adjustPopupPosition(): void {
const left = 0;
const top = 0;
this.popupDiv.setAttribute("style", `{left: ${left} px; top: ${top} px;}`);
}
/** @private */
_adjustPopupPosition(): void {
const hourMinField = (this.shadowRoot?.querySelector('input.'+'hourMin') as HTMLInputElement);
const width = hourMinField.offsetWidth;
const height = hourMinField.offsetHeight;
const viewportWidth: number = window.innerWidth;
const viewportHeight: number = window.innerHeight;
const scrollTop: number = window.pageYOffset;
let left = hourMinField.offsetLeft;
let top = hourMinField.offsetTop + hourMinField.offsetHeight;
let parentField = hourMinField.offsetParent;
while (parentField !== null) {
if (parentField instanceof HTMLElement) {
left += parentField.offsetLeft;
top += parentField.offsetTop;
parentField = parentField.offsetParent;
}
}
// default position is bottom & left
if (left + width > viewportWidth) {
left = left - width + hourMinField.offsetWidth;
}
if (top + height > viewportHeight + scrollTop) {
top = top - height - hourMinField.offsetHeight;
}
console.log(`hourmin popup position: {left: ${left} px; top: ${top} px;}`);
this.popupDiv.setAttribute("style", `{position: absolute; left: ${left} px; top: ${top} px; }`);
}
get time(): DateTime {
return this._time;
}
set time(dt: DateTime) {
this._internalSetTime(dt);
this.updateCallback(this.time);
console.log(`dispatch change from HourMinChooser ${dt.toISO()}`)
this.dispatchEvent(new Event("change"));
}
_internalSetTime(dt: DateTime) {
this._time = dt;
const ntext = (this.shadowRoot?.querySelector('input.'+'hourMin') as HTMLInputElement);
ntext.value = this._time.toFormat(HOUR_MIN_24);
const hourSlider = (this.popupDiv?.querySelector('input.hourSlider') as HTMLInputElement);
hourSlider.value = `${this._time.hour}`;
const minuteSlider = (this.popupDiv?.querySelector('input.minSlider') as HTMLInputElement);
minuteSlider.value = `${this._time.minute}`;
}
}
customElements.define(HOURMIN_ELEMENT, HourMinChooser);
/**
* Date and Time chooser using native date chooser and the above
* HourMinChooser for the hour and minute of time.
*/
export class DateTimeChooser extends HTMLElement {
_time: DateTime;
updateCallback: (time: DateTime) => void;
hourMin: HourMinChooser;
constructor(time?: DateTime) {
super();
const mythis = this;
const attr_date_time = this.getAttribute("date-time");
if (time) {
this._time = time;
this.setAttribute("date-time", time.toISO());
} else if (attr_date_time) {
this._time = isoToDateTime(attr_date_time);
this._time.set({second: 0, millisecond: 0}); // only hour and min?
} else {
this._time = DateTime.utc().set({second: 0, millisecond: 0});
}
this.updateCallback = function(time: DateTime) {};
const shadow = this.attachShadow({mode: 'open'});
const wrapper = document.createElement('span');
const dateField = wrapper.appendChild(document.createElement('input'));
dateField.setAttribute('type','date');
dateField.setAttribute('name','date');
dateField.setAttribute('class','date');
dateField.value = this._time.toISODate();
const hourMin = wrapper.appendChild(new HourMinChooser());
hourMin._time = mythis.time;
this.hourMin = hourMin;
hourMin.addEventListener("change", () => {
const origTime = mythis._time;
const time = hourMin.time;
if (origTime !== time) {
mythis._internalSetTime(time);
mythis.timeModified();
}
});
dateField.addEventListener("change", () => {
const value = dateField.value;
const pikaValue = DateTime.fromISO(value);
const origTime = mythis._time;
if (
pikaValue && (
origTime.year !== pikaValue.year ||
origTime.month !== pikaValue.month ||
origTime.day !== pikaValue.day
)
) {
mythis.time = mythis.time.set({ year: pikaValue.year,
month: pikaValue.month,
day: pikaValue.day
});
mythis.timeModified();
}
});
shadow.appendChild(wrapper);
this._internalSetTime(this.time);
}
/**
* Updates the time without triggering the callback function.
*
* @param newTime new time to update sliders
*/
updateTime(newTime: DateTime): void {
this._internalSetTime(newTime);
}
/**
* triggers the callback function.
*/
timeModified(): void {
this.updateCallback(this.time);
this.dispatchEvent(new Event("change"));
}
get time(): DateTime {
return this._time;
}
set time(dt: DateTime) {
this._internalSetTime(dt);
this.updateCallback(this.time);
this.dispatchEvent(new Event("change"));
}
/**
* internal time set
*
* @private
* @param newTime new time to update
*/
_internalSetTime(newTime: DateTime): void {
this._time = newTime;
const ntext = (this.shadowRoot?.querySelector('input.date') as HTMLInputElement);
ntext.value = this.time.toISODate();
this.hourMin._internalSetTime(newTime);
}
static get observedAttributes() {
return ["date-time"];
}
}
customElements.define(DATETIME_ELEMENT, DateTimeChooser);
export const START_CHANGED = "start";
export const END_CHANGED = "end";
export const DURATION_CHANGED = "duration";
/**
* Combination of two DateTimeChoosers to specify a start and end time.
*
* @param div selected div to append chooser to
* @param updateCallback optional callback function when time is selected/changed
*/
export class TimeRangeChooser extends HTMLElement {
updateCallback: (timerange: Interval) => void;
_duration: Duration;
startChooser: DateTimeChooser;
endChooser: DateTimeChooser;
_mostRecentChanged: string;
constructor() {
super();
this._mostRecentChanged = "end";
this.updateCallback = (timerange: Interval) => {};
const endAttr = this.getAttribute("end");
let endTime: DateTime;
if (endAttr) {
endTime = isoToDateTime(endAttr);
} else {
endTime = DateTime.utc().startOf('minute');
}
const durAttr = this.getAttribute("duration");
if (durAttr) {
this._duration = extractDuration(durAttr);
} else {
this._duration = Duration.fromMillis(1000*300);
}
const startAttr = this.getAttribute("start");
let startTime: DateTime;
if (startAttr) {
startTime = isoToDateTime(startAttr);
if (endAttr) {
const durInterval = Interval.fromDateTimes(startTime, endTime);
this._duration = durInterval.toDuration();
} else {
endTime = startTime.plus(this._duration);
}
} else {
startTime = endTime.minus(this._duration);
}
const shadow = this.attachShadow({mode: 'open'});
const wrapper = document.createElement('span');
// style
const style = document.createElement('style');
style.textContent = `
input.duration {
width: 8em;
}
label {
margin-left: 3px;
margin-right: 1px;
}
`;
shadow.appendChild(style);
const startLabel = wrapper.appendChild(document.createElement('label'));
startLabel.textContent = this.startLabel;
const startChooser = wrapper.appendChild(new DateTimeChooser());
this.startChooser = startChooser as DateTimeChooser;
startChooser.setAttribute("class", "start");
const durationDiv = wrapper.appendChild(document.createElement('span'));
durationDiv.setAttribute("class", "duration");
const durationLabel = wrapper.appendChild(document.createElement('label'));
durationLabel.textContent = this.durationLabel;
const durationInput = wrapper.appendChild(document.createElement('input'));
durationInput.value = `${this.duration}`;
//durationInput.setAttribute("type", "number");
durationInput.setAttribute("class", "duration");
const endLabel = wrapper.appendChild(document.createElement('label'));
endLabel.textContent = this.endLabel;
const endChooser = wrapper.appendChild(new DateTimeChooser());
this.endChooser = endChooser as DateTimeChooser;
endChooser.setAttribute("class", "end");
startChooser.addEventListener("change", () => {
this.start = startChooser.time;
});
durationInput.addEventListener("change", () => {
if (! durationInput.value) {
return;
}
this.duration = extractDuration(durationInput.value);
});
endChooser.addEventListener("change", () => {
this.end = endChooser.time;
});
this.startChooser.updateTime(startTime);
this.endChooser.updateTime(endTime);
shadow.appendChild(wrapper);
}
toInterval(): Interval {
return Interval.fromDateTimes(this.startChooser.time, this.endChooser.time);
}
getTimeRange(): Interval {
return this.toInterval();
}
get startLabel(): string {
const l = this.getAttribute(START_LABEL);
if (isDef(l)) { return l; } else { return DEFAULT_START_LABEL; }
}
get endLabel(): string {
const l = this.getAttribute(END_LABEL);
if (isDef(l)) { return l; } else { return DEFAULT_END_LABEL; }
}
get durationLabel(): string {
const l = this.getAttribute(DUR_LABEL);
if (isDef(l)) { return l; } else { return DEFAULT_DUR_LABEL; }
}
get start(): DateTime {
return this.startChooser.time;
}
set start(time: DateTime) {
this.startChooser.updateTime(time);
this.setAttribute('start', time.toISO());
this.resyncValues(START_CHANGED);
}
get end(): DateTime {
return this.endChooser.time;
}
set end(time: DateTime) {
this.endChooser.updateTime(time);
this.setAttribute('end', time.toISO());
this.resyncValues(END_CHANGED);
}
set duration(duration: Duration) {
this._updateDuration(duration);
this.resyncValues(DURATION_CHANGED);
}
get duration(): Duration {
return this._duration;
}
_updateDuration(duration: Duration) {
this._duration = duration;
this.setAttribute('duration', duration.toISO());
const dur_input = this.shadowRoot?.querySelector('input.duration') as HTMLInputElement;
if ( ! dur_input) {throw new Error("can't find input.duration in sp-timerange");}
dur_input.value = this._duration.toISO();
}
resyncValues(curChanged: string) {
if (curChanged === START_CHANGED) {
if (this._mostRecentChanged === END_CHANGED && this.start < this.end) {
this._updateDuration(this.toInterval().toDuration());
} else {
// this._mostRecentChanged === DURATION_CHANGED || this._mostRecentChanged === START_CHANGED
this.endChooser.updateTime(this.startChooser.time.plus(this._duration));
}
} else if (curChanged === DURATION_CHANGED) {
if (this._mostRecentChanged === START_CHANGED) {
this.endChooser.updateTime(this.startChooser.time.plus(this._duration));
} else {
// this._mostRecentChanged === END_CHANGED || this._mostRecentChanged === DURATION_CHANGED
this.startChooser.updateTime(this.endChooser.time.minus(this._duration));
}
} else {
// assume end
if (this._mostRecentChanged === START_CHANGED && this.start < this.end) {
this._updateDuration(this.toInterval().toDuration());
} else {
// this._mostRecentChanged === END_CHANGED || this._mostRecentChanged === DURATION_CHANGED
this.startChooser.updateTime(this.endChooser.time.minus(this._duration));
}
}
if (curChanged !== this._mostRecentChanged) {
this._mostRecentChanged = curChanged;
}
this.dispatchEvent(new Event("change"));
this.updateCallback(this.getTimeRange());
}
static get observedAttributes() {
return ["start", "duration", "end", START_LABEL, END_LABEL, DUR_LABEL];
}
}
customElements.define(TIMERANGE_ELEMENT, TimeRangeChooser);
/**
* extracts duration from either string as ISO or number as seconds.
* @param value ISO string or number
* @return duration
*/
export function extractDuration(value: string): Duration {
let dur;
if (value.startsWith("P")) {
dur = Duration.fromISO(value);
} else {
const nDur = +Number.parseInt(value);
dur = Duration.fromMillis(nDur*1000);
}
return dur
}