function postToUrl(path, params, method) {
method = method || "post"; // Set method to post by default, if not specified.
var form = $(document.createElement( "form" ))
.attr( {"method": method, "action": path} );
$.each( params, function(key,value){
$.each( value instanceof Array? value : [value], function(i,val){
$(document.createElement("input"))
.attr({ "type": "hidden", "name": key, "value": val })
.appendTo( form );
});
} );
form.appendTo( document.body ).submit();
}
function changeLimit(current_href) {
var limit = $('#pageLimit').val();
if(!window.history.pushState) {
postToUrl(current_href, {"limit": limit});
return false;
}
$.ajax({
url: current_href,
dataType: 'json',
type: 'post',
data: {
'limit': limit,
'no_filter': true
},
beforeSend: function(){
$.blockUI.defaults.css = {};
$('#products_overview').block({ message: '
Loading...
', overlayCSS: { backgroundColor: '#fff', cursor: 'default' } });
},
success: function(response){
$('article.webshop-products').html(response.content);
$.scrollTo('#products_overview', 0, { axis: 'y' });
if (typeof toggleFilter === "function") {
toggleFilter();
}
var title = $('title').text().replace(/&/g, '&').replace(/>>/g, '>').replace(/</g, 'M').replace(/"/g, '"');
var current_url = response.current_url;
history.pushState({'goto':current_url}, title, current_url);
/* Check if page was changed on the server side */
if (current_href.indexOf('page/') !== -1) {
var page = current_href.substr((current_href.indexOf('page/') + 5));
if (page.length) {
var new_page = '';
if (current_url.indexOf('page/') !== -1) {
new_page = current_url.substr((current_url.indexOf('page/') + 5));
}
if (new_page.substr(0, page.length) != page) {
window.location.href = current_url;
}
}
}
if($('#grid-container-products').length === 1){
$('#grid-container-products img').on("load", function() {
$('#grid-container-products').masonry({ itemSelector: '.product', stamp: '.stamp', isFitWidth: true });
});
}
}
});
return false;
}
function changeSort(current_href) {
var sort = $('#pageSort').val();
if(!window.history.pushState) {
postToUrl(current_href, {"sort": sort});
return false;
}
$.ajax({
url: current_href,
dataType: 'json',
type: 'post',
data: {
'sort': sort,
'no_filter': true
},
beforeSend: function(){
$.blockUI.defaults.css = {};
$('#products_overview').block({ message: '
Loading...
', overlayCSS: { backgroundColor: '#fff', cursor: 'default' } });
},
success: function(response){
$('article.webshop-products').html(response.content);
$.scrollTo('#products_overview', 0, { axis: 'y' });
if (typeof toggleFilter === "function") {
toggleFilter();
}
var title = $('title').text().replace(/&/g, '&').replace(/>>/g, '>').replace(/</g, 'M').replace(/"/g, '"');
var current_url = response.current_url;
history.pushState({'goto':current_url}, title, current_url);
if($('#grid-container-products').length === 1){
$('#grid-container-products img').on("load", function() {
$('#grid-container-products').masonry({ itemSelector: '.product', stamp: '.stamp', isFitWidth: true });
});
}
}
});
return false;
}
window.onpopstate = function(e){
if(e.state && e.state.goto){
window.location.href = e.state.goto;
}
};