Start work on paging search results

This commit is contained in:
John R. Supplee 2021-01-27 02:21:12 +02:00
parent 7ad1171c5f
commit 4e8fc46475
5 changed files with 348 additions and 230 deletions

View file

@ -37,6 +37,12 @@ template(name="globalSearch")
a.fa.fa-link(title="{{_ 'link-to-search' }}" href="{{ getSearchHref }}")
each card in results.get
+resultCard(card)
if hasPreviousPage.get
button.js-previous-page
| {{_ 'previous-page' }}
if hasNextPage.get
button.js-next-page
| {{_ 'next-page' }}
else
.global-search-instructions
h2 {{_ 'boards' }}

View file

@ -46,12 +46,15 @@ BlazeComponent.extendComponent({
this.myLabelNames = new ReactiveVar([]);
this.myBoardNames = new ReactiveVar([]);
this.results = new ReactiveVar([]);
this.hasNextPage = new ReactiveVar(false);
this.hasPreviousPage = new ReactiveVar(false);
this.queryParams = null;
this.parsingErrors = [];
this.resultsCount = 0;
this.totalHits = 0;
this.queryErrors = null;
this.colorMap = null;
this.resultsPerPage = 25;
Meteor.call('myLists', (err, data) => {
if (!err) {
@ -100,17 +103,21 @@ BlazeComponent.extendComponent({
this.queryErrors = null;
},
getSessionData() {
return SessionData.findOne({
userId: Meteor.userId(),
sessionId: SessionData.getSessionId(),
});
},
getResults() {
// eslint-disable-next-line no-console
// console.log('getting results');
if (this.queryParams) {
const sessionData = SessionData.findOne({
userId: Meteor.userId(),
sessionId: SessionData.getSessionId(),
});
const sessionData = this.getSessionData();
// eslint-disable-next-line no-console
console.log('selector:', JSON.parse(sessionData.selector));
// console.log('session data:', sessionData);
const cards = Cards.find({ _id: { $in: sessionData.cards } });
this.queryErrors = sessionData.errors;
if (this.queryErrors.length) {
@ -121,8 +128,14 @@ BlazeComponent.extendComponent({
if (cards) {
this.totalHits = sessionData.totalHits;
this.resultsCount = cards.count();
this.resultsStart = sessionData.lastHit - this.resultsCount + 1;
this.resultsEnd = sessionData.lastHit;
this.resultsHeading.set(this.getResultsHeading());
this.results.set(cards);
this.hasNextPage.set(sessionData.lastHit < sessionData.totalHits);
this.hasPreviousPage.set(
sessionData.lastHit - sessionData.resultsCount > 0,
);
}
}
this.resultsCount = 0;
@ -243,6 +256,7 @@ BlazeComponent.extendComponent({
// console.log('operatorMap:', operatorMap);
const params = {
limit: this.resultsPerPage,
boards: [],
swimlanes: [],
lists: [],
@ -395,6 +409,61 @@ BlazeComponent.extendComponent({
});
},
nextPage() {
sessionData = this.getSessionData();
const params = {
limit: this.resultsPerPage,
selector: JSON.parse(sessionData.selector),
skip: sessionData.lastHit,
};
this.autorun(() => {
const handle = Meteor.subscribe(
'globalSearch',
SessionData.getSessionId(),
params,
);
Tracker.nonreactive(() => {
Tracker.autorun(() => {
if (handle.ready()) {
this.getResults();
this.searching.set(false);
this.hasResults.set(true);
}
});
});
});
},
previousPage() {
sessionData = this.getSessionData();
const params = {
limit: this.resultsPerPage,
selector: JSON.parse(sessionData.selector),
skip:
sessionData.lastHit - sessionData.resultsCount - this.resultsPerPage,
};
this.autorun(() => {
const handle = Meteor.subscribe(
'globalSearch',
SessionData.getSessionId(),
params,
);
Tracker.nonreactive(() => {
Tracker.autorun(() => {
if (handle.ready()) {
this.getResults();
this.searching.set(false);
this.hasResults.set(true);
}
});
});
});
},
getResultsHeading() {
if (this.resultsCount === 0) {
return TAPi18n.__('no-cards-found');
@ -405,8 +474,8 @@ BlazeComponent.extendComponent({
}
return TAPi18n.__('n-n-of-n-cards-found', {
start: 1,
end: this.resultsCount,
start: this.resultsStart,
end: this.resultsEnd,
total: this.totalHits,
});
},
@ -526,6 +595,14 @@ BlazeComponent.extendComponent({
evt.preventDefault();
this.searchAllBoards(evt.target.searchQuery.value);
},
'click .js-next-page'(evt) {
evt.preventDefault();
this.nextPage();
},
'click .js-previous-page'(evt) {
evt.preventDefault();
this.previousPage();
},
'click .js-label-color'(evt) {
evt.preventDefault();
const input = document.getElementById('global-search-input');

View file

@ -917,6 +917,8 @@
"operator-number-expected": "operator __operator__ expected a number, got '__value__'",
"operator-sort-invalid": "sort of '%s' is invalid",
"operator-status-invalid": "'%s' is not a valid status",
"next-page": "Next Page",
"previous-page": "Previous Page",
"heading-notes": "Notes",
"globalSearch-instructions-heading": "Search Instructions",
"globalSearch-instructions-description": "Searches can include operators to refine the search. Operators are specified by writing the operator name and value separated by a colon. For example, an operator specification of `list:Blocked` would limit the search to cards that are contained in a list named *Blocked*. If the value contains spaces or special characters it must be enclosed in quotation marks (e.g. `__operator_list__:\"To Review\"`).",

View file

@ -39,6 +39,13 @@ SessionData.attachSchema(
type: Number,
optional: true,
},
resultsCount: {
/**
* number of results returned
*/
type: Number,
optional: true,
},
lastHit: {
/**
* the last hit returned from a report query
@ -50,6 +57,11 @@ SessionData.attachSchema(
type: [String],
optional: true,
},
selector: {
type: String,
optional: true,
blackbox: true,
},
errorMessages: {
type: [String],
optional: true,

View file

@ -205,8 +205,8 @@ Meteor.publish('globalSearch', function(sessionId, queryParams) {
}
hasErrors() {
for (const prop in this.notFound) {
if (this.notFound[prop].length) {
for (const value of Object.values(this.notFound)) {
if (value.length) {
return true;
}
}
@ -247,6 +247,19 @@ Meteor.publish('globalSearch', function(sessionId, queryParams) {
}
})();
let selector = {};
let skip = 0;
if (queryParams.skip) {
skip = queryParams.skip;
}
let limit = 25;
if (queryParams.limit) {
limit = queryParams.limit;
}
if (queryParams.selector) {
selector = queryParams.selector;
} else {
let archived = false;
let endAt = null;
if (queryParams.status.length) {
@ -260,7 +273,7 @@ Meteor.publish('globalSearch', function(sessionId, queryParams) {
}
});
}
const selector = {
selector = {
type: 'cardType-card',
// boardId: { $in: Boards.userBoardIds(userId) },
$and: [],
@ -483,9 +496,6 @@ Meteor.publish('globalSearch', function(sessionId, queryParams) {
});
}
let cards = null;
if (!errors.hasErrors()) {
if (queryParams.text) {
const regex = new RegExp(escapeForRegex(queryParams.text), 'i');
@ -508,12 +518,16 @@ Meteor.publish('globalSearch', function(sessionId, queryParams) {
if (selector.$and.length === 0) {
delete selector.$and;
}
}
// eslint-disable-next-line no-console
console.log('selector:', selector);
// eslint-disable-next-line no-console
console.log('selector.$and:', selector.$and);
let cards = null;
if (!errors.hasErrors()) {
const projection = {
fields: {
_id: 1,
@ -532,7 +546,8 @@ Meteor.publish('globalSearch', function(sessionId, queryParams) {
modifiedAt: 1,
labelIds: 1,
},
limit: 50,
skip,
limit,
};
if (queryParams.sort === 'due') {
@ -569,27 +584,33 @@ Meteor.publish('globalSearch', function(sessionId, queryParams) {
};
}
// eslint-disable-next-line no-console
console.log('projection:', projection);
cards = Cards.find(selector, projection);
// eslint-disable-next-line no-console
// console.log('count:', cards.count());
console.log('count:', cards.count());
}
const update = {
$set: {
totalHits: 0,
lastHit: 0,
resultsCount: 0,
cards: [],
errors: errors.errorMessages(),
selector: JSON.stringify(selector),
},
};
if (cards) {
update.$set.totalHits = cards.count();
update.$set.lastHit = cards.count() > 50 ? 50 : cards.count();
update.$set.lastHit =
skip + limit < cards.count() ? skip + limit : cards.count();
update.$set.cards = cards.map(card => {
return card._id;
});
update.$set.resultsCount = update.$set.cards.length;
}
SessionData.upsert({ userId, sessionId }, update);