sidebar.js 9.1 KB

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