From a40362564fa659ffa92d9ad9bd0ca164b592ff55 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 16 Dec 2020 14:11:43 +0100 Subject: [locations] Avoid potentially duplicate ids in ajax fragments --- modules-available/locations/clientscript.js | 54 +++++++++++++++++------------ 1 file changed, 31 insertions(+), 23 deletions(-) (limited to 'modules-available/locations/clientscript.js') diff --git a/modules-available/locations/clientscript.js b/modules-available/locations/clientscript.js index 25c255fb..89abcefc 100644 --- a/modules-available/locations/clientscript.js +++ b/modules-available/locations/clientscript.js @@ -61,60 +61,68 @@ function newOpeningTime(vals) { * Convert fields from simple mode view to entries in expert mode. * @returns {Array} */ -function simpleToExpert() { - var retval = []; - if ($('#week-open').val() || $('#week-close').val()) { +function simpleToExpert($form) { + var retval = [], $open, $close; + $open = $form.find('.week-open'); + $close = $form.find('.week-close'); + if ($open.val() || $close.val()) { retval.push({ 'days': ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'], - 'openingtime': $('#week-open').val(), - 'closingtime': $('#week-close').val(), - 'tag': '#week' + 'openingtime': $open.val(), + 'closingtime': $close.val(), + 'tag': 'week' }); } - if ($('#saturday-open').val() || $('#saturday-close').val()) { + $open = $form.find('.saturday-open'); + $close = $form.find('.saturday-close'); + if ($open.val() || $close.val()) { retval.push({ 'days': ['Saturday'], - 'openingtime': $('#saturday-open').val(), - 'closingtime': $('#saturday-close').val(), - 'tag': '#saturday' + 'openingtime': $open.val(), + 'closingtime': $close.val(), + 'tag': 'saturday' }); } - if ($('#sunday-open').val() || $('#sunday-close').val()) { + $open = $form.find('.sunday-open'); + $close = $form.find('.sunday-close'); + if ($open.val() || $close.val()) { retval.push({ 'days': ['Sunday'], - 'openingtime': $('#sunday-open').val(), - 'closingtime': $('#sunday-close').val(), - 'tag': '#sunday' + 'openingtime': $open.val(), + 'closingtime': $close.val(), + 'tag': 'sunday' }); } return retval; } /** - * Triggered when the form is submitted + * Triggered when the openingtimes/WOL form is submitted */ -function submitLocationSettings(event) { +function validateOpeningTimes(event) { var schedule, s, e; var badFormat = false; - $('#settings-outer').find('.red-bg').removeClass('red-bg'); - if ($('#week-open').length > 0) { - schedule = simpleToExpert(); + var $form = $(this); + + $form.find('.red-bg').removeClass('red-bg'); + if ($form.find('.week-open').length > 0) { + schedule = simpleToExpert($form); for (var i = 0; i < schedule.length; ++i) { s = getTime(schedule[i].openingtime); e = getTime(schedule[i].closingtime); if (s === false) { - $(schedule[i].tag + '-open').addClass('red-bg'); + $form.find('.' + schedule[i].tag + '-open').addClass('red-bg'); badFormat = true; } if (e === false || e <= s) { - $(schedule[i].tag + '-close').addClass('red-bg'); + $form.find('.' + schedule[i].tag + '-close').addClass('red-bg'); badFormat = true; } } } else { // Serialize schedule = []; - $('#expert-table').find('.expert-row').each(function () { + $form.find('.expert-table .expert-row').each(function () { var $t = $(this); if ($t.find('.i-delete').is(':checked')) return; // Skip marked as delete var entry = { @@ -149,5 +157,5 @@ function submitLocationSettings(event) { if (badFormat) { event.preventDefault(); } - $('#json-openingtimes').val(JSON.stringify(schedule)); + $form.find('input[name="openingtimes"]').val(JSON.stringify(schedule)); } \ No newline at end of file -- cgit v1.2.3-55-g7522