-
Notifications
You must be signed in to change notification settings - Fork 388
/
fnordmetric-webui-datepicker.js
329 lines (276 loc) · 8.95 KB
/
fnordmetric-webui-datepicker.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* This file is part of the "FnordMetric" project
* Copyright (c) 2014 Laura Schlimmer
* Copyright (c) 2014 Paul Asmuth, Google Inc.
*
* FnordMetric is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License v3.0. You should have received a
* copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
if (FnordMetric === undefined) {
FnordMetric = {};
}
if (FnordMetric.views === undefined) {
FnordMetric.views = {};
}
/* generate html for time input and handle input */
FnordMetric.util.timeInput = function(selectedTimestamp, elem, callback) {
var selectedMinutes =
FnordMetric.util.appendLeadingZero(
selectedTimestamp.getMinutes());
var selectedHours =
FnordMetric.util.appendLeadingZero(
selectedTimestamp.getHours());
var input_container = document.createElement("div");
input_container.className = "input_container";
var separator = document.createElement("span");
separator.innerHTML = ":";
var hour_input = document.createElement("input");
hour_input.value = selectedHours;
var minute_input = document.createElement("input");
minute_input.value = selectedMinutes;
function render() {
input_container.appendChild(hour_input);
input_container.appendChild(separator);
input_container.appendChild(minute_input);
elem.appendChild(input_container);
hour_input.addEventListener('focus', function(e) {
e.preventDefault();
FnordMetric.util.validatedTimeInput(this, "hours", callback);
}, false);
minute_input.addEventListener('focus', function(e) {
e.preventDefault();
FnordMetric.util.validatedTimeInput(this, "minutes", callback);
}, false);
}
function getValues() {
var hours = hour_input.value.length > 0 ?
hour_input.value : selectedHours;
var minutes = minute_input.value.length > 0 ?
minute_input.value : selectedMinutes;
return {
"hours" : hours,
"minutes" : minutes
}
}
return {
"render" : render,
"getValues" : getValues,
}
}
/* Displays a calendar from an input for selecting time and date*/
FnordMetric.util.DatePicker = function(elem, dp_input, viewport, callback) {
/* current date */
var currMonth;
var currYear;
var currDate;
/* in datepicker selected date */
var selectedTimestamp;
var selectedYear;
var selectedMonth;
var selectedDate;
var timeInput;
var dp_widget = document.createElement("div");
dp_widget.className = "datepicker_widget";
elem.appendChild(dp_widget);
function getDaysInMonth(year, month) {
return (new Date(year, (month+1), 0).getDate());
}
function getFirstDayOfMonth(year, month) {
return (new Date(year + "-" + (month+1) + "-01").getDay());
}
/*checks if the day is in this month or not
day and first_day are numeric weekday description
rowNum the actual row and date the actual date
*/
function inThisMonth(day, first_day, date, daysInMonth, rowNum) {
if (rowNum > 0 && rowNum < 4) {return true}
if (rowNum == 0) {
/* the calendar has the same number of rows for each month */
if (first_day == 0) {
return false;
} else if (day < first_day) {
return false;
} else {
return true;
}
} else if (rowNum >= 4) {
if (date <= daysInMonth) {
return true;
} else {
return false;
}
}
}
function isSelectable(date, month, year) {
if (month == currMonth) {
return date <= currDate;
}
return (year <= currYear && month < currMonth);
}
/* if date is selectable */
function getCellName(date, month, year) {
var name = "";
/* date is today */
if (date == currDate && month == currMonth && year == currYear) {
name = "highlight_border";
}
/* date is selected date */
if (date == selectedDate &&
month == selectedMonth &&
year == selectedYear) {
name += " highlight";
}
return name;
}
function onSelect(date, month, year) {
/* fallback for time inputs */
var date = (date == undefined) ?
selectedDate : date;
var month = (month == undefined)?
selectedMonth : month;
var year = (year == undefined) ?
selectedYear : year;
var inputs = timeInput.getValues();
var hours = inputs.hours;
var minutes = inputs.minutes;
var ts = new Date(year, month, date, hours, minutes).getTime();
if (ts <= Date.now() ) {
dp_input.value =
FnordMetric.util.appendLeadingZero(month+1) +
"/" +
FnordMetric.util.appendLeadingZero(date) +
"/" + year + " " +
FnordMetric.util.appendLeadingZero(hours) +
":" +
FnordMetric.util.appendLeadingZero(minutes);
callback(ts);
dp_input.setAttribute("id", ts);
resetDatepicker();
}
}
/* sets event listener for selectable dates */
function handleDateLinks(link, date, month, year) {
link.addEventListener('click', function(e) {
e.preventDefault();
onSelect(date, month, year);
}, false);
}
function resetDatepicker() {
dp_widget.innerHTML = "";
dp_widget.className = "datepicker_widget";
}
function resetCalendar(table) {
table.innerHTML = "";
FnordMetric.util.removeIfChild(
table, dp_widget);
}
function renderWeekHeader(table) {
var header = document.createElement("tr");
var dayNames =
["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"];
dayNames.map(function(day) {
var cell = document.createElement("th");
cell.innerHTML = day;
header.appendChild(cell);
});
table.appendChild(header);
}
function renderMonthHeader(year, month, table) {
var year = parseInt(year, 10);
var month = parseInt(month, 10);
var row = document.createElement("tr");
var cell = document.createElement("td");
cell.colSpan = "7";
var month_title = document.createElement("span");
month_title.className = "datepicker_title";
month_title.innerHTML =
FnordMetric.util.getMonthStr(month) + " " + year;
/* tooltip to select previous month */
var prev_ttp = FnordMetric.createButton(
"#", "month_ttp", "<i class='fa fa-chevron-left'></i>");
/* tooltip to select next month */
var next_ttp = FnordMetric.createButton(
"#", "month_ttp", "<i class='fa fa-chevron-right'></i>");
prev_ttp.addEventListener('click', function(e) {
e.preventDefault();
resetCalendar(table);
year = (month == 0)? year-1 : year;
month = (month + 11) % 12;
renderCalendar(year, month);
}, false);
next_ttp.addEventListener('click', function(e) {
e.preventDefault();
resetCalendar(table);
year = (month == 11)? year+1 : year;
month = (month + 1) % 12;
renderCalendar(year, month);
}, false);
cell.appendChild(prev_ttp);
cell.appendChild(month_title);
cell.appendChild(next_ttp);
row.appendChild(cell);
table.appendChild(row);
}
function renderWeeks(year, month, calendar) {
var daysInMonth = getDaysInMonth(year, month);
var fDay = getFirstDayOfMonth(year, month);
var numRows = 6;
var date = 1;
/* generate the rows (that is the weeks) */
for (var dprow = 0; dprow < numRows; dprow++) {
var row = document.createElement("tr");
for (day = 0; day < 7; day ++) {
var cell = document.createElement("td");
if (inThisMonth(day, fDay, date, daysInMonth, dprow)) {
if (isSelectable(date, month, year)) {
var link = FnordMetric.createButton(
"#", undefined, date);
cell.className = getCellName(date, month, year);
cell.appendChild(link);
handleDateLinks(link, date, month, year);
} else {
cell.innerHTML = date;
}
date++;
}
row.appendChild(cell);
}
calendar.appendChild(row);
}
}
function renderCalendar(year, month) {
var table = document.createElement("table");
renderWeekHeader(table);
renderMonthHeader(year, month, table);
renderWeeks(year, month, table);
dp_widget.appendChild(table);
}
function init() {
var now = new Date();
currYear = now.getFullYear();
currMonth = now.getMonth();
currDate = now.getDate();
selectedTimestamp = new Date(
parseInt(dp_input.getAttribute("id"), 10));
selectedYear = selectedTimestamp.getFullYear();
selectedMonth = selectedTimestamp.getMonth();
selectedDate = selectedTimestamp.getDate();
dp_widget.innerHTML = "";
dp_widget.className += " active";
timeInput = FnordMetric.util.timeInput(
selectedTimestamp, dp_widget, onSelect);
timeInput.render();
renderCalendar(currYear, currMonth);
}
dp_input.addEventListener('focus', function(event) {
init();
}, false);
document.addEventListener('click', function() {
resetDatepicker();
}, false);
elem.addEventListener('click', function(e) {
e.stopPropagation();
});
}