const { ipcRenderer } = require('electron'); const DEVICE = require('./device.js'); const PLUGINS = require('./plugins.js'); // const _ = require('lodash/function'); const pinnedDevices = []; module.exports.pinnedDevices = pinnedDevices; let activeDevice = false; let drawCount = 0; module.exports.init = function () { populatePluginLists(); }; drawDeviceFrame = function (id) { // console.log("DRAW") const $deviceDrawArea = document.getElementById(`device-${id}-draw-area`); const $devicePinned = document.getElementById(`device-${id}-pinned`); if ($deviceDrawArea == null) { return true; } d = DEVICE.all[id]; let str = ''; if (d.status == 'ok') { str += ""; } // scrollbar styles are inline to prevent the styles flickering in str += ''; str += ""; str += ''; str += generateBodyHTML(d); str += ''; $deviceDrawArea.setAttribute('class', `${d.type} draw-area`); $deviceDrawArea.contentWindow.document.open(); $deviceDrawArea.contentWindow.document.write(str); if (d.pinIndex) { $devicePinned.style.display = 'block'; } else { $devicePinned.style.display = 'none'; } }; generateBodyHTML = function(d){ let str = ""; if (d.status == 'ok') { try { str += PLUGINS.all[d.type].template({ data: d.data, listName: d.displayName || d.defaultName, }); } catch (err) { console.log(err); str += '

Plugin Template Error

'; } } else { str += '

' + (d.displayName || d.defaultName) + '

'; str += "
"; str += '

' + d.type + ' is not responding to requests for data.

'; str += '

IP ' + d.addresses[0] + '

'; str += '

Port ' + d.port + '

