-
Notifications
You must be signed in to change notification settings - Fork 672
/
date.js
79 lines (72 loc) · 2.27 KB
/
date.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
define(['jquery', 'elgg', 'jquery-ui/widgets/datepicker'], function ($, elgg) {
// the language module may need loading
var i18n_ready = $.Deferred();
if (elgg.get_language() === 'en') {
i18n_ready.resolve();
} else {
require(['jquery-ui/i18n/datepicker-' + elgg.get_language()], function () {
i18n_ready.resolve();
}, function () {
// if load fails (e.g. lang code mismatch), carry on with English
i18n_ready.resolve();
});
}
var datepicker = {
/**
* Initialize the date picker on elements defined by the selector
*
* If the class .elgg-input-timestamp is set on the element, the onSelect
* method converts the date text to a UNIX timestamp in seconds. That value is
* stored in a hidden element and submitted with the form. Note that the UNIX
* timestamp is normalized to start of the day at UTC, so you may need to use
* timezone offsets if you expect a different timezone. Timestamp is determined
* by the selected values of the datepicker instance, and is therefore agnostic
* to the dateFormat option.
*
* @param {string} selector Element selector
* @return void
* @requires jqueryui.datepicker
*/
init: function (selector) {
if (!$(selector).length) {
return;
}
var defaults = {
dateFormat: 'yy-mm-dd',
nextText: '»',
prevText: '«',
changeMonth: true,
changeYear: true
};
$(selector).each(function () {
var $elem = $(this);
var opts = $elem.data('datepickerOpts') || {};
opts = $.extend({}, defaults, opts);
opts.onSelect = function (dateText, instance) {
if ($(this).is('.elgg-input-timestamp')) {
timestamp = '';
if (dateText.length) {
// convert to unix timestamp
var timestamp = Date.UTC(instance.selectedYear, instance.selectedMonth, instance.selectedDay);
timestamp = timestamp / 1000;
}
$('input[rel="' + this.id + '"]').val(timestamp);
}
};
// defer until language loaded
i18n_ready.then(function () {
$elem.datepicker(opts);
$elem.on('keyup', function(event) {
switch (event.keyCode) {
case $.ui.keyCode.DELETE:
case $.ui.keyCode.BACKSPACE:
$.datepicker._clearDate(event.target);
break;
}
});
});
});
}
};
return datepicker;
});