1 |
efrain |
1 |
/**
|
|
|
2 |
* Provides the Calendar class.
|
|
|
3 |
*
|
|
|
4 |
* @module moodle-form-dateselector
|
|
|
5 |
*/
|
|
|
6 |
|
|
|
7 |
/**
|
|
|
8 |
* Calendar class
|
|
|
9 |
*/
|
|
|
10 |
CALENDAR = function() {
|
|
|
11 |
CALENDAR.superclass.constructor.apply(this, arguments);
|
|
|
12 |
};
|
|
|
13 |
CALENDAR.prototype = {
|
|
|
14 |
panel: null,
|
|
|
15 |
yearselect: null,
|
|
|
16 |
monthselect: null,
|
|
|
17 |
dayselect: null,
|
|
|
18 |
calendarimage: null,
|
|
|
19 |
enablecheckbox: null,
|
|
|
20 |
closepopup: true,
|
|
|
21 |
initializer: function() {
|
|
|
22 |
var controls = this.get('node').all('select');
|
|
|
23 |
controls.each(function(node) {
|
|
|
24 |
if (node.get('name').match(/\[year\]/)) {
|
|
|
25 |
this.yearselect = node;
|
|
|
26 |
} else if (node.get('name').match(/\[month\]/)) {
|
|
|
27 |
this.monthselect = node;
|
|
|
28 |
} else if (node.get('name').match(/\[day\]/)) {
|
|
|
29 |
this.dayselect = node;
|
|
|
30 |
}
|
|
|
31 |
node.after('change', this.handle_select_change, this);
|
|
|
32 |
}, this);
|
|
|
33 |
|
|
|
34 |
// Loop through the input fields.
|
|
|
35 |
var inputs = this.get('node').all('input, a');
|
|
|
36 |
inputs.each(function(node) {
|
|
|
37 |
// Check if the current node is a calendar image field.
|
|
|
38 |
if (node.get('name').match(/\[calendar\]/)) {
|
|
|
39 |
// Set it so that when the image is clicked the pop-up displays.
|
|
|
40 |
node.on('click', this.focus_event, this);
|
|
|
41 |
// Set the node to the calendarimage variable.
|
|
|
42 |
this.calendarimage = node;
|
|
|
43 |
} else { // Must be the enabled checkbox field.
|
|
|
44 |
// If the enable checkbox is clicked we want to either disable/enable the calendar image.
|
|
|
45 |
node.on('click', this.toggle_calendar_image, this);
|
|
|
46 |
// Set the node to the enablecheckbox variable.
|
|
|
47 |
this.enablecheckbox = node;
|
|
|
48 |
}
|
|
|
49 |
// Ensure that the calendarimage and enablecheckbox values have been set.
|
|
|
50 |
if (this.calendarimage && this.enablecheckbox) {
|
|
|
51 |
// Set the calendar icon status depending on the value of the checkbox.
|
|
|
52 |
this.toggle_calendar_image();
|
|
|
53 |
}
|
|
|
54 |
}, this);
|
|
|
55 |
|
|
|
56 |
// Get the calendarimage element by its ID and check if any of its parents have the modal-dialog class to
|
|
|
57 |
// know if the link is inside a modal, if so, set the aria-hidden and tabindex properties to the indicated values.
|
|
|
58 |
var calendarimageelement = document.getElementById(this.calendarimage.get('id'));
|
|
|
59 |
if (calendarimageelement.closest('.modal-dialog')) {
|
|
|
60 |
this.calendarimage.set('aria-hidden', true);
|
|
|
61 |
this.calendarimage.set('tabIndex', '-1');
|
|
|
62 |
}
|
|
|
63 |
},
|
|
|
64 |
focus_event: function(e) {
|
|
|
65 |
M.form.dateselector.cancel_any_timeout();
|
|
|
66 |
// If the current owner is set, then the pop-up is currently being displayed, so hide it.
|
|
|
67 |
if (M.form.dateselector.currentowner === this) {
|
|
|
68 |
this.release_calendar();
|
|
|
69 |
} else if ((this.enablecheckbox === null)
|
|
|
70 |
|| (this.enablecheckbox.get('checked'))) { // Must be hidden. If the field is enabled display the pop-up.
|
|
|
71 |
this.claim_calendar();
|
|
|
72 |
}
|
|
|
73 |
// Stop the input image field from submitting the form.
|
|
|
74 |
e.preventDefault();
|
|
|
75 |
},
|
|
|
76 |
handle_select_change: function() {
|
|
|
77 |
// It may seem as if the following variable is not used, however any call to set_date_from_selects will trigger a
|
|
|
78 |
// call to set_selects_from_date if the calendar is open as the date has changed. Whenever the calendar is displayed
|
|
|
79 |
// the set_selects_from_date function is set to trigger on any date change (see function connect_handlers).
|
|
|
80 |
this.closepopup = false;
|
|
|
81 |
this.set_date_from_selects();
|
|
|
82 |
this.closepopup = true;
|
|
|
83 |
},
|
|
|
84 |
claim_calendar: function() {
|
|
|
85 |
M.form.dateselector.cancel_any_timeout();
|
|
|
86 |
if (M.form.dateselector.currentowner === this) {
|
|
|
87 |
return;
|
|
|
88 |
}
|
|
|
89 |
if (M.form.dateselector.currentowner) {
|
|
|
90 |
M.form.dateselector.currentowner.release_calendar();
|
|
|
91 |
}
|
|
|
92 |
if (M.form.dateselector.currentowner !== this) {
|
|
|
93 |
this.connect_handlers();
|
|
|
94 |
this.set_date_from_selects();
|
|
|
95 |
}
|
|
|
96 |
M.form.dateselector.currentowner = this;
|
|
|
97 |
M.form.dateselector.calendar.set('minimumDate', new Date(this.yearselect.firstOptionValue(), 0, 1));
|
|
|
98 |
M.form.dateselector.calendar.set('maximumDate', new Date(this.yearselect.lastOptionValue(), 11, 31));
|
|
|
99 |
M.form.dateselector.panel.show();
|
|
|
100 |
M.form.dateselector.calendar.show();
|
|
|
101 |
M.form.dateselector.fix_position();
|
|
|
102 |
setTimeout(function() {
|
|
|
103 |
M.form.dateselector.cancel_any_timeout();
|
|
|
104 |
}, 100);
|
|
|
105 |
|
|
|
106 |
// Focus on the calendar.
|
|
|
107 |
M.form.dateselector.calendar.focus();
|
|
|
108 |
|
|
|
109 |
// When the user tab out the calendar, close it.
|
|
|
110 |
Y.one(document.body).on('keyup', function(e) {
|
|
|
111 |
// If the calendar is open and we try to access it by pressing tab, we check if it is inside a Bootstrap dropdown-menu,
|
|
|
112 |
// if so, we keep the dropdown open while navigation takes place in the calendar.
|
|
|
113 |
if (M.form.dateselector.currentowner && e.keyCode === 9) {
|
|
|
114 |
e.stopPropagation();
|
|
|
115 |
var calendarimageelement = document.getElementById(M.form.dateselector.currentowner.calendarimage.get('id'));
|
|
|
116 |
if (M.form.dateselector.calendar.get('focused') && calendarimageelement.closest('.dropdown-menu') &&
|
|
|
117 |
!calendarimageelement.closest('.dropdown-menu').classList.contains("show")) {
|
|
|
118 |
calendarimageelement.closest('.dropdown-menu').classList.add('show');
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
// hide the calendar if we press a key and the calendar is not focussed, or if we press ESC in the calendar.
|
|
|
123 |
if ((M.form.dateselector.currentowner === this && !M.form.dateselector.calendar.get('focused')) ||
|
|
|
124 |
((e.keyCode === 27) && M.form.dateselector.calendar.get('focused'))) {
|
|
|
125 |
// Focus back on the calendar button.
|
|
|
126 |
this.calendarimage.focus();
|
|
|
127 |
this.release_calendar();
|
|
|
128 |
}
|
|
|
129 |
}, this);
|
|
|
130 |
|
|
|
131 |
},
|
|
|
132 |
set_date_from_selects: function() {
|
|
|
133 |
var year = parseInt(this.yearselect.get('value'), 10);
|
|
|
134 |
var month = parseInt(this.monthselect.get('value'), 10) - 1;
|
|
|
135 |
var day = parseInt(this.dayselect.get('value'), 10);
|
|
|
136 |
var date = new Date(year, month, day);
|
|
|
137 |
M.form.dateselector.calendar.deselectDates();
|
|
|
138 |
M.form.dateselector.calendar.selectDates([date]);
|
|
|
139 |
M.form.dateselector.calendar.set("date", date);
|
|
|
140 |
M.form.dateselector.calendar.render();
|
|
|
141 |
if (date.getDate() !== day) {
|
|
|
142 |
// Must've selected the 29 to 31st of a month that doesn't have such dates.
|
|
|
143 |
this.dayselect.set('value', date.getDate());
|
|
|
144 |
this.monthselect.set('value', date.getMonth() + 1);
|
|
|
145 |
}
|
|
|
146 |
},
|
|
|
147 |
set_selects_from_date: function(ev) {
|
|
|
148 |
var date = ev.newSelection[0];
|
|
|
149 |
var newyear = Y.DataType.Date.format(date, {format: "%Y"});
|
|
|
150 |
var newindex = newyear - this.yearselect.firstOptionValue();
|
|
|
151 |
this.yearselect.set('selectedIndex', newindex);
|
|
|
152 |
this.monthselect.set('selectedIndex', Y.DataType.Date.format(date, {format: "%m"}) - this.monthselect.firstOptionValue());
|
|
|
153 |
this.dayselect.set('selectedIndex', Y.DataType.Date.format(date, {format: "%d"}) - this.dayselect.firstOptionValue());
|
|
|
154 |
if (M.form.dateselector.currentowner && this.closepopup) {
|
|
|
155 |
this.release_calendar();
|
|
|
156 |
}
|
|
|
157 |
},
|
|
|
158 |
connect_handlers: function() {
|
|
|
159 |
M.form.dateselector.calendar.on('selectionChange', this.set_selects_from_date, this, true);
|
|
|
160 |
},
|
|
|
161 |
release_calendar: function(e) {
|
|
|
162 |
var wasOwner = M.form.dateselector.currentowner === this;
|
|
|
163 |
M.form.dateselector.panel.hide();
|
|
|
164 |
M.form.dateselector.calendar.detach('selectionChange', this.set_selects_from_date);
|
|
|
165 |
M.form.dateselector.calendar.hide();
|
|
|
166 |
M.form.dateselector.currentowner = null;
|
|
|
167 |
|
|
|
168 |
// Put the focus back to the image calendar that we clicked, only if it was visible.
|
|
|
169 |
if (wasOwner && (e === null || typeof e === "undefined" || e.type !== "click")) {
|
|
|
170 |
this.calendarimage.focus();
|
|
|
171 |
}
|
|
|
172 |
},
|
|
|
173 |
toggle_calendar_image: function() {
|
|
|
174 |
// If the enable checkbox is not checked, disable the calendar image and prevent focus.
|
|
|
175 |
if (!this.enablecheckbox.get('checked')) {
|
|
|
176 |
this.calendarimage.addClass('disabled');
|
|
|
177 |
this.release_calendar();
|
|
|
178 |
} else {
|
|
|
179 |
this.calendarimage.removeClass('disabled');
|
|
|
180 |
}
|
|
|
181 |
}
|
|
|
182 |
};
|
|
|
183 |
Y.extend(CALENDAR, Y.Base, CALENDAR.prototype, {
|
|
|
184 |
NAME: 'Date Selector',
|
|
|
185 |
ATTRS: {
|
|
|
186 |
firstdayofweek: {
|
|
|
187 |
validator: Y.Lang.isString
|
|
|
188 |
},
|
|
|
189 |
node: {
|
|
|
190 |
setter: function(node) {
|
|
|
191 |
return Y.one(node);
|
|
|
192 |
}
|
|
|
193 |
}
|
|
|
194 |
}
|
|
|
195 |
});
|