globalSearch.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.labels.forEach(label => {
  72. messages.push({ tag: 'label-not-found', value: label });
  73. });
  74. errors.notFound.users.forEach(user => {
  75. messages.push({ tag: 'user-username-not-found', value: user });
  76. });
  77. return messages;
  78. },
  79. resultsHeading() {
  80. if (this.resultsCount.get() === 0) {
  81. return TAPi18n.__('no-cards-found');
  82. } else if (this.resultsCount.get() === 1) {
  83. return TAPi18n.__('one-card-found');
  84. } else if (this.resultsCount.get() === this.totalHits.get()) {
  85. return TAPi18n.__('n-cards-found', this.resultsCount.get());
  86. }
  87. return TAPi18n.__('n-n-of-n-cards-found', {
  88. start: 1,
  89. end: this.resultsCount.get(),
  90. total: this.totalHits.get(),
  91. });
  92. },
  93. events() {
  94. return [
  95. {
  96. 'submit .js-search-query-form'(evt) {
  97. evt.preventDefault();
  98. this.query.set(evt.target.searchQuery.value);
  99. this.queryErrors.set(null);
  100. if (!this.query.get()) {
  101. this.searching.set(false);
  102. this.hasResults.set(false);
  103. return;
  104. }
  105. this.searching.set(true);
  106. this.hasResults.set(false);
  107. let query = this.query.get();
  108. // eslint-disable-next-line no-console
  109. // console.log('query:', query);
  110. const reOperator1 = /^((?<operator>\w+):|(?<abbrev>[#@]))(?<value>\w+)(\s+|$)/;
  111. const reOperator2 = /^((?<operator>\w+):|(?<abbrev>[#@]))(?<quote>["']*)(?<value>.*?)\k<quote>(\s+|$)/;
  112. const reText = /^(?<text>\S+)(\s+|$)/;
  113. const reQuotedText = /^(?<quote>["'])(?<text>\w+)\k<quote>(\s+|$)/;
  114. const operatorMap = {};
  115. operatorMap[TAPi18n.__('operator-board')] = 'boards';
  116. operatorMap[TAPi18n.__('operator-board-abbrev')] = 'boards';
  117. operatorMap[TAPi18n.__('operator-swimlane')] = 'swimlanes';
  118. operatorMap[TAPi18n.__('operator-swimlane-abbrev')] = 'swimlanes';
  119. operatorMap[TAPi18n.__('operator-list')] = 'lists';
  120. operatorMap[TAPi18n.__('operator-list-abbrev')] = 'lists';
  121. operatorMap[TAPi18n.__('operator-label')] = 'labels';
  122. operatorMap[TAPi18n.__('operator-label-abbrev')] = 'labels';
  123. operatorMap[TAPi18n.__('operator-user')] = 'users';
  124. operatorMap[TAPi18n.__('operator-user-abbrev')] = 'users';
  125. operatorMap[TAPi18n.__('operator-is')] = 'is';
  126. // eslint-disable-next-line no-console
  127. // console.log('operatorMap:', operatorMap);
  128. const params = {
  129. boards: [],
  130. swimlanes: [],
  131. lists: [],
  132. users: [],
  133. labels: [],
  134. is: [],
  135. };
  136. let text = '';
  137. while (query) {
  138. m = query.match(reOperator1);
  139. if (!m) {
  140. m = query.match(reOperator2);
  141. if (m) {
  142. query = query.replace(reOperator2, '');
  143. }
  144. } else {
  145. query = query.replace(reOperator1, '');
  146. }
  147. if (m) {
  148. let op;
  149. if (m.groups.operator) {
  150. op = m.groups.operator.toLowerCase();
  151. } else {
  152. op = m.groups.abbrev;
  153. }
  154. if (op in operatorMap) {
  155. params[operatorMap[op]].push(m.groups.value);
  156. }
  157. continue;
  158. }
  159. m = query.match(reQuotedText);
  160. if (!m) {
  161. m = query.match(reText);
  162. if (m) {
  163. query = query.replace(reText, '');
  164. }
  165. } else {
  166. query = query.replace(reQuotedText, '');
  167. }
  168. if (m) {
  169. text += (text ? ' ' : '') + m.groups.text;
  170. }
  171. }
  172. // eslint-disable-next-line no-console
  173. // console.log('text:', text);
  174. params.text = text;
  175. // eslint-disable-next-line no-console
  176. // console.log('params:', params);
  177. this.queryParams = params;
  178. this.autorun(() => {
  179. const handle = subManager.subscribe('globalSearch', params);
  180. Tracker.nonreactive(() => {
  181. Tracker.autorun(() => {
  182. // eslint-disable-next-line no-console
  183. // console.log('ready:', handle.ready());
  184. if (handle.ready()) {
  185. this.searching.set(false);
  186. this.hasResults.set(true);
  187. }
  188. });
  189. });
  190. });
  191. },
  192. },
  193. ];
  194. },
  195. }).register('globalSearch');