diff --git a/resources/js/functions.js b/resources/js/functions.js index 681dfe179..1178035fd 100644 --- a/resources/js/functions.js +++ b/resources/js/functions.js @@ -321,106 +321,181 @@ function updatePointer() { // TRAININGS $(document).ready(function() { - // Stop early if the element does not exist, i.e., - // we're not on the front page nor on the Trainings page - if ($('.training-items-list').length === 0) { - return; - } + var MONTH_NAMES = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; + var MONTH_NAMES_SHORT = MONTH_NAMES.map(function (month) { + return month.substr(0, 3); + }); - var isFrontPage = $('.upcoming-training').length !== 0; - var MAX_TRAININGS = isFrontPage ? 5 : 999; + function getTrainings() { + function loadTrainings(url) { + return $.getJSON(url) + .then(function (data) { + // filter out extra ajax info + return data; + }, function (jqXHR, textStatus, errorThrown) { + // log the error to the console + console.error("Couldn't load training feed " + url + " : " + textStatus, errorThrown); + + // recover so we can keep processing + return []; + }); + } - {% comment %} Grab all the upcoming training sessions defined in _trainings {% endcomment %} - var scalaLangTrainings = [ - {% for training in site.trainings %}{% if training.date >= site.time %} - { - "title": "{{ training.title | escape }}", - "url": "{{ training.link-out | escape }}", - "location": "{{ training.location | upcase | escape }}", - "when": new Date("{{ training.when | escape }}"), - "organizer": "{{ training.organizer | escape }}" - }, - {% endif %}{% endfor%} - ]; - - function doPopulateTrainingsPane(lightBendTrainings) { - var MONTH_NAMES = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]; - - // combine and sort our trainings by date - var allTrainings = scalaLangTrainings.concat(lightBendTrainings) - .sort(function (lhs, rhs) { - var lhsDate = lhs.when.valueOf(); - var rhsDate = rhs.when.valueOf(); - - if (lhsDate === rhsDate) { - return 0; + // load our trainings + var ourTrainingsPromise = loadTrainings("/resources/json/trainings.json") + .then(function (trainings) { + return trainings.map(function (training) { + var parsedTraining = Object.assign({}, training); + + // parse our date + parsedTraining.when = new Date(training.when); + + return parsedTraining; + }).filter(function(training) { + // make sure our date occurs in the future + return training.when >= new Date(); + }) + }, function (error) { + console.error("Couldn't parse our training data", error); + // if our data is bad recover with an empty array + return []; + }); + + // load the trainings from lightbend + var lightbendTrainingsPromise = loadTrainings("/resources/php/typesafe-feed-trainings.php") + .then(function(data) { + var trainings = (data && data.length > 0) ? data[0] : []; + + // flatten and filter our sessions by date + var flattenedTrainings = []; + for (var i = 0; i < trainings.length; i++) { + var training = trainings[i]; + for (var j = 0; j < training.sessions.length; j++) { + var session = training.sessions[j]; + + // make sure this session occurs in the future + var when = new Date(session.when); + if (when >= new Date()) { + flattenedTrainings.push({ + title: training.title, + url: session.url, + location: session.where.toUpperCase(), + when: when, + organizer: session.organizer + }); + } + } } - return (lhsDate > rhsDate) ? 1 : -1; + return flattenedTrainings; + }, function (error) { + console.error("Couldn't parse Lightbend training data", error); + // if our data is bad recover with an empty array + return []; }); - var listContainer = $('.training-items-list'); - listContainer.empty(); - for (var i = 0; i < Math.min(allTrainings.length, MAX_TRAININGS); i++) { - var training = allTrainings[i]; - // add fallbacks if we can't parse our dates - var month = '?'; - var day = '?'; - if (!isNaN(training.when.valueOf())) { - month = MONTH_NAMES[training.when.getMonth()]; - day = training.when.getDate(); - } - - // build up our training item - var content = '' + - '' + - '' + month + '' + - '' + day + '' + - '' + - '
' + - '

' + training.title + '

' + - '' + - '
'; - - // add it to our list - listContainer.append(content); - } + // load and combine all our trainings + return $.when(ourTrainingsPromise, lightbendTrainingsPromise) + .then(function (ourTrainings, lightBendTrainings) { + return ourTrainings.concat(lightBendTrainings) + .sort(function (lhs, rhs) { + return lhs.when - rhs.when; + }); + }); } - $.getJSON("/resources/php/typesafe-feed-trainings.php") - .done(function(data) { - // flatten and filter our sessions by date - var flattenedTrainings = []; - for (var i = 0; i < data[0].length; i++) { - var training = data[0][i]; - for (var j = 0; j < training.sessions.length; j++) { - var session = training.sessions[j]; - - // make sure this session occurs in the future - var when = new Date(session.when); - if (when >= new Date()) { - flattenedTrainings.push({ - title: training.title, - url: session.url, - location: session.where.toUpperCase(), - when: when, - organizer: session.organizer - }); - } + // Render training data for our training page + var frontPageTrainingList = $('.training-items-list'); + // Stop early if the front page element does not exist + if (frontPageTrainingList.length !== 0) { + getTrainings() + .then(function (trainings) { + var MAX_TRAININGS = 5; + + // clear out any preloaded training info + frontPageTrainingList.empty(); + + for (var i = 0; i < Math.min(trainings.length, MAX_TRAININGS); i++) { + var training = trainings[i]; + + // we should have validated our dates by this point + var month = MONTH_NAMES_SHORT[training.when.getMonth()]; + var day = training.when.getDate(); + + // build up our training item + var content = '
' + + '
' + + '' + month + '' + + '' + day + '' + + '
' + + '
' + + '

' + training.title + '

' + + '' + + '
' + + '
'; + + // add it to our list + frontPageTrainingList.append(content); } - } + }); + } - doPopulateTrainingsPane(flattenedTrainings); - }) - .fail(function(jqXHR, textStatus, errorThrown) { - // log the error to the console - console.error("Could not load Lightbend training feed: " + textStatus, errorThrown); + // Render training data for the front page + var trainingPageItemList = $('.training-events .wrap .inner-box'); + // Stop early if the training page element does not exist + if (trainingPageItemList.length !== 0) { + getTrainings() + .then(function (trainings) { + var MAX_TRAININGS = 999; + + // clear out any preloaded training info + trainingPageItemList.empty(); + + var content = ""; + + var i = 0; + var lastIndex = Math.min(trainings.length, MAX_TRAININGS); + while (i < lastIndex) { + // we should have validated our dates by this point + var year = trainings[i].when.getFullYear(); + var month = trainings[i].when.getMonth(); + + // create our training list container + content += '

' + MONTH_NAMES[month] + ' ' + year + '

'; + content += '
'; + + while (i < lastIndex) { + var training = trainings[i]; + + // check if we're still in the right month + if (training.when.getMonth() !== month || training.when.getFullYear() !== year) { + break; + } + + // build up our training item + content += '' + + '
' + + '' + MONTH_NAMES_SHORT[month] + '' + + '' + training.when.getDate() + '' + + '
' + + '
' + + '

' + training.title + '

' + + '

' + training.location + '

' + + '

' + training.organizer + '

' + + '
' + + '
'; + + i++; + } - // but at least display trainings from scala-lang - doPopulateTrainingsPane([]); - }); + content += '
' + } + + trainingPageItemList.append(content); + }); + } }); diff --git a/resources/json/trainings.json b/resources/json/trainings.json new file mode 100644 index 000000000..d25135f95 --- /dev/null +++ b/resources/json/trainings.json @@ -0,0 +1,14 @@ +--- +--- +[ +{% comment %} Grab all the upcoming training sessions defined in _trainings {% endcomment %} +{% for training in site.trainings %}{% if training.date >= site.time %} + { + "title": "{{ training.title | escape }}", + "url": "{{ training.link-out | escape }}", + "location": "{{ training.location | upcase | escape }}", + "when": "{{ training.when | date: "%Y-%m-%d" }}", + "organizer": "{{ training.organizer | escape }}" + }{% unless forloop.last %},{% endunless %} +{% endif %}{% endfor%} +] \ No newline at end of file