sidebar.js 16 KB

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