summaryrefslogtreecommitdiffstats
path: root/src/js/time.js
blob: 175725701c7fe1bc326f1bd4dae7bbf56a66a96e (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
import { load as load_template } from './templates';
import Mustache from 'mustache';

var template;
var root;
var page = {
    date: {
        day: '',
        hour: ''
    },
    weather: {
        icon: 'fas fa-cloud-sun-rain',
        temperature: '20ºC'
    }
}

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 initInterval() {
    setInterval(function() {
        var date = new Date();
        page.date.day = days[date.getDay()],
        page.date.hour = formatAMPM(date);
        show();
    }, 1000);
}

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