56 lines
		
	
	
	
		
			1.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			56 lines
		
	
	
	
		
			1.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| var destroy = [];
 | |
| 
 | |
| function loadSmiley() {
 | |
| 	var smileys = document.getElementsByTagName('smiley');
 | |
| 	for (var i = 0; i < smileys.length; i++) {
 | |
| 		var smiley = smileys[i];
 | |
| 		destroy.push(getSmiley(smiley, Number.parseInt(smiley.getAttribute('phase'))-1, smiley.getAttribute('type')));
 | |
| 	}
 | |
| }
 | |
| 
 | |
| function destroySmiley() {
 | |
| 	for (var i = 0; i < destroy.length; i++) {
 | |
| 		if (destroy[i] == null) continue;
 | |
| 		destroy[i]();
 | |
| 	}
 | |
| 	destroy = [];
 | |
| }
 | |
| 
 | |
| function getSmiley(ctx, phase, type) {
 | |
| 	if (type == undefined) return null;
 | |
| 	var meter=document.getElementById("meter1").value;
 | |
| 
 | |
| 	var updateSmiley = function() {
 | |
| 		fetch('https://5groningen02.housing.rug.nl/api/trading/'+meter+'/scores')
 | |
| 			.then(function (response) {
 | |
| 				if (response.status != 200) return new Promise((resolve) => { resolve([]); });
 | |
| 				return response.json()
 | |
| 			})
 | |
| 			.then(function (data) {
 | |
| 				if (data == null) return;
 | |
| 
 | |
| 				var value = data[phase][type]
 | |
| 				var c = 'neutral'
 | |
| 				if (value >= 1) {
 | |
| 					c = 'positive'
 | |
| 				}
 | |
| 				else if (value <= -1) {
 | |
| 					c = 'negative'
 | |
| 				}
 | |
| 
 | |
| 				ctx.className = c;
 | |
| 				var text = value.toFixed(1);
 | |
| 				if (text == "-0.0") text = "0.0";
 | |
| 				ctx.innerText = text;
 | |
| 			})
 | |
| 	}
 | |
| 
 | |
| 	updateSmiley();
 | |
| 	var interval = setInterval(function() {
 | |
| 		updateSmiley();
 | |
| 	}, 5000)
 | |
| 
 | |
| 	return function() {
 | |
| 		clearInterval(interval);
 | |
| 	}
 | |
| }
 | 
