/
livetabledate.js
141 lines (130 loc) · 5.71 KB
/
livetabledate.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
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
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
/*!
#set ($paths = {
'daterangepicker': $services.webjars.url('bootstrap-daterangepicker', 'js/bootstrap-daterangepicker.js')
})
#set ($l10nKeys = ['today', 'yesterday', 'lastSevenDays', 'lastThirtyDays', 'thisMonth', 'lastMonth', 'clear', 'apply',
'customRange', 'from', 'to'])
#set ($l10n = {})
#foreach ($key in $l10nKeys)
#set ($discard = $l10n.put($key, $services.localization.render("daterange.$key")))
#end
#[[*/
// Start JavaScript-only code.
(function(paths, l10n) {
"use strict";
require.config({paths});
require([
'jquery',
'moment',
'moment-jdateformatparser',
'daterangepicker',
'xwiki-events-bridge'
], function($, moment) {
var bindInputs = function(livetable) {
$(livetable).find('input[data-type="date"]').each(function(i, element) {
var input = $(element);
var hidden = input.parent().prevAll('input[type="hidden"]');
var dateFormat = moment().toMomentFormatString(input.attr('data-dateformat'));
input.daterangepicker({
drops: 'down',
opens: 'center',
autoUpdateInput: false,
timePicker: true,
timePicker24Hour: true,
ranges: {
[l10n.today]: [moment().startOf('day'), moment().endOf('day')],
[l10n.yesterday]: [moment().subtract(1, 'days').startOf('day'), moment().subtract(1, 'days').endOf('day')],
[l10n.lastSevenDays]: [moment().subtract(6, 'days').startOf('day'), moment().endOf('day')],
[l10n.lastThirtyDays]: [moment().subtract(29, 'days').startOf('day'), moment().endOf('day')],
[l10n.thisMonth]: [moment().startOf('month'), moment().endOf('month')],
[l10n.lastMonth]: [moment().subtract(1, 'month').startOf('month'),
moment().subtract(1, 'month').endOf('month')]
},
locale: {
format: dateFormat,
cancelLabel: l10n.clear,
applyLabel: l10n.apply,
customRangeLabel: l10n.customRange,
fromLabel: l10n.from,
toLabel: l10n.to
}
});
// Connect the input to the picker via the aria-controls attribute.
const pickerId = input.attr('id') + '-picker';
input.data('daterangepicker').container.attr("id", pickerId);
input.parent().attr('aria-controls', pickerId);
var updateInput = function(element, event, picker) {
if (event.type == 'cancel') {
input.val('');
hidden.val('');
} else {
if (picker.startDate.isSame(picker.endDate, 'day')) {
input.val(picker.startDate.format(dateFormat));
} else {
input.val(picker.startDate.format(dateFormat) + ' - ' + picker.endDate.format(dateFormat));
}
// Make sure that the end time is the end of the minute.
hidden.val(picker.startDate.format('x') + '-' + picker.endDate.seconds(59).milliseconds(999).format('x'));
}
$(document).trigger("xwiki:livetable:" + $(livetable).attr('id') + ":filtersChanged");
}
input.on('apply.daterangepicker cancel.daterangepicker', function(event, picker) {
updateInput(this, event, picker);
});
input.on('keypress', function (e) {
if(e.which === 13) {
// when the enter key is pressed.
var txt = input.val().trim();
var range = txt.split(' - ');
if (range.length === 2) {
//range
hidden.val(moment(range[0], dateFormat).format('x') + '-' + moment(range[1], dateFormat).format('x'));
} else {
//date or part of date
hidden.val(txt);
}
}
});
input.on('show.daterangepicker', function(event) {
event.target.parentElement.setAttribute('aria-expanded', true);
});
input.on('hide.daterangepicker', function(event) {
// Overwrite at instance level the 'hide' function added by Prototype.js to the Element prototype.
// This removes the 'hide' function only for the event target.
event.target.hide = undefined;
event.target.parentElement.setAttribute('aria-expanded', false);
// Restore the 'hide' function after the event is handled (i.e. after all the listeners have been called).
setTimeout(function () {
// This deletes the local 'hide' key from the instance, making the 'hide' inherited from the prototype
// visible again (the next calls to 'hide' won't find the key on the instance and thus it will go up
// the prototype chain).
delete event.target['hide'];
}, 0);
});
});
};
$('.xwiki-livetable').each(function(i, livetable) {
bindInputs(livetable);
});
});
// End JavaScript-only code.
}).apply(']]#', $jsontool.serialize([$paths, $l10n]));