Skip to content

Commit 63a4514

Browse files
authored
Merge pull request #924 from dylanowen/issue/921
updated the /training page to include lightbend data
2 parents ab34122 + 8034175 commit 63a4514

File tree

2 files changed

+180
-91
lines changed

2 files changed

+180
-91
lines changed

resources/js/functions.js

+166-91
Original file line numberDiff line numberDiff line change
@@ -321,106 +321,181 @@ function updatePointer() {
321321

322322
// TRAININGS
323323
$(document).ready(function() {
324-
// Stop early if the element does not exist, i.e.,
325-
// we're not on the front page nor on the Trainings page
326-
if ($('.training-items-list').length === 0) {
327-
return;
328-
}
324+
var MONTH_NAMES = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
325+
var MONTH_NAMES_SHORT = MONTH_NAMES.map(function (month) {
326+
return month.substr(0, 3);
327+
});
329328

330-
var isFrontPage = $('.upcoming-training').length !== 0;
331-
var MAX_TRAININGS = isFrontPage ? 5 : 999;
329+
function getTrainings() {
330+
function loadTrainings(url) {
331+
return $.getJSON(url)
332+
.then(function (data) {
333+
// filter out extra ajax info
334+
return data;
335+
}, function (jqXHR, textStatus, errorThrown) {
336+
// log the error to the console
337+
console.error("Couldn't load training feed " + url + " : " + textStatus, errorThrown);
338+
339+
// recover so we can keep processing
340+
return [];
341+
});
342+
}
332343

333-
{% comment %} Grab all the upcoming training sessions defined in _trainings {% endcomment %}
334-
var scalaLangTrainings = [
335-
{% for training in site.trainings %}{% if training.date >= site.time %}
336-
{
337-
"title": "{{ training.title | escape }}",
338-
"url": "{{ training.link-out | escape }}",
339-
"location": "{{ training.location | upcase | escape }}",
340-
"when": new Date("{{ training.when | escape }}"),
341-
"organizer": "{{ training.organizer | escape }}"
342-
},
343-
{% endif %}{% endfor%}
344-
];
345-
346-
function doPopulateTrainingsPane(lightBendTrainings) {
347-
var MONTH_NAMES = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
348-
349-
// combine and sort our trainings by date
350-
var allTrainings = scalaLangTrainings.concat(lightBendTrainings)
351-
.sort(function (lhs, rhs) {
352-
var lhsDate = lhs.when.valueOf();
353-
var rhsDate = rhs.when.valueOf();
354-
355-
if (lhsDate === rhsDate) {
356-
return 0;
344+
// load our trainings
345+
var ourTrainingsPromise = loadTrainings("/resources/json/trainings.json")
346+
.then(function (trainings) {
347+
return trainings.map(function (training) {
348+
var parsedTraining = Object.assign({}, training);
349+
350+
// parse our date
351+
parsedTraining.when = new Date(training.when);
352+
353+
return parsedTraining;
354+
}).filter(function(training) {
355+
// make sure our date occurs in the future
356+
return training.when >= new Date();
357+
})
358+
}, function (error) {
359+
console.error("Couldn't parse our training data", error);
360+
// if our data is bad recover with an empty array
361+
return [];
362+
});
363+
364+
// load the trainings from lightbend
365+
var lightbendTrainingsPromise = loadTrainings("/resources/php/typesafe-feed-trainings.php")
366+
.then(function(data) {
367+
var trainings = (data && data.length > 0) ? data[0] : [];
368+
369+
// flatten and filter our sessions by date
370+
var flattenedTrainings = [];
371+
for (var i = 0; i < trainings.length; i++) {
372+
var training = trainings[i];
373+
for (var j = 0; j < training.sessions.length; j++) {
374+
var session = training.sessions[j];
375+
376+
// make sure this session occurs in the future
377+
var when = new Date(session.when);
378+
if (when >= new Date()) {
379+
flattenedTrainings.push({
380+
title: training.title,
381+
url: session.url,
382+
location: session.where.toUpperCase(),
383+
when: when,
384+
organizer: session.organizer
385+
});
386+
}
387+
}
357388
}
358389

359-
return (lhsDate > rhsDate) ? 1 : -1;
390+
return flattenedTrainings;
391+
}, function (error) {
392+
console.error("Couldn't parse Lightbend training data", error);
393+
// if our data is bad recover with an empty array
394+
return [];
360395
});
361396

362-
var listContainer = $('.training-items-list');
363-
listContainer.empty();
364-
for (var i = 0; i < Math.min(allTrainings.length, MAX_TRAININGS); i++) {
365-
var training = allTrainings[i];
366-
// add fallbacks if we can't parse our dates
367-
var month = '?';
368-
var day = '?';
369-
if (!isNaN(training.when.valueOf())) {
370-
month = MONTH_NAMES[training.when.getMonth()];
371-
day = training.when.getDate();
372-
}
373-
374-
// build up our training item
375-
var content = '<a href=' + training.url + ' class="training-item card">' +
376-
'<span class="calendar">' +
377-
'<span>' + month + '</span>' +
378-
'<span>' + day + '</span>' +
379-
'</span>' +
380-
'<div class="card-text">' +
381-
'<h4>' + training.title + '</h4>' +
382-
'<ul>' +
383-
'<li class="online-courses-price">' + training.location + '</li>' +
384-
'<li class="dot">•</li>' +
385-
'<li class="online-courses-date">' + training.organizer + '</li>' +
386-
'</ul>' +
387-
'</div>';
388-
389-
// add it to our list
390-
listContainer.append(content);
391-
}
397+
// load and combine all our trainings
398+
return $.when(ourTrainingsPromise, lightbendTrainingsPromise)
399+
.then(function (ourTrainings, lightBendTrainings) {
400+
return ourTrainings.concat(lightBendTrainings)
401+
.sort(function (lhs, rhs) {
402+
return lhs.when - rhs.when;
403+
});
404+
});
392405
}
393406

394-
$.getJSON("/resources/php/typesafe-feed-trainings.php")
395-
.done(function(data) {
396-
// flatten and filter our sessions by date
397-
var flattenedTrainings = [];
398-
for (var i = 0; i < data[0].length; i++) {
399-
var training = data[0][i];
400-
for (var j = 0; j < training.sessions.length; j++) {
401-
var session = training.sessions[j];
402-
403-
// make sure this session occurs in the future
404-
var when = new Date(session.when);
405-
if (when >= new Date()) {
406-
flattenedTrainings.push({
407-
title: training.title,
408-
url: session.url,
409-
location: session.where.toUpperCase(),
410-
when: when,
411-
organizer: session.organizer
412-
});
413-
}
407+
// Render training data for our training page
408+
var frontPageTrainingList = $('.training-items-list');
409+
// Stop early if the front page element does not exist
410+
if (frontPageTrainingList.length !== 0) {
411+
getTrainings()
412+
.then(function (trainings) {
413+
var MAX_TRAININGS = 5;
414+
415+
// clear out any preloaded training info
416+
frontPageTrainingList.empty();
417+
418+
for (var i = 0; i < Math.min(trainings.length, MAX_TRAININGS); i++) {
419+
var training = trainings[i];
420+
421+
// we should have validated our dates by this point
422+
var month = MONTH_NAMES_SHORT[training.when.getMonth()];
423+
var day = training.when.getDate();
424+
425+
// build up our training item
426+
var content = '<a href=' + training.url + ' class="training-item card">' +
427+
'<div class="calendar">' +
428+
'<span>' + month + '</span>' +
429+
'<span>' + day + '</span>' +
430+
'</div>' +
431+
'<div class="card-text">' +
432+
'<h4>' + training.title + '</h4>' +
433+
'<ul>' +
434+
'<li class="online-courses-price">' + training.location + '</li>' +
435+
'<li class="dot">•</li>' +
436+
'<li class="online-courses-date">' + training.organizer + '</li>' +
437+
'</ul>' +
438+
'</div>' +
439+
'</a>';
440+
441+
// add it to our list
442+
frontPageTrainingList.append(content);
414443
}
415-
}
444+
});
445+
}
416446

417-
doPopulateTrainingsPane(flattenedTrainings);
418-
})
419-
.fail(function(jqXHR, textStatus, errorThrown) {
420-
// log the error to the console
421-
console.error("Could not load Lightbend training feed: " + textStatus, errorThrown);
447+
// Render training data for the front page
448+
var trainingPageItemList = $('.training-events .wrap .inner-box');
449+
// Stop early if the training page element does not exist
450+
if (trainingPageItemList.length !== 0) {
451+
getTrainings()
452+
.then(function (trainings) {
453+
var MAX_TRAININGS = 999;
454+
455+
// clear out any preloaded training info
456+
trainingPageItemList.empty();
457+
458+
var content = "";
459+
460+
var i = 0;
461+
var lastIndex = Math.min(trainings.length, MAX_TRAININGS);
462+
while (i < lastIndex) {
463+
// we should have validated our dates by this point
464+
var year = trainings[i].when.getFullYear();
465+
var month = trainings[i].when.getMonth();
466+
467+
// create our training list container
468+
content += '<h3>' + MONTH_NAMES[month] + ' ' + year + '</h3>';
469+
content += '<div class="training-list">';
470+
471+
while (i < lastIndex) {
472+
var training = trainings[i];
473+
474+
// check if we're still in the right month
475+
if (training.when.getMonth() !== month || training.when.getFullYear() !== year) {
476+
break;
477+
}
478+
479+
// build up our training item
480+
content += '<a href="' + training.url + '" class="training-item">' +
481+
'<div class="calendar">' +
482+
'<span>' + MONTH_NAMES_SHORT[month] + '</span>' +
483+
'<span>' + training.when.getDate() + '</span>' +
484+
'</div>' +
485+
'<div class="training-text">' +
486+
'<h4>' + training.title + '</h4>' +
487+
'<p>' + training.location + '</p>' +
488+
'<p>' + training.organizer + '</p>' +
489+
'</div>' +
490+
'</a>';
491+
492+
i++;
493+
}
422494

423-
// but at least display trainings from scala-lang
424-
doPopulateTrainingsPane([]);
425-
});
495+
content += '</div>'
496+
}
497+
498+
trainingPageItemList.append(content);
499+
});
500+
}
426501
});

resources/json/trainings.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
---
3+
[
4+
{% comment %} Grab all the upcoming training sessions defined in _trainings {% endcomment %}
5+
{% for training in site.trainings %}{% if training.date >= site.time %}
6+
{
7+
"title": "{{ training.title | escape }}",
8+
"url": "{{ training.link-out | escape }}",
9+
"location": "{{ training.location | upcase | escape }}",
10+
"when": "{{ training.when | date: "%Y-%m-%d" }}",
11+
"organizer": "{{ training.organizer | escape }}"
12+
}{% unless forloop.last %},{% endunless %}
13+
{% endif %}{% endfor%}
14+
]

0 commit comments

Comments
 (0)