Reformat and clean up JS to eliminate warnings

- Legacy-Id: 19989
This commit is contained in:
Jennifer Richards 2022-02-24 20:17:12 +00:00
parent 077e7e2262
commit 5be277484c

View file

@ -1,285 +1,271 @@
var interimRequest = { const interimRequest = (function() {
// functions for Interim Meeting Request 'use strict';
init: function () { return {
// get elements // functions for Interim Meeting Request
interimRequest.form = $(this); init: function () {
interimRequest.addButton = $('#add_session'); // get elements
interimRequest.inPerson = $('#id_in_person'); interimRequest.form = $(this);
interimRequest.timezone = $('#id_time_zone'); interimRequest.addButton = $('#add_session');
interimRequest.addButton.on("click", interimRequest.addSession); interimRequest.inPerson = $('#id_in_person');
$('.btn-delete') interimRequest.timezone = $('#id_time_zone');
.on("click", interimRequest.deleteSession); interimRequest.addButton.on("click", interimRequest.addSession);
interimRequest.inPerson.on("change", interimRequest.toggleLocation); $('.btn-delete')
$('input[name="meeting_type"]') .on("click", interimRequest.deleteSession);
.on("change", interimRequest.meetingTypeChanged); interimRequest.inPerson.on("change", interimRequest.toggleLocation);
$('input[name$="-requested_duration"]') $('input[name="meeting_type"]')
.on("blur", interimRequest.calculateEndTime); .on("change", interimRequest.meetingTypeChanged);
$('input[name$="-time"]') $('input[name$="-requested_duration"]')
.on("blur", interimRequest.calculateEndTime); .on("blur", interimRequest.calculateEndTime);
$('input[name$="-time"]') const timeInput = $('input[name$="-time"]');
.on("blur", interimRequest.updateInfo); timeInput.on("blur", interimRequest.calculateEndTime);
$('input[name$="-end_time"]') timeInput.on("blur", interimRequest.updateInfo);
.prop('disabled', true) $('input[name$="-end_time"]')
.on("change", interimRequest.updateInfo); .prop('disabled', true)
interimRequest.timezone.on("change", interimRequest.timezoneChange); .on("change", interimRequest.updateInfo);
// init interimRequest.timezone.on("change", interimRequest.timezoneChange);
interimRequest.inPerson.each(interimRequest.toggleLocation); // init
interimRequest.checkAddButton(); interimRequest.inPerson.each(interimRequest.toggleLocation);
interimRequest.initTimezone(); interimRequest.checkAddButton();
$('input[name$="-time"]') interimRequest.initTimezone();
.each(interimRequest.calculateEndTime); timeInput.each(interimRequest.calculateEndTime);
$('input[name$="-time"]') timeInput.each(interimRequest.updateInfo);
.each(interimRequest.updateInfo); const remoteParticipations = $('select[id$="-remote_participation"]');
const remoteParticipations = $('select[id$="-remote_participation"]'); remoteParticipations.on(
remoteParticipations.change( 'change',
evt => interimRequest.updateRemoteInstructionsVisibility(evt.target) evt => interimRequest.updateRemoteInstructionsVisibility(evt.target)
); );
remoteParticipations.each((index, elt) => interimRequest.updateRemoteInstructionsVisibility(elt)); remoteParticipations.each((index, elt) => interimRequest.updateRemoteInstructionsVisibility(elt));
}, },
addSession: function () { addSession: function () {
var template = interimRequest.form.find('.fieldset.template'); const template = interimRequest.form.find('.fieldset.template');
var el = template.clone(true); const el = template.clone(true);
var totalField = $('#id_session_set-TOTAL_FORMS'); const totalField = $('#id_session_set-TOTAL_FORMS');
var total = +totalField.val(); let total = +totalField.val();
// var meeting_type = $('input[name="meeting_type"]:checked').val(); // var meeting_type = $('input[name="meeting_type"]:checked').val();
// increment formset counter // increment formset counter
template.find(':input') template.find(':input')
.each(function () { .each(function () {
var name = $(this) const name = $(this)
.attr('name') .attr('name')
.replace('-' + (total - 1) + '-', '-' + total + '-'); .replace('-' + (total - 1) + '-', '-' + total + '-');
var id = 'id_' + name; const id = 'id_' + name;
$(this) $(this)
.attr({ name: name, id: id }) .attr({ name: name, id: id })
.val(''); .val('');
});
template.find('label')
.each(function () {
const newFor = $(this)
.attr('for')
.replace('-' + (total - 1) + '-', '-' + total + '-');
$(this)
.attr('for', newFor);
});
template.find('div.utc-time')
.each(function () {
const newId = $(this)
.attr('id')
.replace('-' + (total - 1) + '-', '-' + total + '-');
$(this)
.attr('id', newId);
});
++total;
totalField.val(total);
template.before(el);
el.removeClass("template visually-hidden");
// copy field contents
const first_session = $(".fieldset:first");
el.find("input[name$='remote_instructions']")
.val(first_session.find("input[name$='remote_instructions']")
.val());
$('.btn-delete')
.removeClass("visually-hidden");
},
updateInfo: function () {
// makes ajax call to server and sets UTC field
const time = $(this)
.val();
if (!time) {
return;
}
const url = "/meeting/ajax/get-utc";
const fieldset = $(this)
.parents(".fieldset");
const date = fieldset.find("input[name$='-date']")
.val();
const timezone = interimRequest.timezone.val();
const name = $(this)
.attr("id") + "_utc";
const utc = fieldset.find("#" + name);
//console.log(name,utc.attr("id"));
$.ajax({
url: url,
type: 'GET',
cache: false,
async: true,
dataType: 'json',
data: {
date: date,
time: time,
timezone: timezone
},
success: function (response) {
if (!response.error && response.html) {
utc.html(response.html);
}
}
}); });
return false;
},
template.find('label') calculateEndTime: function () {
.each(function () { // gets called when either start_time or duration change
var newFor = $(this) const fieldset = $(this)
.attr('for') .parents(".fieldset");
.replace('-' + (total - 1) + '-', '-' + total + '-'); const start_time = fieldset.find("input[name$='-time']");
$(this) const end_time = fieldset.find("input[name$='-end_time']");
.attr('for', newFor); const duration = fieldset.find("input[name$='-requested_duration']");
}); if (!start_time.val() || !duration.val()) {
return;
}
const start_values = start_time.val()
.split(":");
const duration_values = duration.val()
.split(":");
const d = new Date(2000, 1, 1, start_values[0], start_values[1]);
const d1 = new Date(d.getTime() + (duration_values[0] * 60 * 60 * 1000));
const d2 = new Date(d1.getTime() + (duration_values[1] * 60 * 1000));
end_time.val(interimRequest.get_formatted_time(d2));
end_time.trigger('change');
},
template.find('div.utc-time') checkAddButton: function () {
.each(function () { const meeting_type = $('input[name="meeting_type"]:checked')
var newId = $(this) .val();
.attr('id') if (meeting_type === 'single') {
.replace('-' + (total - 1) + '-', '-' + total + '-'); interimRequest.addButton.addClass("visually-hidden");
$(this) } else {
.attr('id', newId); interimRequest.addButton.removeClass("visually-hidden");
}); }
},
++total; checkInPerson: function () {
const meeting_type = $('input[name="meeting_type"]:checked')
.val();
if (meeting_type === 'series') {
interimRequest.inPerson.prop('disabled', true);
interimRequest.inPerson.prop('checked', false);
interimRequest.toggleLocation();
} else {
interimRequest.inPerson.prop('disabled', false);
}
},
totalField.val(total); initTimezone: function () {
if (interimRequest.isEditView()) {
// Don't set timezone in edit view, already set
return true;
}
template.before(el); if (window.Intl && typeof window.Intl === "object") {
el.removeClass("template visually-hidden"); const tzname = Intl.DateTimeFormat()
.resolvedOptions()
// copy field contents .timeZone;
var first_session = $(".fieldset:first"); if ($('#id_time_zone option[value="' + tzname + '"]')
el.find("input[name$='remote_instructions']") .length > 0) {
.val(first_session.find("input[name$='remote_instructions']") $('#id_time_zone')
.val()); .val(tzname);
$('.btn-delete')
.removeClass("visually-hidden");
},
updateInfo: function () {
// makes ajax call to server and sets UTC field
var time = $(this)
.val();
if (!time) {
return;
}
var url = "/meeting/ajax/get-utc";
var fieldset = $(this)
.parents(".fieldset");
var date = fieldset.find("input[name$='-date']")
.val();
var timezone = interimRequest.timezone.val();
var name = $(this)
.attr("id") + "_utc";
var utc = fieldset.find("#" + name);
//console.log(name,utc.attr("id"));
$.ajax({
url: url,
type: 'GET',
cache: false,
async: true,
dataType: 'json',
data: {
date: date,
time: time,
timezone: timezone
},
success: function (response) {
if (!response.error && response.html) {
utc.html(response.html);
} }
} }
}); },
return false;
},
calculateEndTime: function () { get_formatted_time: function (d) {
// gets called when either start_time or duration change // returns time from Date object as HH:MM
var fieldset = $(this) const minutes = d.getMinutes()
.parents(".fieldset"); .toString();
var start_time = fieldset.find("input[name$='-time']"); const hours = d.getHours()
var end_time = fieldset.find("input[name$='-end_time']"); .toString();
var duration = fieldset.find("input[name$='-requested_duration']"); return interimRequest.pad(hours) + ":" + interimRequest.pad(minutes);
if (!start_time.val() || !duration.val()) { },
return;
}
var start_values = start_time.val()
.split(":");
var duration_values = duration.val()
.split(":");
var d = new Date(2000, 1, 1, start_values[0], start_values[1]);
var d1 = new Date(d.getTime() + (duration_values[0] * 60 * 60 * 1000));
var d2 = new Date(d1.getTime() + (duration_values[1] * 60 * 1000));
end_time.val(interimRequest.get_formatted_time(d2));
end_time.trigger('change');
},
checkAddButton: function () { deleteSession: function () {
var meeting_type = $('input[name="meeting_type"]:checked') const fieldset = $(this)
.val(); .parents(".fieldset");
if (meeting_type == 'single') { fieldset.remove();
interimRequest.addButton.addClass("visually-hidden"); const totalField = $('#id_form-TOTAL_FORMS');
} else { let total = +totalField.val();
interimRequest.addButton.removeClass("visually-hidden"); --total;
} totalField.val(total);
}, if (total === 2) {
$(".btn-delete")
.addClass("visually-hidden");
}
},
checkInPerson: function () { isEditView: function () {
var meeting_type = $('input[name="meeting_type"]:checked') // Called on init, returns true if editing existing meeting request
.val(); return !!$('#id_session_set-0-date').val();
if (meeting_type == 'series') { },
interimRequest.inPerson.prop('disabled', true);
interimRequest.inPerson.prop('checked', false);
interimRequest.toggleLocation();
} else {
interimRequest.inPerson.prop('disabled', false);
}
},
initTimezone: function () { meetingTypeChanged: function () {
if (interimRequest.isEditView()) { interimRequest.checkAddButton();
// Don't set timezone in edit view, already set interimRequest.checkInPerson();
return true; },
}
if (window.Intl && typeof window.Intl === "object") { pad: function (str) {
var tzname = Intl.DateTimeFormat() // zero pads string 00
.resolvedOptions() if (str.length === 1) {
.timeZone; str = "0" + str;
if ($('#id_time_zone option[value="' + tzname + '"]') }
.length > 0) { return str;
$('#id_time_zone') },
.val(tzname);
timezoneChange: function () {
$("input[name$='-time']")
.trigger('blur');
$("input[name$='-end_time']")
.trigger('change');
},
toggleLocation: function () {
if (this.checked) {
$(".location")
.prop('disabled', false);
} else {
$(".location")
.prop('disabled', true);
}
},
updateRemoteInstructionsVisibility : function(elt) {
const sessionSetPrefix = elt.id.replace('-remote_participation', '');
const remoteInstructionsId = sessionSetPrefix + '-remote_instructions';
const remoteInstructions = $('#' + remoteInstructionsId);
switch (elt.value) {
case 'meetecho':
remoteInstructions.closest('.form-group').hide();
break;
default:
remoteInstructions.closest('.form-group').show();
break;
} }
} }
}, };
})();
get_formatted_time: function (d) { $(function () {
// returns time from Date object as HH:MM 'use strict';
var minutes = d.getMinutes() $('#interim-request-form').each(interimRequest.init);
.toString(); });
var hours = d.getHours()
.toString();
return interimRequest.pad(hours) + ":" + interimRequest.pad(minutes);
},
deleteSession: function () {
var fieldset = $(this)
.parents(".fieldset");
fieldset.remove();
var totalField = $('#id_form-TOTAL_FORMS');
var total = +totalField.val();
--total;
totalField.val(total);
if (total == 2) {
$(".btn-delete")
.addClass("visually-hidden");
}
},
get_formatted_utc_time: function (d) {
// returns time from Date object as HH:MM
var minutes = d.getUTCMinutes()
.toString();
var hours = d.getUTCHours()
.toString();
return interimRequest.pad(hours) + ":" + interimRequest.pad(minutes);
},
isEditView: function () {
// Called on init, returns true if editing existing meeting request
if ($('#id_session_set-0-date')
.val()) {
return true;
} else {
return false;
}
},
meetingTypeChanged: function () {
interimRequest.checkAddButton();
interimRequest.checkInPerson();
},
pad: function (str) {
// zero pads string 00
if (str.length == 1) {
str = "0" + str;
}
return str;
},
timezoneChange: function () {
$("input[name$='-time']")
.trigger('blur');
$("input[name$='-end_time']")
.trigger('change');
},
toggleLocation: function () {
if (this.checked) {
$(".location")
.prop('disabled', false);
} else {
$(".location")
.prop('disabled', true);
}
},
updateRemoteInstructionsVisibility : function(elt) {
const sessionSetPrefix = elt.id.replace('-remote_participation', '');
const remoteInstructionsId = sessionSetPrefix + '-remote_instructions';
const remoteInstructions = $('#' + remoteInstructionsId);
switch (elt.value) {
case 'meetecho':
remoteInstructions.closest('.form-group').hide();
break;
default:
remoteInstructions.closest('.form-group').show();
break;
}
}
};
$(document)
.ready(function () {
$('#interim-request-form')
.each(interimRequest.init);
});