Advertisement
Guest User

Untitled

a guest
May 20th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. class widget_classrooms extends base_widget
  2. {
  3. constructor(name, id, width, height, startingXpos, startingYpos, allowChanges=false)
  4. {
  5. if (name == null) {
  6. super();
  7. return;
  8. }
  9. super(name, id, width, height, startingXpos, startingYpos, allowChanges);
  10. this.rooster_uren = [
  11. ['8:30', '9:20'],
  12. ['9:20', '10:10'],
  13. ['10:30', '11:20'],
  14. ['11:20', '12:10'],
  15. ['12:10', '13:00'],
  16. ['13:00', '13:50'],
  17. ['13:50', '14:40'],
  18. ['15:00', '15:50'],
  19. ['15:50', '16:40'],
  20. ['17:00', '17:50'],
  21. ['17:50', '18:40'],
  22. ['18:40', '19:30'],
  23. ['19:30', '20:20'],
  24. ['20:20', '21:10'],
  25. ['21:10', '22:00']
  26. ];
  27. this.roosters = [];
  28. this.promises = [];
  29.  
  30. this.canvas = document.createElement("canvas");
  31. this.canvas.style.display = 'block';
  32. this.canvas.setAttribute('width', width - 10);
  33. this.canvas.setAttribute('height', height - 75);
  34. //this.canvas.setAttribute('width', '280');
  35. //this.canvas.setAttribute('height', '180');
  36. this.ctx = this.canvas.getContext('2d');
  37. this.classroom_image = new Image();
  38. this.classroom_image.addEventListener('load', ()=>
  39. {
  40. this.ctx.drawImage(this.classroom_image, 0, 0, this.canvas.width, this.canvas.height);
  41. }, false);
  42. this.classroom_image.src = '../assets/classrooms/test.png';
  43.  
  44. this.classrooms = ["EXT","H.0.405","H.1.110","H.1.112","H.1.114","H.1.204","H.1.206","H.1.306","H.1.308","H.1.312","H.1.315","H.1.318","H.1.403","H.2.111","H.2.204","H.2.306","H.2.308","H.2.312","H.2.318","H.2.403","H.3.308","H.3.312","H.4.308","H.4.318","H.5.314-C120","H.5.314-T60","Pantry","RDM","RDM:ID.1.121","RDM:ID.1.211","W.0.116","WD.00.018","WD.00.026","WD.01.003","WD.01.016","WD.01.019","WD.02.002","WD.02.016","WD.02.019","WD.03.005","WD.03.033","WD.04.002","WD.04.016","WD.04.020","WD.05.002","WD.05.005","WD.05.013","WD.05.018","WN.01.014","WN.01.022","WN.01.023","WN.02.007","WN.02.017","WN.02.022","WN.02.026","WN.03.007","WN.03.017","WN.03.022","WN.05.025"];
  45. this.class_options = document.createElement('select');
  46. for (let classroom of this.classrooms)
  47. {
  48. let class_option = document.createElement('option');
  49. class_option.value = classroom;
  50. class_option.text = classroom;
  51. this.class_options.add(class_option);
  52. }
  53. this.class_options.onchange = ()=>{this.get_classroom_availability(this.class_options.value);};
  54. this.widget_body.appendChild(this.canvas);
  55. this.widget_body.appendChild(this.class_options);
  56. this.text_holder = document.createElement('div');
  57. this.widget_body.appendChild(this.text_holder);
  58. this.get_roosters();
  59. }
  60.  
  61. get_les_uur()
  62. {
  63. let current_time = new Date();
  64. for (let i = 0; i < this.rooster_uren.length; i++)
  65. {
  66. let compare_time = new Date(current_time.getFullYear(), current_time.getMonth(), current_time.getDate(), this.rooster_uren[i][0].split(':')[0], this.rooster_uren[i][0].split(':')[1]);
  67. if (current_time <= compare_time)
  68. {
  69. return i;
  70. }
  71. }
  72. return 0;
  73. }
  74.  
  75. get_school_day()
  76. {
  77. let current_day = new Date().getDay();
  78. if (current_day == 0 || current_day == 6)
  79. {
  80. current_day = 1;
  81. }
  82. return --current_day;
  83. }
  84.  
  85. get_classroom_availability(classroom)
  86. {
  87. let classroom_code = this.classrooms.indexOf(classroom);
  88. let uren = this.roosters[classroom_code];
  89. if (uren == undefined){return;}
  90. if (uren[this.get_les_uur()][this.get_school_day()] == 0)
  91. {
  92. for (let i = this.get_les_uur() + 1; i < 15; i++)
  93. {
  94. if (uren[i][this.get_school_day()] == 1)
  95. {
  96. this.text_holder.innerHTML = classroom + ' is beschikbaar tot ' + this.rooster_uren[i][0];
  97. break;
  98. }
  99. if (i == 14) {this.text_holder.innerHTML = classroom + ' is de hele dag beschikbaar';}
  100. }
  101. }
  102. if (uren[this.get_les_uur()][this.get_school_day()] == 1)
  103. {
  104. for (let i = this.get_les_uur() + 1; i < 15; i++)
  105. {
  106. if (uren[i][this.get_school_day()] == 0)
  107. {
  108. this.text_holder.innerHTML = classroom + ' is bezet tot ' + this.rooster_uren[i-1][1];
  109. break;
  110. }
  111. if (i == 14) {this.text_holder.innerHTML = classroom + ' is de hele dag bezet';}
  112. }
  113. }
  114. }
  115.  
  116. get_roosters()
  117. {
  118. this.roosters = [];
  119. this.promises = [];
  120. for (let i = 0; i < this.classrooms.length; i++)
  121. {
  122. let promise = fetch('http://misc.hro.nl/roosterdienst/webroosters/CMI/kw4/20/r/r000' + (''+(i+1)).padStart(2, '0') + '.htm')
  123. .then(
  124. (response)=> {
  125. if (response.status == 200)
  126. {
  127. response.text().then((data)=>
  128. {
  129. let uren =
  130. [
  131. [0,0,0,0,0],
  132. [0,0,0,0,0],
  133. [0,0,0,0,0],
  134. [0,0,0,0,0],
  135. [0,0,0,0,0],
  136. [0,0,0,0,0],
  137. [0,0,0,0,0],
  138. [0,0,0,0,0],
  139. [0,0,0,0,0],
  140. [0,0,0,0,0],
  141. [0,0,0,0,0],
  142. [0,0,0,0,0],
  143. [0,0,0,0,0],
  144. [0,0,0,0,0],
  145. [0,0,0,0,0]
  146. ];
  147. let rooster_element = document.createElement( 'html' );
  148. rooster_element.innerHTML = data;
  149. let parsed_rooster = rooster_element.getElementsByTagName('table')[0];
  150. parsed_rooster.deleteRow(0);
  151. for (let i = 0; i < parsed_rooster.rows.length; i++)
  152. {
  153. if (parsed_rooster.rows[i].cells.length == 0)
  154. {
  155. parsed_rooster.deleteRow(i--);
  156. continue;
  157. }
  158. parsed_rooster.rows[i].deleteCell(0);
  159. let added_rows = 0;
  160. for (let j = 0; j < parsed_rooster.rows[i].cells.length; j++)
  161. {
  162. if (parsed_rooster.rows[i].cells[j].innerHTML != '<table><tbody><tr><td></td></tr></tbody></table>')
  163. {
  164. let iterations = parsed_rooster.rows[i].cells[j].rowSpan/2;
  165. let missing_rows = 0;
  166. for (let l = 0; l <= j; l++)
  167. {
  168. while (uren[i][l+missing_rows-added_rows] == 1)
  169. {
  170. missing_rows++;
  171. }
  172. }
  173. for (let k = 0; k < iterations; k++)
  174. {
  175. uren[i+k][j+missing_rows-added_rows] = 1;
  176. }
  177. added_rows++;
  178. }
  179. }
  180. }
  181. this.roosters[i] = uren;
  182. });
  183. }
  184. }
  185. )
  186. .catch(function(err) {
  187. console.log('Fetch Error :-S', err);
  188. });
  189. this.promises.push(promise);
  190. }
  191. Promise.all(this.promises).then(values => {
  192. console.log('all promises resolved');
  193. });
  194. }
  195.  
  196. make_new_widget(name, id, width, height, startingXpos, startingYpos, allowChanges)
  197. {
  198. return new widget_classrooms(name, id, width, height, startingXpos, startingYpos, allowChanges);
  199. }
  200.  
  201. resized()
  202. {
  203. super.resized()
  204. this.canvas.setAttribute('width', this.widget_body.clientWidth);
  205. this.canvas.setAttribute('height', this.widget_body.clientHeight - 35);
  206. this.ctx.drawImage(this.classroom_image, 0, 0, this.canvas.width, this.canvas.height);
  207. }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement