Skip to content

Commit

Permalink
Introduce redirect pattern during change of limit
Browse files Browse the repository at this point in the history
  • Loading branch information
lbajsarowicz committed Feb 28, 2020
1 parent 05f1a2b commit f25de7b
Showing 1 changed file with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ define([
direction: 'product_list_dir',
order: 'product_list_order',
limit: 'product_list_limit',
page: 'p',
modeDefault: 'grid',
directionDefault: 'asc',
orderDefault: 'position',
Expand Down Expand Up @@ -80,7 +81,7 @@ define([
);
},

getUrlParams: function() {
getUrlParams: function () {
var decode = window.decodeURIComponent,
urlPaths = this.options.url.split('?'),
urlParams = urlPaths[1] ? urlPaths[1].split('&') : [],
Expand All @@ -96,17 +97,13 @@ define([
return params;
},

getCurrentLimit: function()
{
var currentLimit = this.getUrlParams()[this.options.limit];

if (currentLimit === undefined) {
currentLimit = this.options.limitDefault;
}

return currentLimit;
getCurrentLimit: function () {
return this.getUrlParams()[this.options.limit] || this.options.limitDefault;
},

getCurrentPage: function () {
return this.getUrlParams()[this.options.page] || 1;
},

/**
* @param {String} paramName
Expand All @@ -117,10 +114,17 @@ define([
var urlPaths = this.options.url.split('?'),
baseUrl = urlPaths[0],
paramData = this.getUrlParams(),
form, params, key, input, formKey;
currentPage = this.getCurrentPage(),
form, params, key, input, formKey, newPage;

if (currentPage > 1 && paramName === this.options.limit) {
newPage = Math.floor(this.getCurrentLimit() * this.getCurrentPage() / paramValue);

if (paramName === this.options.limit && paramValue > this.getCurrentLimit()) {
delete paramData['p'];
if (newPage > 1) {
paramData[this.options.page] = newPage;
} else {
delete paramData[this.options.page];
}
}

paramData[paramName] = paramValue;
Expand Down

0 comments on commit f25de7b

Please sign in to comment.