cardSearch.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { TAPi18n } from '/imports/i18n';
  2. import Cards from '../../models/cards';
  3. import SessionData from '../../models/usersessiondata';
  4. import {QueryDebug} from "../../config/query-classes";
  5. import {OPERATOR_DEBUG} from "../../config/search-const";
  6. export class CardSearchPagedComponent extends BlazeComponent {
  7. onCreated() {
  8. this.searching = new ReactiveVar(false);
  9. this.hasResults = new ReactiveVar(false);
  10. this.hasQueryErrors = new ReactiveVar(false);
  11. this.query = new ReactiveVar('');
  12. this.resultsHeading = new ReactiveVar('');
  13. this.searchLink = new ReactiveVar(null);
  14. this.results = new ReactiveVar([]);
  15. this.hasNextPage = new ReactiveVar(false);
  16. this.hasPreviousPage = new ReactiveVar(false);
  17. this.resultsCount = 0;
  18. this.totalHits = 0;
  19. this.queryErrors = null;
  20. this.resultsPerPage = 25;
  21. this.sessionId = SessionData.getSessionId();
  22. this.subscriptionHandle = null;
  23. this.serverError = new ReactiveVar(false);
  24. this.sessionData = null;
  25. this.debug = new ReactiveVar(new QueryDebug());
  26. const that = this;
  27. this.subscriptionCallbacks = {
  28. onReady() {
  29. that.getResults();
  30. that.searching.set(false);
  31. that.hasResults.set(true);
  32. that.serverError.set(false);
  33. },
  34. onError(error) {
  35. that.searching.set(false);
  36. that.hasResults.set(false);
  37. that.serverError.set(true);
  38. // eslint-disable-next-line no-console
  39. //console.log('Error.reason:', error.reason);
  40. // eslint-disable-next-line no-console
  41. //console.log('Error.message:', error.message);
  42. // eslint-disable-next-line no-console
  43. //console.log('Error.stack:', error.stack);
  44. },
  45. };
  46. }
  47. resetSearch() {
  48. this.searching.set(false);
  49. this.results.set([]);
  50. this.hasResults.set(false);
  51. this.hasQueryErrors.set(false);
  52. this.resultsHeading.set('');
  53. this.serverError.set(false);
  54. this.resultsCount = 0;
  55. this.totalHits = 0;
  56. this.queryErrors = null;
  57. this.debug.set(new QueryDebug());
  58. }
  59. getSessionData(sessionId) {
  60. return SessionData.findOne({
  61. sessionId: sessionId ? sessionId : SessionData.getSessionId(),
  62. });
  63. }
  64. getResults() {
  65. // eslint-disable-next-line no-console
  66. // console.log('getting results');
  67. this.sessionData = this.getSessionData();
  68. // eslint-disable-next-line no-console
  69. console.log('session data:', this.sessionData);
  70. const cards = [];
  71. this.sessionData.cards.forEach(cardId => {
  72. cards.push(Cards.findOne({ _id: cardId }));
  73. });
  74. this.queryErrors = this.sessionData.errors;
  75. if (this.queryErrors.length) {
  76. // console.log('queryErrors:', this.queryErrorMessages());
  77. this.hasQueryErrors.set(true);
  78. // return null;
  79. }
  80. this.debug.set(new QueryDebug(this.sessionData.debug));
  81. console.log('debug:', this.debug.get().get());
  82. console.log('debug.show():', this.debug.get().show());
  83. console.log('debug.showSelector():', this.debug.get().showSelector());
  84. if (cards) {
  85. this.totalHits = this.sessionData.totalHits;
  86. this.resultsCount = cards.length;
  87. this.resultsStart = this.sessionData.lastHit - this.resultsCount + 1;
  88. this.resultsEnd = this.sessionData.lastHit;
  89. this.resultsHeading.set(this.getResultsHeading());
  90. this.results.set(cards);
  91. this.hasNextPage.set(this.sessionData.lastHit < this.sessionData.totalHits);
  92. this.hasPreviousPage.set(
  93. this.sessionData.lastHit - this.sessionData.resultsCount > 0,
  94. );
  95. return cards;
  96. }
  97. this.resultsCount = 0;
  98. return null;
  99. }
  100. stopSubscription() {
  101. if (this.subscriptionHandle) {
  102. this.subscriptionHandle.stop();
  103. }
  104. }
  105. getSubscription(queryParams) {
  106. return Meteor.subscribe(
  107. 'globalSearch',
  108. this.sessionId,
  109. queryParams.params,
  110. queryParams.text,
  111. this.subscriptionCallbacks,
  112. );
  113. }
  114. runGlobalSearch(queryParams) {
  115. this.searching.set(true);
  116. this.debug.set(new QueryDebug());
  117. this.stopSubscription();
  118. this.subscriptionHandle = this.getSubscription(queryParams);
  119. }
  120. queryErrorMessages() {
  121. const messages = [];
  122. this.queryErrors.forEach(err => {
  123. let value = err.color ? TAPi18n.__(`color-${err.value}`) : err.value;
  124. if (!value) {
  125. value = err.value;
  126. }
  127. messages.push(TAPi18n.__(err.tag, value));
  128. });
  129. return messages;
  130. }
  131. nextPage() {
  132. this.searching.set(true);
  133. this.stopSubscription();
  134. this.subscriptionHandle = Meteor.subscribe(
  135. 'nextPage',
  136. this.sessionId,
  137. this.subscriptionCallbacks,
  138. );
  139. }
  140. previousPage() {
  141. this.searching.set(true);
  142. this.stopSubscription();
  143. this.subscriptionHandle = Meteor.subscribe(
  144. 'previousPage',
  145. this.sessionId,
  146. this.subscriptionCallbacks,
  147. );
  148. }
  149. getResultsHeading() {
  150. if (this.resultsCount === 0) {
  151. return TAPi18n.__('no-cards-found');
  152. } else if (this.resultsCount === 1) {
  153. return TAPi18n.__('one-card-found');
  154. } else if (this.resultsCount === this.totalHits) {
  155. return TAPi18n.__('n-cards-found', this.resultsCount);
  156. }
  157. return TAPi18n.__('n-n-of-n-cards-found', {
  158. start: this.resultsStart,
  159. end: this.resultsEnd,
  160. total: this.totalHits,
  161. });
  162. }
  163. getSearchHref() {
  164. const baseUrl = window.location.href.replace(/([?#].*$|\s*$)/, '');
  165. return `${baseUrl}?q=${encodeURIComponent(this.query.get())}`;
  166. }
  167. events() {
  168. return [
  169. {
  170. 'click .js-next-page'(evt) {
  171. evt.preventDefault();
  172. this.nextPage();
  173. },
  174. 'click .js-previous-page'(evt) {
  175. evt.preventDefault();
  176. this.previousPage();
  177. },
  178. },
  179. ];
  180. }
  181. }