317 lines
13 KiB
JavaScript
317 lines
13 KiB
JavaScript
|
|
|
|
/**
|
|
* Currently selected broadcast zone row in the table
|
|
* @type {JQuery<HTMLElement>|null}
|
|
*/
|
|
window.selectedBroadcastZoneRow = null;
|
|
|
|
/**
|
|
* List of sound channels available
|
|
* @type {String[]}
|
|
*/
|
|
window.SoundChannelList = []
|
|
|
|
dtBroadcastZone = null;
|
|
|
|
/**
|
|
* Fill broadcast zone table body with values
|
|
* @param {BroadcastZone[]} vv values to fill
|
|
*/
|
|
function fill_broadcastzonetablebody(vv) {
|
|
dtBroadcastZone.clear();
|
|
if (!Array.isArray(vv) || vv.length === 0) return;
|
|
dtBroadcastZone.rows.add(vv);
|
|
dtBroadcastZone.draw();
|
|
|
|
$('#broadcastzonetable tbody').off('click').on('click', 'tr', function () {
|
|
// if no data
|
|
if (!dtBroadcastZone) return;
|
|
// if user click an empty row
|
|
if (!dtBroadcastZone.data().any()) return;
|
|
|
|
const selected = dtBroadcastZone.row(this)
|
|
// toggle behaviour - unselect if already selected
|
|
if ($(this).hasClass('row-selected')) {
|
|
$(this).removeClass('row-selected').find('td').css('background-color', '');
|
|
window.selectedBroadcastZoneRow = null;
|
|
$('#btnRemove').prop('disabled', true);
|
|
$('#btnEdit').prop('disabled', true);
|
|
return;
|
|
}
|
|
|
|
// unselect previously selected row
|
|
$('#broadcastzonetable tbody tr.row-selected').removeClass('row-selected').find('td').css('background-color', '');
|
|
|
|
$(this).addClass('row-selected').find('td').css('background-color', '#ffeeba');
|
|
window.selectedBroadcastZoneRow = selected.data();
|
|
$('#btnRemove').prop('disabled', false);
|
|
$('#btnEdit').prop('disabled', false);
|
|
});
|
|
|
|
$('#tablesize').text("Table Size: " + vv.length);
|
|
}
|
|
|
|
function fetchSoundChannels(APIURL = "SoundChannel/") {
|
|
window.SoundChannelList = [];
|
|
fetchAPI(APIURL + "SoundChannelDescriptions", "GET", {}, null, (okdata) => {
|
|
if (Array.isArray(okdata)) {
|
|
//console.log("fetchSoundChannels : ", okdata)
|
|
window.SoundChannelList.push(...okdata);
|
|
} else console.log("fetchSoundChannels: okdata is not array");
|
|
}, (errdata) => {
|
|
alert("Error loading sound channels : " + errdata.message);
|
|
});
|
|
}
|
|
|
|
$(document).ready(function () {
|
|
console.log("broadcastzones.js loaded successfully");
|
|
window.selectedBroadcastZoneRow = null;
|
|
let $btnClear = $('#btnClear');
|
|
let $btnAdd = $('#btnAdd');
|
|
let $btnEdit = $('#btnEdit');
|
|
let $btnRemove = $('#btnRemove');
|
|
let $btnExport = $('#btnExport');
|
|
let $btnImport = $('#btnImport');
|
|
$btnEdit.prop('disabled', true);
|
|
$btnRemove.prop('disabled', true);
|
|
let APIURL_BroadcastZone = "BroadcastZones/";
|
|
|
|
|
|
let $broadcastzonemodal = $('#broadcastzonemodal');
|
|
let $broadcastzoneindex = $broadcastzonemodal.find('#broadcastzoneindex');
|
|
let $broadcastzonedescription = $broadcastzonemodal.find('#broadcastzonedescription');
|
|
let $broadcastzonesoundchannel = $broadcastzonemodal.find('#broadcastzonesoundchannel');
|
|
let $broadcastzonebox = $broadcastzonemodal.find('#broadcastzonebox');
|
|
|
|
if (dtBroadcastZone === null) {
|
|
dtBroadcastZone = new DataTable('#broadcastzonetable', {
|
|
data: [],
|
|
pageLength: 25,
|
|
columns: [
|
|
{ title: "No", data: "index" },
|
|
{ title: "Description", data: "description" },
|
|
{ title: "Sound Channel", data: "soundChannel" },
|
|
{ title: "Address", data: "id" },
|
|
{ title: "Contact Output", data: "bp" },
|
|
]
|
|
});
|
|
}
|
|
|
|
|
|
// let $findzone = $('#findzone');
|
|
// $findzone.off('input').on('input', function () {
|
|
// let searchTerm = $findzone.val().trim().toLowerCase();
|
|
// if (searchTerm.length > 0) {
|
|
// window.selectedBroadcastZoneRow = null;
|
|
// let filtered = window.BroadcastZoneList.filter(item => item.description.toLowerCase().includes(searchTerm) || item.id.toLowerCase().includes(searchTerm) || item.soundChannel.toLowerCase().includes(searchTerm) || item.bp.toLowerCase().includes(searchTerm));
|
|
// fill_broadcastzonetablebody(filtered);
|
|
// } else {
|
|
// window.selectedBroadcastZoneRow = null;
|
|
// fill_broadcastzonetablebody(window.BroadcastZoneList);
|
|
// }
|
|
// });
|
|
|
|
/**
|
|
* Find Checkbox for relays 1 to 32
|
|
* Checkbox id is 01 to 32 with leading zero for 1 to 9
|
|
* @param {number} id 1 - 32
|
|
* @returns JQuery<HTMLElement>
|
|
*/
|
|
function cbRelay(id) {
|
|
return $broadcastzonemodal.find('#R' + (id < 10 ? '0' : '') + id);
|
|
}
|
|
|
|
/**
|
|
* Clear broadcast zone modal to default state
|
|
*/
|
|
function clearBroadcastZoneModal() {
|
|
$broadcastzoneindex.prop('disabled', true).val('');
|
|
$broadcastzonedescription.val('');
|
|
// fill broadcastzonesoundchannel from SoundChannelList
|
|
$broadcastzonesoundchannel.empty();
|
|
//console.log("SoundChannelList:", window.SoundChannelList);
|
|
if (Array.isArray(window.SoundChannelList) && window.SoundChannelList.length > 0) {
|
|
// SoundChannelList ada isinya
|
|
window.SoundChannelList.forEach(ch => {
|
|
if (ch && ch.length > 0) {
|
|
$broadcastzonesoundchannel.append($('<option>').val(ch).text(ch));
|
|
}
|
|
|
|
});
|
|
}
|
|
$broadcastzonebox.val('1').prop('disabled', true);
|
|
for (let i = 1; i <= 32; i++) {
|
|
cbRelay(i).prop('checked', false);
|
|
}
|
|
}
|
|
|
|
fetchSoundChannels();
|
|
reloadBroadcastZones(APIURL_BroadcastZone, () => fill_broadcastzonetablebody(window.BroadcastZoneList));
|
|
|
|
$btnClear.off('click').on('click', () => {
|
|
DoClear(APIURL_BroadcastZone, "BroadcastZones", (okdata) => {
|
|
reloadBroadcastZones(APIURL_BroadcastZone, () => fill_broadcastzonetablebody(window.BroadcastZoneList));
|
|
alert("Success clear broadcast zones: " + okdata.message);
|
|
}, (errdata) => {
|
|
alert("Error clear broadcast zones: " + errdata.message);
|
|
});
|
|
});
|
|
|
|
$btnAdd.off('click').on('click', () => {
|
|
$broadcastzonemodal.modal('show');
|
|
clearBroadcastZoneModal();
|
|
|
|
$broadcastzonemodal.off('click.broadcastzonesave').on('click.broadcastzonesave', '#broadcastzonesave', function () {
|
|
|
|
let description = $broadcastzonedescription.val().trim();
|
|
let soundChannel = $broadcastzonesoundchannel.val().trim();
|
|
let box = $broadcastzonebox.val().trim();
|
|
let relayArray = [];
|
|
for (let i = 1; i <= 32; i++) {
|
|
if (cbRelay(i).is(':checked')) {
|
|
relayArray.push(i);
|
|
}
|
|
}
|
|
let relay = relayArray.join(';');
|
|
if (description.length === 0) {
|
|
alert("Description cannot be empty");
|
|
return;
|
|
}
|
|
if (soundChannel.length === 0) {
|
|
alert("Sound Channel cannot be empty");
|
|
return;
|
|
}
|
|
if (box.length === 0) {
|
|
alert("Box cannot be empty");
|
|
return;
|
|
}
|
|
if (relayArray.length === 0) {
|
|
alert("At least one relay must be selected");
|
|
return;
|
|
}
|
|
let bz = {
|
|
description: description,
|
|
SoundChannel: soundChannel,
|
|
Box: box,
|
|
Relay: relay
|
|
};
|
|
fetchAPI(APIURL_BroadcastZone + "Add", "POST", {}, bz, (okdata) => {
|
|
reloadBroadcastZones(APIURL_BroadcastZone, () => fill_broadcastzonetablebody(window.BroadcastZoneList));
|
|
alert("Success add new broadcast zone: " + okdata.message);
|
|
}, (errdata) => {
|
|
alert("Error add new broadcast zone: " + errdata.message);
|
|
});
|
|
|
|
$broadcastzonemodal.modal('hide');
|
|
});
|
|
$broadcastzonemodal.off('click.broadcastzoneclose').on('click.broadcastzoneclose', '#broadcastzoneclose', function () {
|
|
$broadcastzonemodal.modal('hide');
|
|
});
|
|
});
|
|
|
|
$btnRemove.off('click').on('click', () => {
|
|
if (window.selectedBroadcastZoneRow) {
|
|
/** @type {BroadcastZone} */
|
|
let bz = {
|
|
index: window.selectedBroadcastZoneRow.index,
|
|
description: window.selectedBroadcastZoneRow.description,
|
|
SoundChannel: window.selectedBroadcastZoneRow.soundChannel,
|
|
Box: window.selectedBroadcastZoneRow.id,
|
|
Relay: window.selectedBroadcastZoneRow.bp
|
|
};
|
|
if (confirm(`Are you sure to delete broadcast zone [${bz.index}] Description=${bz.description}?`)) {
|
|
fetchAPI(APIURL_BroadcastZone + "DeleteByIndex/" + bz.index, "DELETE", {}, null, (okdata) => {
|
|
reloadBroadcastZones(APIURL_BroadcastZone, () => fill_broadcastzonetablebody(window.BroadcastZoneList));
|
|
alert("Success delete broadcast zone: " + okdata.message);
|
|
}, (errdata) => {
|
|
alert("Error delete broadcast zone: " + errdata.message);
|
|
});
|
|
}
|
|
}
|
|
});
|
|
|
|
$btnEdit.off('click').on('click', () => {
|
|
if (window.selectedBroadcastZoneRow) {
|
|
/** @type {BroadcastZone} */
|
|
let bz = {
|
|
index: window.selectedBroadcastZoneRow.index,
|
|
description: window.selectedBroadcastZoneRow.description,
|
|
SoundChannel: window.selectedBroadcastZoneRow.soundChannel,
|
|
Box: window.selectedBroadcastZoneRow.id,
|
|
Relay: window.selectedBroadcastZoneRow.bp
|
|
};
|
|
if (confirm(`Are you sure to edit broadcast zone [${bz.index}] Description=${bz.description} SoundChannel=${bz.SoundChannel} Box=${bz.id} Relay=${bz.bp}?`)) {
|
|
$broadcastzonemodal.modal('show');
|
|
clearBroadcastZoneModal();
|
|
$broadcastzoneindex.val(bz.index);
|
|
$broadcastzonedescription.val(bz.description);
|
|
$broadcastzonesoundchannel.val(bz.SoundChannel);
|
|
$broadcastzonebox.val(bz.Box);
|
|
if (bz.Relay && bz.Relay.length > 0) {
|
|
bz.Relay.split(';').map(Number).filter(n => !isNaN(n) && n >= 1 && n <= 8).forEach(relayId => {
|
|
cbRelay(relayId).prop('checked', true);
|
|
});
|
|
|
|
}
|
|
$broadcastzonemodal.off('click.broadcastzonesave').on('click.broadcastzonesave', '#broadcastzonesave', function () {
|
|
let description = $broadcastzonedescription.val().trim();
|
|
let soundChannel = $broadcastzonesoundchannel.val().trim();
|
|
let box = $broadcastzonebox.val().trim();
|
|
let relayArray = [];
|
|
for (let i = 1; i <= 32; i++) {
|
|
if (cbRelay(i).is(':checked')) {
|
|
relayArray.push(i);
|
|
}
|
|
}
|
|
let relay = relayArray.join(';');
|
|
if (description.length === 0) {
|
|
alert("Description cannot be empty");
|
|
return;
|
|
}
|
|
if (soundChannel.length === 0) {
|
|
alert("Sound Channel cannot be empty");
|
|
return;
|
|
}
|
|
if (box.length === 0) {
|
|
alert("Box cannot be empty");
|
|
return;
|
|
}
|
|
if (relayArray.length === 0) {
|
|
alert("At least one relay must be selected");
|
|
return;
|
|
}
|
|
let bzUpdate = {
|
|
description: description,
|
|
SoundChannel: soundChannel,
|
|
Box: box,
|
|
Relay: relay
|
|
};
|
|
fetchAPI(APIURL_BroadcastZone + "UpdateByIndex/" + bz.index, "PATCH", {}, bzUpdate, (okdata) => {
|
|
reloadBroadcastZones(APIURL_BroadcastZone, () => fill_broadcastzonetablebody(window.BroadcastZoneList));
|
|
alert("Success edit broadcast zone: " + okdata.message);
|
|
}, (errdata) => {
|
|
alert("Error edit broadcast zone: " + errdata.message);
|
|
});
|
|
$broadcastzonemodal.modal('hide');
|
|
});
|
|
$broadcastzonemodal.off('click.broadcastzoneclose').on('click.broadcastzoneclose', '#broadcastzoneclose', function () {
|
|
$broadcastzonemodal.modal('hide');
|
|
});
|
|
}
|
|
}
|
|
});
|
|
|
|
$btnExport.off('click').on('click', () => {
|
|
DoExport(APIURL_BroadcastZone, "broadcastzones.xlsx", {});
|
|
});
|
|
|
|
$btnImport.off('click').on('click', () => {
|
|
DoImport(APIURL_BroadcastZone, (okdata) => {
|
|
reloadBroadcastZones(APIURL_BroadcastZone, () => fill_broadcastzonetablebody(window.BroadcastZoneList));
|
|
alert("Success import broadcast zones: " + okdata.message);
|
|
}, (errdata) => {
|
|
alert("Error importing broadcast zones from XLSX: " + errdata.message);
|
|
});
|
|
});
|
|
}); |