2
0

cardSearch.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. export class CardSearchPagedComponent extends BlazeComponent {
  2. onCreated() {
  3. this.searching = new ReactiveVar(false);
  4. this.hasResults = new ReactiveVar(false);
  5. this.hasQueryErrors = new ReactiveVar(false);
  6. this.query = new ReactiveVar('');
  7. this.resultsHeading = new ReactiveVar('');
  8. this.searchLink = new ReactiveVar(null);
  9. this.results = new ReactiveVar([]);
  10. this.hasNextPage = new ReactiveVar(false);
  11. this.hasPreviousPage = new ReactiveVar(false);
  12. this.resultsCount = 0;
  13. this.totalHits = 0;
  14. this.queryErrors = null;
  15. this.resultsPerPage = 25;
  16. }
  17. resetSearch() {
  18. this.searching.set(false);
  19. this.results.set([]);
  20. this.hasResults.set(false);
  21. this.hasQueryErrors.set(false);
  22. this.resultsHeading.set('');
  23. this.resultsCount = 0;
  24. this.totalHits = 0;
  25. this.queryErrors = null;
  26. }
  27. getSessionData() {
  28. return SessionData.findOne({
  29. userId: Meteor.userId(),
  30. sessionId: SessionData.getSessionId(),
  31. });
  32. }
  33. getResults() {
  34. // eslint-disable-next-line no-console
  35. // console.log('getting results');
  36. const sessionData = this.getSessionData();
  37. // eslint-disable-next-line no-console
  38. // console.log('selector:', sessionData.getSelector());
  39. console.log('session data:', sessionData);
  40. const projection = sessionData.getProjection();
  41. projection.skip = 0;
  42. const cards = Cards.find({ _id: { $in: sessionData.cards } }, projection);
  43. this.queryErrors = sessionData.errors;
  44. if (this.queryErrors.length) {
  45. this.hasQueryErrors.set(true);
  46. return null;
  47. }
  48. if (cards) {
  49. this.totalHits = sessionData.totalHits;
  50. this.resultsCount = cards.count();
  51. this.resultsStart = sessionData.lastHit - this.resultsCount + 1;
  52. this.resultsEnd = sessionData.lastHit;
  53. this.resultsHeading.set(this.getResultsHeading());
  54. this.results.set(cards);
  55. this.hasNextPage.set(sessionData.lastHit < sessionData.totalHits);
  56. this.hasPreviousPage.set(
  57. sessionData.lastHit - sessionData.resultsCount > 0,
  58. );
  59. return cards;
  60. }
  61. this.resultsCount = 0;
  62. return null;
  63. }
  64. autorunGlobalSearch(params) {
  65. this.searching.set(true);
  66. this.autorun(() => {
  67. const handle = Meteor.subscribe(
  68. 'globalSearch',
  69. SessionData.getSessionId(),
  70. params,
  71. );
  72. Tracker.nonreactive(() => {
  73. Tracker.autorun(() => {
  74. if (handle.ready()) {
  75. this.getResults();
  76. this.searching.set(false);
  77. this.hasResults.set(true);
  78. }
  79. });
  80. });
  81. });
  82. }
  83. queryErrorMessages() {
  84. const messages = [];
  85. this.queryErrors.forEach(err => {
  86. let value = err.color ? TAPi18n.__(`color-${err.value}`) : err.value;
  87. if (!value) {
  88. value = err.value;
  89. }
  90. messages.push(TAPi18n.__(err.tag, value));
  91. });
  92. return messages;
  93. }
  94. nextPage() {
  95. const sessionData = this.getSessionData();
  96. this.autorun(() => {
  97. const handle = Meteor.subscribe('nextPage', sessionData.sessionId);
  98. Tracker.nonreactive(() => {
  99. Tracker.autorun(() => {
  100. if (handle.ready()) {
  101. this.getResults();
  102. this.searching.set(false);
  103. this.hasResults.set(true);
  104. }
  105. });
  106. });
  107. });
  108. }
  109. previousPage() {
  110. const sessionData = this.getSessionData();
  111. this.autorun(() => {
  112. const handle = Meteor.subscribe('previousPage', sessionData.sessionId);
  113. Tracker.nonreactive(() => {
  114. Tracker.autorun(() => {
  115. if (handle.ready()) {
  116. this.getResults();
  117. this.searching.set(false);
  118. this.hasResults.set(true);
  119. }
  120. });
  121. });
  122. });
  123. }
  124. getResultsHeading() {
  125. if (this.resultsCount === 0) {
  126. return TAPi18n.__('no-cards-found');
  127. } else if (this.resultsCount === 1) {
  128. return TAPi18n.__('one-card-found');
  129. } else if (this.resultsCount === this.totalHits) {
  130. return TAPi18n.__('n-cards-found', this.resultsCount);
  131. }
  132. return TAPi18n.__('n-n-of-n-cards-found', {
  133. start: this.resultsStart,
  134. end: this.resultsEnd,
  135. total: this.totalHits,
  136. });
  137. }
  138. getSearchHref() {
  139. const baseUrl = window.location.href.replace(/([?#].*$|\s*$)/, '');
  140. return `${baseUrl}?q=${encodeURIComponent(this.query.get())}`;
  141. }
  142. events() {
  143. return [
  144. {
  145. 'click .js-next-page'(evt) {
  146. evt.preventDefault();
  147. this.nextPage();
  148. },
  149. 'click .js-previous-page'(evt) {
  150. evt.preventDefault();
  151. this.previousPage();
  152. },
  153. },
  154. ];
  155. }
  156. }