aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/time.js
blob: 3c22419f378431d5d8d629b1662400d9e72ebd52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { load as load_template } from './templates';
import Mustache from 'mustache';
import { weather } from 'agl-js-api';

var template;
var root;
var counter = 0;
var interval;
var updateTime = 10000;
var page = {
    date: {
        day: '',
        hour: ''
    },
    weather: undefined,
    showTemperature: function() {
        return counter%5 === 0;
    },
    showWind: function() {
        return counter%5 === 1;
    },
    showPosition: function() {
        return counter%5 === 2;
    },
    showHumidity: function() {
        return counter%5 === 3;
    },
    showDescription: function() {
        return counter%5 === 4;
    }
}

var days = ['SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY'];

function show() {
    root.innerHTML = Mustache.render(template, page);
}

function formatAMPM(date) {
    var hours = date.getHours();
    var minutes = date.getMinutes();
    var ampm = hours >= 12 ? 'PM' : 'AM';
    hours = hours % 12;
    hours = hours ? hours : 12;
    minutes = minutes < 10 ? '0'+minutes : minutes;
    var strTime = hours + ':' + minutes + ' ' + ampm;
    return strTime;
}

function update() {
    var date = new Date();
    page.date.day = days[date.getDay()],
    page.date.hour = formatAMPM(date);
    if( counter === 0 || !page.weather) {
        weather.current_weather().then(function(result) {
            page.weather = result;
            show();
        }, function(error) {
            console.error(error);
            show();
        });
    } else {
        show();
    }
    counter = (counter+1) % Math.floor(300000/updateTime);
}

function initInterval() {
    clearInterval(interval);
    interval = setInterval(update, updateTime);
}

export function init(node) {
    load_template('time.template.html').then(function(result) {
        template = result;
        root = node;
        Mustache.parse(template);
        update();
        initInterval();
    }, function(error) {
        console.error('ERRROR loading main template', error);
    });
}

export function refresh() {
    clearInterval();
    counter = 0;
    update();
    initInterval();
}