// Refresh counter with number of shortlisted items
function refresh_shortlist_counter() {
// Get shortlist from cookie
var shortlist = cookie_get();
// Count items
var shortlist_length = $(shortlist).length;
// Get counter element
var shortlist_counter = $('.shortlist-counter');
// Change HTML
if (shortlist_length > 0) {
$(shortlist_counter).html(shortlist_length);
$(shortlist_counter).removeClass('d-none');
} else {
$(shortlist_counter).html('');
$(shortlist_counter).addClass('d-none');
}
}
// Get all shortlisted items and mark their corresponding togglers as selected
function mark_selected_items() {
// Get shortlist from cookie
var shortlist = cookie_get();
//console.log('cookie: ' + shortlist);
// Mark each item as selected
$.each( shortlist, function( key, element_id ) {
// Get element to target
var shortlist_toggler = $('#shortlist-' + element_id);
// Change HTML
change_state(shortlist_toggler, 'selected');
});
}
// Change the state of a toggler
function change_state(element, state) {
//console.log(element+': '+state);
// Check if this is a small or a large type button
var large_button = false;
if ($(element).hasClass( 'btn-shortlist-lg' ))
large_button = true;
$(element).data('state',state);
if (state == 'selected') {
if (large_button) {
$(element).html(' Add to shortlist');
} else {
$(element).html('');
}
}
else {
if (large_button) {
$(element).html(' Add to shortlist');
} else {
$(element).html('');
}
}
}
// Save an array with shortlisted items to cookie
function cookie_save(shortlist) {
shortlist = JSON.stringify(shortlist);
//console.log(shortlist);
$.cookie('shortlist', shortlist, { path: '/' });
}
// Get array with shortlisted items
function cookie_get() {
var cookie = $.cookie('shortlist');
if (cookie == undefined)
return [];
else
return JSON.parse(cookie);
}
function cookie_clear(id) {
}
$( document ).ready(function() {
// Initiate shortlist counter
refresh_shortlist_counter();
// Mark shortlisted items as selected
mark_selected_items();
//$.removeCookie('shortlist');
// Delete from shortlist in shortlist viewer
$('a.short-list-delete').click(function() {
// Get items that are already shortlisted in cookie
var shortlist = cookie_get();
// Get ID of selected item
var element_id = $(this).attr('id');
element_id = element_id.replace('shortlist-del-','');
// Remove from cookie
shortlist.splice( $.inArray(element_id, shortlist), 1 );
// Store shortlist into cookie
cookie_save(shortlist);
// Refresh page
location.reload();
});
// Shortlist toggler click behaviour
$('a.shortlist-toggle').click(function() {
// Get items that are already shortlisted in cookie
var shortlist = cookie_get();
// Get current state
var state = $(this).data('state');
// Get ID of selected item
var element_id = $(this).attr('id');
element_id = element_id.replace('shortlist-','');
// Clicked item is not yet on the shortlist
if (state == 'unselected') {
// Add to cookie
shortlist.push(element_id);
// Change HTML
change_state($(this), 'selected');
} else {
// Remove from cookie
shortlist.splice( $.inArray(element_id, shortlist), 1 );
// Change HTML
change_state($(this), 'unselected');
}
// Store shortlist into cookie
cookie_save(shortlist);
// Refresh top counter
refresh_shortlist_counter();
});
});