2
0

sidebar.js 19 KB

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