-
Notifications
You must be signed in to change notification settings - Fork 21
/
custom.js
351 lines (308 loc) · 11.4 KB
/
custom.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
(function(){
const ltc = {};
ltc.map;
ltc.meetups = [];
ltc.markers = {};
// get week range for meetups
let today = new Date;
let lastDow = 14 - today.getDay();
const api = {};
api.group = 'LearnTeachCode';
api.offset = 0;
api.path = 'https://api.meetup.com/2/events?&sign=true&photo-host=public';
api.startWeekDate = '0';
api.endWeekDate = lastDow + 'd';
api.status = 'upcoming';
api.url = api.path + '&group_urlname=' + api.group + '&status=' + api.status + '&time=' + api.startWeekDate + ',' + api.endWeekDate;
api.err = "Error occurred processing Meetup API URL";
// Get Meetup Data
function getData(url, successFunc, errMsg) {
$.ajax({
type: "GET",
url: url,
success: successFunc,
error: function(){ console.log( errMsg ); },
dataType: 'jsonp'
});
}
function processData(data) {
data.days = getCurrentDates(2);
data.results.forEach( function( meetup, index ) {
// Get event formatted dates and time
data.results[index].d = getDateFormats( meetup );
});
// Append new meetups
ltc.meetups.push(...data.results);
// List new meetups
listMeetups(data);
mapMeetups(data);
listMeetupsinWeekView(data);
}
function mapMeetups(data){
if (data.meta.count > 0) {
data.results.forEach( meetup => {
let meetingListItem, updatedPopupContent;
let has = {
lat: !!Math.abs(meetup.venue.lat),
lon: !!Math.abs(meetup.venue.lon),
venue: meetup.venue.name.toLowerCase().trim() !== "online event"
};
if (has.lat && has.lon && has.venue) {
meetingListItem = '<li>'+meetup.d.month+' '+meetup.d.d+': <a href="#meetup-'+meetup.id+'" title="'+meetup.name+'">'+meetup.name+'</a></li>';
meetup.popup = { meetings: [] };
if( ltc.markers[meetup.venue.id] ) {
meetup.marker = ltc.markers[meetup.venue.id];
meetup.popup.meetings.push( meetingListItem );
updatedPopupContent = meetup.marker.getPopup().getContent().split("</ul>")[0] + meetingListItem + "</ul>";
meetup.marker.setPopupContent( updatedPopupContent );
} else {
meetup.popup.title = '<strong>' + meetup.venue.name + '</strong>';
meetup.popup.location = '<br><small>' + meetup.venue.address_1 +', ';
meetup.popup.location += (meetup.venue.city)? meetup.venue.city : '';
meetup.popup.location += (meetup.venue.state)? ', '+ meetup.venue.state.toUpperCase() : '';
meetup.popup.location += ( (meetup.venue.zip)? ', '+meetup.venue.zip : '' )+'</small>';
meetup.popup.meetings.push( meetingListItem );
meetup.popup.content = meetup.popup.title + meetup.popup.location;
meetup.popup.content += "<ul>";
meetup.popup.meetings.forEach( meeting => {
meetup.popup.content += meeting;
});
meetup.popup.content += "</ul>";
meetup.marker = L.marker([meetup.venue.lat, meetup.venue.lon]).bindPopup( meetup.popup.content );
ltc.markers[meetup.venue.id] = meetup.marker;
}
}
});
// Check if ltc.markers (object values = actual markers object) exist
if( Object.values(ltc.markers).length > 0 ) {
drawMap();
// Create grouping markers and add group of markers to map
ltc.map.featureGroup = new L.featureGroup( Object.values(ltc.markers) ).addTo(ltc.map);
// Size the map to the boundries of outer most markers and give it a little padding
ltc.map.fitBounds( ltc.map.featureGroup.getBounds().pad(0.1) );
}
}
}
function drawMap() {
// If a map has not been created
if( !ltc.map ) {
// Add height to map div via active class
document.getElementById('mapid').classList.add('active');
// Map Center Coordinates
let latlng = [ 34.0522, -118.2437 ]; // Los Angeles
let zoomlevel = 13; // Greater LA Metro Zoom view
// Initialize Map and assign to ltc.map
ltc.map = L.map('mapid').setView( latlng, zoomlevel );
// Use Open Street Map default (Mapnik)
// L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
// maxZoom: 20,
// attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
// }).addTo(ltc.map);
//// CARTO BASE MAPS - FREE TO USE ////
//// Max use 75,000 map impressions a Month per CartoDB, Inc.
//// MAP STYLE: Voyager
// L.tileLayer('https://{s}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}{r}.png', {
//// MAP STYLE: Voyager Labels Under
L.tileLayer('https://{s}.basemaps.cartocdn.com/rastertiles/voyager_labels_under/{z}/{x}/{y}{r}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a>',
subdomains: 'abcd',
maxZoom: 20
}).addTo(ltc.map);
}
}
// Display Meetup Data
function listMeetups(data){
// List Items
let list = '';
// Check count of upcoming events
if(data.meta.count){
// Get formatted meetups list items
list = getFormattedMeetups(data.results).join('');
// If more items are available add a note
if(data.meta.total_count > data.meta.count && data.meta.count >= api.perPage){
list += '<li class="load-more"><a href="https://www.meetup.com/LearnTeachCode/events/">Load More</a></li>';
}
} else{
// No upcoming events note
list += '<li>No Meetups Currently Scheduled. Stay tuned.</li>';
}
// Remove load-more button just before adding new elements, which may include new load-more button
$('.load-more').remove();
// Add the list to the element
$(".listview").append(list);
}
// Display Meetup Data in Week View
function listMeetupsinWeekView(data) {
let today = new Date();
let weekdays = getDays();
let days = data.days;
let meetups = data.results;
let meetupsByDay = getWeekFormattedMeetups(meetups);
// 14 days, 0-indexed
for(let i=0; i < 14; i++) {
// Determine which week we're on
let weekid = ( i<7 ) ? 'first' : 'second';
let weekdiv = '<div class="day" id="' + days[i].dow.toLowerCase() + days[i].date + '"></div>';
document.querySelector('#' + weekid + 'week').insertAdjacentHTML('beforeend', weekdiv);
}
for(let i=0; i < days.length; i++) {
let weekday = days[i];
let dowDay = weekday.dow.substring(0,3) + weekday.date;
let meetupString = meetupsByDay[dowDay];
let isToday = ( (weekdays[today.getDay()].substring(0, 3) + today.getDate()) == dowDay )? " today" : "";
let formattedWeek = '<div class="weekday'+ isToday + '">'
+ weekday.dow.substring(0,3) + ' '
+ weekday.month.substring(0,3) + ' '
+ weekday.date
+ '</div>';
// If meetups exist
if(meetupString) {
formattedWeek += meetupString.join('');
// Otherwise indicate no meetups for the day
} else {
formattedWeek += '<div class="week-meetup-none">No meetups!</div';
}
// Append to weekday
$('#' + weekday.dow.toLowerCase() + weekday.date).append(formattedWeek);
}
}
// Format Meetup Data for Week View
function getWeekFormattedMeetups( meetups ) {
let dayArrays = {};
// For each event create a list item
meetups.forEach( function( meetup ) {
let d = getDateFormats( meetup );
// does d.dow exist within dayArray as array, if not create array
let dowDay = d.dow + d.d;
if( !dayArrays[dowDay] ) {
dayArrays[dowDay] = [];
}
let formattedMeetup = '<li id="week-meetup-' + meetup.id + '" class="week-meetup">'
+ '<div class="week-time">' + d.time + '</div>'
+ '<div class="week-infobox">'
+ ' <div class="title"><a href="' + meetup.event_url + '">' + meetup.name + '</a></div>'
+ ' <div class="week-city">' + ((meetup.venue.city)?meetup.venue.city+ ' - ' :'') + meetup.venue.name + '</div>'
+ '</div>'
+'</li>';
dayArrays[dowDay].push(formattedMeetup);
});
return dayArrays;
}
/**
* formatEvents() will get a set of meetups and format accordingly
* @param {meetups}
* @returns {(object|Array)}
*/
function getFormattedMeetups( meetups ) {
let formattedMeetups = [];
// For each event create a list item
meetups.filter( function( meetup ) {
// Get event formatted dates and time
let d = getDateFormats( meetup );
// Formant and add current event to list
formattedMeetups.push(
'<li id="list-meetup-' + meetup.id + '" class="list-meetup">'
+ '<div class="datebox">'
+ ' <div class="dow">' + d.dow + '</div>'
+ ' <div class="date">' + d.month + ' ' + d.day + '</div>'
+ ' <div class="time">' + d.time + '</div>'
+ '</div>'
+ '<div class="infobox">'
+ ' <div class="title"><a href="' + meetup.event_url + '">' + meetup.name + '</a></div>'
+ ' <div class="city">' + ((meetup.venue.city)?meetup.venue.city + ' - ':'') + meetup.venue.name + '</div>'
+ '</div>'
+'</li>'
);
});
return formattedMeetups;
}
// Get Week Range
function getCurrentDates(numOfWeeks) {
const months = getMonths();
const weekdays = getDays();
let numOfDays = numOfWeeks * 7;
let days = [];
let today = new Date;
let firstDay = today.getDate() - today.getDay();
for(let i=0; i<numOfDays; i++) {
let nextDate = new Date(today.getTime());
nextDate.setDate(firstDay+i);
days.push({
dow: weekdays[nextDate.getDay()],
date: nextDate.getDate(),
month: months[nextDate.getMonth()],
year: nextDate.getFullYear()
});
}
return days;
}
function getDateFormats(meetup) {
const months = getMonths();
const weekdays = getDays();
const dt = new Date(meetup.time);
// Setup event date info
let d = {};
d.year = dt.getFullYear();
d.yyyy = d.year;
d.monthFull = months[dt.getMonth()];
d.month = d.monthFull.substring(0, 3);
d.m = dt.getMonth()+1;
d.mm = (d.m > 9)? d.m : "0"+d.m;
d.d = dt.getDate();
d.dowFull = weekdays[dt.getDay()];
d.dow = d.dowFull.substring(0, 3);
d.day = (d.d > 9)? d.d : "0"+d.d;
d.dd = d.day;
d.time = formatAMPM( dt );
return d;
}
function getMonths() {
return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
}
function getDays() {
return ['Sunday','Monday','Tueday','Wednesday','Thursday','Friday','Saturday'];
}
function formatAMPM(date) {
let hours = date.getHours();
let minutes = date.getMinutes();
let ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
let strTime = hours + ':' + minutes + ' ' + ampm;
return strTime;
}
/**
* Get initial set of group meetups
*/
$(document).ready(function() {
// Get intial set of meetups
getData( api.url, processData, api.err);
// Toggle between calendar and list views
$('.viewLinks').on('click', '.button', function(e) {
e.preventDefault();
$('.viewLinks .button').removeClass('active');
$('.view').removeClass('showing');
$(this).addClass('active');
$('.view.' + this.id ).addClass('showing');
});
// Click Event for Load More
$('.listview').on('click', '.load-more a', function(e) {
e.preventDefault();
api.offset++;
getData( api.url + '&offset=' + api.offset, processData, api.err);
});
// Click Popup Event (when it exists) link to go to info
$('#mapid').on('click', '.leaflet-popup-content a', function(e) {
e.preventDefault();
let id = e.target.hash.replace("#", "");
let activeView = document.querySelector('.view.showing');
let meetupListItem = activeView.querySelector("[id$='"+id+"']");
if ( meetupListItem ) {
meetupListItem.scrollIntoView();
meetupListItem.classList.add('active');
setTimeout( () => { meetupListItem.classList.remove('active'); }, 3000);
}
});
});
})();