11// html/DeviceConfig/deviceConfig.js
22// Description: JavaScript for the device configuration page.
33
4- import { GetPSQLTable , GetPSQL } from '/includes/functions.js' ;
4+ import { GetPSQLTable } from '/includes/functions.js' ;
5+ import { dataTable } from '/includes/tooldaq.js' ;
6+ import '/includes/ace.js'
57
6- if ( document . readyState !== 'loading' ) {
7- //console.log("already loaded, initing");
8- Init ( ) ;
8+ let editor ;
9+
10+ if ( document . readyState !== 'loading' ) {
11+ //console.log("already loaded, initing");
12+ Init ( ) ;
913} else {
10- //console.log("page still loading, waiting to finish...");
11- document . addEventListener ( "DOMContentLoaded" , function ( ) {
12- //console.log("page loaded, initing");
13- Init ( ) ;
14- } ) ;
14+ //console.log("page still loading, waiting to finish...");
15+ document . addEventListener ( "DOMContentLoaded" , function ( ) {
16+ //console.log("page loaded, initing");
17+ Init ( ) ;
18+ } ) ;
1519}
1620
17- function Init ( ) {
18- //console.log("Initialising page");
19- GetDeviceConfigs ( ) ;
20- document . getElementById ( "addDeviceConfigBtn" ) . addEventListener ( "click" , addDeviceConfig ) ;
21+ function Init ( ) {
22+ //console.log("Initialising page");
23+ setupEditor ( ) ;
24+ GetDeviceConfigs ( ) ;
25+ document . getElementById ( "addDeviceConfigBtn" ) . addEventListener ( "click" , addDeviceConfig ) ;
26+ }
27+
28+ function setupEditor ( ) {
29+ editor = ace . edit ( "jsonEditor" ) ;
30+ editor . setTheme ( "ace/theme/chrome" ) ;
31+ editor . session . setMode ( "ace/mode/json" ) ;
32+ editor . setValue ( "{\n \"devices\": []\n}" , - 1 ) ;
2133}
2234
2335function GetDeviceConfigs ( ) {
24- const query = "SELECT * FROM device_config ORDER BY time DESC LIMIT 10" ;
25- GetPSQLTable ( query , "root" , "daq" , true ) . then ( function ( result ) {
26- const deviceConfigOutput = document . getElementById ( "deviceConfigOutput" ) ;
27- deviceConfigOutput . innerHTML = result ;
28- } ) . catch ( function ( error ) {
29- console . error ( "Error fetching device configurations:" , error ) ;
30- } ) ;
36+ const query = "SELECT * FROM device_config ORDER BY time DESC LIMIT 20" ;
37+ dataTable ( query , "deviceConfigOutput" ) ;
3138}
3239
3340function addDeviceConfig ( ) {
3441 const device = document . getElementById ( "device" ) . value ;
3542 const author = document . getElementById ( "author" ) . value ;
3643 const description = document . getElementById ( "description" ) . value ;
37- const data = document . getElementById ( "data" ) . value ;
44+ const data = editor . getValue ( ) ;
3845
3946 // Ensure data is in JSON format
4047 try {
@@ -51,9 +58,17 @@ function addDeviceConfig() {
5158 ` ;
5259
5360 GetPSQLTable ( query , "root" , "daq" , true ) . then ( ( ) => {
54- // Refresh the table after adding a new config
61+ alert ( "Device configuration saved successfully." ) ;
62+ clearDeviceConfigInputs ( ) ;
5563 GetDeviceConfigs ( ) ;
5664 } ) . catch ( function ( error ) {
5765 console . error ( "Error adding device configuration:" , error ) ;
5866 } ) ;
5967}
68+
69+ function clearDeviceConfigInputs ( ) {
70+ document . getElementById ( "device" ) . value = "" ;
71+ document . getElementById ( "author" ) . value = "" ;
72+ document . getElementById ( "description" ) . value = "" ;
73+ editor . setValue ( "{\n \"devices\": []\n}" , - 1 ) ;
74+ }
0 commit comments