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
@@ -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);
}
setTransient('lastSearch', { searchKey, model }, 5);
this.set("model", model);
}).finally(() => this.set("searching", false));
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);
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();
}
},
}
});