sidebar.js 17 KB

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