Skip to content

Instantly share code, notes, and snippets.

@martindrapeau
Last active February 22, 2019 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martindrapeau/20028ef4cb71d48a77dd6fc28c782d1f to your computer and use it in GitHub Desktop.
Save martindrapeau/20028ef4cb71d48a77dd6fc28c782d1f to your computer and use it in GitHub Desktop.
Sports Montreal new activities table for a sub-category on their website
Namespace("axis.sm.activities");
axis.sm.activities.display = function (evt, ajaxArgs) {
var english = $("body").hasClass("l_en");
var strings = {
lang: english ? 'en' : 'fr',
months: english
? ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
: ["janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"],
month: english ? "month" : "mois",
week: english ? "week" : "semaine",
day: english ? "day" : "jour",
moreThan5: english ? "More than 5" : "Plus de 5",
noVacancy: english ? "Full" : "Complet",
pleaseWait: english ? "Please wait..." : "Un instant...",
register: english ? "Register" : "S'inscrire"
};
function getDate(dateArg) {
var date = null;
if (dateArg) {
try {
date = new Date(dateArg);
date.setTime(date.getTime() + date.getTimezoneOffset()*60*1000);
} catch (err) {}
}
return date;
}
function getDaysBetween(date1, date2) {
if (!date1 || !date2) return null;
var one_day=1000*60*60*24;
var date1_ms = (new Date(date1)).getTime();
var date2_ms = (new Date(date2)).getTime()+1;
var difference_ms = date2_ms - date1_ms;
return Math.ceil(difference_ms/one_day);
}
function fillActivitiesTable($table, subCategoryId) {
var $tbody = $table.find('tbody');
$tbody.find('tr').remove();
if ($table.find('thead>tr>th').size() == 6) $table.find('thead>tr').append('<th></th>');
$tbody.append('<tr><td colspan="7" style="text-align:center;">' + strings.pleaseWait + '</td></tr>');
$.get('https://www.amilia.com/PublicApi/sports-montreal/SubCategory/' + subCategoryId)
.done(function(subCategory) {
var activities = subCategory.Activities;
$tbody.find('tr').remove();
for (var i = 0; i < activities.length; i++) {
var activity = activities[i];
var name = $.trim((activity.Name + '').split('|')[0]);
if (activity.ShortBeginString) {
if ($.trim(subCategory.Name) == $.trim(activity.Name))
name = activity.ShortBeginString;
else
name += ' (' + activity.ShortBeginString + ')';
}
var dateStr = '';
var duration = '';
if (activity.Schedule && activity.Schedule.TimePeriod) {
var dateBegin = getDate(activity.Schedule.TimePeriod.StartDate);
if (dateBegin) {
var dateStr = english
? [strings.months[dateBegin.getMonth()], dateBegin.getDate()].join(' ')
: [dateBegin.getDate(), strings.months[dateBegin.getMonth()]].join(' ');
}
var occurences = activity.Schedule.TimePeriod.NbOccurences || 1;
var daysBetween = getDaysBetween(activity.Schedule.TimePeriod.StartDate, activity.Schedule.TimePeriod.RecurrenceEndDate);
var periodString = occurences == 1 || daysBetween == occurences || occurences > Math.ceil(daysBetween/7)+1 ? strings.day : strings.week;
/*console.log({
start: activity.Schedule.TimePeriod.StartDate,
end: activity.Schedule.TimePeriod.RecurrenceEndDate,
occurences: occurences,
daysBetween: daysBetween,
daysBetweenDiv7: Math.ceil(daysBetween/7)+1,
periodString: periodString
});*/
duration = [occurences, ' ', periodString, occurences > 1 ? 's' : ''].join('');
}
var price = !isNaN(activity.Price) ? activity.Price.toFixed(2) + ' $' : '';
var spots = '';
if (!isNaN(activity.NbPlacesLeft)) {
if (activity.NbPlacesLeft == 0) {
spots = strings.noVacancy;
}
else if (activity.NbPlacesLeft <= 5) {
spots = activity.NbPlacesLeft;
}
else {
spots = strings.moreThan5;
}
}
var url = activity.Url.replace('/fr/', '/' + strings.lang + '/');
$tbody.append(
'<tr>' +
' <td class="name">' + name + '</td>' +
' <td class="location">' + $.trim(activity.Location.split('|')[0]) + '</td>' +
' <td class="date">' + dateStr + '</td>' +
' <td class="duration">' + duration + '</td>' +
' <td class="price">' + price + '</td>' +
' <td class="spots">' + spots + '</td>' +
' <td class="cta"><a class="divLnkNewsFormatage" href="' + url + '" target="_blank">' + strings.register + '</a></td>' +
'</tr>'
);
}
});
}
$('.sm-subcategory').each(function() {
var $table = $(this).find('table.sm-activities');
var id = parseInt($(this).find('input.sm-sub-val').val(), 10);
if (id && !isNaN(id) && $table.size()) fillActivitiesTable($table, id);
});
};
$(axis.sm.activities.display);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment