/*global jQuery */
var oscar = (function(o, $) {
// Replicate Django's flash messages so they can be used by AJAX callbacks.
o.messages = {
addMessage: function(tag, msg) {
var msgHTML = '
';
$('#messages').append($(msgHTML));
},
debug: function(msg) { o.messages.addMessage('debug', msg); },
info: function(msg) { o.messages.addMessage('info', msg); },
success: function(msg) { o.messages.addMessage('success', msg); },
warning: function(msg) { o.messages.addMessage('warning', msg); },
error: function(msg) { o.messages.addMessage('danger', msg); },
clear: function() {
$('#messages').html('');
},
scrollTo: function() {
$('html').animate({scrollTop: $('#messages').offset().top});
}
};
o.search = {
init: function() {
o.search.initSortWidget();
o.search.initFacetWidgets();
},
initSortWidget: function() {
// Auto-submit (hidden) search form when selecting a new sort-by option
$('#id_sort_by').on('change', function() {
$(this).closest('form').submit();
});
},
initFacetWidgets: function() {
// Bind events to facet checkboxes
$('.facet_checkbox').on('change', function() {
window.location.href = $(this).nextAll('.facet_url').val();
});
}
};
// Notifications inbox within 'my account' section.
o.notifications = {
init: function() {
$('a[data-behaviours~="archive"]').click(function() {
o.notifications.checkAndSubmit($(this), 'archive');
});
$('a[data-behaviours~="delete"]').click(function() {
o.notifications.checkAndSubmit($(this), 'delete');
});
},
checkAndSubmit: function($ele, btn_val) {
$ele.closest('tr').find('input').attr('checked', 'checked');
$ele.closest('form').find('button[value="' + btn_val + '"]').click();
return false;
}
};
// Site-wide forms events
o.forms = {
init: function() {
// Forms with this behaviour are 'locked' once they are submitted to
// prevent multiple submissions
$('form[data-behaviours~="lock"]').submit(o.forms.submitIfNotLocked);
// Disable buttons when they are clicked and show a "loading" message taken from the
// data-loading-text attribute.
// Do not disable if button is inside a form with invalid fields.
// This uses a delegated event so that it keeps working for forms that are reloaded
// via AJAX: https://api.jquery.com/on/#direct-and-delegated-events
$(document.body).on('click', '[data-loading-text]', function(){
var $btn_or_input = $(this),
form = $btn_or_input.parents("form");
if (!form || $(":invalid", form).length == 0) {
var d = 'disabled',
val = $btn_or_input.is('input') ? 'val' : 'html';
// push to event loop so as not to delay form submission
setTimeout(function() {
$btn_or_input[val]($btn_or_input.data('loading-text'));
$btn_or_input.addClass(d).attr(d, d).prop(d, true);
});
}
});
// stuff for star rating on review page
// show clickable stars instead of a select dropdown for product rating
var ratings = $('.reviewrating');
if(ratings.length){
ratings.find('.star-rating i').on('click',o.forms.reviewRatingClick);
}
},
submitIfNotLocked: function() {
var $form = $(this);
if ($form.data('locked')) {
return false;
}
$form.data('locked', true);
},
reviewRatingClick: function(){
var ratings = ['One','Two','Three','Four','Five']; //possible classes for display state
$(this).parent().removeClass('One Two Three Four Five').addClass(ratings[$(this).index()]);
$(this).closest('.controls').find('select').val($(this).index() + 1); //select is hidden, set value
}
};
o.basket = {
is_form_being_submitted: false,
init: function(options) {
if (typeof options == 'undefined') {
options = {'basketURL': document.URL};
}
o.basket.url = options.basketURL || document.URL;
$('#content_inner').on('click', '#basket_formset a[data-behaviours~="remove"]', function(event) {
o.basket.checkAndSubmit($(this), 'form', 'DELETE');
event.preventDefault();
});
$('#content_inner').on('click', '#basket_formset a[data-behaviours~="save"]', function(event) {
o.basket.checkAndSubmit($(this), 'form', 'save_for_later');
event.preventDefault();
});
$('#content_inner').on('click', '#saved_basket_formset a[data-behaviours~="move"]', function() {
o.basket.checkAndSubmit($(this), 'saved', 'move_to_basket');
});
$('#content_inner').on('click', '#saved_basket_formset a[data-behaviours~="remove"]', function(event) {
o.basket.checkAndSubmit($(this), 'saved', 'DELETE');
event.preventDefault();
});
$('#content_inner').on('click', '#voucher_form_link a', function(event) {
o.basket.showVoucherForm();
event.preventDefault();
});
$('#content_inner').on('click', '#voucher_form_cancel', function(event) {
o.basket.hideVoucherForm();
event.preventDefault();
});
$('#content_inner').on('submit', '#basket_formset', o.basket.submitBasketForm);
if (window.location.hash == '#voucher') {
o.basket.showVoucherForm();
}
},
submitBasketForm: function(event) {
$('#messages').html('');
var payload = $('#basket_formset').serializeArray();
$.post(o.basket.url, payload, o.basket.submitFormSuccess, 'json');
if (event) {
event.preventDefault();
}
},
submitFormSuccess: function(data) {
$('#content_inner').html(data.content_html);
// Show any flash messages
o.messages.clear();
for (var level in data.messages) {
for (var i=0; i