cardSearch.js 5.7 KB

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