sidebar.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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'() {
  133. const boardId = Session.get('currentBoard');
  134. Meteor.call('quitBoard', boardId, (err, ret) => {
  135. if (!ret && ret) {
  136. Popup.close();
  137. FlowRouter.go('home');
  138. }
  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.membersWidget.helpers({
  151. isInvited() {
  152. const user = Meteor.user();
  153. return user && user.isInvitedTo(Session.get('currentBoard'));
  154. },
  155. });
  156. Template.membersWidget.events({
  157. 'click .js-member': Popup.open('member'),
  158. 'click .js-manage-board-members': Popup.open('addMember'),
  159. 'click .sandstorm-powerbox-request-identity'() {
  160. window.sandstormRequestIdentity();
  161. },
  162. 'click .js-member-invite-accept'() {
  163. const boardId = Session.get('currentBoard');
  164. Meteor.user().removeInvite(boardId);
  165. },
  166. 'click .js-member-invite-decline'() {
  167. const boardId = Session.get('currentBoard');
  168. Meteor.call('quitBoard', boardId, (err, ret) => {
  169. if (!err && ret) {
  170. Meteor.user().removeInvite(boardId);
  171. FlowRouter.go('home');
  172. }
  173. });
  174. },
  175. });
  176. Template.labelsWidget.events({
  177. 'click .js-label': Popup.open('editLabel'),
  178. 'click .js-add-label': Popup.open('createLabel'),
  179. });
  180. // Board members can assign people or labels by drag-dropping elements from the
  181. // sidebar to the cards on the board. In order to re-initialize the jquery-ui
  182. // plugin any time a draggable member or label is modified or removed we use a
  183. // autorun function and register a dependency on the both members and labels
  184. // fields of the current board document.
  185. function draggableMembersLabelsWidgets() {
  186. this.autorun(() => {
  187. const currentBoardId = Tracker.nonreactive(() => {
  188. return Session.get('currentBoard');
  189. });
  190. Boards.findOne(currentBoardId, {
  191. fields: {
  192. members: 1,
  193. labels: 1,
  194. },
  195. });
  196. Tracker.afterFlush(() => {
  197. const $draggables = this.$('.js-member,.js-label');
  198. $draggables.draggable({
  199. appendTo: 'body',
  200. helper: 'clone',
  201. revert: 'invalid',
  202. revertDuration: 150,
  203. snap: false,
  204. snapMode: 'both',
  205. start() {
  206. EscapeActions.executeUpTo('popup-back');
  207. },
  208. });
  209. function userIsMember() {
  210. return Meteor.user() && Meteor.user().isBoardMember();
  211. }
  212. this.autorun(() => {
  213. $draggables.draggable('option', 'disabled', !userIsMember());
  214. });
  215. });
  216. });
  217. }
  218. Template.membersWidget.onRendered(draggableMembersLabelsWidgets);
  219. Template.labelsWidget.onRendered(draggableMembersLabelsWidgets);
  220. BlazeComponent.extendComponent({
  221. onCreated() {
  222. this.error = new ReactiveVar('');
  223. this.loading = new ReactiveVar(false);
  224. },
  225. onRendered() {
  226. this.find('.js-search-member input').focus();
  227. this.setLoading(false);
  228. },
  229. isBoardMember() {
  230. const userId = this.currentData()._id;
  231. const user = Users.findOne(userId);
  232. return user && user.isBoardMember();
  233. },
  234. isValidEmail(email) {
  235. return SimpleSchema.RegEx.Email.test(email);
  236. },
  237. setError(error) {
  238. this.error.set(error);
  239. },
  240. setLoading(w) {
  241. this.loading.set(w);
  242. },
  243. isLoading() {
  244. return this.loading.get();
  245. },
  246. inviteUser(idNameEmail) {
  247. const boardId = Session.get('currentBoard');
  248. this.setLoading(true);
  249. const self = this;
  250. Meteor.call('inviteUserToBoard', idNameEmail, boardId, (err, ret) => {
  251. self.setLoading(false);
  252. if (err) self.setError(err.error);
  253. else if (ret.email) self.setError('email-sent');
  254. else Popup.close();
  255. });
  256. },
  257. events() {
  258. return [{
  259. 'keyup input'() {
  260. this.setError('');
  261. },
  262. 'click .js-select-member'() {
  263. const userId = this.currentData()._id;
  264. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  265. if (!currentBoard.hasMember(userId)) {
  266. this.inviteUser(userId);
  267. }
  268. },
  269. 'click .js-email-invite'() {
  270. const idNameEmail = $('.js-search-member input').val();
  271. if (idNameEmail.indexOf('@')<0 || this.isValidEmail(idNameEmail)) {
  272. this.inviteUser(idNameEmail);
  273. } else this.setError('email-invalid');
  274. },
  275. }];
  276. },
  277. }).register('addMemberPopup');
  278. Template.changePermissionsPopup.events({
  279. 'click .js-set-admin, click .js-set-normal, click .js-set-comment-only'(event) {
  280. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  281. const memberId = this.userId;
  282. const isAdmin = $(event.currentTarget).hasClass('js-set-admin');
  283. const isCommentOnly = $(event.currentTarget).hasClass('js-set-comment-only');
  284. currentBoard.setMemberPermission(memberId, isAdmin, isCommentOnly);
  285. Popup.back(1);
  286. },
  287. });
  288. Template.changePermissionsPopup.helpers({
  289. isAdmin() {
  290. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  291. return currentBoard.hasAdmin(this.userId);
  292. },
  293. isNormal() {
  294. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  295. return !currentBoard.hasAdmin(this.userId) && !currentBoard.hasCommentOnly(this.userId);
  296. },
  297. isCommentOnly() {
  298. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  299. return !currentBoard.hasAdmin(this.userId) && currentBoard.hasCommentOnly(this.userId);
  300. },
  301. isLastAdmin() {
  302. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  303. return currentBoard.hasAdmin(this.userId) && (currentBoard.activeAdmins() === 1);
  304. },
  305. });