globalSearch.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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.searching = new ReactiveVar(false);
  35. this.hasResults = new ReactiveVar(false);
  36. this.hasQueryErrors = new ReactiveVar(false);
  37. this.query = new ReactiveVar('');
  38. this.resultsHeading = new ReactiveVar('');
  39. this.searchLink = new ReactiveVar(null);
  40. this.queryParams = null;
  41. this.parsingErrors = [];
  42. this.resultsCount = 0;
  43. this.totalHits = 0;
  44. this.queryErrors = null;
  45. Meteor.subscribe('setting');
  46. if (Session.get('globalQuery')) {
  47. this.searchAllBoards(Session.get('globalQuery'));
  48. }
  49. },
  50. resetSearch() {
  51. this.searching.set(false);
  52. this.hasResults.set(false);
  53. this.hasQueryErrors.set(false);
  54. this.resultsHeading.set('');
  55. this.parsingErrors = [];
  56. this.resultsCount = 0;
  57. this.totalHits = 0;
  58. this.queryErrors = null;
  59. },
  60. results() {
  61. // eslint-disable-next-line no-console
  62. // console.log('getting results');
  63. if (this.queryParams) {
  64. const results = Cards.globalSearch(this.queryParams);
  65. this.queryErrors = results.errors;
  66. // eslint-disable-next-line no-console
  67. // console.log('errors:', this.queryErrors);
  68. if (this.errorMessages().length) {
  69. this.hasQueryErrors.set(true);
  70. return null;
  71. }
  72. if (results.cards) {
  73. const sessionData = SessionData.findOne({ userId: Meteor.userId() });
  74. this.totalHits = sessionData.totalHits;
  75. this.resultsCount = results.cards.count();
  76. this.resultsHeading.set(this.getResultsHeading());
  77. return results.cards;
  78. }
  79. }
  80. this.resultsCount = 0;
  81. return [];
  82. },
  83. errorMessages() {
  84. const messages = [];
  85. if (this.queryErrors) {
  86. this.queryErrors.notFound.boards.forEach(board => {
  87. messages.push({ tag: 'board-title-not-found', value: board });
  88. });
  89. this.queryErrors.notFound.swimlanes.forEach(swim => {
  90. messages.push({ tag: 'swimlane-title-not-found', value: swim });
  91. });
  92. this.queryErrors.notFound.lists.forEach(list => {
  93. messages.push({ tag: 'list-title-not-found', value: list });
  94. });
  95. this.queryErrors.notFound.labels.forEach(label => {
  96. messages.push({ tag: 'label-not-found', value: label });
  97. });
  98. this.queryErrors.notFound.users.forEach(user => {
  99. messages.push({ tag: 'user-username-not-found', value: user });
  100. });
  101. this.queryErrors.notFound.members.forEach(user => {
  102. messages.push({ tag: 'user-username-not-found', value: user });
  103. });
  104. this.queryErrors.notFound.assignees.forEach(user => {
  105. messages.push({ tag: 'user-username-not-found', value: user });
  106. });
  107. }
  108. if (this.parsingErrors.length) {
  109. this.parsingErrors.forEach(err => {
  110. messages.push(err);
  111. });
  112. }
  113. return messages;
  114. },
  115. searchAllBoards(query) {
  116. this.query.set(query);
  117. this.resetSearch();
  118. if (!query) {
  119. return;
  120. }
  121. this.searching.set(true);
  122. // eslint-disable-next-line no-console
  123. // console.log('query:', query);
  124. const reOperator1 = /^((?<operator>\w+):|(?<abbrev>[#@]))(?<value>\w+)(\s+|$)/;
  125. const reOperator2 = /^((?<operator>\w+):|(?<abbrev>[#@]))(?<quote>["']*)(?<value>.*?)\k<quote>(\s+|$)/;
  126. const reText = /^(?<text>\S+)(\s+|$)/;
  127. const reQuotedText = /^(?<quote>["'])(?<text>\w+)\k<quote>(\s+|$)/;
  128. const operatorMap = {};
  129. operatorMap[TAPi18n.__('operator-board')] = 'boards';
  130. operatorMap[TAPi18n.__('operator-board-abbrev')] = 'boards';
  131. operatorMap[TAPi18n.__('operator-swimlane')] = 'swimlanes';
  132. operatorMap[TAPi18n.__('operator-swimlane-abbrev')] = 'swimlanes';
  133. operatorMap[TAPi18n.__('operator-list')] = 'lists';
  134. operatorMap[TAPi18n.__('operator-list-abbrev')] = 'lists';
  135. operatorMap[TAPi18n.__('operator-label')] = 'labels';
  136. operatorMap[TAPi18n.__('operator-label-abbrev')] = 'labels';
  137. operatorMap[TAPi18n.__('operator-user')] = 'users';
  138. operatorMap[TAPi18n.__('operator-user-abbrev')] = 'users';
  139. operatorMap[TAPi18n.__('operator-member')] = 'members';
  140. operatorMap[TAPi18n.__('operator-member-abbrev')] = 'members';
  141. operatorMap[TAPi18n.__('operator-assignee')] = 'assignees';
  142. operatorMap[TAPi18n.__('operator-assignee-abbrev')] = 'assignees';
  143. operatorMap[TAPi18n.__('operator-is')] = 'is';
  144. // eslint-disable-next-line no-console
  145. // console.log('operatorMap:', operatorMap);
  146. const params = {
  147. boards: [],
  148. swimlanes: [],
  149. lists: [],
  150. users: [],
  151. members: [],
  152. assignees: [],
  153. labels: [],
  154. is: [],
  155. };
  156. let text = '';
  157. while (query) {
  158. m = query.match(reOperator1);
  159. if (!m) {
  160. m = query.match(reOperator2);
  161. if (m) {
  162. query = query.replace(reOperator2, '');
  163. }
  164. } else {
  165. query = query.replace(reOperator1, '');
  166. }
  167. if (m) {
  168. let op;
  169. if (m.groups.operator) {
  170. op = m.groups.operator.toLowerCase();
  171. } else {
  172. op = m.groups.abbrev;
  173. }
  174. if (op in operatorMap) {
  175. params[operatorMap[op]].push(m.groups.value);
  176. } else {
  177. this.parsingErrors.push({
  178. tag: 'operator-unknown-error',
  179. value: op,
  180. });
  181. }
  182. continue;
  183. }
  184. m = query.match(reQuotedText);
  185. if (!m) {
  186. m = query.match(reText);
  187. if (m) {
  188. query = query.replace(reText, '');
  189. }
  190. } else {
  191. query = query.replace(reQuotedText, '');
  192. }
  193. if (m) {
  194. text += (text ? ' ' : '') + m.groups.text;
  195. }
  196. }
  197. // eslint-disable-next-line no-console
  198. // console.log('text:', text);
  199. params.text = text;
  200. // eslint-disable-next-line no-console
  201. // console.log('params:', params);
  202. this.queryParams = params;
  203. this.autorun(() => {
  204. const handle = subManager.subscribe('globalSearch', params);
  205. Tracker.nonreactive(() => {
  206. Tracker.autorun(() => {
  207. // eslint-disable-next-line no-console
  208. // console.log('ready:', handle.ready());
  209. if (handle.ready()) {
  210. this.searching.set(false);
  211. this.hasResults.set(true);
  212. }
  213. });
  214. });
  215. });
  216. },
  217. getResultsHeading() {
  218. if (this.resultsCount === 0) {
  219. return TAPi18n.__('no-cards-found');
  220. } else if (this.resultsCount === 1) {
  221. return TAPi18n.__('one-card-found');
  222. } else if (this.resultsCount === this.totalHits) {
  223. return TAPi18n.__('n-cards-found', this.resultsCount);
  224. }
  225. return TAPi18n.__('n-n-of-n-cards-found', {
  226. start: 1,
  227. end: this.resultsCount,
  228. total: this.totalHits,
  229. });
  230. },
  231. getSearchHref() {
  232. const baseUrl = window.location.href.replace(/([?#].*$|\s*$)/, '');
  233. return `${baseUrl}?q=${encodeURIComponent(this.query.get())}`;
  234. },
  235. searchInstructions() {
  236. tags = {
  237. operator_board: TAPi18n.__('operator-board'),
  238. operator_list: TAPi18n.__('operator-list'),
  239. operator_swimlane: TAPi18n.__('operator-swimlane'),
  240. operator_label: TAPi18n.__('operator-label'),
  241. operator_label_abbrev: TAPi18n.__('operator-label-abbrev'),
  242. operator_user: TAPi18n.__('operator-user'),
  243. operator_user_abbrev: TAPi18n.__('operator-user-abbrev'),
  244. operator_member: TAPi18n.__('operator-member'),
  245. operator_member_abbrev: TAPi18n.__('operator-member-abbrev'),
  246. operator_assignee: TAPi18n.__('operator-assignee'),
  247. operator_assignee_abbrev: TAPi18n.__('operator-assignee-abbrev'),
  248. };
  249. text = `# ${TAPi18n.__('globalSearch-instructions-heading')}`;
  250. text += `\n${TAPi18n.__('globalSearch-instructions-description', tags)}`;
  251. text += `\n${TAPi18n.__('globalSearch-instructions-operators', tags)}`;
  252. text += `\n* ${TAPi18n.__(
  253. 'globalSearch-instructions-operator-board',
  254. tags,
  255. )}`;
  256. text += `\n* ${TAPi18n.__(
  257. 'globalSearch-instructions-operator-list',
  258. tags,
  259. )}`;
  260. text += `\n* ${TAPi18n.__(
  261. 'globalSearch-instructions-operator-swimlane',
  262. tags,
  263. )}`;
  264. text += `\n* ${TAPi18n.__(
  265. 'globalSearch-instructions-operator-label',
  266. tags,
  267. )}`;
  268. text += `\n* ${TAPi18n.__(
  269. 'globalSearch-instructions-operator-hash',
  270. tags,
  271. )}`;
  272. text += `\n* ${TAPi18n.__(
  273. 'globalSearch-instructions-operator-user',
  274. tags,
  275. )}`;
  276. text += `\n* ${TAPi18n.__('globalSearch-instructions-operator-at', tags)}`;
  277. text += `\n* ${TAPi18n.__(
  278. 'globalSearch-instructions-operator-member',
  279. tags,
  280. )}`;
  281. text += `\n* ${TAPi18n.__(
  282. 'globalSearch-instructions-operator-assignee',
  283. tags,
  284. )}`;
  285. text += `\n## ${TAPi18n.__('heading-notes')}`;
  286. text += `\n* ${TAPi18n.__('globalSearch-instructions-notes-1', tags)}`;
  287. text += `\n* ${TAPi18n.__('globalSearch-instructions-notes-2', tags)}`;
  288. text += `\n* ${TAPi18n.__('globalSearch-instructions-notes-3', tags)}`;
  289. text += `\n* ${TAPi18n.__('globalSearch-instructions-notes-4', tags)}`;
  290. text += `\n* ${TAPi18n.__('globalSearch-instructions-notes-5', tags)}`;
  291. return text;
  292. },
  293. events() {
  294. return [
  295. {
  296. 'submit .js-search-query-form'(evt) {
  297. evt.preventDefault();
  298. this.searchAllBoards(evt.target.searchQuery.value);
  299. },
  300. },
  301. ];
  302. },
  303. }).register('globalSearch');