From 114282ff708eea03e49fbee67b115eec1ab5b1e1 Mon Sep 17 00:00:00 2001 From: LordGrey <48840279+Lord-Grey@users.noreply.github.com> Date: Mon, 13 Nov 2023 23:14:18 +0100 Subject: [PATCH] Validate that one CEC Event can only trigger one action --- assets/webconfig/i18n/en.json | 1 + assets/webconfig/js/content_events.js | 36 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/assets/webconfig/i18n/en.json b/assets/webconfig/i18n/en.json index b01d1cc0..24a42b56 100644 --- a/assets/webconfig/i18n/en.json +++ b/assets/webconfig/i18n/en.json @@ -267,6 +267,7 @@ "edt_conf_cec_actions_header_title": "Actions", "edt_conf_cec_actions_header_expl": "Define which action should take place on a recognised CEC event", "edt_conf_cec_actions_header_item_title": "Action", + "edt_conf_cec_action_record_validation_error": "One CEC Event can trigger only one action. Clean up Actions $1", "edt_conf_cec_button_release_delay_ms_title": "Button release time", "edt_conf_cec_button_release_delay_ms_expl": "Remote button press release time", "edt_conf_cec_button_repeat_rate_ms_title": "Button repeat rate", diff --git a/assets/webconfig/js/content_events.js b/assets/webconfig/js/content_events.js index e4097fc0..18347fcc 100644 --- a/assets/webconfig/js/content_events.js +++ b/assets/webconfig/js/content_events.js @@ -27,6 +27,42 @@ $(document).ready(function () { } } + function findDuplicateCecEventsIndices(data) { + const cecEventIndices = {}; + data.forEach((item, index) => { + const cecEvent = item.cec_event; + if (!cecEventIndices[cecEvent]) { + cecEventIndices[cecEvent] = [index]; + } else { + cecEventIndices[cecEvent].push(index); + } + }); + + return Object.values(cecEventIndices).filter(indices => indices.length > 1); + } + + JSONEditor.defaults.custom_validators.push(function (schema, value, path) { + let errors = []; + if (schema.type === 'array' && Array.isArray(value)) { + const duplicateCecEventIndices = findDuplicateCecEventsIndices(value); + + if (duplicateCecEventIndices.length > 0) { + + let recs; + duplicateCecEventIndices.forEach(indices => { + const displayIndices = indices.map(index => index + 1); + recs = displayIndices.join(', '); + }); + + errors.push({ + path: path, + message: $.i18n('edt_conf_cec_action_record_validation_error', recs) + }); + } + } + return errors; + }); + //Operating System Events conf_editor_osEvents = createJsonEditor('editor_container_os_events', { osEvents: window.schema.osEvents