listBody.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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. Meteor.user() &&
  195. Meteor.user().isBoardMember() &&
  196. !Meteor.user().isCommentOnly() &&
  197. !Meteor.user().isWorker()
  198. );
  199. },
  200. reachedWipLimit() {
  201. const list = Template.currentData();
  202. return (
  203. !list.getWipLimit('soft') &&
  204. list.getWipLimit('enabled') &&
  205. list.getWipLimit('value') <= list.cards().count()
  206. );
  207. },
  208. cardDetailsPopup(event) {
  209. if (!Popup.isOpen()) {
  210. Popup.open("cardDetails")(event);
  211. }
  212. },
  213. events() {
  214. return [
  215. {
  216. 'click .js-minicard': this.clickOnMiniCard,
  217. 'click .js-toggle-multi-selection': this.toggleMultiSelection,
  218. 'click .open-minicard-composer': this.scrollToBottom,
  219. submit: this.addCard,
  220. },
  221. ];
  222. },
  223. }).register('listBody');
  224. function toggleValueInReactiveArray(reactiveValue, value) {
  225. const array = reactiveValue.get();
  226. const valueIndex = array.indexOf(value);
  227. if (valueIndex === -1) {
  228. array.push(value);
  229. } else {
  230. array.splice(valueIndex, 1);
  231. }
  232. reactiveValue.set(array);
  233. }
  234. BlazeComponent.extendComponent({
  235. onCreated() {
  236. this.labels = new ReactiveVar([]);
  237. this.members = new ReactiveVar([]);
  238. this.customFields = new ReactiveVar([]);
  239. const currentBoardId = Session.get('currentBoard');
  240. arr = [];
  241. _.forEach(
  242. ReactiveCache.getBoard(currentBoardId)
  243. .customFields()
  244. .fetch(),
  245. function (field) {
  246. if (field.automaticallyOnCard || field.alwaysOnCard)
  247. arr.push({ _id: field._id, value: null });
  248. },
  249. );
  250. this.customFields.set(arr);
  251. },
  252. reset() {
  253. this.labels.set([]);
  254. this.members.set([]);
  255. this.customFields.set([]);
  256. },
  257. getLabels() {
  258. const currentBoardId = Session.get('currentBoard');
  259. if (ReactiveCache.getBoard(currentBoardId).labels) {
  260. return ReactiveCache.getBoard(currentBoardId).labels.filter(label => {
  261. return this.labels.get().indexOf(label._id) > -1;
  262. });
  263. }
  264. return false;
  265. },
  266. pressKey(evt) {
  267. // Pressing Enter should submit the card
  268. if (evt.keyCode === 13 && !evt.shiftKey) {
  269. evt.preventDefault();
  270. const $form = $(evt.currentTarget).closest('form');
  271. // XXX For some reason $form.submit() does not work (it's probably a bug
  272. // of blaze-component related to the fact that the submit event is non-
  273. // bubbling). This is why we click on the submit button instead -- which
  274. // work.
  275. $form.find('button[type=submit]').click();
  276. // Pressing Tab should open the form of the next column, and Maj+Tab go
  277. // in the reverse order
  278. } else if (evt.keyCode === 9) {
  279. evt.preventDefault();
  280. const isReverse = evt.shiftKey;
  281. const list = $(`#js-list-${this.data().listId}`);
  282. const listSelector = '.js-list:not(.js-list-composer)';
  283. let nextList = list[isReverse ? 'prev' : 'next'](listSelector).get(0);
  284. // If there is no next list, loop back to the beginning.
  285. if (!nextList) {
  286. nextList = $(listSelector + (isReverse ? ':last' : ':first')).get(0);
  287. }
  288. BlazeComponent.getComponentForElement(nextList).openForm({
  289. position: this.data().position,
  290. });
  291. }
  292. },
  293. events() {
  294. return [
  295. {
  296. keydown: this.pressKey,
  297. 'click .js-link': Popup.open('linkCard'),
  298. 'click .js-search': Popup.open('searchElement'),
  299. 'click .js-card-template': Popup.open('searchElement'),
  300. },
  301. ];
  302. },
  303. onRendered() {
  304. const editor = this;
  305. const $textarea = this.$('textarea');
  306. autosize($textarea);
  307. $textarea.escapeableTextComplete(
  308. [
  309. // User mentions
  310. {
  311. match: /\B@([\w.-]*)$/,
  312. search(term, callback) {
  313. const currentBoard = Utils.getCurrentBoard();
  314. callback(
  315. $.map(currentBoard.activeMembers(), member => {
  316. const user = ReactiveCache.getUser(member.userId);
  317. return user.username.indexOf(term) === 0 ? user : null;
  318. }),
  319. );
  320. },
  321. template(user) {
  322. if (user.profile && user.profile.fullname) {
  323. return (user.username + " (" + user.profile.fullname + ")");
  324. }
  325. return user.username;
  326. },
  327. replace(user) {
  328. toggleValueInReactiveArray(editor.members, user._id);
  329. return '';
  330. },
  331. index: 1,
  332. },
  333. // Labels
  334. {
  335. match: /\B#(\w*)$/,
  336. search(term, callback) {
  337. const currentBoard = Utils.getCurrentBoard();
  338. callback(
  339. $.map(currentBoard.labels, label => {
  340. if (label.name == undefined) {
  341. label.name = "";
  342. }
  343. if (
  344. label.name.indexOf(term) > -1 ||
  345. label.color.indexOf(term) > -1
  346. ) {
  347. return label;
  348. }
  349. return null;
  350. }),
  351. );
  352. },
  353. template(label) {
  354. return Blaze.toHTMLWithData(Template.autocompleteLabelLine, {
  355. hasNoName: !label.name,
  356. colorName: label.color,
  357. labelName: label.name || label.color,
  358. });
  359. },
  360. replace(label) {
  361. toggleValueInReactiveArray(editor.labels, label._id);
  362. return '';
  363. },
  364. index: 1,
  365. },
  366. ],
  367. {
  368. // When the autocomplete menu is shown we want both a press of both `Tab`
  369. // or `Enter` to validation the auto-completion. We also need to stop the
  370. // event propagation to prevent the card from submitting (on `Enter`) or
  371. // going on the next column (on `Tab`).
  372. onKeydown(evt, commands) {
  373. if (evt.keyCode === 9 || evt.keyCode === 13) {
  374. evt.stopPropagation();
  375. return commands.KEY_ENTER;
  376. }
  377. return null;
  378. },
  379. },
  380. );
  381. },
  382. }).register('addCardForm');
  383. BlazeComponent.extendComponent({
  384. onCreated() {
  385. this.selectedBoardId = new ReactiveVar('');
  386. this.selectedSwimlaneId = new ReactiveVar('');
  387. this.selectedListId = new ReactiveVar('');
  388. this.boardId = Session.get('currentBoard');
  389. // In order to get current board info
  390. subManager.subscribe('board', this.boardId, false);
  391. this.board = ReactiveCache.getBoard(this.boardId);
  392. // List where to insert card
  393. this.list = $(Popup._getTopStack().openerElement).closest('.js-list');
  394. this.listId = Blaze.getData(this.list[0])._id;
  395. // Swimlane where to insert card
  396. const swimlane = $(Popup._getTopStack().openerElement).closest(
  397. '.js-swimlane',
  398. );
  399. this.swimlaneId = '';
  400. if (Utils.boardView() === 'board-view-swimlanes')
  401. this.swimlaneId = Blaze.getData(swimlane[0])._id;
  402. else if (Utils.boardView() === 'board-view-lists' || !Utils.boardView)
  403. this.swimlaneId = Swimlanes.findOne({ boardId: this.boardId })._id;
  404. },
  405. boards() {
  406. const boards = Boards.find(
  407. {
  408. archived: false,
  409. 'members.userId': Meteor.userId(),
  410. _id: { $ne: Session.get('currentBoard') },
  411. type: 'board',
  412. },
  413. {
  414. sort: { sort: 1 /* boards default sorting */ },
  415. },
  416. );
  417. return boards;
  418. },
  419. swimlanes() {
  420. if (!this.selectedBoardId.get()) {
  421. return [];
  422. }
  423. const swimlanes = Swimlanes.find(
  424. {
  425. boardId: this.selectedBoardId.get()
  426. },
  427. {
  428. sort: { sort: 1 },
  429. });
  430. if (swimlanes.count())
  431. this.selectedSwimlaneId.set(swimlanes.fetch()[0]._id);
  432. return swimlanes;
  433. },
  434. lists() {
  435. if (!this.selectedBoardId.get()) {
  436. return [];
  437. }
  438. const lists = Lists.find(
  439. {
  440. boardId: this.selectedBoardId.get()
  441. },
  442. {
  443. sort: { sort: 1 },
  444. });
  445. if (lists.count()) this.selectedListId.set(lists.fetch()[0]._id);
  446. return lists;
  447. },
  448. cards() {
  449. if (!this.board) {
  450. return [];
  451. }
  452. const ownCardsIds = this.board.cards().map(card => card.getRealId());
  453. return Cards.find(
  454. {
  455. boardId: this.selectedBoardId.get(),
  456. swimlaneId: this.selectedSwimlaneId.get(),
  457. listId: this.selectedListId.get(),
  458. archived: false,
  459. linkedId: { $nin: ownCardsIds },
  460. _id: { $nin: ownCardsIds },
  461. type: { $nin: ['template-card'] },
  462. },
  463. {
  464. sort: { sort: 1 },
  465. });
  466. },
  467. getSortIndex() {
  468. const position = this.currentData().position;
  469. let ret;
  470. if (position === 'top') {
  471. const firstCardDom = this.list.find('.js-minicard:first')[0];
  472. ret = Utils.calculateIndex(null, firstCardDom).base;
  473. } else if (position === 'bottom') {
  474. const lastCardDom = this.list.find('.js-minicard:last')[0];
  475. ret = Utils.calculateIndex(lastCardDom, null).base;
  476. }
  477. return ret;
  478. },
  479. events() {
  480. return [
  481. {
  482. 'change .js-select-boards'(evt) {
  483. subManager.subscribe('board', $(evt.currentTarget).val(), false);
  484. this.selectedBoardId.set($(evt.currentTarget).val());
  485. },
  486. 'change .js-select-swimlanes'(evt) {
  487. this.selectedSwimlaneId.set($(evt.currentTarget).val());
  488. },
  489. 'change .js-select-lists'(evt) {
  490. this.selectedListId.set($(evt.currentTarget).val());
  491. },
  492. 'click .js-done'(evt) {
  493. // LINK CARD
  494. evt.stopPropagation();
  495. evt.preventDefault();
  496. const linkedId = $('.js-select-cards option:selected').val();
  497. if (!linkedId) {
  498. Popup.back();
  499. return;
  500. }
  501. const nextCardNumber = this.board.getNextCardNumber();
  502. const sortIndex = this.getSortIndex();
  503. const _id = Cards.insert({
  504. title: $('.js-select-cards option:selected').text(), //dummy
  505. listId: this.listId,
  506. swimlaneId: this.swimlaneId,
  507. boardId: this.boardId,
  508. sort: sortIndex,
  509. type: 'cardType-linkedCard',
  510. linkedId,
  511. cardNumber: nextCardNumber,
  512. });
  513. Filter.addException(_id);
  514. Popup.back();
  515. },
  516. 'click .js-link-board'(evt) {
  517. //LINK BOARD
  518. evt.stopPropagation();
  519. evt.preventDefault();
  520. const impBoardId = $('.js-select-boards option:selected').val();
  521. if (
  522. !impBoardId ||
  523. Cards.findOne({ linkedId: impBoardId, archived: false })
  524. ) {
  525. Popup.back();
  526. return;
  527. }
  528. const nextCardNumber = this.board.getNextCardNumber();
  529. const sortIndex = this.getSortIndex();
  530. const _id = Cards.insert({
  531. title: $('.js-select-boards option:selected').text(), //dummy
  532. listId: this.listId,
  533. swimlaneId: this.swimlaneId,
  534. boardId: this.boardId,
  535. sort: sortIndex,
  536. type: 'cardType-linkedBoard',
  537. linkedId: impBoardId,
  538. cardNumber: nextCardNumber,
  539. });
  540. Filter.addException(_id);
  541. Popup.back();
  542. },
  543. },
  544. ];
  545. },
  546. }).register('linkCardPopup');
  547. BlazeComponent.extendComponent({
  548. mixins() {
  549. return [];
  550. },
  551. onCreated() {
  552. this.isCardTemplateSearch = $(Popup._getTopStack().openerElement).hasClass(
  553. 'js-card-template',
  554. );
  555. this.isListTemplateSearch = $(Popup._getTopStack().openerElement).hasClass(
  556. 'js-list-template',
  557. );
  558. this.isSwimlaneTemplateSearch = $(
  559. Popup._getTopStack().openerElement,
  560. ).hasClass('js-open-add-swimlane-menu');
  561. this.isBoardTemplateSearch = $(Popup._getTopStack().openerElement).hasClass(
  562. 'js-add-board',
  563. );
  564. this.isTemplateSearch =
  565. this.isCardTemplateSearch ||
  566. this.isListTemplateSearch ||
  567. this.isSwimlaneTemplateSearch ||
  568. this.isBoardTemplateSearch;
  569. this.board = {};
  570. if (this.isTemplateSearch) {
  571. const boardId = (Meteor.user().profile || {}).templatesBoardId;
  572. if (boardId) {
  573. this.board = ReactiveCache.getBoard(boardId);
  574. }
  575. } else {
  576. this.board = Utils.getCurrentBoard();
  577. }
  578. if (!this.board) {
  579. Popup.back();
  580. return;
  581. }
  582. this.boardId = this.board._id;
  583. // Subscribe to this board
  584. subManager.subscribe('board', this.boardId, false);
  585. this.selectedBoardId = new ReactiveVar(this.boardId);
  586. this.list = $(Popup._getTopStack().openerElement).closest('.js-list');
  587. if (!this.isBoardTemplateSearch) {
  588. this.swimlaneId = '';
  589. // Swimlane where to insert card
  590. const swimlane = $(Popup._getTopStack().openerElement).parents(
  591. '.js-swimlane',
  592. );
  593. if (Utils.boardView() === 'board-view-swimlanes')
  594. this.swimlaneId = Blaze.getData(swimlane[0])._id;
  595. else this.swimlaneId = Swimlanes.findOne({ boardId: this.boardId })._id;
  596. // List where to insert card
  597. this.listId = Blaze.getData(this.list[0])._id;
  598. }
  599. this.term = new ReactiveVar('');
  600. },
  601. boards() {
  602. const boards = Boards.find(
  603. {
  604. archived: false,
  605. 'members.userId': Meteor.userId(),
  606. _id: { $ne: Session.get('currentBoard') },
  607. type: 'board',
  608. },
  609. {
  610. sort: { sort: 1 /* boards default sorting */ },
  611. },
  612. );
  613. return boards;
  614. },
  615. results() {
  616. if (!this.selectedBoardId) {
  617. return [];
  618. }
  619. const board = ReactiveCache.getBoard(this.selectedBoardId.get());
  620. if (!this.isTemplateSearch || this.isCardTemplateSearch) {
  621. return board.searchCards(this.term.get(), false);
  622. } else if (this.isListTemplateSearch) {
  623. return board.searchLists(this.term.get());
  624. } else if (this.isSwimlaneTemplateSearch) {
  625. return board.searchSwimlanes(this.term.get());
  626. } else if (this.isBoardTemplateSearch) {
  627. const boards = board.searchBoards(this.term.get());
  628. boards.forEach(board => {
  629. subManager.subscribe('board', board.linkedId, false);
  630. });
  631. return boards;
  632. } else {
  633. return [];
  634. }
  635. },
  636. getSortIndex() {
  637. const position = this.data().position;
  638. let ret;
  639. if (position === 'top') {
  640. const firstCardDom = this.list.find('.js-minicard:first')[0];
  641. ret = Utils.calculateIndex(null, firstCardDom).base;
  642. } else if (position === 'bottom') {
  643. const lastCardDom = this.list.find('.js-minicard:last')[0];
  644. ret = Utils.calculateIndex(lastCardDom, null).base;
  645. }
  646. return ret;
  647. },
  648. events() {
  649. return [
  650. {
  651. 'change .js-select-boards'(evt) {
  652. subManager.subscribe('board', $(evt.currentTarget).val(), false);
  653. this.selectedBoardId.set($(evt.currentTarget).val());
  654. },
  655. 'submit .js-search-term-form'(evt) {
  656. evt.preventDefault();
  657. this.term.set(evt.target.searchTerm.value);
  658. },
  659. 'click .js-minicard'(evt) {
  660. // 0. Common
  661. const title = $('.js-element-title')
  662. .val()
  663. .trim();
  664. if (!title) return;
  665. const element = Blaze.getData(evt.currentTarget);
  666. element.title = title;
  667. let _id = '';
  668. if (!this.isTemplateSearch || this.isCardTemplateSearch) {
  669. // Card insertion
  670. // 1. Common
  671. element.cardNumber = this.board.getNextCardNumber();
  672. element.sort = this.getSortIndex();
  673. // 1.A From template
  674. if (this.isTemplateSearch) {
  675. element.type = 'cardType-card';
  676. element.linkedId = '';
  677. _id = element.copy(this.boardId, this.swimlaneId, this.listId);
  678. // 1.B Linked card
  679. } else {
  680. _id = element.link(this.boardId, this.swimlaneId, this.listId);
  681. }
  682. Filter.addException(_id);
  683. // List insertion
  684. } else if (this.isListTemplateSearch) {
  685. element.sort = ReactiveCache.getSwimlane(this.swimlaneId)
  686. .lists()
  687. .count();
  688. element.type = 'list';
  689. _id = element.copy(this.boardId, this.swimlaneId);
  690. } else if (this.isSwimlaneTemplateSearch) {
  691. element.sort = ReactiveCache.getBoard(this.boardId)
  692. .swimlanes()
  693. .count();
  694. element.type = 'swimlane';
  695. _id = element.copy(this.boardId);
  696. } else if (this.isBoardTemplateSearch) {
  697. Meteor.call(
  698. 'copyBoard',
  699. element.linkedId,
  700. {
  701. sort: Boards.find({ archived: false }).count(),
  702. type: 'board',
  703. title: element.title,
  704. },
  705. (err, data) => {
  706. _id = data;
  707. subManager.subscribe('board', _id, false);
  708. FlowRouter.go('board', {
  709. id: _id,
  710. slug: getSlug(element.title),
  711. });
  712. },
  713. );
  714. }
  715. Popup.back();
  716. },
  717. },
  718. ];
  719. },
  720. }).register('searchElementPopup');
  721. (class extends Spinner {
  722. onCreated() {
  723. this.cardlimit = this.parentComponent().cardlimit;
  724. this.listId = this.parentComponent().data()._id;
  725. this.swimlaneId = '';
  726. const isSandstorm =
  727. Meteor.settings &&
  728. Meteor.settings.public &&
  729. Meteor.settings.public.sandstorm;
  730. if (isSandstorm) {
  731. const user = Meteor.user();
  732. if (user) {
  733. if (Utils.boardView() === 'board-view-swimlanes') {
  734. this.swimlaneId = this.parentComponent()
  735. .parentComponent()
  736. .parentComponent()
  737. .data()._id;
  738. }
  739. }
  740. } else if (Utils.boardView() === 'board-view-swimlanes') {
  741. this.swimlaneId = this.parentComponent()
  742. .parentComponent()
  743. .parentComponent()
  744. .data()._id;
  745. }
  746. }
  747. onRendered() {
  748. this.spinner = this.find('.sk-spinner-list');
  749. this.container = this.$(this.spinner).parents('.list-body')[0];
  750. $(this.container).on(
  751. `scroll.spinner_${this.swimlaneId}_${this.listId}`,
  752. () => this.updateList(),
  753. );
  754. $(window).on(`resize.spinner_${this.swimlaneId}_${this.listId}`, () =>
  755. this.updateList(),
  756. );
  757. this.updateList();
  758. }
  759. onDestroyed() {
  760. $(this.container).off(`scroll.spinner_${this.swimlaneId}_${this.listId}`);
  761. $(window).off(`resize.spinner_${this.swimlaneId}_${this.listId}`);
  762. }
  763. checkIdleTime() {
  764. return window.requestIdleCallback ||
  765. function (handler) {
  766. const startTime = Date.now();
  767. return setTimeout(function () {
  768. handler({
  769. didTimeout: false,
  770. timeRemaining() {
  771. return Math.max(0, 50.0 - (Date.now() - startTime));
  772. },
  773. });
  774. }, 1);
  775. };
  776. }
  777. updateList() {
  778. // Use fallback when requestIdleCallback is not available on iOS and Safari
  779. // https://www.afasterweb.com/2017/11/20/utilizing-idle-moments/
  780. if (this.spinnerInView()) {
  781. this.cardlimit.set(this.cardlimit.get() + InfiniteScrollIter);
  782. this.checkIdleTime(() => this.updateList());
  783. }
  784. }
  785. spinnerInView() {
  786. // spinner deleted
  787. if (!this.spinner.offsetTop) {
  788. return false;
  789. }
  790. const spinnerViewPosition = this.spinner.offsetTop - this.container.offsetTop + this.spinner.clientHeight;
  791. const parentViewHeight = this.container.clientHeight;
  792. const bottomViewPosition = this.container.scrollTop + parentViewHeight;
  793. return bottomViewPosition > spinnerViewPosition;
  794. }
  795. getSkSpinnerName() {
  796. return "sk-spinner-" + super.getSpinnerName().toLowerCase();
  797. }
  798. }.register('spinnerList'));