// Currently selected sound channel row in the table window.selectedSoundChannel = null; /** * Fills the sound channel table body with the provided data. * @param {SoundChannel[]} vv Sound channel data to populate the table. */ function fill_soundchanneltablebody(vv) { let $tbody = $('#soundchanneltablebody'); let $btnEditSoundChannel = $('#btnEditSoundChannel'); let $tablesizeSoundChannel = $('#tablesizeSoundChannel'); $tbody.empty(); $tablesizeSoundChannel.text('Table Length : N/A'); if (!Array.isArray(vv) || vv.length === 0) return; vv.forEach(item => { const row = ` ${item.index} ${item.channel} ${item.ip} `; $tbody.append(row); let $addedrow = $('#soundchanneltablebody tr:last'); $addedrow.click(function () { if (selectedSoundChannel) { selectedSoundChannel.find('td').css('background-color', ''); if (selectedSoundChannel.is($(this))) { selectedSoundChannel = null; $btnEditSoundChannel.prop('disabled', true); return; } } $(this).find('td').css('background-color', '#ffeeba'); selectedSoundChannel = $(this); $btnEditSoundChannel.prop('disabled', false); }); }); $tablesizeSoundChannel.text("Table Size: " + vv.length); } $(document).ready(function () { console.log("soundchannel.js loaded successfully"); let $soundchannelmodal = $('#soundchannelmodal'); let $soundchannelindex = $soundchannelmodal.find('#soundchannelindex'); let $soundchanneldescription = $soundchannelmodal.find('#soundchanneldescription'); let $soundchannelip = $soundchannelmodal.find('#soundchannelip'); let $btnReinitializeSoundChannel = $('#btnReinitializeSoundChannel'); let $btnEditSoundChannel = $('#btnEditSoundChannel'); let $btnExportSoundChannel = $('#btnExportSoundChannel'); let $btnImportSoundChannel = $('#btnImportSoundChannel'); let $findsoundchannel = $('#findsoundchannel'); $btnEditSoundChannel.prop('disabled', true); let API_SoundChannel = "SoundChannel/"; $findsoundchannel.on('input', function () { let searchTerm = $(this).val().toLowerCase(); if (searchTerm.length==0){ window.selectedSoundChannel = null; fill_soundchanneltablebody(window.soundChannels); } else { window.selectedSoundChannel = null; let filteredChannels = window.soundChannels.filter(xx => xx.index.toString().includes(searchTerm) || xx.channel.toLowerCase().includes(searchTerm) || xx.ip.toLowerCase().includes(searchTerm) ); fill_soundchanneltablebody(filteredChannels); } }); /** * Clear sound channel modal inputs */ function clearSoundChannelModal() { $soundchannelindex.val(''); $soundchanneldescription.val(''); $soundchannelip.val(''); } reloadSoundChannel(API_SoundChannel, () => { fill_soundchanneltablebody(window.soundChannels); }); $btnReinitializeSoundChannel.click(() => { DoClear(API_SoundChannel, "SoundChannels", (okdata) => { reloadSoundChannel(API_SoundChannel, () => { fill_soundchanneltablebody(window.soundChannels); alert("Success clear sound channels: " + okdata.message); }); }, (errdata) => { alert("Error clear sound channels: " + errdata.message); }); }); $btnEditSoundChannel.click(() => { if (selectedSoundChannel) { let cells = selectedSoundChannel.find('td'); /** @type {SoundChannel} */ let sc = { index: parseInt(cells.eq(0).text(), 10), description: cells.eq(1).text(), ip: cells.eq(2).text() }; if (confirm(`Are you sure to edit sound channel [${sc.index}] Description=${sc.description} IP=${sc.ip}?`)) { $soundchannelmodal.modal('show'); clearSoundChannelModal(); $soundchannelindex.val(sc.index).prop('disabled', true); $soundchanneldescription.val(sc.description); $soundchannelip.val(sc.ip); // Handle save changes $soundchannelmodal.off('click.soundchannelsave').on('click.soundchannelsave', '#soundchannelsave', function () { let newsc = { index: parseInt($soundchannelindex.val(), 10), description: $soundchanneldescription.val(), ip: $soundchannelip.val() }; if (newsc.description.trim().length === 0) { alert("Description cannot be empty"); return; } if (newsc.ip.trim().length === 0) { alert("IP cannot be empty"); return; } if (newsc.description===sc.description && newsc.ip===sc.ip){ alert("No changes detected"); return; } fetchAPI(API_SoundChannel + "UpdateByIndex/" + newsc.index, "PATCH", {}, newsc, (okdata) => { reloadSoundChannel(API_SoundChannel, () => { fill_soundchanneltablebody(window.soundChannels); alert("Success edit sound channel: " + okdata.message); }); }, (errdata) => { alert("Error edit sound channel: " + errdata.message); }); $soundchannelmodal.modal('hide'); }); $soundchannelmodal.off('click.soundchannelclose').on('click.soundchannelclose', '#soundchannelclose', function () { $soundchannelmodal.modal('hide'); }); } } }); $btnExportSoundChannel.click(() => { DoExport(API_SoundChannel, "soundchannels.xlsx", {}); }); $btnImportSoundChannel.click(() => { DoImport(API_SoundChannel, (okdata) => { reloadSoundChannel(API_SoundChannel, () => { fill_soundchanneltablebody(window.soundChannels); alert("Success import sound channels: " + okdata.message); }); }, (errdata) => { alert("Error importing sound channels from XLSX: " + errdata.message); }); }); });