'; } return str; } module.exports.draw = function (device) { if (device == undefined || device.status=="new") { return true; } if(drawCount==0){ drawDeviceFrame(device.id); }else{ let $deviceDrawArea = document.getElementById(`device-${device.id}-draw-area`).contentDocument.body; $deviceDrawArea.innerHTML = generateBodyHTML(DEVICE.all[device.id]); let $scrollTo = $deviceDrawArea.getElementsByClassName("scroll-position"); if($scrollTo.length==1){ document.getElementById(`device-${device.id}-draw-area`).contentWindow.scroll({top: $scrollTo[0].offsetTop-200, behavior: 'smooth'}); } } drawCount++; }; module.exports.addDeviceToList = function (device) { var d = device; var addressStr = d.addresses[0] || ''; for (var i = 1; i < d.addresses.length; i++) { addressStr += ', ' + d.addresses[i]; } var html = ''; if (d.status == 'ok') { html += "
done
"; } else if (d.status == 'refresh') { html += "
refresh
"; } else { html += "
clear
"; } html += "
"; html += "
" + (d.displayName || d.defaultName) + '
'; let elem = document.getElementById(d.id); if (elem == null) { document .getElementById('device-list') .insertAdjacentHTML( 'beforeend', "" + html + '' ); } else { elem.innerHTML = html; } }; module.exports.removeDeviceFromList = function (device) { var d = device; document.getElementById(device.id).remove(); }; switchDevice = function (id) { if (activeDevice && activeDevice.pinIndex == false) { document.getElementById('device-' + activeDevice.id).remove(); activeDevice = false; } activeDevice = DEVICE.all[id]; var cols = 1; if (pinnedDevices.length > 0) { cols += pinnedDevices.length; } if (pinnedDevices.indexOf(activeDevice) >= 0 || id == undefined) { cols--; } document.getElementById('all-devices').style.gridTemplateColumns = 'repeat(' + cols + ', 1fr)'; if (id == undefined) { // document.getElementById('refresh-device-button').style.opacity = 0.2; document.getElementById('refresh-device-button').disabled = true; document.getElementById('device-settings-table').style.display = 'none'; return true; } else { document.getElementById('refresh-device-button').disabled = false; // document.getElementById('refresh-device-button').style.opacity = 1; } var i = DEVICE.all[id].id; var $deviceWrapper = document.getElementById('device-' + i); if (!$deviceWrapper) { var html = `
`; document .getElementById('all-devices') .insertAdjacentHTML('afterbegin', html); $deviceWrapper = document.getElementById(`device-${i}`); } switchClass(document.getElementById(id), 'active-device'); drawDeviceFrame(id); switchClass(document.getElementById(`device-${id}`), 'active-device-outline'); document.getElementById('device-settings-table').style.display = 'block'; document.getElementById('device-settings-plugin-dropdown').value = activeDevice.type; document.getElementById('device-settings-name').value = activeDevice.displayName || activeDevice.defaultName || ''; document.getElementById('device-settings-ip').value = activeDevice.addresses[0] || ''; document.getElementById('device-settings-port').value = activeDevice.port || ''; document.getElementById('device-settings-pin').checked = activeDevice.pinIndex; ipcRenderer.send('enableDeviceDropdown', ''); ipcRenderer.send('setDevicePin', !DEVICE.all[id].pinIndex == false); }; module.exports.switchDevice = switchDevice; module.exports.getActiveDevice = function () { return activeDevice; }; module.exports.pinActiveDevice = function () { if (activeDevice == undefined) { return true; } if (pinnedDevices.indexOf(DEVICE.all[activeDevice.id]) == -1) { pinnedDevices.push(DEVICE.all[activeDevice.id]); } DEVICE.changeActivePinIndex(true); }; module.exports.unpinActiveDevice = function () { if (activeDevice == undefined) { return true; } pinnedDevices.splice(pinnedDevices.indexOf(DEVICE.all[activeDevice.id]), 1); DEVICE.changeActivePinIndex(false); }; module.exports.pinDevice = function (device) { pinnedDevices.push(device); DEVICE.changePinIndex(device, true); }; module.exports.unpinDevice = function (device) { pinnedDevices.push(device); DEVICE.changePinIndex(device, false); }; module.exports.resetPinned = function () { pinnedDevices.length = 0; activeDevice = false; // switchDevice(); try { document .querySelector('#device-list .active-device') .classList.remove('active-device'); } catch (err) {} document.getElementById('all-devices').innerHTML = ''; }; module.exports.getPinnedDevices = function () { return pinnedDevices; }; module.exports.toggleSlotButtons = function (slotIndex) { if (slotIndex == 1) { document.getElementById('save-slot-1').classList.add('active'); document.getElementById('save-slot-2').classList.remove('active'); document.getElementById('save-slot-3').classList.remove('active'); } else if (slotIndex == 2) { document.getElementById('save-slot-1').classList.remove('active'); document.getElementById('save-slot-2').classList.add('active'); document.getElementById('save-slot-3').classList.remove('active'); } else if (slotIndex == 3) { document.getElementById('save-slot-1').classList.remove('active'); document.getElementById('save-slot-2').classList.remove('active'); document.getElementById('save-slot-3').classList.add('active'); } }; module.exports.selectPreviousDevice = function () { if (activeDevice == undefined) { return true; } var keys = Object.keys(DEVICE.all); var prevIndex = Math.max(0, keys.indexOf(activeDevice.id) - 1); switchDevice(keys[prevIndex]); }; module.exports.selectNextDevice = function () { if (activeDevice == undefined) { return true; } var keys = Object.keys(DEVICE.all); var prevIndex = Math.min(keys.length - 1, keys.indexOf(activeDevice.id) + 1); switchDevice(keys[prevIndex]); }; populatePluginLists = function () { var typeSelect = ''; var addSelect = ''; for (const pluginType in PLUGINS.all) { var plugin = PLUGINS.all[pluginType]; addSelect += "'; typeSelect += "'; } document.getElementById('device-settings-plugin-dropdown').innerHTML = typeSelect; document.getElementById('add-device-button').innerHTML = addSelect; }; function debounce(func, wait, immediate) { var timeout; return function () { var context = this, args = arguments; var later = function () { timeout = null; if (!immediate) func.apply(context, args); }; var callNow = immediate && !timeout; clearTimeout(timeout); timeout = setTimeout(later, wait); if (callNow) func.apply(context, args); }; }