-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
164 lines (152 loc) · 6.05 KB
/
index.html
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dayparting - A Micro-library to Convert Time into Human-friendly part of the day.</title>
<link rel="stylesheet" href="styles.css" media="screen" title="no title" charset="utf-8">
<script src="dayparting.js" charset="utf-8"></script>
</head>
<body>
<header>
<h1 class="header-title">DaypartingJS</h1>
<h3 class="header-desc">A Micro-library to Convert Time into Human-friendly part of the day.</h3>
<h4 class="header-title">Static Locale</h4>
<h5 class="header-desc">Default Locale is English (en-US)</h5>
</header>
<section class="body-container">
<table id="daypartTable" class="pure-table dayparting-timetable">
<thead>
<tr>
<th>Time</th>
<th>Daypart</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<h4 class="header-title">Live Locale Switching</h4>
<label for="">
Select Language
<select id="selectedLocale">
<option value="en-US">English</option>
<option value="hi-IN">हिन्दी</option>
<option value="fr-FR">Français</option>
</select>
</label>
<table id="daypartTable2" class="pure-table dayparting-timetable">
<thead>
<tr>
<th>Time</th>
<th>Daypart</th>
</tr>
</thead>
<tbody>
<tr>
<td>7:06 pm</td>
<td id="static1"></td>
</tr>
<tr>
<td>10:46 am</td>
<td id="static2"></td>
</tr>
<tr>
<td>5:19 pm</td>
<td id="static3"></td>
</tr>
<tr>
<td>8:05 am</td>
<td id="static4"></td>
</tr>
<tr>
<td>3:51 am</td>
<td id="static5"></td>
</tr>
</tbody>
</table>
</section>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script charset="utf-8">
$(document).ready(function() {
var now = new Date(),
myDaypart,
myDaypartBound,
localeJSON,
fnGetRandomDate,
fnFormatTime,
datesArr = [],
datesStaticArr = [],
i;
/**
* Handy function to generate random date object that lies between 'start' and 'end' dates.
* Source http://stackoverflow.com/a/9035732/414749
*/
fnGetRandomDate = function(start, end) {
return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
};
/**
* Format time from dateObject into `hh:mm tt` format.
*/
fnFormatTime = function(dateObject) {
var hh = dateObject.getHours(),
mm = dateObject.getMinutes(),
ampm = hh >= 12 ? 'pm' : 'am',
hh = hh % 12,
hh = hh ? hh : 12,
mm = mm < 10 ? '0' + mm : mm;
return (hh + ':' + mm + ' ' + ampm);
};
// Create array of size 10 with random timed Date Objects for daypartTable
for (i = 0; i < 10; i++)
datesArr.push(fnGetRandomDate(new Date(2012, 0, 1), new Date()));
// Create array of static date objects with Time as per given in daypartTable2
datesStaticArr = [
(new Date(now.getYear(), now.getMonth(), now.getDate(), 19, 6)), // 07:06 PM
(new Date(now.getYear(), now.getMonth(), now.getDate(), 10, 46)), // 10:46 AM
(new Date(now.getYear(), now.getMonth(), now.getDate(), 17, 19)), // 05:19 PM
(new Date(now.getYear(), now.getMonth(), now.getDate(), 8, 5)), // 08:05 AM
(new Date(now.getYear(), now.getMonth(), now.getDate(), 3, 51)) // 03:51 AM
];
// Load Locale JSON file
// and populate daypartTable with random time values and their appropriate daypart strings.
$.getJSON("/locale.json", function(data) {
var daypartTableBody = $('#daypartTable tbody'),
selectedLocale = $('#selectedLocale'),
trArr = '';
// Create static Daypart instance.
myDaypart = daypart({
locale: 'en-US',
localeJSON: data
});
// Create data-bound Daypart instance.
myDaypartBound = daypart({
locale: 'en-US',
localeJSON: data,
binding: true
});
// Create Random timed Table with matching Daypart strings.
for (i = 0; i < 10; i++)
{
trArr += [
'<tr>',
'<td>',
fnFormatTime(datesArr[i]),
'</td>',
'<td>',
myDaypart.for(datesArr[i]),
'</td>',
'</tr>'
].join('');
}
daypartTableBody.append(trArr);
// Attach static table items with data-bound Daypart.
for (i = 0; i < 5; i++)
myDaypartBound.attach(datesStaticArr[i], $('#static' + (i + 1))[0]);
// Change locale easily by using setLocale.
selectedLocale.on('change', function(e) {
myDaypartBound.setLocale($(this).val());
});
});
});
</script>
</body>
</html>