globalSearch.js 11 KB

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