Added search query minifying

Seeing 'page=1' and 'query=' in every other link was tiresome. I changed
the rules so that such keys are appended only if they hold nontrivial
values.
This commit is contained in:
Marcin Kurczewski 2014-12-20 10:36:29 +01:00
parent 970b9bf06d
commit 1897297127
5 changed files with 30 additions and 9 deletions

View file

@ -82,11 +82,12 @@ App.Presenters.PagerPresenter = function(
function getUrl(options) { function getUrl(options) {
return util.appendComplexRouteParam( return util.appendComplexRouteParam(
baseUri, baseUri,
_.extend( util.simplifySearchQuery(
{}, _.extend(
pager.getSearchParams(), {},
{page: pager.getPage()}, pager.getSearchParams(),
options)); {page: pager.getPage()},
options)));
} }
function syncUrl(options) { function syncUrl(options) {

View file

@ -41,7 +41,11 @@ App.Services.PostsAroundCalculator = function(_, promise, util, pager) {
if (position + direction >= 0 && position + direction < postIds.length) { if (position + direction >= 0 && position + direction < postIds.length) {
var url = util.appendComplexRouteParam( var url = util.appendComplexRouteParam(
'#/post/' + postIds[position + direction], '#/post/' + postIds[position + direction],
_.extend({page: page}, pager.getSearchParams())); util.simplifySearchQuery(
_.extend(
{page: page},
pager.getSearchParams())));
resolve(url); resolve(url);
} else if (page + direction >= 1) { } else if (page + direction >= 1) {
pager.setPage(page + direction); pager.setPage(page + direction);
@ -54,7 +58,11 @@ App.Services.PostsAroundCalculator = function(_, promise, util, pager) {
var url = util.appendComplexRouteParam( var url = util.appendComplexRouteParam(
'#/post/' + post.id, '#/post/' + post.id,
_.extend({page: page + direction}, pager.getSearchParams())); util.simplifySearchQuery(
_.extend(
{page: page + direction},
pager.getSearchParams())));
resolve(url); resolve(url);
} else { } else {
resolve(null); resolve(null);

View file

@ -237,6 +237,17 @@ App.Util.Misc = function(_, jQuery, marked, promise) {
return result.slice(0, -1); return result.slice(0, -1);
} }
function simplifySearchQuery(query) {
if (typeof(query) === 'undefined') {
return {};
}
if (query.page === 1) {
delete query.page;
}
query = _.pick(query, _.identity); //remove falsy values
return query;
}
return { return {
promiseTemplate: promiseTemplate, promiseTemplate: promiseTemplate,
formatRelativeTime: formatRelativeTime, formatRelativeTime: formatRelativeTime,
@ -249,6 +260,7 @@ App.Util.Misc = function(_, jQuery, marked, promise) {
transparentPixel: transparentPixel, transparentPixel: transparentPixel,
loadImagesNicely: loadImagesNicely, loadImagesNicely: loadImagesNicely,
appendComplexRouteParam: appendComplexRouteParam, appendComplexRouteParam: appendComplexRouteParam,
simplifySearchQuery: simplifySearchQuery,
}; };
}; };

View file

@ -2,7 +2,7 @@
<% if (canViewPosts) { %> <% if (canViewPosts) { %>
<a class="link" <a class="link"
href="<%= util.appendComplexRouteParam('#/post/' + post.id, typeof(query) !== 'undefined' ? query : {}) %>" href="<%= util.appendComplexRouteParam('#/post/' + post.id, util.simplifySearchQuery(query)) %>"
title="<%= _.map(post.tags, function(tag) { return '#' + tag.name; }).join(', ') %>"> title="<%= _.map(post.tags, function(tag) { return '#' + tag.name; }).join(', ') %>">
<% } else { %> <% } else { %>
<span class="link"> <span class="link">

View file

@ -10,7 +10,7 @@
</div> </div>
<div class="search"> <div class="search">
<a class="enabled" href="<%= util.appendComplexRouteParam('#/posts', {query: query.query, order: query.order}) %>"> <a class="enabled" href="<%= util.appendComplexRouteParam('#/posts', util.simplifySearchQuery({query: query.query, order: query.order})) %>">
Current search: <%= query.query || '-' %> Current search: <%= query.query || '-' %>
</a> </a>
</div> </div>