forked from bryanbraun/your-life
-
Notifications
You must be signed in to change notification settings - Fork 0
/
your-life.js
110 lines (98 loc) · 3.24 KB
/
your-life.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Interactive form and chart events / logic.
*/
(function() {
var yearEl = document.getElementById('year'),
monthEl = document.getElementById('month'),
dayEl = document.getElementById('day'),
unitboxEl = document.getElementById('unitbox'),
unitText = document.querySelector('.unitbox-label').textContent.toLowerCase(),
items = document.querySelectorAll('.chart li'),
itemCount,
COLOR = 'red',
KEY = {
UP: 38,
DOWN: 40
};
// Set listeners
unitboxEl.addEventListener('change', _handleUnitChange);
yearEl.addEventListener('input', _handleDateChange);
yearEl.addEventListener('keydown', _handleUpdown);
yearEl.addEventListener('blur', _unhideValidationStyles);
monthEl.addEventListener('change', _handleDateChange);
dayEl.addEventListener('input', _handleDateChange);
dayEl.addEventListener('blur', _unhideValidationStyles);
// Ensure the month is unselected by default.
monthEl.selectedIndex = -1;
// Event Handlers
function _handleUnitChange(e) {
window.location = '' + e.currentTarget.value + '.html';
}
function _handleDateChange(e) {
if (_dateIsValid()) {
itemCount = calculateElapsedTime();
_repaintItems(itemCount);
} else {
_repaintItems(0);
}
}
function _handleUpdown(e) {
var newNum;
// A crossbrowser keycode option.
thisKey = e.keyCode || e.which;
if (e.target.checkValidity()) {
if (thisKey === KEY.UP) {
newNum = parseInt(e.target.value, 10);
e.target.value = newNum += 1;
// we call the date change function manually because the input event isn't
// triggered by arrow keys, or by manually setting the value, as we've done.
_handleDateChange();
} else if (thisKey === KEY.DOWN) {
newNum = parseInt(e.target.value, 10);
e.target.value = newNum -= 1;
_handleDateChange();
}
}
}
function _unhideValidationStyles(e) {
e.target.classList.add('touched');
}
function calculateElapsedTime() {
var currentDate = new Date(),
dateOfBirth = _getDateOfBirth(),
diff = currentDate.getTime() - dateOfBirth.getTime(),
elapsedTime;
switch (unitText) {
case 'days':
elapsedTime = Math.round(diff/(1000*60*60*24));
break;
case 'weeks':
elapsedTime = Math.round(diff/(1000*60*60*24*7));
break;
case 'months':
// Months are tricky, being variable length, so I opted for the average number
// of days in a month as a close-enough approximation.
elapsedTime = Math.round(diff/(1000*60*60*24*30.4375));
break;
case 'years':
elapsedTime = Math.round(diff/(1000*60*60*24*365.25));
break;
}
return elapsedTime || diff;
}
function _dateIsValid() {
return monthEl.checkValidity() && dayEl.checkValidity() && yearEl.checkValidity();
}
function _getDateOfBirth() {
return new Date(yearEl.value, monthEl.value, dayEl.value);
}
function _repaintItems(number) {
for (var i = 0; i < items.length; i++) {
if (i < number) {
items[i].style.backgroundColor = COLOR;
} else {
items[i].style.backgroundColor = '';
}
}
}
})();