script_dummy.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. let dataJson = '[{ \
  2. "id": 0, \
  3. "name": "OnStep",\
  4. "state": true,\
  5. "voltage": 19.5,\
  6. "pin": 32\
  7. },\
  8. {"id": 1, \
  9. "name": "Canon EOS 50D",\
  10. "state": false,\
  11. "voltage": 8.7,\
  12. "pin": 33\
  13. },\
  14. {"id": 2, \
  15. "name": "Raspberry Pi",\
  16. "state": false,\
  17. "voltage": 5,\
  18. "pin": 25\
  19. },\
  20. {"id": 3, \
  21. "name": "Dew heaters",\
  22. "state": false,\
  23. "voltage": 12,\
  24. "pin": 26\
  25. }]';
  26. let dataObject = JSON.parse(dataJson)
  27. function toggleState(id) {
  28. let data = dataObject[id];
  29. console.log(data);
  30. if (data.state == true) {
  31. data.state = false;
  32. }
  33. else {
  34. data.state = true;
  35. }
  36. let led = document.getElementById("led_" + id);
  37. led.className = data.state ? 'led-green' : 'led-red';
  38. let button = document.getElementById("toggle_button_" + id);
  39. button.innerHTML = 'toggle ' + (data.state ? 'off' : 'on');
  40. dataObject[id] = data;
  41. }
  42. function turnOffAll() {
  43. for (let i = 0; i < dataObject.length; i++) {
  44. dataObject[i].state = false;
  45. let led = document.getElementById("led_" + i);
  46. led.className = 'led-red';
  47. }
  48. }
  49. function initialLoad() {
  50. let data = dataObject;
  51. console.log(data);
  52. const content = document.getElementById('content');
  53. for (let i = 0; i < data.length; i++) {
  54. let card = document.createElement("div");
  55. card.id = "card" + i;
  56. card.className = 'card'
  57. let name = document.createElement("h2");
  58. name.id = "card_name_" + i;
  59. name.innerHTML = data[i].name;
  60. card.appendChild(name);
  61. let state = document.createElement("p");
  62. let led = document.createElement("div");
  63. led.id = 'led_' + i;
  64. led.className = data[i].state ? 'led-green' : 'led-red';
  65. state.appendChild(led)
  66. card.appendChild(state);
  67. let button = document.createElement("button");
  68. button.className = 'toggle_button';
  69. button.id = 'toggle_button_' + i;
  70. button.innerHTML = 'toggle ' + (data[i].state ? 'off' : 'on');
  71. button.name = data[i].name;
  72. button.onclick = function () { toggleState(i) };
  73. card.appendChild(button);
  74. let div = document.createElement("div");
  75. div.className = 'options_button_div';
  76. let optionsButton = document.createElement("button");
  77. optionsButton.id = "options_button_" + i;
  78. optionsButton.className = 'options_button';
  79. optionsButton.onclick = function () { editProperties(i) };
  80. let img = document.createElement("img");
  81. img.setAttribute("src", "options.svg");
  82. optionsButton.appendChild(img);
  83. div.appendChild(optionsButton);
  84. card.appendChild(div);
  85. content.appendChild(card);
  86. }
  87. }
  88. function editProperties(id) {
  89. let data = dataObject[id];
  90. // Get the modal
  91. var modal = document.getElementById("propertiesWindow");
  92. modal.style.display = "block";
  93. // When the user clicks anywhere outside of the modal, close it
  94. window.onclick = function (event) {
  95. if (event.target == modal) {
  96. closePropertiesWindow();
  97. }
  98. }
  99. let p = document.getElementById("propertiesContent");
  100. let divName = document.createElement("div");
  101. divName.id = "div_name"
  102. let labelName = document.createElement("label");
  103. labelName.setAttribute('for', 'powerOutletName');
  104. labelName.innerHTML = 'Name';
  105. divName.appendChild(labelName)
  106. let inputName = document.createElement("input", 'type="text"');
  107. inputName.id = "powerOutletName";
  108. inputName.setAttribute("value", data.name);
  109. divName.appendChild(inputName);
  110. p.appendChild(divName);
  111. let divPin = document.createElement("div");
  112. divPin.id = 'div_pin'
  113. let labelPin = document.createElement("label");
  114. labelPin.setAttribute('for', 'powerOutletPin');
  115. labelPin.innerHTML = 'Pin';
  116. divPin.appendChild(labelPin)
  117. let inputPin = document.createElement("input", 'type="text"');
  118. inputPin.id = 'powerOutletPin';
  119. inputPin.setAttribute("value", data.pin);
  120. divPin.appendChild(inputPin);
  121. p.appendChild(divPin);
  122. let divState = document.createElement("div");
  123. divState.id = 'div_state';
  124. let labelState = document.createElement("label");
  125. labelState.setAttribute('for', 'powerOutletState');
  126. labelState.innerHTML = 'State';
  127. divState.appendChild(labelState)
  128. let inputState = document.createElement("input", 'type="text"');
  129. inputState.id = 'powerOutletState';
  130. inputState.setAttribute("value", data.state);
  131. divState.appendChild(inputState);
  132. p.appendChild(divState);
  133. let buttonSave = document.createElement("button");
  134. buttonSave.id = "button_save";
  135. buttonSave.className = 'toggle_button';
  136. buttonSave.innerHTML = "Save";
  137. buttonSave.onclick = function () { saveProperties(id) };
  138. p.appendChild(buttonSave);
  139. }
  140. function saveProperties(id) {
  141. const requestOptions = {
  142. method: 'PUT',
  143. headers: { 'Content-Type': 'application/json' },
  144. };
  145. let response = {};
  146. response.id = id;
  147. response.name = document.getElementById('powerOutletName').value;
  148. requestOptions.body = JSON.stringify(response);
  149. console.log(response);
  150. document.getElementById('card_name_' + id).innerHTML = response.name;
  151. closePropertiesWindow();
  152. }
  153. function closePropertiesWindow() {
  154. document.getElementById('propertiesWindow').style.display = 'none';
  155. document.getElementById('div_name').remove();
  156. document.getElementById('div_pin').remove();
  157. document.getElementById('div_state').remove();
  158. document.getElementById('button_save').remove();
  159. }