Skip to content

Commit 2f211b8

Browse files
authored
Merge pull request #919 from dylanowen/issue/698
Fixed up the front page training feed
2 parents 509f80e + 62b2561 commit 2f211b8

File tree

3 files changed

+110
-439
lines changed

3 files changed

+110
-439
lines changed

_includes/upcoming-training.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<h2><span>Upcoming Training</span></h2>
55
</div>
66
<div class="training-items-list">
7+
<!-- Prefill the training items list, we'll overwrite this once our async method returns -->
78
{% assign upcomingTrainings = '' | split: ',' %}
89
{% capture now %}{{site.time | date: '%s' | plus: 0}}{% endcapture %}
910
{% for training in site.trainings %}

resources/js/functions.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
---
2+
---
3+
14
// Sliding Panel and scala in a nutshell
25
$(document).ready(function() {
36
$('.navigation-panel-button,.navigation-fade-screen,.navigation-panel-close').on('click touchstart', function(e) {
@@ -315,3 +318,109 @@ function updatePointer() {
315318
pointer.css('top', (target.y));
316319
pointer.css('left', (target.x) * xScale);
317320
}
321+
322+
// TRAININGS
323+
$(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+
}
329+
330+
var isFrontPage = $('.upcoming-training').length !== 0;
331+
var MAX_TRAININGS = isFrontPage ? 5 : 999;
332+
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;
357+
}
358+
359+
return (lhsDate > rhsDate) ? 1 : -1;
360+
});
361+
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+
}
392+
}
393+
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+
}
414+
}
415+
}
416+
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);
422+
423+
// but at least display trainings from scala-lang
424+
doPopulateTrainingsPane([]);
425+
});
426+
});

0 commit comments

Comments
 (0)