globalSearch.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. const subManager = new SubsManager();
  2. BlazeComponent.extendComponent({
  3. events() {
  4. return [
  5. {
  6. 'click .js-due-cards-view-change': Popup.open('globalSearchViewChange'),
  7. },
  8. ];
  9. },
  10. }).register('globalSearchHeaderBar');
  11. Template.globalSearch.helpers({
  12. userId() {
  13. return Meteor.userId();
  14. },
  15. });
  16. BlazeComponent.extendComponent({
  17. events() {
  18. return [
  19. {
  20. 'click .js-due-cards-view-me'() {
  21. Utils.setDueCardsView('me');
  22. Popup.close();
  23. },
  24. 'click .js-due-cards-view-all'() {
  25. Utils.setDueCardsView('all');
  26. Popup.close();
  27. },
  28. },
  29. ];
  30. },
  31. }).register('globalSearchViewChangePopup');
  32. BlazeComponent.extendComponent({
  33. onCreated() {
  34. this.isPageReady = new ReactiveVar(true);
  35. this.searching = new ReactiveVar(false);
  36. this.hasResults = new ReactiveVar(false);
  37. this.query = new ReactiveVar('');
  38. this.queryParams = null;
  39. this.resultsCount = new ReactiveVar(0);
  40. this.totalHits = new ReactiveVar(0);
  41. this.queryErrors = new ReactiveVar(null);
  42. Meteor.subscribe('setting');
  43. },
  44. results() {
  45. if (this.queryParams) {
  46. const results = Cards.globalSearch(this.queryParams);
  47. const sessionData = SessionData.findOne({ userId: Meteor.userId() });
  48. // eslint-disable-next-line no-console
  49. // console.log('sessionData:', sessionData);
  50. // console.log('errors:', results.errors);
  51. this.totalHits.set(sessionData.totalHits);
  52. this.resultsCount.set(results.cards.count());
  53. this.queryErrors.set(results.errors);
  54. return results.cards;
  55. }
  56. this.resultsCount.set(0);
  57. return [];
  58. },
  59. errorMessages() {
  60. const errors = this.queryErrors.get();
  61. const messages = [];
  62. errors.notFound.boards.forEach(board => {
  63. messages.push({ tag: 'board-title-not-found', value: board });
  64. });
  65. errors.notFound.swimlanes.forEach(swim => {
  66. messages.push({ tag: 'swimlane-title-not-found', value: swim });
  67. });
  68. errors.notFound.lists.forEach(list => {
  69. messages.push({ tag: 'list-title-not-found', value: list });
  70. });
  71. errors.notFound.users.forEach(user => {
  72. messages.push({ tag: 'user-username-not-found', value: user });
  73. });
  74. return messages;
  75. },
  76. resultsHeading() {
  77. if (this.resultsCount.get() === 0) {
  78. return TAPi18n.__('no-cards-found');
  79. } else if (this.resultsCount.get() === 1) {
  80. return TAPi18n.__('one-card-found');
  81. } else if (this.resultsCount.get() === this.totalHits.get()) {
  82. return TAPi18n.__('n-cards-found', this.resultsCount.get());
  83. }
  84. return TAPi18n.__('n-n-of-n-cards-found', {
  85. start: 1,
  86. end: this.resultsCount.get(),
  87. total: this.totalHits.get(),
  88. });
  89. },
  90. events() {
  91. return [
  92. {
  93. 'submit .js-search-query-form'(evt) {
  94. evt.preventDefault();
  95. this.query.set(evt.target.searchQuery.value);
  96. this.queryErrors.set(null);
  97. if (!this.query.get()) {
  98. this.searching.set(false);
  99. this.hasResults.set(false);
  100. return;
  101. }
  102. this.searching.set(true);
  103. this.hasResults.set(false);
  104. let query = this.query.get();
  105. // eslint-disable-next-line no-console
  106. // console.log('query:', query);
  107. const reOperator1 = /^((?<operator>\w+):|(?<abbrev>[#@]))(?<value>\w+)(\s+|$)/;
  108. const reOperator2 = /^((?<operator>\w+):|(?<abbrev>[#@]))(?<quote>["']*)(?<value>.*?)\k<quote>(\s+|$)/;
  109. const reText = /^(?<text>\S+)(\s+|$)/;
  110. const reQuotedText = /^(?<quote>["'])(?<text>\w+)\k<quote>(\s+|$)/;
  111. const operatorMap = {};
  112. operatorMap[TAPi18n.__('operator-board')] = 'boards';
  113. operatorMap[TAPi18n.__('operator-board-abbrev')] = 'boards';
  114. operatorMap[TAPi18n.__('operator-swimlane')] = 'swimlanes';
  115. operatorMap[TAPi18n.__('operator-swimlane-abbrev')] = 'swimlanes';
  116. operatorMap[TAPi18n.__('operator-list')] = 'lists';
  117. operatorMap[TAPi18n.__('operator-list-abbrev')] = 'lists';
  118. operatorMap[TAPi18n.__('operator-label')] = 'labels';
  119. operatorMap[TAPi18n.__('operator-label-abbrev')] = 'labels';
  120. operatorMap[TAPi18n.__('operator-user')] = 'users';
  121. operatorMap[TAPi18n.__('operator-user-abbrev')] = 'users';
  122. operatorMap[TAPi18n.__('operator-is')] = 'is';
  123. // eslint-disable-next-line no-console
  124. // console.log('operatorMap:', operatorMap);
  125. const params = {
  126. boards: [],
  127. swimlanes: [],
  128. lists: [],
  129. users: [],
  130. labels: [],
  131. is: [],
  132. };
  133. let text = '';
  134. while (query) {
  135. m = query.match(reOperator1);
  136. if (!m) {
  137. m = query.match(reOperator2);
  138. if (m) {
  139. query = query.replace(reOperator2, '');
  140. }
  141. } else {
  142. query = query.replace(reOperator1, '');
  143. }
  144. if (m) {
  145. let op;
  146. if (m.groups.operator) {
  147. op = m.groups.operator.toLowerCase();
  148. } else {
  149. op = m.groups.abbrev;
  150. }
  151. if (op in operatorMap) {
  152. params[operatorMap[op]].push(m.groups.value);
  153. }
  154. continue;
  155. }
  156. m = query.match(reQuotedText);
  157. if (!m) {
  158. m = query.match(reText);
  159. if (m) {
  160. query = query.replace(reText, '');
  161. }
  162. } else {
  163. query = query.replace(reQuotedText, '');
  164. }
  165. if (m) {
  166. text += (text ? ' ' : '') + m.groups.text;
  167. }
  168. }
  169. // eslint-disable-next-line no-console
  170. // console.log('text:', text);
  171. params.text = text;
  172. // eslint-disable-next-line no-console
  173. // console.log('params:', params);
  174. this.queryParams = params;
  175. this.autorun(() => {
  176. const handle = subManager.subscribe('globalSearch', params);
  177. Tracker.nonreactive(() => {
  178. Tracker.autorun(() => {
  179. // eslint-disable-next-line no-console
  180. // console.log('ready:', handle.ready());
  181. if (handle.ready()) {
  182. this.searching.set(false);
  183. this.hasResults.set(true);
  184. }
  185. });
  186. });
  187. });
  188. },
  189. },
  190. ];
  191. },
  192. }).register('globalSearch');