summaryrefslogtreecommitdiffstats
path: root/src/js/date.js
blob: 81f3dbc7562c3831185f44b667b938b530dd4840 (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
91
92
93
import Mustache from 'mustache';
import { load as load_template } from './templates';
import * as app from './app';

var monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];

var template;
var page = {
    date: {
        days: [],
        months: [],
        years: [],
        isDay: function() {
            var now = new Date();
            return now.getDate() === this;
        },
        getMonthName: function() {
            return monthNames[this];
        },
        isMonth: function() {
            var now = new Date();
            return now.getMonth() === this;
        },
        isYear: function() {
            var now = new Date();
            return now.getFullYear() === this;
        }
    },
    time: {
        hours: [],
        minutes: [],
        ampm: ['AM', 'PM'],
        isHour: function() {
            var now = new Date();
            return now.getHours()%12 === this;
        },
        isMinutes: function() {
            var now = new Date();
            return now.getMinutes() === this;
        },
        isAmpm: function (){
            var now = new Date();
            if( this === 'AM' && now.getHours() < 12 ) {
                return true;
            } else {
                return false;
            }
        }
    }
}

function render(){
    document.body.innerHTML = Mustache.render(template, page);
}

export function init() {
    for ( var i=0; i<31; i++) {
        page.date.days[i] = i+1;
    };

    for ( var i = 0; i < 50; i++){
        page.date.years[i] = 2010 + i;
    };

    for ( var i = 0; i < 12; i++){
        page.date.months[i] = i;
    };

    for ( var i=0; i<12; i++ ) {
        page.time.hours[i] = i+1;
    };

    for ( var i=0; i<60; i++ ) {
        page.time.minutes[i] = i
    }

    load_template('date.template.html').then(function(result) {
        template = result;
        Mustache.parse(template);
    }, function(error) {
        console.error('ERROR Loading date template', error);
    });
}

export function show() {
    render();
}

export function hide() {
    app.show();
}