-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtimecard_entries.js
More file actions
53 lines (44 loc) · 1.8 KB
/
timecard_entries.js
File metadata and controls
53 lines (44 loc) · 1.8 KB
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
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
$(function () {
var eventSelect = $('#timecard_entry_eventdate_id_and_eventpart');
var timecardSelect = $('#timecard_entry_timecard_id');
if (!eventSelect.length || !timecardSelect.length) return;
var allOptions = timecardSelect.find('option').clone();
function setMultiMode(options) {
timecardSelect.empty().append(options.clone()).show().prop('disabled', false);
$('#timecard-single-display').hide();
$('#timecard_id_single').prop('disabled', true);
$('#timecard-no-match-message').hide();
}
function setSingleMode(option) {
timecardSelect.hide().prop('disabled', true);
$('#timecard-single-display').text(option.text()).show();
$('#timecard_id_single').val(option.val()).prop('disabled', false);
$('#timecard-no-match-message').hide();
}
function setNoMatchMode() {
timecardSelect.empty().append(allOptions.clone()).show().prop('disabled', false);
$('#timecard-single-display').hide();
$('#timecard_id_single').prop('disabled', true);
$('#timecard-no-match-message').show();
}
function updateTimecardField() {
var startDate = eventSelect.find(':selected').data('startdate');
if (!startDate) { setMultiMode(allOptions); return; }
var eventDate = new Date(startDate);
var matching = allOptions.filter(function () {
return eventDate >= new Date($(this).data('start')) &&
eventDate <= new Date($(this).data('end'));
});
if (matching.length === 0) {
setNoMatchMode();
} else if (matching.length === 1) {
setSingleMode(matching.first());
} else {
setMultiMode(matching);
}
}
eventSelect.change(updateTimecardField);
updateTimecardField();
});