sidebar.js 17 KB

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