globalSearch.js 6.0 KB

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