sidebar.js 18 KB

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