sidebar.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. import { Cookies } from 'meteor/ostrio:cookies';
  2. const cookies = new Cookies();
  3. Sidebar = null;
  4. const defaultView = 'home';
  5. const MCB = '.materialCheckBox';
  6. const CKCLS = 'is-checked';
  7. const viewTitles = {
  8. filter: 'filter-cards',
  9. search: 'search-cards',
  10. multiselection: 'multi-selection',
  11. customFields: 'custom-fields',
  12. archives: 'archives',
  13. };
  14. BlazeComponent.extendComponent({
  15. mixins() {
  16. return [Mixins.InfiniteScrolling, Mixins.PerfectScrollbar];
  17. },
  18. onCreated() {
  19. this._isOpen = new ReactiveVar(false);
  20. this._view = new ReactiveVar(defaultView);
  21. Sidebar = this;
  22. },
  23. onDestroyed() {
  24. Sidebar = null;
  25. },
  26. isOpen() {
  27. return this._isOpen.get();
  28. },
  29. open() {
  30. if (!this._isOpen.get()) {
  31. this._isOpen.set(true);
  32. EscapeActions.executeUpTo('detailsPane');
  33. }
  34. },
  35. hide() {
  36. if (this._isOpen.get()) {
  37. this._isOpen.set(false);
  38. }
  39. },
  40. toggle() {
  41. this._isOpen.set(!this._isOpen.get());
  42. },
  43. calculateNextPeak() {
  44. const sidebarElement = this.find('.js-board-sidebar-content');
  45. if (sidebarElement) {
  46. const altitude = sidebarElement.scrollHeight;
  47. this.callFirstWith(this, 'setNextPeak', altitude);
  48. }
  49. },
  50. reachNextPeak() {
  51. const activitiesComponent = this.childComponents('activities')[0];
  52. activitiesComponent.loadNextPage();
  53. },
  54. isTongueHidden() {
  55. return this.isOpen() && this.getView() !== defaultView;
  56. },
  57. scrollTop() {
  58. this.$('.js-board-sidebar-content').scrollTop(0);
  59. },
  60. getView() {
  61. return this._view.get();
  62. },
  63. setView(view) {
  64. view = _.isString(view) ? view : defaultView;
  65. if (this._view.get() !== view) {
  66. this._view.set(view);
  67. this.scrollTop();
  68. EscapeActions.executeUpTo('detailsPane');
  69. }
  70. this.open();
  71. },
  72. isDefaultView() {
  73. return this.getView() === defaultView;
  74. },
  75. getViewTemplate() {
  76. return `${this.getView()}Sidebar`;
  77. },
  78. getViewTitle() {
  79. return TAPi18n.__(viewTitles[this.getView()]);
  80. },
  81. showTongueTitle() {
  82. if (this.isOpen()) return `${TAPi18n.__('sidebar-close')}`;
  83. else return `${TAPi18n.__('sidebar-open')}`;
  84. },
  85. events() {
  86. return [
  87. {
  88. 'click .js-hide-sidebar': this.hide,
  89. 'click .js-toggle-sidebar': this.toggle,
  90. 'click .js-back-home': this.setView,
  91. 'click .js-toggle-minicard-label-text'() {
  92. currentUser = Meteor.user();
  93. if (currentUser) {
  94. Meteor.call('toggleMinicardLabelText');
  95. } else if (cookies.has('hiddenMinicardLabelText')) {
  96. cookies.remove('hiddenMinicardLabelText');
  97. } else {
  98. cookies.set('hiddenMinicardLabelText', 'true');
  99. }
  100. },
  101. 'click .js-shortcuts'() {
  102. FlowRouter.go('shortcuts');
  103. },
  104. },
  105. ];
  106. },
  107. }).register('sidebar');
  108. Blaze.registerHelper('Sidebar', () => Sidebar);
  109. Template.homeSidebar.helpers({
  110. hiddenMinicardLabelText() {
  111. currentUser = Meteor.user();
  112. if (currentUser) {
  113. return (currentUser.profile || {}).hiddenMinicardLabelText;
  114. } else if (cookies.has('hiddenMinicardLabelText')) {
  115. return true;
  116. } else {
  117. return false;
  118. }
  119. },
  120. });
  121. EscapeActions.register(
  122. 'sidebarView',
  123. () => {
  124. Sidebar.setView(defaultView);
  125. },
  126. () => {
  127. return Sidebar && Sidebar.getView() !== defaultView;
  128. },
  129. );
  130. Template.memberPopup.helpers({
  131. user() {
  132. return Users.findOne(this.userId);
  133. },
  134. memberType() {
  135. const type = Users.findOne(this.userId).isBoardAdmin() ? 'admin' : 'normal';
  136. if (type === 'normal') {
  137. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  138. const commentOnly = currentBoard.hasCommentOnly(this.userId);
  139. const noComments = currentBoard.hasNoComments(this.userId);
  140. if (commentOnly) {
  141. return TAPi18n.__('comment-only').toLowerCase();
  142. } else if (noComments) {
  143. return TAPi18n.__('no-comments').toLowerCase();
  144. } else {
  145. return TAPi18n.__(type).toLowerCase();
  146. }
  147. } else {
  148. return TAPi18n.__(type).toLowerCase();
  149. }
  150. },
  151. isInvited() {
  152. return Users.findOne(this.userId).isInvitedTo(Session.get('currentBoard'));
  153. },
  154. });
  155. Template.boardMenuPopup.events({
  156. 'click .js-rename-board': Popup.open('boardChangeTitle'),
  157. 'click .js-custom-fields'() {
  158. Sidebar.setView('customFields');
  159. Popup.close();
  160. },
  161. 'click .js-open-archives'() {
  162. Sidebar.setView('archives');
  163. Popup.close();
  164. },
  165. 'click .js-change-board-color': Popup.open('boardChangeColor'),
  166. 'click .js-change-language': Popup.open('changeLanguage'),
  167. 'click .js-archive-board ': Popup.afterConfirm('archiveBoard', function() {
  168. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  169. currentBoard.archive();
  170. // XXX We should have some kind of notification on top of the page to
  171. // confirm that the board was successfully archived.
  172. FlowRouter.go('home');
  173. }),
  174. 'click .js-delete-board': Popup.afterConfirm('deleteBoard', function() {
  175. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  176. Popup.close();
  177. Boards.remove(currentBoard._id);
  178. FlowRouter.go('home');
  179. }),
  180. 'click .js-outgoing-webhooks': Popup.open('outgoingWebhooks'),
  181. 'click .js-import-board': Popup.open('chooseBoardSource'),
  182. 'click .js-subtask-settings': Popup.open('boardSubtaskSettings'),
  183. });
  184. Template.boardMenuPopup.helpers({
  185. exportUrl() {
  186. const params = {
  187. boardId: Session.get('currentBoard'),
  188. };
  189. const queryParams = {
  190. authToken: Accounts._storedLoginToken(),
  191. };
  192. return FlowRouter.path('/api/boards/:boardId/export', params, queryParams);
  193. },
  194. exportFilename() {
  195. const boardId = Session.get('currentBoard');
  196. return `wekan-export-board-${boardId}.json`;
  197. },
  198. });
  199. Template.memberPopup.events({
  200. 'click .js-filter-member'() {
  201. Filter.members.toggle(this.userId);
  202. Popup.close();
  203. },
  204. 'click .js-change-role': Popup.open('changePermissions'),
  205. 'click .js-remove-member': Popup.afterConfirm('removeMember', function() {
  206. const boardId = Session.get('currentBoard');
  207. const memberId = this.userId;
  208. Cards.find({ boardId, members: memberId }).forEach(card => {
  209. card.unassignMember(memberId);
  210. });
  211. Boards.findOne(boardId).removeMember(memberId);
  212. Popup.close();
  213. }),
  214. 'click .js-leave-member': Popup.afterConfirm('leaveBoard', () => {
  215. const boardId = Session.get('currentBoard');
  216. Meteor.call('quitBoard', boardId, () => {
  217. Popup.close();
  218. FlowRouter.go('home');
  219. });
  220. }),
  221. });
  222. Template.removeMemberPopup.helpers({
  223. user() {
  224. return Users.findOne(this.userId);
  225. },
  226. board() {
  227. return Boards.findOne(Session.get('currentBoard'));
  228. },
  229. });
  230. Template.leaveBoardPopup.helpers({
  231. board() {
  232. return Boards.findOne(Session.get('currentBoard'));
  233. },
  234. });
  235. Template.membersWidget.helpers({
  236. isInvited() {
  237. const user = Meteor.user();
  238. return user && user.isInvitedTo(Session.get('currentBoard'));
  239. },
  240. });
  241. Template.membersWidget.events({
  242. 'click .js-member': Popup.open('member'),
  243. 'click .js-open-board-menu': Popup.open('boardMenu'),
  244. 'click .js-manage-board-members': Popup.open('addMember'),
  245. 'click .js-import': Popup.open('boardImportBoard'),
  246. submit: this.onSubmit,
  247. 'click .js-import-board': Popup.open('chooseBoardSource'),
  248. 'click .js-open-archived-board'() {
  249. Modal.open('archivedBoards');
  250. },
  251. 'click .sandstorm-powerbox-request-identity'() {
  252. window.sandstormRequestIdentity();
  253. },
  254. 'click .js-member-invite-accept'() {
  255. const boardId = Session.get('currentBoard');
  256. Meteor.user().removeInvite(boardId);
  257. },
  258. 'click .js-member-invite-decline'() {
  259. const boardId = Session.get('currentBoard');
  260. Meteor.call('quitBoard', boardId, (err, ret) => {
  261. if (!err && ret) {
  262. Meteor.user().removeInvite(boardId);
  263. FlowRouter.go('home');
  264. }
  265. });
  266. },
  267. });
  268. BlazeComponent.extendComponent({
  269. boardId() {
  270. return Session.get('currentBoard') || Integrations.Const.GLOBAL_WEBHOOK_ID;
  271. },
  272. integrations() {
  273. const boardId = this.boardId();
  274. return Integrations.find({ boardId: `${boardId}` }).fetch();
  275. },
  276. types() {
  277. return Integrations.Const.WEBHOOK_TYPES;
  278. },
  279. integration(cond) {
  280. const boardId = this.boardId();
  281. const condition = { boardId, ...cond };
  282. for (const k in condition) {
  283. if (!condition[k]) delete condition[k];
  284. }
  285. return Integrations.findOne(condition);
  286. },
  287. onCreated() {
  288. this.disabled = new ReactiveVar(false);
  289. },
  290. events() {
  291. return [
  292. {
  293. 'click a.flex'(evt) {
  294. this.disabled.set(!this.disabled.get());
  295. $(evt.target).toggleClass(CKCLS, this.disabled.get());
  296. },
  297. submit(evt) {
  298. evt.preventDefault();
  299. const url = evt.target.url.value;
  300. const boardId = this.boardId();
  301. let id = null;
  302. let integration = null;
  303. const title = evt.target.title.value;
  304. const token = evt.target.token.value;
  305. const type = evt.target.type.value;
  306. const enabled = !this.disabled.get();
  307. let remove = false;
  308. const values = {
  309. url,
  310. type,
  311. token,
  312. title,
  313. enabled,
  314. };
  315. if (evt.target.id) {
  316. id = evt.target.id.value;
  317. integration = this.integration({ _id: id });
  318. remove = !url;
  319. } else if (url) {
  320. integration = this.integration({ url, token });
  321. }
  322. if (remove) {
  323. Integrations.remove(integration._id);
  324. } else if (integration && integration._id) {
  325. Integrations.update(integration._id, {
  326. $set: values,
  327. });
  328. } else if (url) {
  329. Integrations.insert({
  330. ...values,
  331. userId: Meteor.userId(),
  332. enabled: true,
  333. boardId,
  334. activities: ['all'],
  335. });
  336. }
  337. Popup.close();
  338. },
  339. },
  340. ];
  341. },
  342. }).register('outgoingWebhooksPopup');
  343. BlazeComponent.extendComponent({
  344. template() {
  345. return 'chooseBoardSource';
  346. },
  347. }).register('chooseBoardSourcePopup');
  348. Template.labelsWidget.events({
  349. 'click .js-label': Popup.open('editLabel'),
  350. 'click .js-add-label': Popup.open('createLabel'),
  351. });
  352. // Board members can assign people or labels by drag-dropping elements from the
  353. // sidebar to the cards on the board. In order to re-initialize the jquery-ui
  354. // plugin any time a draggable member or label is modified or removed we use a
  355. // autorun function and register a dependency on the both members and labels
  356. // fields of the current board document.
  357. function draggableMembersLabelsWidgets() {
  358. this.autorun(() => {
  359. const currentBoardId = Tracker.nonreactive(() => {
  360. return Session.get('currentBoard');
  361. });
  362. Boards.findOne(currentBoardId, {
  363. fields: {
  364. members: 1,
  365. labels: 1,
  366. },
  367. });
  368. Tracker.afterFlush(() => {
  369. const $draggables = this.$('.js-member,.js-label');
  370. $draggables.draggable({
  371. appendTo: 'body',
  372. helper: 'clone',
  373. revert: 'invalid',
  374. revertDuration: 150,
  375. snap: false,
  376. snapMode: 'both',
  377. start() {
  378. EscapeActions.executeUpTo('popup-back');
  379. },
  380. });
  381. function userIsMember() {
  382. return Meteor.user() && Meteor.user().isBoardMember();
  383. }
  384. this.autorun(() => {
  385. $draggables.draggable('option', 'disabled', !userIsMember());
  386. });
  387. });
  388. });
  389. }
  390. Template.membersWidget.onRendered(draggableMembersLabelsWidgets);
  391. Template.labelsWidget.onRendered(draggableMembersLabelsWidgets);
  392. BlazeComponent.extendComponent({
  393. backgroundColors() {
  394. return Boards.simpleSchema()._schema.color.allowedValues;
  395. },
  396. isSelected() {
  397. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  398. return currentBoard.color === this.currentData().toString();
  399. },
  400. events() {
  401. return [
  402. {
  403. 'click .js-select-background'(evt) {
  404. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  405. const newColor = this.currentData().toString();
  406. currentBoard.setColor(newColor);
  407. evt.preventDefault();
  408. },
  409. },
  410. ];
  411. },
  412. }).register('boardChangeColorPopup');
  413. BlazeComponent.extendComponent({
  414. onCreated() {
  415. this.currentBoard = Boards.findOne(Session.get('currentBoard'));
  416. },
  417. allowsSubtasks() {
  418. return this.currentBoard.allowsSubtasks;
  419. },
  420. isBoardSelected() {
  421. return this.currentBoard.subtasksDefaultBoardId === this.currentData()._id;
  422. },
  423. isNullBoardSelected() {
  424. return (
  425. this.currentBoard.subtasksDefaultBoardId === null ||
  426. this.currentBoard.subtasksDefaultBoardId === undefined
  427. );
  428. },
  429. boards() {
  430. return Boards.find(
  431. {
  432. archived: false,
  433. 'members.userId': Meteor.userId(),
  434. },
  435. {
  436. sort: ['title'],
  437. },
  438. );
  439. },
  440. lists() {
  441. return Lists.find(
  442. {
  443. boardId: this.currentBoard._id,
  444. archived: false,
  445. },
  446. {
  447. sort: ['title'],
  448. },
  449. );
  450. },
  451. hasLists() {
  452. return this.lists().count() > 0;
  453. },
  454. isListSelected() {
  455. return this.currentBoard.subtasksDefaultBoardId === this.currentData()._id;
  456. },
  457. presentParentTask() {
  458. let result = this.currentBoard.presentParentTask;
  459. if (result === null || result === undefined) {
  460. result = 'no-parent';
  461. }
  462. return result;
  463. },
  464. events() {
  465. return [
  466. {
  467. 'click .js-field-has-subtasks'(evt) {
  468. evt.preventDefault();
  469. this.currentBoard.allowsSubtasks = !this.currentBoard.allowsSubtasks;
  470. this.currentBoard.setAllowsSubtasks(this.currentBoard.allowsSubtasks);
  471. $(`.js-field-has-subtasks ${MCB}`).toggleClass(
  472. CKCLS,
  473. this.currentBoard.allowsSubtasks,
  474. );
  475. $('.js-field-has-subtasks').toggleClass(
  476. CKCLS,
  477. this.currentBoard.allowsSubtasks,
  478. );
  479. $('.js-field-deposit-board').prop(
  480. 'disabled',
  481. !this.currentBoard.allowsSubtasks,
  482. );
  483. },
  484. 'change .js-field-deposit-board'(evt) {
  485. let value = evt.target.value;
  486. if (value === 'null') {
  487. value = null;
  488. }
  489. this.currentBoard.setSubtasksDefaultBoardId(value);
  490. evt.preventDefault();
  491. },
  492. 'change .js-field-deposit-list'(evt) {
  493. this.currentBoard.setSubtasksDefaultListId(evt.target.value);
  494. evt.preventDefault();
  495. },
  496. 'click .js-field-show-parent-in-minicard'(evt) {
  497. const value =
  498. evt.target.id ||
  499. $(evt.target).parent()[0].id ||
  500. $(evt.target)
  501. .parent()[0]
  502. .parent()[0].id;
  503. const options = [
  504. 'prefix-with-full-path',
  505. 'prefix-with-parent',
  506. 'subtext-with-full-path',
  507. 'subtext-with-parent',
  508. 'no-parent',
  509. ];
  510. options.forEach(function(element) {
  511. if (element !== value) {
  512. $(`#${element} ${MCB}`).toggleClass(CKCLS, false);
  513. $(`#${element}`).toggleClass(CKCLS, false);
  514. }
  515. });
  516. $(`#${value} ${MCB}`).toggleClass(CKCLS, true);
  517. $(`#${value}`).toggleClass(CKCLS, true);
  518. this.currentBoard.setPresentParentTask(value);
  519. evt.preventDefault();
  520. },
  521. },
  522. ];
  523. },
  524. }).register('boardSubtaskSettingsPopup');
  525. BlazeComponent.extendComponent({
  526. onCreated() {
  527. this.error = new ReactiveVar('');
  528. this.loading = new ReactiveVar(false);
  529. },
  530. onRendered() {
  531. this.find('.js-search-member input').focus();
  532. this.setLoading(false);
  533. },
  534. isBoardMember() {
  535. const userId = this.currentData()._id;
  536. const user = Users.findOne(userId);
  537. return user && user.isBoardMember();
  538. },
  539. isValidEmail(email) {
  540. return SimpleSchema.RegEx.Email.test(email);
  541. },
  542. setError(error) {
  543. this.error.set(error);
  544. },
  545. setLoading(w) {
  546. this.loading.set(w);
  547. },
  548. isLoading() {
  549. return this.loading.get();
  550. },
  551. inviteUser(idNameEmail) {
  552. const boardId = Session.get('currentBoard');
  553. this.setLoading(true);
  554. const self = this;
  555. Meteor.call('inviteUserToBoard', idNameEmail, boardId, (err, ret) => {
  556. self.setLoading(false);
  557. if (err) self.setError(err.error);
  558. else if (ret.email) self.setError('email-sent');
  559. else Popup.close();
  560. });
  561. },
  562. events() {
  563. return [
  564. {
  565. 'keyup input'() {
  566. this.setError('');
  567. },
  568. 'click .js-select-member'() {
  569. const userId = this.currentData()._id;
  570. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  571. if (!currentBoard.hasMember(userId)) {
  572. this.inviteUser(userId);
  573. }
  574. },
  575. 'click .js-email-invite'() {
  576. const idNameEmail = $('.js-search-member input').val();
  577. if (idNameEmail.indexOf('@') < 0 || this.isValidEmail(idNameEmail)) {
  578. this.inviteUser(idNameEmail);
  579. } else this.setError('email-invalid');
  580. },
  581. },
  582. ];
  583. },
  584. }).register('addMemberPopup');
  585. Template.changePermissionsPopup.events({
  586. 'click .js-set-admin, click .js-set-normal, click .js-set-no-comments, click .js-set-comment-only'(
  587. event,
  588. ) {
  589. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  590. const memberId = this.userId;
  591. const isAdmin = $(event.currentTarget).hasClass('js-set-admin');
  592. const isCommentOnly = $(event.currentTarget).hasClass(
  593. 'js-set-comment-only',
  594. );
  595. const isNoComments = $(event.currentTarget).hasClass('js-set-no-comments');
  596. currentBoard.setMemberPermission(
  597. memberId,
  598. isAdmin,
  599. isNoComments,
  600. isCommentOnly,
  601. );
  602. Popup.back(1);
  603. },
  604. });
  605. Template.changePermissionsPopup.helpers({
  606. isAdmin() {
  607. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  608. return currentBoard.hasAdmin(this.userId);
  609. },
  610. isNormal() {
  611. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  612. return (
  613. !currentBoard.hasAdmin(this.userId) &&
  614. !currentBoard.hasNoComments(this.userId) &&
  615. !currentBoard.hasCommentOnly(this.userId)
  616. );
  617. },
  618. isNoComments() {
  619. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  620. return (
  621. !currentBoard.hasAdmin(this.userId) &&
  622. currentBoard.hasNoComments(this.userId)
  623. );
  624. },
  625. isCommentOnly() {
  626. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  627. return (
  628. !currentBoard.hasAdmin(this.userId) &&
  629. currentBoard.hasCommentOnly(this.userId)
  630. );
  631. },
  632. isLastAdmin() {
  633. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  634. return (
  635. currentBoard.hasAdmin(this.userId) && currentBoard.activeAdmins() === 1
  636. );
  637. },
  638. });