sidebar.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. const worker = currentBoard.hasWorker(this.userId);
  141. if (commentOnly) {
  142. return TAPi18n.__('comment-only').toLowerCase();
  143. } else if (noComments) {
  144. return TAPi18n.__('no-comments').toLowerCase();
  145. } else if (worker) {
  146. return TAPi18n.__('worker').toLowerCase();
  147. } else {
  148. return TAPi18n.__(type).toLowerCase();
  149. }
  150. } else {
  151. return TAPi18n.__(type).toLowerCase();
  152. }
  153. },
  154. isInvited() {
  155. return Users.findOne(this.userId).isInvitedTo(Session.get('currentBoard'));
  156. },
  157. });
  158. Template.boardMenuPopup.events({
  159. 'click .js-rename-board': Popup.open('boardChangeTitle'),
  160. 'click .js-custom-fields'() {
  161. Sidebar.setView('customFields');
  162. Popup.close();
  163. },
  164. 'click .js-open-archives'() {
  165. Sidebar.setView('archives');
  166. Popup.close();
  167. },
  168. 'click .js-change-board-color': Popup.open('boardChangeColor'),
  169. 'click .js-change-language': Popup.open('changeLanguage'),
  170. 'click .js-archive-board ': Popup.afterConfirm('archiveBoard', function() {
  171. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  172. currentBoard.archive();
  173. // XXX We should have some kind of notification on top of the page to
  174. // confirm that the board was successfully archived.
  175. FlowRouter.go('home');
  176. }),
  177. 'click .js-delete-board': Popup.afterConfirm('deleteBoard', function() {
  178. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  179. Popup.close();
  180. Boards.remove(currentBoard._id);
  181. FlowRouter.go('home');
  182. }),
  183. 'click .js-outgoing-webhooks': Popup.open('outgoingWebhooks'),
  184. 'click .js-import-board': Popup.open('chooseBoardSource'),
  185. 'click .js-subtask-settings': Popup.open('boardSubtaskSettings'),
  186. });
  187. Template.boardMenuPopup.helpers({
  188. exportUrl() {
  189. const params = {
  190. boardId: Session.get('currentBoard'),
  191. };
  192. const queryParams = {
  193. authToken: Accounts._storedLoginToken(),
  194. };
  195. return FlowRouter.path('/api/boards/:boardId/export', params, queryParams);
  196. },
  197. exportFilename() {
  198. const boardId = Session.get('currentBoard');
  199. return `wekan-export-board-${boardId}.json`;
  200. },
  201. });
  202. Template.memberPopup.events({
  203. 'click .js-filter-member'() {
  204. Filter.members.toggle(this.userId);
  205. Popup.close();
  206. },
  207. 'click .js-change-role': Popup.open('changePermissions'),
  208. 'click .js-remove-member': Popup.afterConfirm('removeMember', function() {
  209. const boardId = Session.get('currentBoard');
  210. const memberId = this.userId;
  211. Cards.find({ boardId, members: memberId }).forEach(card => {
  212. card.unassignMember(memberId);
  213. });
  214. Boards.findOne(boardId).removeMember(memberId);
  215. Popup.close();
  216. }),
  217. 'click .js-leave-member': Popup.afterConfirm('leaveBoard', () => {
  218. const boardId = Session.get('currentBoard');
  219. Meteor.call('quitBoard', boardId, () => {
  220. Popup.close();
  221. FlowRouter.go('home');
  222. });
  223. }),
  224. });
  225. Template.removeMemberPopup.helpers({
  226. user() {
  227. return Users.findOne(this.userId);
  228. },
  229. board() {
  230. return Boards.findOne(Session.get('currentBoard'));
  231. },
  232. });
  233. Template.leaveBoardPopup.helpers({
  234. board() {
  235. return Boards.findOne(Session.get('currentBoard'));
  236. },
  237. });
  238. Template.membersWidget.helpers({
  239. isInvited() {
  240. const user = Meteor.user();
  241. return user && user.isInvitedTo(Session.get('currentBoard'));
  242. },
  243. isWorker() {
  244. const user = Meteor.user();
  245. if (user) {
  246. return Meteor.call(Boards.hasWorker(user.memberId));
  247. } else {
  248. return false;
  249. }
  250. },
  251. });
  252. Template.membersWidget.events({
  253. 'click .js-member': Popup.open('member'),
  254. 'click .js-open-board-menu': Popup.open('boardMenu'),
  255. 'click .js-manage-board-members': Popup.open('addMember'),
  256. 'click .js-import': Popup.open('boardImportBoard'),
  257. submit: this.onSubmit,
  258. 'click .js-import-board': Popup.open('chooseBoardSource'),
  259. 'click .js-open-archived-board'() {
  260. Modal.open('archivedBoards');
  261. },
  262. 'click .sandstorm-powerbox-request-identity'() {
  263. window.sandstormRequestIdentity();
  264. },
  265. 'click .js-member-invite-accept'() {
  266. const boardId = Session.get('currentBoard');
  267. Meteor.user().removeInvite(boardId);
  268. },
  269. 'click .js-member-invite-decline'() {
  270. const boardId = Session.get('currentBoard');
  271. Meteor.call('quitBoard', boardId, (err, ret) => {
  272. if (!err && ret) {
  273. Meteor.user().removeInvite(boardId);
  274. FlowRouter.go('home');
  275. }
  276. });
  277. },
  278. });
  279. BlazeComponent.extendComponent({
  280. boardId() {
  281. return Session.get('currentBoard') || Integrations.Const.GLOBAL_WEBHOOK_ID;
  282. },
  283. integrations() {
  284. const boardId = this.boardId();
  285. return Integrations.find({ boardId: `${boardId}` }).fetch();
  286. },
  287. types() {
  288. return Integrations.Const.WEBHOOK_TYPES;
  289. },
  290. integration(cond) {
  291. const boardId = this.boardId();
  292. const condition = { boardId, ...cond };
  293. for (const k in condition) {
  294. if (!condition[k]) delete condition[k];
  295. }
  296. return Integrations.findOne(condition);
  297. },
  298. onCreated() {
  299. this.disabled = new ReactiveVar(false);
  300. },
  301. events() {
  302. return [
  303. {
  304. 'click a.flex'(evt) {
  305. this.disabled.set(!this.disabled.get());
  306. $(evt.target).toggleClass(CKCLS, this.disabled.get());
  307. },
  308. submit(evt) {
  309. evt.preventDefault();
  310. const url = evt.target.url.value;
  311. const boardId = this.boardId();
  312. let id = null;
  313. let integration = null;
  314. const title = evt.target.title.value;
  315. const token = evt.target.token.value;
  316. const type = evt.target.type.value;
  317. const enabled = !this.disabled.get();
  318. let remove = false;
  319. const values = {
  320. url,
  321. type,
  322. token,
  323. title,
  324. enabled,
  325. };
  326. if (evt.target.id) {
  327. id = evt.target.id.value;
  328. integration = this.integration({ _id: id });
  329. remove = !url;
  330. } else if (url) {
  331. integration = this.integration({ url, token });
  332. }
  333. if (remove) {
  334. Integrations.remove(integration._id);
  335. } else if (integration && integration._id) {
  336. Integrations.update(integration._id, {
  337. $set: values,
  338. });
  339. } else if (url) {
  340. Integrations.insert({
  341. ...values,
  342. userId: Meteor.userId(),
  343. enabled: true,
  344. boardId,
  345. activities: ['all'],
  346. });
  347. }
  348. Popup.close();
  349. },
  350. },
  351. ];
  352. },
  353. }).register('outgoingWebhooksPopup');
  354. BlazeComponent.extendComponent({
  355. template() {
  356. return 'chooseBoardSource';
  357. },
  358. }).register('chooseBoardSourcePopup');
  359. Template.labelsWidget.events({
  360. 'click .js-label': Popup.open('editLabel'),
  361. 'click .js-add-label': Popup.open('createLabel'),
  362. });
  363. // Board members can assign people or labels by drag-dropping elements from the
  364. // sidebar to the cards on the board. In order to re-initialize the jquery-ui
  365. // plugin any time a draggable member or label is modified or removed we use a
  366. // autorun function and register a dependency on the both members and labels
  367. // fields of the current board document.
  368. function draggableMembersLabelsWidgets() {
  369. this.autorun(() => {
  370. const currentBoardId = Tracker.nonreactive(() => {
  371. return Session.get('currentBoard');
  372. });
  373. Boards.findOne(currentBoardId, {
  374. fields: {
  375. members: 1,
  376. labels: 1,
  377. },
  378. });
  379. Tracker.afterFlush(() => {
  380. const $draggables = this.$('.js-member,.js-label');
  381. $draggables.draggable({
  382. appendTo: 'body',
  383. helper: 'clone',
  384. revert: 'invalid',
  385. revertDuration: 150,
  386. snap: false,
  387. snapMode: 'both',
  388. start() {
  389. EscapeActions.executeUpTo('popup-back');
  390. },
  391. });
  392. function userIsMember() {
  393. return Meteor.user() && Meteor.user().isBoardMember();
  394. }
  395. this.autorun(() => {
  396. $draggables.draggable('option', 'disabled', !userIsMember());
  397. });
  398. });
  399. });
  400. }
  401. Template.membersWidget.onRendered(draggableMembersLabelsWidgets);
  402. Template.labelsWidget.onRendered(draggableMembersLabelsWidgets);
  403. BlazeComponent.extendComponent({
  404. backgroundColors() {
  405. return Boards.simpleSchema()._schema.color.allowedValues;
  406. },
  407. isSelected() {
  408. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  409. return currentBoard.color === this.currentData().toString();
  410. },
  411. events() {
  412. return [
  413. {
  414. 'click .js-select-background'(evt) {
  415. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  416. const newColor = this.currentData().toString();
  417. currentBoard.setColor(newColor);
  418. evt.preventDefault();
  419. },
  420. },
  421. ];
  422. },
  423. }).register('boardChangeColorPopup');
  424. BlazeComponent.extendComponent({
  425. onCreated() {
  426. this.currentBoard = Boards.findOne(Session.get('currentBoard'));
  427. },
  428. allowsSubtasks() {
  429. return this.currentBoard.allowsSubtasks;
  430. },
  431. isBoardSelected() {
  432. return this.currentBoard.subtasksDefaultBoardId === this.currentData()._id;
  433. },
  434. isNullBoardSelected() {
  435. return (
  436. this.currentBoard.subtasksDefaultBoardId === null ||
  437. this.currentBoard.subtasksDefaultBoardId === undefined
  438. );
  439. },
  440. boards() {
  441. return Boards.find(
  442. {
  443. archived: false,
  444. 'members.userId': Meteor.userId(),
  445. },
  446. {
  447. sort: ['title'],
  448. },
  449. );
  450. },
  451. lists() {
  452. return Lists.find(
  453. {
  454. boardId: this.currentBoard._id,
  455. archived: false,
  456. },
  457. {
  458. sort: ['title'],
  459. },
  460. );
  461. },
  462. hasLists() {
  463. return this.lists().count() > 0;
  464. },
  465. isListSelected() {
  466. return this.currentBoard.subtasksDefaultBoardId === this.currentData()._id;
  467. },
  468. presentParentTask() {
  469. let result = this.currentBoard.presentParentTask;
  470. if (result === null || result === undefined) {
  471. result = 'no-parent';
  472. }
  473. return result;
  474. },
  475. events() {
  476. return [
  477. {
  478. 'click .js-field-has-subtasks'(evt) {
  479. evt.preventDefault();
  480. this.currentBoard.allowsSubtasks = !this.currentBoard.allowsSubtasks;
  481. this.currentBoard.setAllowsSubtasks(this.currentBoard.allowsSubtasks);
  482. $(`.js-field-has-subtasks ${MCB}`).toggleClass(
  483. CKCLS,
  484. this.currentBoard.allowsSubtasks,
  485. );
  486. $('.js-field-has-subtasks').toggleClass(
  487. CKCLS,
  488. this.currentBoard.allowsSubtasks,
  489. );
  490. $('.js-field-deposit-board').prop(
  491. 'disabled',
  492. !this.currentBoard.allowsSubtasks,
  493. );
  494. },
  495. 'change .js-field-deposit-board'(evt) {
  496. let value = evt.target.value;
  497. if (value === 'null') {
  498. value = null;
  499. }
  500. this.currentBoard.setSubtasksDefaultBoardId(value);
  501. evt.preventDefault();
  502. },
  503. 'change .js-field-deposit-list'(evt) {
  504. this.currentBoard.setSubtasksDefaultListId(evt.target.value);
  505. evt.preventDefault();
  506. },
  507. 'click .js-field-show-parent-in-minicard'(evt) {
  508. const value =
  509. evt.target.id ||
  510. $(evt.target).parent()[0].id ||
  511. $(evt.target)
  512. .parent()[0]
  513. .parent()[0].id;
  514. const options = [
  515. 'prefix-with-full-path',
  516. 'prefix-with-parent',
  517. 'subtext-with-full-path',
  518. 'subtext-with-parent',
  519. 'no-parent',
  520. ];
  521. options.forEach(function(element) {
  522. if (element !== value) {
  523. $(`#${element} ${MCB}`).toggleClass(CKCLS, false);
  524. $(`#${element}`).toggleClass(CKCLS, false);
  525. }
  526. });
  527. $(`#${value} ${MCB}`).toggleClass(CKCLS, true);
  528. $(`#${value}`).toggleClass(CKCLS, true);
  529. this.currentBoard.setPresentParentTask(value);
  530. evt.preventDefault();
  531. },
  532. },
  533. ];
  534. },
  535. }).register('boardSubtaskSettingsPopup');
  536. BlazeComponent.extendComponent({
  537. onCreated() {
  538. this.error = new ReactiveVar('');
  539. this.loading = new ReactiveVar(false);
  540. },
  541. onRendered() {
  542. this.find('.js-search-member input').focus();
  543. this.setLoading(false);
  544. },
  545. isBoardMember() {
  546. const userId = this.currentData()._id;
  547. const user = Users.findOne(userId);
  548. return user && user.isBoardMember();
  549. },
  550. isValidEmail(email) {
  551. return SimpleSchema.RegEx.Email.test(email);
  552. },
  553. setError(error) {
  554. this.error.set(error);
  555. },
  556. setLoading(w) {
  557. this.loading.set(w);
  558. },
  559. isLoading() {
  560. return this.loading.get();
  561. },
  562. inviteUser(idNameEmail) {
  563. const boardId = Session.get('currentBoard');
  564. this.setLoading(true);
  565. const self = this;
  566. Meteor.call('inviteUserToBoard', idNameEmail, boardId, (err, ret) => {
  567. self.setLoading(false);
  568. if (err) self.setError(err.error);
  569. else if (ret.email) self.setError('email-sent');
  570. else Popup.close();
  571. });
  572. },
  573. events() {
  574. return [
  575. {
  576. 'keyup input'() {
  577. this.setError('');
  578. },
  579. 'click .js-select-member'() {
  580. const userId = this.currentData()._id;
  581. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  582. if (!currentBoard.hasMember(userId)) {
  583. this.inviteUser(userId);
  584. }
  585. },
  586. 'click .js-email-invite'() {
  587. const idNameEmail = $('.js-search-member input').val();
  588. if (idNameEmail.indexOf('@') < 0 || this.isValidEmail(idNameEmail)) {
  589. this.inviteUser(idNameEmail);
  590. } else this.setError('email-invalid');
  591. },
  592. },
  593. ];
  594. },
  595. }).register('addMemberPopup');
  596. Template.changePermissionsPopup.events({
  597. 'click .js-set-admin, click .js-set-normal, click .js-set-no-comments, click .js-set-comment-only, click .js-set-worker'(
  598. event,
  599. ) {
  600. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  601. const memberId = this.userId;
  602. const isAdmin = $(event.currentTarget).hasClass('js-set-admin');
  603. const isCommentOnly = $(event.currentTarget).hasClass(
  604. 'js-set-comment-only',
  605. );
  606. const isNoComments = $(event.currentTarget).hasClass('js-set-no-comments');
  607. const isWorker = $(event.currentTarget).hasClass('js-set-worker');
  608. currentBoard.setMemberPermission(
  609. memberId,
  610. isAdmin,
  611. isNoComments,
  612. isCommentOnly,
  613. isWorker,
  614. );
  615. Popup.back(1);
  616. },
  617. });
  618. Template.changePermissionsPopup.helpers({
  619. isAdmin() {
  620. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  621. return currentBoard.hasAdmin(this.userId);
  622. },
  623. isNormal() {
  624. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  625. return (
  626. !currentBoard.hasAdmin(this.userId) &&
  627. !currentBoard.hasNoComments(this.userId) &&
  628. !currentBoard.hasCommentOnly(this.userId) &&
  629. !currentBoard.hasWorker(this.userId)
  630. );
  631. },
  632. isNoComments() {
  633. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  634. return (
  635. !currentBoard.hasAdmin(this.userId) &&
  636. currentBoard.hasNoComments(this.userId)
  637. );
  638. },
  639. isCommentOnly() {
  640. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  641. return (
  642. !currentBoard.hasAdmin(this.userId) &&
  643. currentBoard.hasCommentOnly(this.userId)
  644. );
  645. },
  646. isWorker() {
  647. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  648. return (
  649. !currentBoard.hasAdmin(this.userId) && currentBoard.hasWorker(this.userId)
  650. );
  651. },
  652. isLastAdmin() {
  653. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  654. return (
  655. currentBoard.hasAdmin(this.userId) && currentBoard.activeAdmins() === 1
  656. );
  657. },
  658. });