/* * Training Event Calendar - Simple Embed Library Object * * Written by: P.Sherratt (innoflair) * Version: 1.0 (13/09/17) * Copyright: EUMETSAT (2017) */ function TrainingCalendar(completedCallback){ var self = this; // Check whether the event should be displayed this.eventToBeDisplayed = function(event){ var display = false; var registrationDateCheck = false; if (event.hasOwnProperty('status') && event.status.hasOwnProperty('value') && event.status.value == "Confirmed"){ display = self.checkEventListValueContains(event,'initiative',["EUMETSAT","EUMeTrain"]); if (!display){ display = self.checkEventListValueContains(event,'trainingPartner',["EUMETSAT"]); } } return display; } // Check whether the events named list contains certain values this.checkEventListValueContains = function(event,listName,contains){ var exists = false; if (event.hasOwnProperty(listName)){ $.each( event[listName], function( initKey, initItem ) { if ((initItem.hasOwnProperty('value') && ($.inArray(initItem.value,contains) > -1))){ exists = true; return true; // break out of inner $.each } }); } return exists; } this.getEventRow = function(event){ row = rowTemplate; if (event.id !=""){ row = row.replace(/\[id\]/,event.id); } // Display Title if (event.title !=""){ row = row.replace(/\[title\]/,event.title); } // Display Venue (City,Country) var venue = []; if (event.city !=""){ venue.push(event.city); } if (event.country != null && event.country.name !=""){ venue.push(event.country.name); } if (venue.length > 0){ row = row.replace(/\[venue\]/,venue.join(", ")); } // Display Date row = row.replace(/\[date\]/,self.getDateRangeStr(event.startDate,event.endDate)); // Clear any unset tokens row = row.replace(/\[[^\]]*\]/,""); return row; } this.getTodaysDateStr = function(time){ return time.getFullYear().toString()+"-"+self.pad(time.getMonth()+1,2).toString()+"-"+self.pad(time.getDate(),2).toString(); } this.startOfDay = function(d){ var year = d.getUTCFullYear(); var month = d.getUTCMonth(); var day = d.getUTCDate(); return Date.UTC(year,month,day,0,0,0,0); } this.pad = function(num, size){ return ('000000000' + num).substr(-size) }; this.getDateRangeStr = function(startDateVal,endDateVal){ var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]; var now = new Date(); var startDateValue = new Date(startDateVal); var endDateValue = new Date(endDateVal); if (startDateValue != null){ if (endDateValue != null){ if (startDateValue == endDateValue){ // Event only on one day return startDateValue.getDate()+" "+months[startDateValue.getMonth()]+", "+startDateValue.getFullYear(); // 'D MMMM, YYYY' } else if (startDateValue.getFullYear() == endDateValue.getFullYear()) { // Dates in same month if (startDateValue.getMonth() == endDateValue.getMonth()) { // Dates in same month if (startDateValue.getDate() == endDateValue.getDate()) { // Dates in same day if (startDateValue == this.startOfDay(startDateValue)) { // Ignore time if set to 00:00:00 UTC return startDateValue.getDate(); } else { return startDateValue.getDate()+" "+months[startDateValue.getMonth()]+", "+startDateValue.getFullYear(); // 'D MMMM, YYYY' } } else { return startDateValue.getDate()+"-"+endDateValue.getDate()+" "+months[endDateValue.getMonth()]+", "+endDateValue.getFullYear(); // 'D-D MMMM, YYYY'; } } else { return startDateValue.getDate()+" "+months[startDateValue.getMonth()]+" - "+endDateValue.getDate()+" "+months[endDateValue.getMonth()]+", "+endDateValue.getFullYear(); // 'D MMMM - D MMMM, YYYY'); } } else { return startDateValue.getDate()+" "+months[startDateValue.getMonth()]+", "+startDateValue.getFullYear()+' - '+endDateValue.getDate()+" "+months[endDateValue.getMonth()]+", "+endDateValue.getFullYear(); } } else { return ''; } } } var now = new Date(/*"2017-01-01"*/); // Set date for testing var todaysDate = this.getTodaysDateStr(now); var displayCount = 5; // Set to -1 to display all var trainingCalTagId = "trainingEventCalendar"; var trainingCalendarUrl="http://trainingevents.eumetsat.int"; var apiQuery = "https://usc.tools.eumetsat.int/trapi/resources/public/events?filter=startDate>='"+todaysDate+"' or endDate>='"+todaysDate+"' order by startDate asc"; // Display template variables var headerTemplate = ""; var rowTemplate = ""; var footerTemplate = "
Upcoming courses
TitleVenueDate
[title][venue][date]
"; // Get the data from the API and display the relevant events $.getJSON( encodeURI(apiQuery), function( data ) { var count = 0; var displayFlag = false; var rows = ""; $.each( data, function( listkey, event ) { if (self.eventToBeDisplayed(event)){ rows += self.getEventRow(event); count++; // break out of the loop if we have reached the displayCount if (count === displayCount){ return false; } } }); if (count>0){ $('#'+trainingCalTagId).html(headerTemplate+rows+footerTemplate).promise().done(function(){ if (completedCallback != null){ completedCallback(); } } ); }else{ $('#'+trainingCalTagId).html("Note: No Training Calendar entries match the criteria."); } }).fail(function() { // Handle failure case $('#'+trainingCalTagId).html("

ERROR: It is currently not possible to load the Training Calendar, please try the following direct link: Training Calendar.

") }); }