test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. let sd = {};
  2. let rf=false;
  3. function getStatus() {
  4. const config = {};
  5. window.$(`#valuesTable input:text, #valuesTable input:checked`).each(function(_index, entry) {
  6. switch (entry.attributes.dtype.value) {
  7. case 'string':
  8. config[entry.name] = entry.value;
  9. break;
  10. case 'number':
  11. config[entry.name] = Number(entry.value);
  12. break;
  13. case 'boolean':
  14. config[entry.name] = entry.value=='true';
  15. break;
  16. default:
  17. break;
  18. }
  19. });
  20. return config;
  21. }
  22. // function getOptions(entry) {
  23. // let output='';
  24. // for (const property in entry) {
  25. // output+=`<option value="${entry[property]}">${property}</option>`;
  26. // }
  27. // return output;
  28. // }
  29. function getRadioButton(entry){
  30. let output='';
  31. for (const property in sd[entry]) {
  32. output+=`
  33. <div class="custom-control custom-radio">
  34. <input type="radio" class="custom-control-input" id="${entry}_${sd[entry][property]}" name="${entry}" value="${sd[entry][property]}" dtype='${typeof(sd[entry][property])}'>
  35. <label class="custom-control-label" for="${entry}_${sd[entry][property]}">${property}</label>
  36. </div>
  37. `;
  38. }
  39. return output;
  40. }
  41. window.refreshStatus = function() {
  42. if(Object.keys(sd).length>0){
  43. if(rf) return;
  44. rf=true;
  45. window.$.getJSON('/status.json', function(data) {
  46. for (const property in data) {
  47. const val = data[property];
  48. let input = $(`#val_${property}, #valuesTable input[name="${property}"]`) ;
  49. if(input.length>0){
  50. if(input.is(':radio') ){
  51. $(`#${property}_${val ?? 0}`).prop('checked',true);
  52. }
  53. else {
  54. if(input.val() !==val && !input.is(":focus")){
  55. input.val(val);
  56. }
  57. }
  58. }
  59. else {
  60. if(sd[property]){
  61. window.$('#valuesTable').append(
  62. `<tr><td>${property}</td>
  63. <td >
  64. ${getRadioButton(property)}
  65. </td></tr>`);
  66. $(`#${property}_${val ?? 0}`).prop('checked',true);
  67. }
  68. else {
  69. window.$('#valuesTable').append(`<tr><td>${property}</td><td><input type='text' class='value form-control nvs' id="val_${property}" name='${property}' dtype='${typeof(val)}' ></input></td></tr>`);
  70. window.$(`#val_${property}`).val(val);
  71. }
  72. }
  73. }
  74. })
  75. .fail(function() {
  76. })
  77. .done(function(){
  78. rf=false;
  79. });
  80. }
  81. else {
  82. window.$.getJSON('/statusdefinition.json', function(data) {
  83. sd=data;
  84. })
  85. .fail(function() {
  86. })
  87. .done(function(){
  88. });
  89. }
  90. }
  91. function pushStatus(){
  92. const data = {
  93. timestamp: Date.now(),
  94. status: getStatus()
  95. };
  96. window.$.ajax({
  97. url: '/status.json',
  98. dataType: 'text',
  99. method: 'POST',
  100. cache: false,
  101. contentType: 'application/json; charset=utf-8',
  102. data: JSON.stringify(data),
  103. });
  104. console.log('sent config JSON with data:', JSON.stringify(data));
  105. }
  106. window.$(document).ready(function() {
  107. window.$('#save_status').on('click', function() {
  108. pushStatus();
  109. });
  110. window.$( "#valuesTable" ).change(function() {
  111. pushStatus();
  112. });
  113. setInterval(window.refreshStatus, 1000);
  114. $('svg >> symbol').each(function() {
  115. $('#allIcons').append( `<svg style="fill:white; width:1.5rem; height: 1.5rem;">
  116. <use xlink:href="#${this.id}"></use>
  117. </svg>`);
  118. });
  119. }) ;