Skip to content

Commit 80b4bd5

Browse files
committed
Merge pull request #466 from /issues/371/1
Fixes #371 - Enable issues "sort" dropdown
2 parents 76534f9 + 7cb2985 commit 80b4bd5

File tree

2 files changed

+31
-36
lines changed

2 files changed

+31
-36
lines changed

webcompat/static/js/lib/issue-list.js

+30-35
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,19 @@ issueList.DropdownView = Backbone.View.extend({
3333
},
3434
selectDropdownOption: function(e) {
3535
var option = $(e.target);
36-
var paramKey = option.data('paramKey');
37-
var paramValue = option.data('paramValue');
36+
var params = option.data('params');
3837
option.addClass('is-active')
3938
.siblings().removeClass('is-active');
4039

4140
this.updateDropdownTitle(option);
4241

4342
// persist value of selection to be used on subsequent page loads
4443
if ('localStorage' in window) {
45-
window.localStorage.setItem(paramKey, paramValue);
44+
window.localStorage.setItem("params", params);
4645
}
4746

4847
// fire an event so other views can react to dropdown changes
49-
wcEvents.trigger('dropdown:change', paramKey, paramValue);
48+
wcEvents.trigger('dropdown:change', params);
5049
e.preventDefault();
5150
},
5251
updateDropdownTitle: function(optionElm) {
@@ -203,20 +202,21 @@ issueList.SortingView = Backbone.View.extend({
203202
// TODO(miket): persist selected page limit to survive page loads
204203
dropdownTitle: 'Show 50',
205204
dropdownOptions: [
206-
{title: 'Show 25', paramKey: 'per_page', paramValue: '25'},
207-
{title: 'Show 50', paramKey: 'per_page', paramValue: '50'},
208-
{title: 'Show 100', paramKey: 'per_page', paramValue: '100'}
205+
{title: 'Show 25', params: 'per_page=25'},
206+
{title: 'Show 50', params: 'per_page=50'},
207+
{title: 'Show 100', params: 'per_page=100'}
209208
]
210209
});
211210

212-
// TODO(miket): update model to have paramKey and paramValue
213211
this.sortModel = new Backbone.Model({
214212
dropdownTitle: 'Newest',
215213
dropdownOptions: [
216-
{title: 'Newest', params: ''},
217-
{title: 'Oldest', params: ''},
218-
{title: 'Most Commented', params: ''},
219-
{title: 'etc.', params: ''}
214+
{title: 'Newest', params: 'sort=created&direction=desc'},
215+
{title: 'Oldest', params: 'sort=created&direction=asc'},
216+
{title: 'Most Commented', params: 'sort=comments&direction=desc'},
217+
{title: 'Least Commented', params: 'sort=comments&direction=asc'},
218+
{title: 'Recently Updated', params: 'sort=updated&direction=desc'},
219+
{title: 'Least Recently Updated', params: 'sort=updated&direction=asc'}
220220
]
221221
});
222222

@@ -226,17 +226,15 @@ issueList.SortingView = Backbone.View.extend({
226226
this.paginationDropdown = new issueList.DropdownView({
227227
model: this.paginationModel
228228
});
229-
/* Commenting out for now, see Issues #312, #266
230229
this.sortDropdown = new issueList.DropdownView({
231230
model: this.sortModel
232-
}); */
231+
});
233232
},
234233
template: _.template($('#issuelist-sorting-tmpl').html()),
235234
render: function() {
236235
this.$el.html(this.template());
237236
this.paginationDropdown.setElement(this.$el.find('.js-dropdown-pagination')).render();
238-
/* Commenting out for now, see Issues #312, #266
239-
this.sortDropdown.setElement(this.$el.find('.js-dropdown-sort')).render(); */
237+
this.sortDropdown.setElement(this.$el.find('.js-dropdown-sort')).render();
240238
return this;
241239
}
242240
});
@@ -406,37 +404,34 @@ issueList.IssueView = Backbone.View.extend({
406404
}
407405
this.fetchAndRenderIssues();
408406
},
409-
updateModelParams: function(paramKey, paramValue) {
407+
updateModelParams: function(params) {
410408
var decomposeUrl = function(url) {
411409
var _url = url.split('?');
412410
return {path: _url[0], params: _url[1]};
413411
};
414-
var linkUrl;
412+
415413
var newParams;
416414
var modelUrl = decomposeUrl(this.issues.url);
417-
var parsedModelParams = $.deparam(modelUrl.params);
418-
415+
var paramsArray = params.split('&');
419416
var updateParams = {};
420-
updateParams[paramKey] = paramValue;
421-
422-
// do we have a ?link param in the model URL from traversing pagination?
423-
if (parsedModelParams.hasOwnProperty('link')) {
424-
// if so, decompose link param url, merge updated params, and recompose
425-
linkUrl = decomposeUrl(parsedModelParams.link);
426-
newParams = $.extend($.deparam(linkUrl.params), updateParams);
427-
this.issues.url = modelUrl.path + '?link=' + encodeURIComponent(linkUrl.path + '?' + $.param(newParams));
428-
this.fetchAndRenderIssues();
429-
return;
430-
}
417+
418+
// paramsArray is an array of param 'key=value' string pairs,
419+
// iterate over them in case there are multiple pairs
420+
_.forEach(paramsArray, function(param) {
421+
var kvArray = param.split('=');
422+
var key = kvArray[0];
423+
var value = kvArray[1];
424+
updateParams[key] = value;
425+
426+
if (key === 'per_page') {
427+
this._pageLimit = value;
428+
}
429+
});
431430

432431
// merge old params with passed in param data
433432
// $.extend will update existing object keys, and add new ones
434433
newParams = $.extend($.deparam(modelUrl.params), updateParams);
435434

436-
if (paramKey === 'per_page') {
437-
this._pageLimit = paramValue;
438-
}
439-
440435
// construct new model URL and re-request issues
441436
this.issues.url = modelUrl.path + '?' + $.param(newParams);
442437
this.fetchAndRenderIssues();

webcompat/templates/issue-list.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ <h1 class="IssueList-title wc-Title--l">Issues</h1>
117117
<ul class="Dropdown-content js-dropdown-options" aria-hidden="false">
118118
<% _.each(dropdownOptions, function(option) { %>
119119
<li class="Dropdown-item">
120-
<a href="#" class="Dropdown-link" data-param-key="<%= option.paramKey %>" data-param-value="<%= option.paramValue %>">
120+
<a href="#" class="Dropdown-link" data-params="<%= option.params %>">
121121
<%= option.title %>
122122
</a>
123123
</li>

0 commit comments

Comments
 (0)