-
-
Notifications
You must be signed in to change notification settings - Fork 227
/
datetime.js
63 lines (51 loc) · 1.89 KB
/
datetime.js
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
import $ from 'jquery';
import { config } from 'grav-config';
import '../../utils/bootstrap-datetimepicker';
export default class DateTimeField {
get defaults() {
return {
showTodayButton: true,
showClear: true,
locale: config.language || 'en',
icons: {
time: 'fa fa-clock-o',
date: 'fa fa-calendar-o',
up: 'fa fa-chevron-up',
down: 'fa fa-chevron-down',
previous: 'fa fa-chevron-left',
next: 'fa fa-chevron-right',
today: 'fa fa-bullseye',
clear: 'fa fa-trash-o',
close: 'fa fa-remove'
}
};
}
constructor(options) {
this.items = $();
this.options = Object.assign({}, this.defaults, options);
$('[data-grav-datetime]').each((index, field) => this.addItem(field));
$('body').on('mutation._grav', this._onAddedNodes.bind(this));
}
addItem(list) {
list = $(list);
this.items = this.items.add(list);
if (list.data('DateTimePicker')) { return; }
let options = Object.assign({}, this.options, list.data('grav-datetime') || {});
list.datetimepicker(options).on('dp.show dp.update', this._disableDecades);
list.siblings('.field-icons').on('click', () => list.mousedown().focus());
}
_onAddedNodes(event, target/* , record, instance */) {
let fields = $(target).find('[data-grav-datetime]');
if (!fields.length) { return; }
fields.each((index, field) => {
field = $(field);
if (!~this.items.index(field)) {
this.addItem(field);
}
});
}
_disableDecades() {
$('.datepicker-years .picker-switch').removeAttr('title').on('click', (e) => e.stopPropagation());
}
}
export let Instance = new DateTimeField();