Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class widget_classrooms extends base_widget
- {
- constructor(name, id, width, height, startingXpos, startingYpos, allowChanges=false)
- {
- if (name == null) {
- super();
- return;
- }
- super(name, id, width, height, startingXpos, startingYpos, allowChanges);
- this.rooster_uren = [
- ['8:30', '9:20'],
- ['9:20', '10:10'],
- ['10:30', '11:20'],
- ['11:20', '12:10'],
- ['12:10', '13:00'],
- ['13:00', '13:50'],
- ['13:50', '14:40'],
- ['15:00', '15:50'],
- ['15:50', '16:40'],
- ['17:00', '17:50'],
- ['17:50', '18:40'],
- ['18:40', '19:30'],
- ['19:30', '20:20'],
- ['20:20', '21:10'],
- ['21:10', '22:00']
- ];
- this.roosters = [];
- this.promises = [];
- this.canvas = document.createElement("canvas");
- this.canvas.style.display = 'block';
- this.canvas.setAttribute('width', width - 10);
- this.canvas.setAttribute('height', height - 75);
- //this.canvas.setAttribute('width', '280');
- //this.canvas.setAttribute('height', '180');
- this.ctx = this.canvas.getContext('2d');
- this.classroom_image = new Image();
- this.classroom_image.addEventListener('load', ()=>
- {
- this.ctx.drawImage(this.classroom_image, 0, 0, this.canvas.width, this.canvas.height);
- }, false);
- this.classroom_image.src = '../assets/classrooms/test.png';
- 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"];
- this.class_options = document.createElement('select');
- for (let classroom of this.classrooms)
- {
- let class_option = document.createElement('option');
- class_option.value = classroom;
- class_option.text = classroom;
- this.class_options.add(class_option);
- }
- this.class_options.onchange = ()=>{this.get_classroom_availability(this.class_options.value);};
- this.widget_body.appendChild(this.canvas);
- this.widget_body.appendChild(this.class_options);
- this.text_holder = document.createElement('div');
- this.widget_body.appendChild(this.text_holder);
- this.get_roosters();
- }
- get_les_uur()
- {
- let current_time = new Date();
- for (let i = 0; i < this.rooster_uren.length; i++)
- {
- 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]);
- if (current_time <= compare_time)
- {
- return i;
- }
- }
- return 0;
- }
- get_school_day()
- {
- let current_day = new Date().getDay();
- if (current_day == 0 || current_day == 6)
- {
- current_day = 1;
- }
- return --current_day;
- }
- get_classroom_availability(classroom)
- {
- let classroom_code = this.classrooms.indexOf(classroom);
- let uren = this.roosters[classroom_code];
- if (uren == undefined){return;}
- if (uren[this.get_les_uur()][this.get_school_day()] == 0)
- {
- for (let i = this.get_les_uur() + 1; i < 15; i++)
- {
- if (uren[i][this.get_school_day()] == 1)
- {
- this.text_holder.innerHTML = classroom + ' is beschikbaar tot ' + this.rooster_uren[i][0];
- break;
- }
- if (i == 14) {this.text_holder.innerHTML = classroom + ' is de hele dag beschikbaar';}
- }
- }
- if (uren[this.get_les_uur()][this.get_school_day()] == 1)
- {
- for (let i = this.get_les_uur() + 1; i < 15; i++)
- {
- if (uren[i][this.get_school_day()] == 0)
- {
- this.text_holder.innerHTML = classroom + ' is bezet tot ' + this.rooster_uren[i-1][1];
- break;
- }
- if (i == 14) {this.text_holder.innerHTML = classroom + ' is de hele dag bezet';}
- }
- }
- }
- get_roosters()
- {
- this.roosters = [];
- this.promises = [];
- for (let i = 0; i < this.classrooms.length; i++)
- {
- let promise = fetch('http://misc.hro.nl/roosterdienst/webroosters/CMI/kw4/20/r/r000' + (''+(i+1)).padStart(2, '0') + '.htm')
- .then(
- (response)=> {
- if (response.status == 200)
- {
- response.text().then((data)=>
- {
- let uren =
- [
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0]
- ];
- let rooster_element = document.createElement( 'html' );
- rooster_element.innerHTML = data;
- let parsed_rooster = rooster_element.getElementsByTagName('table')[0];
- parsed_rooster.deleteRow(0);
- for (let i = 0; i < parsed_rooster.rows.length; i++)
- {
- if (parsed_rooster.rows[i].cells.length == 0)
- {
- parsed_rooster.deleteRow(i--);
- continue;
- }
- parsed_rooster.rows[i].deleteCell(0);
- let added_rows = 0;
- for (let j = 0; j < parsed_rooster.rows[i].cells.length; j++)
- {
- if (parsed_rooster.rows[i].cells[j].innerHTML != '<table><tbody><tr><td></td></tr></tbody></table>')
- {
- let iterations = parsed_rooster.rows[i].cells[j].rowSpan/2;
- let missing_rows = 0;
- for (let l = 0; l <= j; l++)
- {
- while (uren[i][l+missing_rows-added_rows] == 1)
- {
- missing_rows++;
- }
- }
- for (let k = 0; k < iterations; k++)
- {
- uren[i+k][j+missing_rows-added_rows] = 1;
- }
- added_rows++;
- }
- }
- }
- this.roosters[i] = uren;
- });
- }
- }
- )
- .catch(function(err) {
- console.log('Fetch Error :-S', err);
- });
- this.promises.push(promise);
- }
- Promise.all(this.promises).then(values => {
- console.log('all promises resolved');
- });
- }
- make_new_widget(name, id, width, height, startingXpos, startingYpos, allowChanges)
- {
- return new widget_classrooms(name, id, width, height, startingXpos, startingYpos, allowChanges);
- }
- resized()
- {
- super.resized()
- this.canvas.setAttribute('width', this.widget_body.clientWidth);
- this.canvas.setAttribute('height', this.widget_body.clientHeight - 35);
- this.ctx.drawImage(this.classroom_image, 0, 0, this.canvas.width, this.canvas.height);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement