sidebar.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. const initOpen = Utils.isMiniScreen() ? false : (!Session.get('currentCard'));
  16. this._isOpen = new ReactiveVar(initOpen);
  17. this._view = new ReactiveVar(defaultView);
  18. Sidebar = this;
  19. },
  20. onDestroyed() {
  21. Sidebar = null;
  22. },
  23. isOpen() {
  24. return this._isOpen.get();
  25. },
  26. open() {
  27. if (!this._isOpen.get()) {
  28. this._isOpen.set(true);
  29. EscapeActions.executeUpTo('detailsPane');
  30. }
  31. },
  32. hide() {
  33. if (this._isOpen.get()) {
  34. this._isOpen.set(false);
  35. }
  36. },
  37. toggle() {
  38. this._isOpen.set(!this._isOpen.get());
  39. },
  40. calculateNextPeak() {
  41. const altitude = this.find('.js-board-sidebar-content').scrollHeight;
  42. this.callFirstWith(this, 'setNextPeak', altitude);
  43. },
  44. reachNextPeak() {
  45. const activitiesComponent = this.childComponents('activities')[0];
  46. activitiesComponent.loadNextPage();
  47. },
  48. isTongueHidden() {
  49. return this.isOpen() && this.getView() !== defaultView;
  50. },
  51. scrollTop() {
  52. this.$('.js-board-sidebar-content').scrollTop(0);
  53. },
  54. getView() {
  55. return this._view.get();
  56. },
  57. setView(view) {
  58. view = _.isString(view) ? view : defaultView;
  59. if (this._view.get() !== view) {
  60. this._view.set(view);
  61. this.scrollTop();
  62. EscapeActions.executeUpTo('detailsPane');
  63. }
  64. this.open();
  65. },
  66. isDefaultView() {
  67. return this.getView() === defaultView;
  68. },
  69. getViewTemplate() {
  70. return `${this.getView()}Sidebar`;
  71. },
  72. getViewTitle() {
  73. return TAPi18n.__(viewTitles[this.getView()]);
  74. },
  75. showTongueTitle() {
  76. if (this.isOpen())
  77. return `${TAPi18n.__('sidebar-close')}`;
  78. else
  79. return `${TAPi18n.__('sidebar-open')}`;
  80. },
  81. events() {
  82. return [{
  83. 'click .js-hide-sidebar': this.hide,
  84. 'click .js-toggle-sidebar': this.toggle,
  85. 'click .js-back-home': this.setView,
  86. 'click .js-shortcuts'() {
  87. FlowRouter.go('shortcuts');
  88. },
  89. }];
  90. },
  91. }).register('sidebar');
  92. Blaze.registerHelper('Sidebar', () => Sidebar);
  93. EscapeActions.register('sidebarView',
  94. () => { Sidebar.setView(defaultView); },
  95. () => { return Sidebar && Sidebar.getView() !== defaultView; }
  96. );
  97. Template.memberPopup.helpers({
  98. user() {
  99. return Users.findOne(this.userId);
  100. },
  101. memberType() {
  102. const type = Users.findOne(this.userId).isBoardAdmin() ? 'admin' : 'normal';
  103. if(type === 'normal'){
  104. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  105. const commentOnly = currentBoard.hasCommentOnly(this.userId);
  106. if(commentOnly){
  107. return TAPi18n.__('comment-only').toLowerCase();
  108. } else {
  109. return TAPi18n.__(type).toLowerCase();
  110. }
  111. } else {
  112. return TAPi18n.__(type).toLowerCase();
  113. }
  114. },
  115. isInvited() {
  116. return Users.findOne(this.userId).isInvitedTo(Session.get('currentBoard'));
  117. },
  118. });
  119. Template.memberPopup.events({
  120. 'click .js-filter-member'() {
  121. Filter.members.toggle(this.userId);
  122. Popup.close();
  123. },
  124. 'click .js-change-role': Popup.open('changePermissions'),
  125. 'click .js-remove-member': Popup.afterConfirm('removeMember', function() {
  126. const boardId = Session.get('currentBoard');
  127. const memberId = this.userId;
  128. Cards.find({ boardId, members: memberId }).forEach((card) => {
  129. card.unassignMember(memberId);
  130. });
  131. Boards.findOne(boardId).removeMember(memberId);
  132. Popup.close();
  133. }),
  134. 'click .js-leave-member': Popup.afterConfirm('leaveBoard', () => {
  135. const boardId = Session.get('currentBoard');
  136. Meteor.call('quitBoard', boardId, () => {
  137. Popup.close();
  138. FlowRouter.go('home');
  139. });
  140. }),
  141. });
  142. Template.removeMemberPopup.helpers({
  143. user() {
  144. return Users.findOne(this.userId);
  145. },
  146. board() {
  147. return Boards.findOne(Session.get('currentBoard'));
  148. },
  149. });
  150. Template.leaveBoardPopup.helpers({
  151. board() {
  152. return Boards.findOne(Session.get('currentBoard'));
  153. },
  154. });
  155. Template.membersWidget.helpers({
  156. isInvited() {
  157. const user = Meteor.user();
  158. return user && user.isInvitedTo(Session.get('currentBoard'));
  159. },
  160. });
  161. Template.membersWidget.events({
  162. 'click .js-member': Popup.open('member'),
  163. 'click .js-manage-board-members': Popup.open('addMember'),
  164. 'click .sandstorm-powerbox-request-identity'() {
  165. window.sandstormRequestIdentity();
  166. },
  167. 'click .js-member-invite-accept'() {
  168. const boardId = Session.get('currentBoard');
  169. Meteor.user().removeInvite(boardId);
  170. },
  171. 'click .js-member-invite-decline'() {
  172. const boardId = Session.get('currentBoard');
  173. Meteor.call('quitBoard', boardId, (err, ret) => {
  174. if (!err && ret) {
  175. Meteor.user().removeInvite(boardId);
  176. FlowRouter.go('home');
  177. }
  178. });
  179. },
  180. });
  181. Template.labelsWidget.events({
  182. 'click .js-label': Popup.open('editLabel'),
  183. 'click .js-add-label': Popup.open('createLabel'),
  184. });
  185. // Board members can assign people or labels by drag-dropping elements from the
  186. // sidebar to the cards on the board. In order to re-initialize the jquery-ui
  187. // plugin any time a draggable member or label is modified or removed we use a
  188. // autorun function and register a dependency on the both members and labels
  189. // fields of the current board document.
  190. function draggableMembersLabelsWidgets() {
  191. this.autorun(() => {
  192. const currentBoardId = Tracker.nonreactive(() => {
  193. return Session.get('currentBoard');
  194. });
  195. Boards.findOne(currentBoardId, {
  196. fields: {
  197. members: 1,
  198. labels: 1,
  199. },
  200. });
  201. Tracker.afterFlush(() => {
  202. const $draggables = this.$('.js-member,.js-label');
  203. $draggables.draggable({
  204. appendTo: 'body',
  205. helper: 'clone',
  206. revert: 'invalid',
  207. revertDuration: 150,
  208. snap: false,
  209. snapMode: 'both',
  210. start() {
  211. EscapeActions.executeUpTo('popup-back');
  212. },
  213. });
  214. function userIsMember() {
  215. return Meteor.user() && Meteor.user().isBoardMember();
  216. }
  217. this.autorun(() => {
  218. $draggables.draggable('option', 'disabled', !userIsMember());
  219. });
  220. });
  221. });
  222. }
  223. Template.membersWidget.onRendered(draggableMembersLabelsWidgets);
  224. Template.labelsWidget.onRendered(draggableMembersLabelsWidgets);
  225. BlazeComponent.extendComponent({
  226. onCreated() {
  227. this.error = new ReactiveVar('');
  228. this.loading = new ReactiveVar(false);
  229. },
  230. onRendered() {
  231. this.find('.js-search-member input').focus();
  232. this.setLoading(false);
  233. },
  234. isBoardMember() {
  235. const userId = this.currentData()._id;
  236. const user = Users.findOne(userId);
  237. return user && user.isBoardMember();
  238. },
  239. isValidEmail(email) {
  240. return SimpleSchema.RegEx.Email.test(email);
  241. },
  242. setError(error) {
  243. this.error.set(error);
  244. },
  245. setLoading(w) {
  246. this.loading.set(w);
  247. },
  248. isLoading() {
  249. return this.loading.get();
  250. },
  251. inviteUser(idNameEmail) {
  252. const boardId = Session.get('currentBoard');
  253. this.setLoading(true);
  254. const self = this;
  255. Meteor.call('inviteUserToBoard', idNameEmail, boardId, (err, ret) => {
  256. self.setLoading(false);
  257. if (err) self.setError(err.error);
  258. else if (ret.email) self.setError('email-sent');
  259. else Popup.close();
  260. });
  261. },
  262. events() {
  263. return [{
  264. 'keyup input'() {
  265. this.setError('');
  266. },
  267. 'click .js-select-member'() {
  268. const userId = this.currentData()._id;
  269. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  270. if (!currentBoard.hasMember(userId)) {
  271. this.inviteUser(userId);
  272. }
  273. },
  274. 'click .js-email-invite'() {
  275. const idNameEmail = $('.js-search-member input').val();
  276. if (idNameEmail.indexOf('@')<0 || this.isValidEmail(idNameEmail)) {
  277. this.inviteUser(idNameEmail);
  278. } else this.setError('email-invalid');
  279. },
  280. }];
  281. },
  282. }).register('addMemberPopup');
  283. Template.changePermissionsPopup.events({
  284. 'click .js-set-admin, click .js-set-normal, click .js-set-comment-only'(event) {
  285. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  286. const memberId = this.userId;
  287. const isAdmin = $(event.currentTarget).hasClass('js-set-admin');
  288. const isCommentOnly = $(event.currentTarget).hasClass('js-set-comment-only');
  289. currentBoard.setMemberPermission(memberId, isAdmin, isCommentOnly);
  290. Popup.back(1);
  291. },
  292. });
  293. Template.changePermissionsPopup.helpers({
  294. isAdmin() {
  295. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  296. return currentBoard.hasAdmin(this.userId);
  297. },
  298. isNormal() {
  299. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  300. return !currentBoard.hasAdmin(this.userId) && !currentBoard.hasCommentOnly(this.userId);
  301. },
  302. isCommentOnly() {
  303. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  304. return !currentBoard.hasAdmin(this.userId) && currentBoard.hasCommentOnly(this.userId);
  305. },
  306. isLastAdmin() {
  307. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  308. return currentBoard.hasAdmin(this.userId) && (currentBoard.activeAdmins() === 1);
  309. },
  310. });