Add infinite loading to full page search.

This commit is contained in:
Jakub Macina 2017-07-25 15:33:25 +02:00
parent 4eb7f7cd10
commit 2d45b3fc6d
2 changed files with 80 additions and 45 deletions

View File

@ -14,12 +14,13 @@ const SortOrders = [
{name: I18n.t('search.latest_topic'), id: 4, term: 'order:latest_topic'},
];
const PAGE_LIMIT = 10;
export default Ember.Controller.extend({
application: Ember.inject.controller(),
bulkSelectEnabled: null,
loading: Em.computed.not("model"),
loading: false,
queryParams: ["q", "expanded", "context_id", "context", "skip_context"],
q: null,
selected: [],
@ -30,11 +31,8 @@ export default Ember.Controller.extend({
sortOrder: 0,
sortOrders: SortOrders,
invalidSearch: false,
@computed('model.posts')
resultCount(posts) {
return posts && posts.length;
},
page: 1,
resultCount: null,
@computed('resultCount')
hasResults(resultCount) {
@ -113,6 +111,7 @@ export default Ember.Controller.extend({
@observes('sortOrder')
triggerSearch() {
if (this._searchOnSortChange) {
this.set("page", 1);
this._search();
}
},
@ -143,6 +142,11 @@ export default Ember.Controller.extend({
this.set("application.showFooter", !this.get("loading"));
},
@observes('model.posts.length')
resultCountChanged() {
this.set("resultCount", this.get("model.posts.length"));
},
@computed('hasResults')
canBulkSelect(hasResults) {
return this.currentUser && this.currentUser.staff && hasResults;
@ -158,6 +162,11 @@ export default Ember.Controller.extend({
return iconHTML(expanded ? "caret-down" : "caret-right");
},
@computed('page')
isLastPage(page) {
return page == PAGE_LIMIT;
},
_search() {
if (this.get("searching")) { return; }
@ -169,10 +178,11 @@ export default Ember.Controller.extend({
}
this.set("searching", true);
this.set("loading", true);
this.set('bulkSelectEnabled', false);
this.get('selected').clear();
var args = { q: searchTerm };
var args = { q: searchTerm, page: this.get('page') };
const sortOrder = this.get("sortOrder");
if (sortOrder && SortOrders[sortOrder].term) {
@ -180,7 +190,6 @@ export default Ember.Controller.extend({
}
this.set("q", args.q);
this.set("model", null);
const skip = this.get("skip_context");
if ((!skip && this.get('context')) || skip==="false"){
@ -199,9 +208,20 @@ export default Ember.Controller.extend({
this.set('q', results.grouped_search_result.term);
}
if(args.page > 1){
if (model){
this.get("model").posts.pushObjects(model.posts);
this.get("model").topics.pushObjects(model.topics);
this.get("model").set('grouped_search_result', results.grouped_search_result);
}
}else{
setTransient('lastSearch', { searchKey, model }, 5);
this.set("model", model);
}).finally(() => this.set("searching", false));
}
}).finally(() => {
this.set("searching", false);
this.set("loading", false);
});
},
actions: {
@ -227,11 +247,20 @@ export default Ember.Controller.extend({
},
search() {
this.set("page", 1);
this._search();
},
toggleAdvancedSearch() {
this.toggleProperty('expanded');
},
loadMore() {
var page = this.get('page');
if (this.get('model.grouped_search_result.more_full_page_results') && !this.get("loading") && page < PAGE_LIMIT){
this.incrementProperty("page");
this._search();
}
},
}
});

View File

@ -45,16 +45,6 @@
</div>
{{/if}}
{{#conditional-loading-spinner condition=loading}}
{{#unless hasResults}}
<h3>
{{#if searchActive}}
{{i18n "search.no_results"}}
{{/if}}
</h3>
{{/unless}}
{{#if hasResults}}
<div class='search-title clearfix'>
<div class='result-count'>
@ -71,6 +61,7 @@
</div>
{{/if}}
{{#load-more selector=".fps-result" action="loadMore"}}
{{#each model.posts as |result|}}
<div class='fps-result'>
<div class='author'>
@ -124,15 +115,30 @@
</div>
{{/each}}
{{#conditional-loading-spinner condition=loading }}
{{#unless hasResults}}
<h3>
{{#if searchActive}}
{{i18n "search.no_results"}}
{{/if}}
</h3>
{{/unless}}
{{#if hasResults}}
{{#unless loading}}
<h3 class="search-footer">
{{#if model.grouped_search_result.more_full_page_results}}
{{#if isLastPage }}
{{i18n "search.more_results"}}
{{/if}}
{{else}}
{{i18n "search.no_more_results"}}
{{/if}}
</h3>
{{/unless}}
{{/if}}
{{/conditional-loading-spinner}}
{{/load-more}}
{{/d-section}}