-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
328 lines (269 loc) · 9.41 KB
/
script.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
// TODO's
// 08. Validating startTime and endTime
// 13. edge case: when event duration is more than 24hours or
// if the event lies in more than two days
// 15. periodically update the .event__duration
// 16. write a function to handle setInterval (and delay using setTimeout)
// 17. use moment.js where ever possible(renderEvents) to increase
// performance [ISSUE - with moment.js month starts with 1 i,e january]
var renderData = [],
renderDateTime = new Date(),
renderDate = renderDateTime.getDate(),
renderMonth = renderDateTime.getMonth(),
renderYear = renderDateTime.getFullYear(),
/* 17. with moment
renderDateTime = moment(),
renderDate = renderDateTime.format('D'),
renderMonth = renderDateTime.format('M'),
renderYear = renderDateTime.format('YYYY'),
*/
$eventsHolder = $('#events'),
$currentTimeMarker = '';
// Generate the view
function init() {
// make the ajax call and get the data
getData();
// get current marker up
currentTimeMarker();
// initiate time_labels
var $html = '';
for(var hour = 0; hour < 24; hour++) {
$html += "<div class='time__label'><div>" + moment().set('hour', hour).format('h A') + "</div></div>";
}
$('#time_lable').html($html);
// initiate time_line
$html = "<div class='hour_markers_holder'>";
for(var hour = 0; hour < 24; hour++) {
$html += "<div class='hour_markers'></div>";
}
$html += "</div>";
$('#markers').html($html);
}
// get and prepare data
function getData() {
$.ajax({
url: './sample-data.json' ,
type: 'get',
dataType: 'json',
success: function( data ) {
renderData = data;
renderEvents( renderData );
}
});
}
// render calender events handler
function renderEvents( data ) {
var dataIn = data || renderData,
dataInLenght = dataIn.length,
currentDate = renderDateTime.getDate(),
renderMonth = renderDateTime.getMonth(),
event = {};
$('#renderDateTime').html(moment(renderDateTime).format("dddd, MMMM DD YYYY"));
// clear exiting events
$eventsHolder.find('.event').remove();
// Use good old over for..in for itirating array
// http://stackoverflow.com/questions/1963102/
for(var index = 0; index < dataInLenght ; index++) {
var givenEvent = dataIn[index],
startDateTime = new Date(givenEvent.startTime),
endDateTime = new Date(givenEvent.endTime),
startMonth = startDateTime.getMonth(),
startDate = startDateTime.getDate(),
startYear = startDateTime.getFullYear(),
endMonth = endDateTime.getMonth(),
endDate = endDateTime.getDate(),
endYear = endDateTime.getFullYear();
// filter events only from current month and date
/*
(renderYear === (startYear || endYear)) && (renderMonth === (startMonth || endMonth)) && (currentDate === (endDate || startDate))
this is slower than the below one
*/
if( (renderYear === startYear && renderMonth === startMonth && currentDate === startDate ) || (renderYear === endYear && renderMonth === endMonth && currentDate === endDate) ) {
event.id = givenEvent.title.replace(' ', '-');
event.title = givenEvent.title;
event.startDateTime = moment(startDateTime).format('ddd, D-MM-YYYY h:mm:ss A');
event.endDateTime = moment(endDateTime).format('ddd, D-MM-YYYY h:mm:ss A');
event.startTime = moment(startDateTime).format('hh:mm A');
event.endTime = moment(endDateTime).format('hh:mm A');
event.duration = {
hours : moment(endDateTime).diff(startDateTime, 'hours'),
minutes : moment(endDateTime).diff(startDateTime, 'minute') % 60
};
// get
if( moment().isAfter(startDateTime) ) {
if( moment().isAfter(endDateTime) ) {
event.fromNow = moment(endDateTime).fromNow();
} else {
event.fromNow = 'Happening';
}
} else {
event.fromNow = 'is ' + moment(startDateTime).fromNow();
}
// @TODO 13. edge case: when event duration is more than 24hours
// finding if the event lies in more than one days
if( startDate !== endDate ) {
if( renderDate === startDate ) {
event.offset = createEvent(givenEvent.startTime);
event.height = 1440 - createEvent(givenEvent.startTime);
} else {
event.offset = 0;
event.height = createEvent(givenEvent.endTime);
}
} else {
event.offset = createEvent(givenEvent.startTime);
event.height = createEvent(givenEvent.endTime) - createEvent(givenEvent.startTime);
}
// render the event
eventTemplate(event);
}
}
// check for event collisions
collisionCheck();
// render time marker if the renderDate is equal to current date
if(renderMonth === getCurrentDateTime('month') && renderDate === getCurrentDateTime('date')) {
$currentTimeMarker.show();
} else {
$currentTimeMarker.hide();
}
}
function eventTemplate( event ) {
var $html = '';
$html += "<div id='"+ event.id +"' class='event event__raised' style='width: 99%; top:"+ event.offset +"px;' title='"+ event.startDateTime + " - "+ event.endDateTime +"'>";
$html += "<div style='height:" + event.height + "px'>";
$html += event.title;
$html += " <span class='event__time'> [";
$html += event.startTime + ' - ' + event.endTime;
$html += "] </span>";
$html += " <span class='event__duration'> Duration: ";
var hours = event.duration.hours;
if( hours ) {
$html += hours;
$html += (hours <= 1) ? ' hour ' : ' hours ';
}
var minutes = event.duration.minutes;
if( minutes ) {
$html += minutes;
$html += (minutes <= 1) ? ' minute ' : ' minutes ';
}
$html += "</span>";
if( event.fromNow ) {
$html += " <span class='event__fromnow'> (";
$html += event.fromNow;
$html += ") </span>";
}
$html += " <span class='event__hidden'> (";
$html += event.startDateTime + " - "+ event.endDateTime;
$html += ") </span>";
$html += "</div>";
$html += "</div>";
$eventsHolder.append($html);
}
// current time marker
// @TODO use workers to inprove performance
function currentTimeMarker() {
var currentHour = getCurrentDateTime('hours'),
currentMinute = getCurrentDateTime('minutes'),
offset = (currentHour * 60) + currentMinute;
if(!$currentTimeMarker.length) {
$eventsHolder.append("<div id='currentTimeMarker' style='top: "+ offset +"px'></div>");
$currentTimeMarker = $('#currentTimeMarker');
} else {
$currentTimeMarker.css({top:offset + 'px'});
}
}
// Utility
// ===========================================
// Get current date, month, hours, minutes
function getCurrentDateTime( arg ) {
var type = arg || '',
currentDateTime = new Date();
switch(type.toLowerCase()) {
case 'date':
return currentDateTime.getDate();
case 'month':
return currentDateTime.getMonth();
case 'hour':
case 'hours':
return currentDateTime.getHours();
case 'minute':
case 'minutes':
return currentDateTime.getMinutes();
// @TODO case 'time':
// @TODO case 'datetime':
default:
return currentDateTime;
}
}
// use setinterval to trigger currentTimeMarker once in 60s
window.setInterval(function() {currentTimeMarker();}, 60000);
// update render global variables
// @TODO change the date argument to moment object and handle accordingly
function updateRenderDateTime( date ) {
renderDateTime = new Date(renderYear, renderMonth, date);
renderDate = renderDateTime.getDate();
renderMonth = renderDateTime.getMonth();
renderYear = renderDateTime.getFullYear();
// triger renderEvents when renderDateTime updates
renderEvents();
}
// rename or remove this function
function createEvent( date ) {
var currentDate = new Date(date) || new Date(),
currentHour = currentDate.getHours(),
currentMinute = currentDate.getMinutes();
return (currentHour * 60) + currentMinute;
}
// handle event ovelapping
function collisionCheck() {
var $events = $eventsHolder.find('.event');
for(var i = 0; i < $events.length; i++) {
for(var j = (i+1); j < ($events.length); j++) {
var rect1 = $events[i].getBoundingClientRect(),
rect2 = $events[j].getBoundingClientRect(),
overlap = !(rect1.right < rect2.left ||
rect1.left > rect2.right ||
rect1.bottom < rect2.top ||
rect1.top > rect2.bottom),
event1Style = $events[i].style,
event2Style = $events[j].style,
event1Width = parseInt(event1Style.width),
event2Width = parseInt(event2Style.width);
if(overlap) {
// console.log(event1Width, event2Width);
if( event1Width < 50 || event2Width < 50 ) {
event2Style.left = ( (event1Width/1.2) - 1) + '%';
event2Style.width = event2Width/2 + '%';
console.log(event1Width, event2Width);
} else {
//
event2Style.left = ( (event1Width/2) - 1) + '%';
// change the width to 50%
event1Style.width = event1Width/2 + '%';
event2Style.width = event2Width/2 + '%';
// console.log(event1Width, event2Width);
// console.log(event1Style.width, event2Style.width);
}
}
}
}
}
// register events
// 17. migrating to moment library
// =================================
// today button
$('#today').on('click', function() {
updateRenderDateTime( moment().format('D') );
// updateRenderDateTime( moment( renderDateTime ) );
});
// previous button
$('#previous_day').on('click', function() {
updateRenderDateTime( renderDate - 1 );
// updateRenderDateTime( moment( renderDateTime ).subtract( 1, 'day' ) );
});
// next button
$('#next_day').on('click', function() {
updateRenderDateTime( renderDate + 1 );
// updateRenderDateTime( moment( renderDateTime ).add( 1, 'day' ) );
});
// initialize everything
init();