@@ -33,10 +33,20 @@ issueList.DropdownView = Backbone.View.extend({
33
33
} ,
34
34
selectDropdownOption : function ( e ) {
35
35
var option = $ ( e . target ) ;
36
+ var paramKey = option . data ( 'paramKey' ) ;
37
+ var paramValue = option . data ( 'paramValue' ) ;
36
38
option . addClass ( 'is-active' )
37
39
. siblings ( ) . removeClass ( 'is-active' ) ;
38
- // TODO: persist in localStorage for page refreshes?
40
+
39
41
this . updateDropdownTitle ( option ) ;
42
+
43
+ // persist value of selection to be used on subsequent page loads
44
+ if ( 'localStorage' in window ) {
45
+ window . localStorage . setItem ( paramKey , paramValue ) ;
46
+ }
47
+
48
+ // fire an event so other views can react to dropdown changes
49
+ wcEvents . trigger ( 'dropdown:change' , paramKey , paramValue ) ;
40
50
e . preventDefault ( ) ;
41
51
} ,
42
52
updateDropdownTitle : function ( optionElm ) {
@@ -56,6 +66,7 @@ issueList.FilterView = Backbone.View.extend({
56
66
57
67
issueList . events . on ( 'filter:activate' , _ . bind ( this . toggleFilter , this ) ) ;
58
68
69
+ // TODO(miket): update with paramKey & paramValue
59
70
var options = [
60
71
{ title : "View all open issues" , params : "" } ,
61
72
{ title : "View all issues" , params : "filter=all" }
@@ -175,14 +186,16 @@ issueList.SortingView = Backbone.View.extend({
175
186
events : { } ,
176
187
initialize : function ( ) {
177
188
this . paginationModel = new Backbone . Model ( {
178
- dropdownTitle : "Show 25" ,
189
+ // TODO(miket): persist selected page limit to survive page loads
190
+ dropdownTitle : "Show 50" ,
179
191
dropdownOptions : [
180
- { title : "Show 25" , params : "" } ,
181
- { title : "Show 50" , params : "" } ,
182
- { title : "Show 100" , params : "" }
192
+ { title : "Show 25" , paramKey : "per_page" , paramValue : "25 "} ,
193
+ { title : "Show 50" , paramKey : "per_page" , paramValue : "50 "} ,
194
+ { title : "Show 100" , paramKey : "per_page" , paramValue : "100 "}
183
195
]
184
196
} ) ;
185
197
198
+ // TODO(miket): update model to have paramKey and paramValue
186
199
this . sortModel = new Backbone . Model ( {
187
200
dropdownTitle : "Newest" ,
188
201
dropdownOptions : [
@@ -196,19 +209,19 @@ issueList.SortingView = Backbone.View.extend({
196
209
this . initSubViews ( ) ;
197
210
} ,
198
211
initSubViews : function ( ) {
199
- /* Commenting out for now, see Issues #312, #266
200
212
this . paginationDropdown = new issueList . DropdownView ( {
201
213
model : this . paginationModel
202
214
} ) ;
215
+ /* Commenting out for now, see Issues #312, #266
203
216
this.sortDropdown = new issueList.DropdownView({
204
217
model: this.sortModel
205
218
}); */
206
219
} ,
207
220
template : _ . template ( $ ( '#issuelist-sorting-tmpl' ) . html ( ) ) ,
208
221
render : function ( ) {
209
222
this . $el . html ( this . template ( ) ) ;
210
- /* Commenting out for now, see Issues #312, #266
211
223
this . paginationDropdown . setElement ( this . $el . find ( '.js-dropdown-pagination' ) ) . render ( ) ;
224
+ /* Commenting out for now, see Issues #312, #266
212
225
this.sortDropdown.setElement(this.$el.find('.js-dropdown-sort')).render(); */
213
226
return this ;
214
227
}
@@ -238,20 +251,23 @@ issueList.IssueView = Backbone.View.extend({
238
251
events : {
239
252
'click .js-issue-label' : 'labelSearch' ,
240
253
} ,
254
+ _isLoggedIn : $ ( 'body' ) . data ( 'username' ) ,
255
+ _pageLimit : null ,
241
256
initialize : function ( ) {
242
257
this . issues = new issueList . IssueCollection ( ) ;
243
258
// check to see if we should pre-filter results
244
- this . checkParams ( ) ;
259
+ this . loadIssues ( ) ;
245
260
246
261
// set up event listeners.
247
262
issueList . events . on ( 'issues:update' , _ . bind ( this . updateIssues , this ) ) ;
248
263
issueList . events . on ( 'paginate:next' , _ . bind ( this . requestNextPage , this ) ) ;
249
264
issueList . events . on ( 'paginate:previous' , _ . bind ( this . requestPreviousPage , this ) ) ;
265
+ wcEvents . on ( 'dropdown:change' , _ . bind ( this . updateModelParams , this ) ) ;
250
266
} ,
251
267
template : _ . template ( $ ( '#issuelist-issue-tmpl' ) . html ( ) ) ,
252
- checkParams : function ( ) {
253
- // Assumes a URI like: /?untriaged=1 and activates the untriaged filter,
254
- // for example.
268
+ loadIssues : function ( ) {
269
+ // First checks URL params, e.g., /?untriaged=1 and activates the untriaged filter,
270
+ // or loads default unsorted/unfiltered issues
255
271
var category ;
256
272
var filterRegex = / \? ( u n t r i a g e d | n e e d s d i a g n o s i s | c o n t a c t r e a d y | s i t e w a i t | c l o s e d ) = 1 / ;
257
273
if ( category = window . location . search . match ( filterRegex ) ) {
@@ -267,6 +283,7 @@ issueList.IssueView = Backbone.View.extend({
267
283
}
268
284
} ,
269
285
fetchAndRenderIssues : function ( ) {
286
+ //assumes this.issues.url has already been set to something meaninful.
270
287
var headers = { headers : { 'Accept' : 'application/json' } } ;
271
288
this . issues . fetch ( headers ) . success ( _ . bind ( function ( ) {
272
289
this . render ( this . issues ) ;
@@ -285,13 +302,15 @@ issueList.IssueView = Backbone.View.extend({
285
302
wcEvents . trigger ( 'flash:error' , { message : message , timeout : timeout } ) ;
286
303
} ) ;
287
304
} ,
305
+ getPageLimit : function ( ) {
306
+ return this . _pageLimit ;
307
+ } ,
288
308
render : function ( issues ) {
289
309
this . $el . html ( this . template ( {
290
310
issues : issues . toJSON ( )
291
311
} ) ) ;
292
312
return this ;
293
313
} ,
294
- _isLoggedIn : $ ( 'body' ) . data ( 'username' ) ,
295
314
initPaginationLinks : function ( issues ) {
296
315
// if either the next or previous page numbers are null
297
316
// disable the buttons and add .is-disabled classes.
@@ -332,41 +351,61 @@ issueList.IssueView = Backbone.View.extend({
332
351
e . preventDefault ( ) ;
333
352
} ,
334
353
requestNextPage : function ( ) {
335
- var newUrl ;
336
354
if ( this . issues . getNextPageNumber ( ) ) {
337
355
// chop off the last character, which is the page number
338
356
// TODO: this feels gross. ideally we should get the URL from the
339
357
// link header and send that to an API endpoint which then requests
340
358
// it from GitHub.
341
- newUrl = this . issues . url . slice ( 0 , - 1 ) + this . issues . getNextPageNumber ( ) ;
342
- this . issues . url = newUrl ;
343
- this . fetchAndRenderIssues ( ) ;
359
+ this . updateModelParams ( "page" , this . issues . getNextPageNumber ( ) ) ;
344
360
}
345
361
} ,
346
362
requestPreviousPage : function ( ) {
347
- var newUrl ;
348
363
if ( this . issues . getPreviousPageNumber ( ) ) {
349
- newUrl = this . issues . url . slice ( 0 , - 1 ) + this . issues . getPreviousPageNumber ( ) ;
350
- this . issues . url = newUrl ;
351
- this . fetchAndRenderIssues ( ) ;
364
+ this . updateModelParams ( "page" , this . issues . getPreviousPageNumber ( ) ) ;
352
365
}
353
366
} ,
354
367
updateIssues : function ( category ) {
355
368
// depending on what category was clicked (or if a search came in),
356
369
// update the collection instance url property and fetch the issues.
357
370
var labelCategories = [ 'closed' , 'contactready' , 'needsdiagnosis' , 'sitewait' ] ;
358
371
372
+ //TODO(miket): make generic getModelParams method which can get the latest state
373
+ // merge param objects and serialize
374
+ var paramsBag = $ . extend ( { page : 1 } , this . getPageLimit ( ) ) ;
375
+ var params = $ . param ( paramsBag ) ;
376
+
359
377
// note: if query is the empty string, it will load all issues from the
360
378
// '/api/issues' endpoint (which I think we want).
361
379
if ( category && category . query ) {
362
- this . issues . url = '/api/issues/search?q=' + category . query + '&page=1' ;
380
+ params = $ . param ( $ . extend ( paramsBag , { q : category . query } ) ) ;
381
+ this . issues . url = '/api/issues/search?' + params ;
363
382
} else if ( _ . contains ( labelCategories , category ) ) {
364
- this . issues . url = '/api/issues/category/' + category + '?page=1' ;
383
+ this . issues . url = '/api/issues/category/' + category + '?' + params ;
365
384
} else if ( category === "untriaged" ) {
366
- this . issues . url = '/api/issues/search/untriaged?page=1' ;
385
+ this . issues . url = '/api/issues/search/untriaged?' + params ;
367
386
} else {
368
- this . issues . url = '/api/issues?page=1' ;
387
+ this . issues . url = '/api/issues?' + params ;
388
+ }
389
+ this . fetchAndRenderIssues ( ) ;
390
+ } ,
391
+ updateModelParams : function ( paramKey , paramValue ) {
392
+ var modelUrl = this . issues . url . split ( '?' ) ;
393
+ var modelPath = modelUrl [ 0 ] ;
394
+ var modelParams = modelUrl [ 1 ] ;
395
+
396
+ var updateParams = { } ;
397
+ updateParams [ paramKey ] = paramValue ;
398
+
399
+ // merge old params with passed in param data
400
+ // $.extend will update existing object keys, and add new ones
401
+ var newParams = $ . extend ( $ . deparam ( modelParams ) , updateParams ) ;
402
+
403
+ if ( paramKey === 'per_page' ) {
404
+ this . _pageLimit = paramValue ;
369
405
}
406
+
407
+ // construct new model URL and re-request issues
408
+ this . issues . url = modelPath + '?' + $ . param ( newParams ) ;
370
409
this . fetchAndRenderIssues ( ) ;
371
410
}
372
411
} ) ;
0 commit comments