cardSearch.js 5.6 KB

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