globalSearch.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 colorMap = {};
  129. colorMap[TAPi18n.__('color-black')] = 'black';
  130. colorMap[TAPi18n.__('color-blue')] = 'blue';
  131. colorMap[TAPi18n.__('color-crimson')] = 'crimson';
  132. colorMap[TAPi18n.__('color-darkgreen')] = 'darkgreen';
  133. colorMap[TAPi18n.__('color-gold')] = 'gold';
  134. colorMap[TAPi18n.__('color-gray')] = 'gray';
  135. colorMap[TAPi18n.__('color-green')] = 'green';
  136. colorMap[TAPi18n.__('color-indigo')] = 'indigo';
  137. colorMap[TAPi18n.__('color-lime')] = 'lime';
  138. colorMap[TAPi18n.__('color-magenta')] = 'magenta';
  139. colorMap[TAPi18n.__('color-mistyrose')] = 'mistyrose';
  140. colorMap[TAPi18n.__('color-navy')] = 'navy';
  141. colorMap[TAPi18n.__('color-orange')] = 'orange';
  142. colorMap[TAPi18n.__('color-paleturquoise')] = 'paleturquoise';
  143. colorMap[TAPi18n.__('color-peachpuff')] = 'peachpuff';
  144. colorMap[TAPi18n.__('color-pink')] = 'pink';
  145. colorMap[TAPi18n.__('color-plum')] = 'plum';
  146. colorMap[TAPi18n.__('color-purple')] = 'purple';
  147. colorMap[TAPi18n.__('color-red')] = 'red';
  148. colorMap[TAPi18n.__('color-saddlebrown')] = 'saddlebrown';
  149. colorMap[TAPi18n.__('color-silver')] = 'silver';
  150. colorMap[TAPi18n.__('color-sky')] = 'sky';
  151. colorMap[TAPi18n.__('color-slateblue')] = 'slateblue';
  152. colorMap[TAPi18n.__('color-white')] = 'white';
  153. colorMap[TAPi18n.__('color-yellow')] = 'yellow';
  154. const operatorMap = {};
  155. operatorMap[TAPi18n.__('operator-board')] = 'boards';
  156. operatorMap[TAPi18n.__('operator-board-abbrev')] = 'boards';
  157. operatorMap[TAPi18n.__('operator-swimlane')] = 'swimlanes';
  158. operatorMap[TAPi18n.__('operator-swimlane-abbrev')] = 'swimlanes';
  159. operatorMap[TAPi18n.__('operator-list')] = 'lists';
  160. operatorMap[TAPi18n.__('operator-list-abbrev')] = 'lists';
  161. operatorMap[TAPi18n.__('operator-label')] = 'labels';
  162. operatorMap[TAPi18n.__('operator-label-abbrev')] = 'labels';
  163. operatorMap[TAPi18n.__('operator-user')] = 'users';
  164. operatorMap[TAPi18n.__('operator-user-abbrev')] = 'users';
  165. operatorMap[TAPi18n.__('operator-member')] = 'members';
  166. operatorMap[TAPi18n.__('operator-member-abbrev')] = 'members';
  167. operatorMap[TAPi18n.__('operator-assignee')] = 'assignees';
  168. operatorMap[TAPi18n.__('operator-assignee-abbrev')] = 'assignees';
  169. operatorMap[TAPi18n.__('operator-is')] = 'is';
  170. // eslint-disable-next-line no-console
  171. // console.log('operatorMap:', operatorMap);
  172. const params = {
  173. boards: [],
  174. swimlanes: [],
  175. lists: [],
  176. users: [],
  177. members: [],
  178. assignees: [],
  179. labels: [],
  180. is: [],
  181. };
  182. let text = '';
  183. while (query) {
  184. m = query.match(reOperator1);
  185. if (!m) {
  186. m = query.match(reOperator2);
  187. if (m) {
  188. query = query.replace(reOperator2, '');
  189. }
  190. } else {
  191. query = query.replace(reOperator1, '');
  192. }
  193. if (m) {
  194. let op;
  195. if (m.groups.operator) {
  196. op = m.groups.operator.toLowerCase();
  197. } else {
  198. op = m.groups.abbrev;
  199. }
  200. if (op in operatorMap) {
  201. let value = m.groups.value;
  202. if (operatorMap[op] === 'labels') {
  203. if (value in colorMap) {
  204. value = colorMap[value];
  205. }
  206. }
  207. params[operatorMap[op]].push(value);
  208. } else {
  209. this.parsingErrors.push({
  210. tag: 'operator-unknown-error',
  211. value: op,
  212. });
  213. }
  214. continue;
  215. }
  216. m = query.match(reQuotedText);
  217. if (!m) {
  218. m = query.match(reText);
  219. if (m) {
  220. query = query.replace(reText, '');
  221. }
  222. } else {
  223. query = query.replace(reQuotedText, '');
  224. }
  225. if (m) {
  226. text += (text ? ' ' : '') + m.groups.text;
  227. }
  228. }
  229. // eslint-disable-next-line no-console
  230. // console.log('text:', text);
  231. params.text = text;
  232. // eslint-disable-next-line no-console
  233. // console.log('params:', params);
  234. this.queryParams = params;
  235. this.autorun(() => {
  236. const handle = subManager.subscribe('globalSearch', params);
  237. Tracker.nonreactive(() => {
  238. Tracker.autorun(() => {
  239. // eslint-disable-next-line no-console
  240. // console.log('ready:', handle.ready());
  241. if (handle.ready()) {
  242. this.searching.set(false);
  243. this.hasResults.set(true);
  244. }
  245. });
  246. });
  247. });
  248. },
  249. getResultsHeading() {
  250. if (this.resultsCount === 0) {
  251. return TAPi18n.__('no-cards-found');
  252. } else if (this.resultsCount === 1) {
  253. return TAPi18n.__('one-card-found');
  254. } else if (this.resultsCount === this.totalHits) {
  255. return TAPi18n.__('n-cards-found', this.resultsCount);
  256. }
  257. return TAPi18n.__('n-n-of-n-cards-found', {
  258. start: 1,
  259. end: this.resultsCount,
  260. total: this.totalHits,
  261. });
  262. },
  263. getSearchHref() {
  264. const baseUrl = window.location.href.replace(/([?#].*$|\s*$)/, '');
  265. return `${baseUrl}?q=${encodeURIComponent(this.query.get())}`;
  266. },
  267. searchInstructions() {
  268. tags = {
  269. operator_board: TAPi18n.__('operator-board'),
  270. operator_list: TAPi18n.__('operator-list'),
  271. operator_swimlane: TAPi18n.__('operator-swimlane'),
  272. operator_label: TAPi18n.__('operator-label'),
  273. operator_label_abbrev: TAPi18n.__('operator-label-abbrev'),
  274. operator_user: TAPi18n.__('operator-user'),
  275. operator_user_abbrev: TAPi18n.__('operator-user-abbrev'),
  276. operator_member: TAPi18n.__('operator-member'),
  277. operator_member_abbrev: TAPi18n.__('operator-member-abbrev'),
  278. operator_assignee: TAPi18n.__('operator-assignee'),
  279. operator_assignee_abbrev: TAPi18n.__('operator-assignee-abbrev'),
  280. };
  281. text = `# ${TAPi18n.__('globalSearch-instructions-heading')}`;
  282. text += `\n${TAPi18n.__('globalSearch-instructions-description', tags)}`;
  283. text += `\n${TAPi18n.__('globalSearch-instructions-operators', tags)}`;
  284. text += `\n* ${TAPi18n.__(
  285. 'globalSearch-instructions-operator-board',
  286. tags,
  287. )}`;
  288. text += `\n* ${TAPi18n.__(
  289. 'globalSearch-instructions-operator-list',
  290. tags,
  291. )}`;
  292. text += `\n* ${TAPi18n.__(
  293. 'globalSearch-instructions-operator-swimlane',
  294. tags,
  295. )}`;
  296. text += `\n* ${TAPi18n.__(
  297. 'globalSearch-instructions-operator-label',
  298. tags,
  299. )}`;
  300. text += `\n* ${TAPi18n.__(
  301. 'globalSearch-instructions-operator-hash',
  302. tags,
  303. )}`;
  304. text += `\n* ${TAPi18n.__(
  305. 'globalSearch-instructions-operator-user',
  306. tags,
  307. )}`;
  308. text += `\n* ${TAPi18n.__('globalSearch-instructions-operator-at', tags)}`;
  309. text += `\n* ${TAPi18n.__(
  310. 'globalSearch-instructions-operator-member',
  311. tags,
  312. )}`;
  313. text += `\n* ${TAPi18n.__(
  314. 'globalSearch-instructions-operator-assignee',
  315. tags,
  316. )}`;
  317. text += `\n## ${TAPi18n.__('heading-notes')}`;
  318. text += `\n* ${TAPi18n.__('globalSearch-instructions-notes-1', tags)}`;
  319. text += `\n* ${TAPi18n.__('globalSearch-instructions-notes-2', tags)}`;
  320. text += `\n* ${TAPi18n.__('globalSearch-instructions-notes-3', tags)}`;
  321. text += `\n* ${TAPi18n.__('globalSearch-instructions-notes-4', tags)}`;
  322. text += `\n* ${TAPi18n.__('globalSearch-instructions-notes-5', tags)}`;
  323. return text;
  324. },
  325. events() {
  326. return [
  327. {
  328. 'submit .js-search-query-form'(evt) {
  329. evt.preventDefault();
  330. this.searchAllBoards(evt.target.searchQuery.value);
  331. },
  332. },
  333. ];
  334. },
  335. }).register('globalSearch');