listBody.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import { TAPi18n } from '/imports/i18n';
  3. import { Spinner } from '/client/lib/spinner';
  4. const subManager = new SubsManager();
  5. const InfiniteScrollIter = 10;
  6. BlazeComponent.extendComponent({
  7. onCreated() {
  8. // for infinite scrolling
  9. this.cardlimit = new ReactiveVar(InfiniteScrollIter);
  10. },
  11. mixins() {
  12. return [];
  13. },
  14. customFieldsSum() {
  15. return CustomFields.find({
  16. boardIds: { $in: [Session.get('currentBoard')] },
  17. showSumAtTopOfList: true,
  18. });
  19. },
  20. openForm(options) {
  21. options = options || {};
  22. options.position = options.position || 'top';
  23. const forms = this.childComponents('inlinedForm');
  24. let form = forms.find(component => {
  25. return component.data().position === options.position;
  26. });
  27. if (!form && forms.length > 0) {
  28. form = forms[0];
  29. }
  30. form.open();
  31. },
  32. addCard(evt) {
  33. evt.preventDefault();
  34. const firstCardDom = this.find('.js-minicard:first');
  35. const lastCardDom = this.find('.js-minicard:last');
  36. const textarea = $(evt.currentTarget).find('textarea');
  37. const position = this.currentData().position;
  38. const title = textarea.val().trim();
  39. let sortIndex;
  40. if (position === 'top') {
  41. sortIndex = Utils.calculateIndex(null, firstCardDom).base;
  42. } else if (position === 'bottom') {
  43. sortIndex = Utils.calculateIndex(lastCardDom, null).base;
  44. }
  45. const formComponent = this.cardFormComponent();
  46. const members = formComponent.members.get();
  47. const labelIds = formComponent.labels.get();
  48. const customFields = formComponent.customFields.get();
  49. const board = this.data().board();
  50. let linkedId = '';
  51. let swimlaneId = '';
  52. let cardType = 'cardType-card';
  53. if (title) {
  54. if (board.isTemplatesBoard()) {
  55. swimlaneId = this.parentComponent()
  56. .parentComponent()
  57. .data()._id; // Always swimlanes view
  58. const swimlane = ReactiveCache.getSwimlane(swimlaneId);
  59. // If this is the card templates swimlane, insert a card template
  60. if (swimlane.isCardTemplatesSwimlane()) cardType = 'template-card';
  61. // If this is the board templates swimlane, insert a board template and a linked card
  62. else if (swimlane.isBoardTemplatesSwimlane()) {
  63. linkedId = Boards.insert({
  64. title,
  65. permission: 'private',
  66. type: 'template-board',
  67. });
  68. Swimlanes.insert({
  69. title: TAPi18n.__('default'),
  70. boardId: linkedId,
  71. });
  72. cardType = 'cardType-linkedBoard';
  73. }
  74. } else if (Utils.boardView() === 'board-view-swimlanes')
  75. swimlaneId = this.parentComponent()
  76. .parentComponent()
  77. .data()._id;
  78. else if (
  79. Utils.boardView() === 'board-view-lists' ||
  80. Utils.boardView() === 'board-view-cal' ||
  81. !Utils.boardView()
  82. )
  83. swimlaneId = board.getDefaultSwimline()._id;
  84. const nextCardNumber = board.getNextCardNumber();
  85. const _id = Cards.insert({
  86. title,
  87. members,
  88. labelIds,
  89. customFields,
  90. listId: this.data()._id,
  91. boardId: board._id,
  92. sort: sortIndex,
  93. swimlaneId,
  94. type: cardType,
  95. cardNumber: nextCardNumber,
  96. linkedId,
  97. });
  98. // if the displayed card count is less than the total cards in the list,
  99. // we need to increment the displayed card count to prevent the spinner
  100. // to appear
  101. const cardCount = this.data()
  102. .cards(this.idOrNull(swimlaneId))
  103. .count();
  104. if (this.cardlimit.get() < cardCount) {
  105. this.cardlimit.set(this.cardlimit.get() + InfiniteScrollIter);
  106. }
  107. // In case the filter is active we need to add the newly inserted card in
  108. // the list of exceptions -- cards that are not filtered. Otherwise the
  109. // card will disappear instantly.
  110. // See https://github.com/wekan/wekan/issues/80
  111. Filter.addException(_id);
  112. // We keep the form opened, empty it, and scroll to it.
  113. textarea.val('').focus();
  114. autosize.update(textarea);
  115. if (position === 'bottom') {
  116. this.scrollToBottom();
  117. }
  118. }
  119. },
  120. cardFormComponent() {
  121. for (const inlinedForm of this.childComponents('inlinedForm')) {
  122. const [addCardForm] = inlinedForm.childComponents('addCardForm');
  123. if (addCardForm) {
  124. return addCardForm;
  125. }
  126. }
  127. return null;
  128. },
  129. scrollToBottom() {
  130. const container = this.firstNode();
  131. $(container).animate({
  132. scrollTop: container.scrollHeight,
  133. });
  134. },
  135. clickOnMiniCard(evt) {
  136. if (MultiSelection.isActive() || evt.shiftKey) {
  137. evt.stopImmediatePropagation();
  138. evt.preventDefault();
  139. const methodName = evt.shiftKey ? 'toggleRange' : 'toggle';
  140. MultiSelection[methodName](this.currentData()._id);
  141. // If the card is already selected, we want to de-select it.
  142. // XXX We should probably modify the minicard href attribute instead of
  143. // overwriting the event in case the card is already selected.
  144. } else if (Utils.isMiniScreen()) {
  145. evt.preventDefault();
  146. Session.set('popupCardId', this.currentData()._id);
  147. this.cardDetailsPopup(evt);
  148. } else if (Session.equals('currentCard', this.currentData()._id)) {
  149. evt.stopImmediatePropagation();
  150. evt.preventDefault();
  151. Utils.goBoardId(Session.get('currentBoard'));
  152. }
  153. },
  154. cardIsSelected() {
  155. return Session.equals('currentCard', this.currentData()._id);
  156. },
  157. toggleMultiSelection(evt) {
  158. evt.stopPropagation();
  159. evt.preventDefault();
  160. MultiSelection.toggle(this.currentData()._id);
  161. },
  162. idOrNull(swimlaneId) {
  163. if (
  164. Utils.boardView() === 'board-view-swimlanes' ||
  165. this.data()
  166. .board()
  167. .isTemplatesBoard()
  168. )
  169. return swimlaneId;
  170. return undefined;
  171. },
  172. cardsWithLimit(swimlaneId) {
  173. const limit = this.cardlimit.get();
  174. const defaultSort = { sort: 1 };
  175. const sortBy = Session.get('sortBy') ? Session.get('sortBy') : defaultSort;
  176. const selector = {
  177. listId: this.currentData()._id,
  178. archived: false,
  179. };
  180. if (swimlaneId) selector.swimlaneId = swimlaneId;
  181. return Cards.find(Filter.mongoSelector(selector), {
  182. // sort: ['sort'],
  183. sort: sortBy,
  184. limit,
  185. });
  186. },
  187. showSpinner(swimlaneId) {
  188. const list = Template.currentData();
  189. return list.cards(swimlaneId).count() > this.cardlimit.get();
  190. },
  191. canSeeAddCard() {
  192. return (
  193. !this.reachedWipLimit() &&
  194. Utils.canModifyCard()
  195. );
  196. },
  197. reachedWipLimit() {
  198. const list = Template.currentData();
  199. return (
  200. !list.getWipLimit('soft') &&
  201. list.getWipLimit('enabled') &&
  202. list.getWipLimit('value') <= list.cards().count()
  203. );
  204. },
  205. cardDetailsPopup(event) {
  206. if (!Popup.isOpen()) {
  207. Popup.open("cardDetails")(event);
  208. }
  209. },
  210. events() {
  211. return [
  212. {
  213. 'click .js-minicard': this.clickOnMiniCard,
  214. 'click .js-toggle-multi-selection': this.toggleMultiSelection,
  215. 'click .open-minicard-composer': this.scrollToBottom,
  216. submit: this.addCard,
  217. },
  218. ];
  219. },
  220. }).register('listBody');
  221. function toggleValueInReactiveArray(reactiveValue, value) {
  222. const array = reactiveValue.get();
  223. const valueIndex = array.indexOf(value);
  224. if (valueIndex === -1) {
  225. array.push(value);
  226. } else {
  227. array.splice(valueIndex, 1);
  228. }
  229. reactiveValue.set(array);
  230. }
  231. BlazeComponent.extendComponent({
  232. onCreated() {
  233. this.labels = new ReactiveVar([]);
  234. this.members = new ReactiveVar([]);
  235. this.customFields = new ReactiveVar([]);
  236. const currentBoardId = Session.get('currentBoard');
  237. arr = [];
  238. _.forEach(
  239. ReactiveCache.getBoard(currentBoardId)
  240. .customFields()
  241. .fetch(),
  242. function (field) {
  243. if (field.automaticallyOnCard || field.alwaysOnCard)
  244. arr.push({ _id: field._id, value: null });
  245. },
  246. );
  247. this.customFields.set(arr);
  248. },
  249. reset() {
  250. this.labels.set([]);
  251. this.members.set([]);
  252. this.customFields.set([]);
  253. },
  254. getLabels() {
  255. const currentBoardId = Session.get('currentBoard');
  256. if (ReactiveCache.getBoard(currentBoardId).labels) {
  257. return ReactiveCache.getBoard(currentBoardId).labels.filter(label => {
  258. return this.labels.get().indexOf(label._id) > -1;
  259. });
  260. }
  261. return false;
  262. },
  263. pressKey(evt) {
  264. // Pressing Enter should submit the card
  265. if (evt.keyCode === 13 && !evt.shiftKey) {
  266. evt.preventDefault();
  267. const $form = $(evt.currentTarget).closest('form');
  268. // XXX For some reason $form.submit() does not work (it's probably a bug
  269. // of blaze-component related to the fact that the submit event is non-
  270. // bubbling). This is why we click on the submit button instead -- which
  271. // work.
  272. $form.find('button[type=submit]').click();
  273. // Pressing Tab should open the form of the next column, and Maj+Tab go
  274. // in the reverse order
  275. } else if (evt.keyCode === 9) {
  276. evt.preventDefault();
  277. const isReverse = evt.shiftKey;
  278. const list = $(`#js-list-${this.data().listId}`);
  279. const listSelector = '.js-list:not(.js-list-composer)';
  280. let nextList = list[isReverse ? 'prev' : 'next'](listSelector).get(0);
  281. // If there is no next list, loop back to the beginning.
  282. if (!nextList) {
  283. nextList = $(listSelector + (isReverse ? ':last' : ':first')).get(0);
  284. }
  285. BlazeComponent.getComponentForElement(nextList).openForm({
  286. position: this.data().position,
  287. });
  288. }
  289. },
  290. events() {
  291. return [
  292. {
  293. keydown: this.pressKey,
  294. 'click .js-link': Popup.open('linkCard'),
  295. 'click .js-search': Popup.open('searchElement'),
  296. 'click .js-card-template': Popup.open('searchElement'),
  297. },
  298. ];
  299. },
  300. onRendered() {
  301. const editor = this;
  302. const $textarea = this.$('textarea');
  303. autosize($textarea);
  304. $textarea.escapeableTextComplete(
  305. [
  306. // User mentions
  307. {
  308. match: /\B@([\w.-]*)$/,
  309. search(term, callback) {
  310. const currentBoard = Utils.getCurrentBoard();
  311. callback(
  312. $.map(currentBoard.activeMembers(), member => {
  313. const user = ReactiveCache.getUser(member.userId);
  314. return user.username.indexOf(term) === 0 ? user : null;
  315. }),
  316. );
  317. },
  318. template(user) {
  319. if (user.profile && user.profile.fullname) {
  320. return (user.username + " (" + user.profile.fullname + ")");
  321. }
  322. return user.username;
  323. },
  324. replace(user) {
  325. toggleValueInReactiveArray(editor.members, user._id);
  326. return '';
  327. },
  328. index: 1,
  329. },
  330. // Labels
  331. {
  332. match: /\B#(\w*)$/,
  333. search(term, callback) {
  334. const currentBoard = Utils.getCurrentBoard();
  335. callback(
  336. $.map(currentBoard.labels, label => {
  337. if (label.name == undefined) {
  338. label.name = "";
  339. }
  340. if (
  341. label.name.indexOf(term) > -1 ||
  342. label.color.indexOf(term) > -1
  343. ) {
  344. return label;
  345. }
  346. return null;
  347. }),
  348. );
  349. },
  350. template(label) {
  351. return Blaze.toHTMLWithData(Template.autocompleteLabelLine, {
  352. hasNoName: !label.name,
  353. colorName: label.color,
  354. labelName: label.name || label.color,
  355. });
  356. },
  357. replace(label) {
  358. toggleValueInReactiveArray(editor.labels, label._id);
  359. return '';
  360. },
  361. index: 1,
  362. },
  363. ],
  364. {
  365. // When the autocomplete menu is shown we want both a press of both `Tab`
  366. // or `Enter` to validation the auto-completion. We also need to stop the
  367. // event propagation to prevent the card from submitting (on `Enter`) or
  368. // going on the next column (on `Tab`).
  369. onKeydown(evt, commands) {
  370. if (evt.keyCode === 9 || evt.keyCode === 13) {
  371. evt.stopPropagation();
  372. return commands.KEY_ENTER;
  373. }
  374. return null;
  375. },
  376. },
  377. );
  378. },
  379. }).register('addCardForm');
  380. BlazeComponent.extendComponent({
  381. onCreated() {
  382. this.selectedBoardId = new ReactiveVar('');
  383. this.selectedSwimlaneId = new ReactiveVar('');
  384. this.selectedListId = new ReactiveVar('');
  385. this.boardId = Session.get('currentBoard');
  386. // In order to get current board info
  387. subManager.subscribe('board', this.boardId, false);
  388. this.board = ReactiveCache.getBoard(this.boardId);
  389. // List where to insert card
  390. this.list = $(Popup._getTopStack().openerElement).closest('.js-list');
  391. this.listId = Blaze.getData(this.list[0])._id;
  392. // Swimlane where to insert card
  393. const swimlane = $(Popup._getTopStack().openerElement).closest(
  394. '.js-swimlane',
  395. );
  396. this.swimlaneId = '';
  397. if (Utils.boardView() === 'board-view-swimlanes')
  398. this.swimlaneId = Blaze.getData(swimlane[0])._id;
  399. else if (Utils.boardView() === 'board-view-lists' || !Utils.boardView)
  400. this.swimlaneId = ReactiveCache.getSwimlane({ boardId: this.boardId })._id;
  401. },
  402. boards() {
  403. const boards = Boards.find(
  404. {
  405. archived: false,
  406. 'members.userId': Meteor.userId(),
  407. _id: { $ne: Session.get('currentBoard') },
  408. type: 'board',
  409. },
  410. {
  411. sort: { sort: 1 /* boards default sorting */ },
  412. },
  413. );
  414. return boards;
  415. },
  416. swimlanes() {
  417. if (!this.selectedBoardId.get()) {
  418. return [];
  419. }
  420. const swimlanes = Swimlanes.find(
  421. {
  422. boardId: this.selectedBoardId.get()
  423. },
  424. {
  425. sort: { sort: 1 },
  426. });
  427. if (swimlanes.count())
  428. this.selectedSwimlaneId.set(swimlanes.fetch()[0]._id);
  429. return swimlanes;
  430. },
  431. lists() {
  432. if (!this.selectedBoardId.get()) {
  433. return [];
  434. }
  435. const lists = Lists.find(
  436. {
  437. boardId: this.selectedBoardId.get()
  438. },
  439. {
  440. sort: { sort: 1 },
  441. });
  442. if (lists.count()) this.selectedListId.set(lists.fetch()[0]._id);
  443. return lists;
  444. },
  445. cards() {
  446. if (!this.board) {
  447. return [];
  448. }
  449. const ownCardsIds = this.board.cards().map(card => card.getRealId());
  450. return Cards.find(
  451. {
  452. boardId: this.selectedBoardId.get(),
  453. swimlaneId: this.selectedSwimlaneId.get(),
  454. listId: this.selectedListId.get(),
  455. archived: false,
  456. linkedId: { $nin: ownCardsIds },
  457. _id: { $nin: ownCardsIds },
  458. type: { $nin: ['template-card'] },
  459. },
  460. {
  461. sort: { sort: 1 },
  462. });
  463. },
  464. getSortIndex() {
  465. const position = this.currentData().position;
  466. let ret;
  467. if (position === 'top') {
  468. const firstCardDom = this.list.find('.js-minicard:first')[0];
  469. ret = Utils.calculateIndex(null, firstCardDom).base;
  470. } else if (position === 'bottom') {
  471. const lastCardDom = this.list.find('.js-minicard:last')[0];
  472. ret = Utils.calculateIndex(lastCardDom, null).base;
  473. }
  474. return ret;
  475. },
  476. events() {
  477. return [
  478. {
  479. 'change .js-select-boards'(evt) {
  480. subManager.subscribe('board', $(evt.currentTarget).val(), false);
  481. this.selectedBoardId.set($(evt.currentTarget).val());
  482. },
  483. 'change .js-select-swimlanes'(evt) {
  484. this.selectedSwimlaneId.set($(evt.currentTarget).val());
  485. },
  486. 'change .js-select-lists'(evt) {
  487. this.selectedListId.set($(evt.currentTarget).val());
  488. },
  489. 'click .js-done'(evt) {
  490. // LINK CARD
  491. evt.stopPropagation();
  492. evt.preventDefault();
  493. const linkedId = $('.js-select-cards option:selected').val();
  494. if (!linkedId) {
  495. Popup.back();
  496. return;
  497. }
  498. const nextCardNumber = this.board.getNextCardNumber();
  499. const sortIndex = this.getSortIndex();
  500. const _id = Cards.insert({
  501. title: $('.js-select-cards option:selected').text(), //dummy
  502. listId: this.listId,
  503. swimlaneId: this.swimlaneId,
  504. boardId: this.boardId,
  505. sort: sortIndex,
  506. type: 'cardType-linkedCard',
  507. linkedId,
  508. cardNumber: nextCardNumber,
  509. });
  510. Filter.addException(_id);
  511. Popup.back();
  512. },
  513. 'click .js-link-board'(evt) {
  514. //LINK BOARD
  515. evt.stopPropagation();
  516. evt.preventDefault();
  517. const impBoardId = $('.js-select-boards option:selected').val();
  518. if (
  519. !impBoardId ||
  520. ReactiveCache.getCard({ linkedId: impBoardId, archived: false })
  521. ) {
  522. Popup.back();
  523. return;
  524. }
  525. const nextCardNumber = this.board.getNextCardNumber();
  526. const sortIndex = this.getSortIndex();
  527. const _id = Cards.insert({
  528. title: $('.js-select-boards option:selected').text(), //dummy
  529. listId: this.listId,
  530. swimlaneId: this.swimlaneId,
  531. boardId: this.boardId,
  532. sort: sortIndex,
  533. type: 'cardType-linkedBoard',
  534. linkedId: impBoardId,
  535. cardNumber: nextCardNumber,
  536. });
  537. Filter.addException(_id);
  538. Popup.back();
  539. },
  540. },
  541. ];
  542. },
  543. }).register('linkCardPopup');
  544. BlazeComponent.extendComponent({
  545. mixins() {
  546. return [];
  547. },
  548. onCreated() {
  549. this.isCardTemplateSearch = $(Popup._getTopStack().openerElement).hasClass(
  550. 'js-card-template',
  551. );
  552. this.isListTemplateSearch = $(Popup._getTopStack().openerElement).hasClass(
  553. 'js-list-template',
  554. );
  555. this.isSwimlaneTemplateSearch = $(
  556. Popup._getTopStack().openerElement,
  557. ).hasClass('js-open-add-swimlane-menu');
  558. this.isBoardTemplateSearch = $(Popup._getTopStack().openerElement).hasClass(
  559. 'js-add-board',
  560. );
  561. this.isTemplateSearch =
  562. this.isCardTemplateSearch ||
  563. this.isListTemplateSearch ||
  564. this.isSwimlaneTemplateSearch ||
  565. this.isBoardTemplateSearch;
  566. this.board = {};
  567. if (this.isTemplateSearch) {
  568. const boardId = (ReactiveCache.getCurrentUser().profile || {}).templatesBoardId;
  569. if (boardId) {
  570. this.board = ReactiveCache.getBoard(boardId);
  571. }
  572. } else {
  573. this.board = Utils.getCurrentBoard();
  574. }
  575. if (!this.board) {
  576. Popup.back();
  577. return;
  578. }
  579. this.boardId = this.board._id;
  580. // Subscribe to this board
  581. subManager.subscribe('board', this.boardId, false);
  582. this.selectedBoardId = new ReactiveVar(this.boardId);
  583. this.list = $(Popup._getTopStack().openerElement).closest('.js-list');
  584. if (!this.isBoardTemplateSearch) {
  585. this.swimlaneId = '';
  586. // Swimlane where to insert card
  587. const swimlane = $(Popup._getTopStack().openerElement).parents(
  588. '.js-swimlane',
  589. );
  590. if (Utils.boardView() === 'board-view-swimlanes')
  591. this.swimlaneId = Blaze.getData(swimlane[0])._id;
  592. else this.swimlaneId = ReactiveCache.getSwimlane({ boardId: this.boardId })._id;
  593. // List where to insert card
  594. this.listId = Blaze.getData(this.list[0])._id;
  595. }
  596. this.term = new ReactiveVar('');
  597. },
  598. boards() {
  599. const boards = Boards.find(
  600. {
  601. archived: false,
  602. 'members.userId': Meteor.userId(),
  603. _id: { $ne: Session.get('currentBoard') },
  604. type: 'board',
  605. },
  606. {
  607. sort: { sort: 1 /* boards default sorting */ },
  608. },
  609. );
  610. return boards;
  611. },
  612. results() {
  613. if (!this.selectedBoardId) {
  614. return [];
  615. }
  616. const board = ReactiveCache.getBoard(this.selectedBoardId.get());
  617. if (!this.isTemplateSearch || this.isCardTemplateSearch) {
  618. return board.searchCards(this.term.get(), false);
  619. } else if (this.isListTemplateSearch) {
  620. return board.searchLists(this.term.get());
  621. } else if (this.isSwimlaneTemplateSearch) {
  622. return board.searchSwimlanes(this.term.get());
  623. } else if (this.isBoardTemplateSearch) {
  624. const boards = board.searchBoards(this.term.get());
  625. boards.forEach(board => {
  626. subManager.subscribe('board', board.linkedId, false);
  627. });
  628. return boards;
  629. } else {
  630. return [];
  631. }
  632. },
  633. getSortIndex() {
  634. const position = this.data().position;
  635. let ret;
  636. if (position === 'top') {
  637. const firstCardDom = this.list.find('.js-minicard:first')[0];
  638. ret = Utils.calculateIndex(null, firstCardDom).base;
  639. } else if (position === 'bottom') {
  640. const lastCardDom = this.list.find('.js-minicard:last')[0];
  641. ret = Utils.calculateIndex(lastCardDom, null).base;
  642. }
  643. return ret;
  644. },
  645. events() {
  646. return [
  647. {
  648. 'change .js-select-boards'(evt) {
  649. subManager.subscribe('board', $(evt.currentTarget).val(), false);
  650. this.selectedBoardId.set($(evt.currentTarget).val());
  651. },
  652. 'submit .js-search-term-form'(evt) {
  653. evt.preventDefault();
  654. this.term.set(evt.target.searchTerm.value);
  655. },
  656. 'click .js-minicard'(evt) {
  657. // 0. Common
  658. const title = $('.js-element-title')
  659. .val()
  660. .trim();
  661. if (!title) return;
  662. const element = Blaze.getData(evt.currentTarget);
  663. element.title = title;
  664. let _id = '';
  665. if (!this.isTemplateSearch || this.isCardTemplateSearch) {
  666. // Card insertion
  667. // 1. Common
  668. element.cardNumber = this.board.getNextCardNumber();
  669. element.sort = this.getSortIndex();
  670. // 1.A From template
  671. if (this.isTemplateSearch) {
  672. element.type = 'cardType-card';
  673. element.linkedId = '';
  674. _id = element.copy(this.boardId, this.swimlaneId, this.listId);
  675. // 1.B Linked card
  676. } else {
  677. _id = element.link(this.boardId, this.swimlaneId, this.listId);
  678. }
  679. Filter.addException(_id);
  680. // List insertion
  681. } else if (this.isListTemplateSearch) {
  682. element.sort = ReactiveCache.getSwimlane(this.swimlaneId)
  683. .lists()
  684. .count();
  685. element.type = 'list';
  686. _id = element.copy(this.boardId, this.swimlaneId);
  687. } else if (this.isSwimlaneTemplateSearch) {
  688. element.sort = ReactiveCache.getBoard(this.boardId)
  689. .swimlanes()
  690. .count();
  691. element.type = 'swimlane';
  692. _id = element.copy(this.boardId);
  693. } else if (this.isBoardTemplateSearch) {
  694. Meteor.call(
  695. 'copyBoard',
  696. element.linkedId,
  697. {
  698. sort: Boards.find({ archived: false }).count(),
  699. type: 'board',
  700. title: element.title,
  701. },
  702. (err, data) => {
  703. _id = data;
  704. subManager.subscribe('board', _id, false);
  705. FlowRouter.go('board', {
  706. id: _id,
  707. slug: getSlug(element.title),
  708. });
  709. },
  710. );
  711. }
  712. Popup.back();
  713. },
  714. },
  715. ];
  716. },
  717. }).register('searchElementPopup');
  718. (class extends Spinner {
  719. onCreated() {
  720. this.cardlimit = this.parentComponent().cardlimit;
  721. this.listId = this.parentComponent().data()._id;
  722. this.swimlaneId = '';
  723. const isSandstorm =
  724. Meteor.settings &&
  725. Meteor.settings.public &&
  726. Meteor.settings.public.sandstorm;
  727. if (isSandstorm) {
  728. const user = ReactiveCache.getCurrentUser();
  729. if (user) {
  730. if (Utils.boardView() === 'board-view-swimlanes') {
  731. this.swimlaneId = this.parentComponent()
  732. .parentComponent()
  733. .parentComponent()
  734. .data()._id;
  735. }
  736. }
  737. } else if (Utils.boardView() === 'board-view-swimlanes') {
  738. this.swimlaneId = this.parentComponent()
  739. .parentComponent()
  740. .parentComponent()
  741. .data()._id;
  742. }
  743. }
  744. onRendered() {
  745. this.spinner = this.find('.sk-spinner-list');
  746. this.container = this.$(this.spinner).parents('.list-body')[0];
  747. $(this.container).on(
  748. `scroll.spinner_${this.swimlaneId}_${this.listId}`,
  749. () => this.updateList(),
  750. );
  751. $(window).on(`resize.spinner_${this.swimlaneId}_${this.listId}`, () =>
  752. this.updateList(),
  753. );
  754. this.updateList();
  755. }
  756. onDestroyed() {
  757. $(this.container).off(`scroll.spinner_${this.swimlaneId}_${this.listId}`);
  758. $(window).off(`resize.spinner_${this.swimlaneId}_${this.listId}`);
  759. }
  760. checkIdleTime() {
  761. return window.requestIdleCallback ||
  762. function (handler) {
  763. const startTime = Date.now();
  764. return setTimeout(function () {
  765. handler({
  766. didTimeout: false,
  767. timeRemaining() {
  768. return Math.max(0, 50.0 - (Date.now() - startTime));
  769. },
  770. });
  771. }, 1);
  772. };
  773. }
  774. updateList() {
  775. // Use fallback when requestIdleCallback is not available on iOS and Safari
  776. // https://www.afasterweb.com/2017/11/20/utilizing-idle-moments/
  777. if (this.spinnerInView()) {
  778. this.cardlimit.set(this.cardlimit.get() + InfiniteScrollIter);
  779. this.checkIdleTime(() => this.updateList());
  780. }
  781. }
  782. spinnerInView() {
  783. // spinner deleted
  784. if (!this.spinner.offsetTop) {
  785. return false;
  786. }
  787. const spinnerViewPosition = this.spinner.offsetTop - this.container.offsetTop + this.spinner.clientHeight;
  788. const parentViewHeight = this.container.clientHeight;
  789. const bottomViewPosition = this.container.scrollTop + parentViewHeight;
  790. return bottomViewPosition > spinnerViewPosition;
  791. }
  792. getSkSpinnerName() {
  793. return "sk-spinner-" + super.getSpinnerName().toLowerCase();
  794. }
  795. }.register('spinnerList'));