sidebar.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. Sidebar = null;
  2. const defaultView = 'home';
  3. const viewTitles = {
  4. filter: 'filter-cards',
  5. search: 'search-cards',
  6. multiselection: 'multi-selection',
  7. archives: 'archives',
  8. };
  9. BlazeComponent.extendComponent({
  10. mixins() {
  11. return [Mixins.InfiniteScrolling, Mixins.PerfectScrollbar];
  12. },
  13. onCreated() {
  14. const initOpen = Utils.isMiniScreen() ? false : (!Session.get('currentCard'));
  15. this._isOpen = new ReactiveVar(initOpen);
  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. if(commentOnly){
  106. return TAPi18n.__('comment-only').toLowerCase();
  107. } else {
  108. return TAPi18n.__(type).toLowerCase();
  109. }
  110. } else {
  111. return TAPi18n.__(type).toLowerCase();
  112. }
  113. },
  114. isInvited() {
  115. return Users.findOne(this.userId).isInvitedTo(Session.get('currentBoard'));
  116. },
  117. });
  118. Template.memberPopup.events({
  119. 'click .js-filter-member'() {
  120. Filter.members.toggle(this.userId);
  121. Popup.close();
  122. },
  123. 'click .js-change-role': Popup.open('changePermissions'),
  124. 'click .js-remove-member': Popup.afterConfirm('removeMember', function() {
  125. const boardId = Session.get('currentBoard');
  126. const memberId = this.userId;
  127. Cards.find({ boardId, members: memberId }).forEach((card) => {
  128. card.unassignMember(memberId);
  129. });
  130. Boards.findOne(boardId).removeMember(memberId);
  131. Popup.close();
  132. }),
  133. 'click .js-leave-member': Popup.afterConfirm('leaveBoard', () => {
  134. const boardId = Session.get('currentBoard');
  135. Meteor.call('quitBoard', boardId, () => {
  136. Popup.close();
  137. FlowRouter.go('home');
  138. });
  139. }),
  140. });
  141. Template.removeMemberPopup.helpers({
  142. user() {
  143. return Users.findOne(this.userId);
  144. },
  145. board() {
  146. return Boards.findOne(Session.get('currentBoard'));
  147. },
  148. });
  149. Template.leaveBoardPopup.helpers({
  150. board() {
  151. return Boards.findOne(Session.get('currentBoard'));
  152. },
  153. });
  154. Template.membersWidget.helpers({
  155. isInvited() {
  156. const user = Meteor.user();
  157. return user && user.isInvitedTo(Session.get('currentBoard'));
  158. },
  159. });
  160. Template.membersWidget.events({
  161. 'click .js-member': Popup.open('member'),
  162. 'click .js-manage-board-members': Popup.open('addMember'),
  163. 'click .sandstorm-powerbox-request-identity'() {
  164. window.sandstormRequestIdentity();
  165. },
  166. 'click .js-member-invite-accept'() {
  167. const boardId = Session.get('currentBoard');
  168. Meteor.user().removeInvite(boardId);
  169. },
  170. 'click .js-member-invite-decline'() {
  171. const boardId = Session.get('currentBoard');
  172. Meteor.call('quitBoard', boardId, (err, ret) => {
  173. if (!err && ret) {
  174. Meteor.user().removeInvite(boardId);
  175. FlowRouter.go('home');
  176. }
  177. });
  178. },
  179. });
  180. Template.labelsWidget.events({
  181. 'click .js-label': Popup.open('editLabel'),
  182. 'click .js-add-label': Popup.open('createLabel'),
  183. });
  184. // Board members can assign people or labels by drag-dropping elements from the
  185. // sidebar to the cards on the board. In order to re-initialize the jquery-ui
  186. // plugin any time a draggable member or label is modified or removed we use a
  187. // autorun function and register a dependency on the both members and labels
  188. // fields of the current board document.
  189. function draggableMembersLabelsWidgets() {
  190. this.autorun(() => {
  191. const currentBoardId = Tracker.nonreactive(() => {
  192. return Session.get('currentBoard');
  193. });
  194. Boards.findOne(currentBoardId, {
  195. fields: {
  196. members: 1,
  197. labels: 1,
  198. },
  199. });
  200. Tracker.afterFlush(() => {
  201. const $draggables = this.$('.js-member,.js-label');
  202. $draggables.draggable({
  203. appendTo: 'body',
  204. helper: 'clone',
  205. revert: 'invalid',
  206. revertDuration: 150,
  207. snap: false,
  208. snapMode: 'both',
  209. start() {
  210. EscapeActions.executeUpTo('popup-back');
  211. },
  212. });
  213. function userIsMember() {
  214. return Meteor.user() && Meteor.user().isBoardMember();
  215. }
  216. this.autorun(() => {
  217. $draggables.draggable('option', 'disabled', !userIsMember());
  218. });
  219. });
  220. });
  221. }
  222. Template.membersWidget.onRendered(draggableMembersLabelsWidgets);
  223. Template.labelsWidget.onRendered(draggableMembersLabelsWidgets);
  224. BlazeComponent.extendComponent({
  225. onCreated() {
  226. this.error = new ReactiveVar('');
  227. this.loading = new ReactiveVar(false);
  228. },
  229. onRendered() {
  230. this.find('.js-search-member input').focus();
  231. this.setLoading(false);
  232. },
  233. isBoardMember() {
  234. const userId = this.currentData()._id;
  235. const user = Users.findOne(userId);
  236. return user && user.isBoardMember();
  237. },
  238. isValidEmail(email) {
  239. return SimpleSchema.RegEx.Email.test(email);
  240. },
  241. setError(error) {
  242. this.error.set(error);
  243. },
  244. setLoading(w) {
  245. this.loading.set(w);
  246. },
  247. isLoading() {
  248. return this.loading.get();
  249. },
  250. inviteUser(idNameEmail) {
  251. const boardId = Session.get('currentBoard');
  252. this.setLoading(true);
  253. const self = this;
  254. Meteor.call('inviteUserToBoard', idNameEmail, boardId, (err, ret) => {
  255. self.setLoading(false);
  256. if (err) self.setError(err.error);
  257. else if (ret.email) self.setError('email-sent');
  258. else Popup.close();
  259. });
  260. },
  261. events() {
  262. return [{
  263. 'keyup input'() {
  264. this.setError('');
  265. },
  266. 'click .js-select-member'() {
  267. const userId = this.currentData()._id;
  268. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  269. if (!currentBoard.hasMember(userId)) {
  270. this.inviteUser(userId);
  271. }
  272. },
  273. 'click .js-email-invite'() {
  274. const idNameEmail = $('.js-search-member input').val();
  275. if (idNameEmail.indexOf('@')<0 || this.isValidEmail(idNameEmail)) {
  276. this.inviteUser(idNameEmail);
  277. } else this.setError('email-invalid');
  278. },
  279. }];
  280. },
  281. }).register('addMemberPopup');
  282. Template.changePermissionsPopup.events({
  283. 'click .js-set-admin, click .js-set-normal, click .js-set-comment-only'(event) {
  284. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  285. const memberId = this.userId;
  286. const isAdmin = $(event.currentTarget).hasClass('js-set-admin');
  287. const isCommentOnly = $(event.currentTarget).hasClass('js-set-comment-only');
  288. currentBoard.setMemberPermission(memberId, isAdmin, isCommentOnly);
  289. Popup.back(1);
  290. },
  291. });
  292. Template.changePermissionsPopup.helpers({
  293. isAdmin() {
  294. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  295. return currentBoard.hasAdmin(this.userId);
  296. },
  297. isNormal() {
  298. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  299. return !currentBoard.hasAdmin(this.userId) && !currentBoard.hasCommentOnly(this.userId);
  300. },
  301. isCommentOnly() {
  302. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  303. return !currentBoard.hasAdmin(this.userId) && currentBoard.hasCommentOnly(this.userId);
  304. },
  305. isLastAdmin() {
  306. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  307. return currentBoard.hasAdmin(this.userId) && (currentBoard.activeAdmins() === 1);
  308. },
  309. });