userHeader.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. Template.headerUserBar.events({
  2. 'click .js-open-header-member-menu': Popup.open('memberMenu'),
  3. 'click .js-change-avatar': Popup.open('changeAvatar'),
  4. });
  5. Template.memberMenuPopup.helpers({
  6. templatesBoardId() {
  7. currentUser = Meteor.user();
  8. if (currentUser) {
  9. return Meteor.user().getTemplatesBoardId();
  10. } else {
  11. // No need to getTemplatesBoardId on public board
  12. return false;
  13. }
  14. },
  15. templatesBoardSlug() {
  16. currentUser = Meteor.user();
  17. if (currentUser) {
  18. return Meteor.user().getTemplatesBoardSlug();
  19. } else {
  20. // No need to getTemplatesBoardSlug() on public board
  21. return false;
  22. }
  23. },
  24. });
  25. Template.memberMenuPopup.events({
  26. 'click .js-edit-profile': Popup.open('editProfile'),
  27. 'click .js-change-settings': Popup.open('changeSettings'),
  28. 'click .js-change-avatar': Popup.open('changeAvatar'),
  29. 'click .js-change-password': Popup.open('changePassword'),
  30. 'click .js-change-language': Popup.open('changeLanguage'),
  31. 'click .js-logout'(event) {
  32. event.preventDefault();
  33. AccountsTemplates.logout();
  34. },
  35. 'click .js-go-setting'() {
  36. Popup.close();
  37. },
  38. });
  39. Template.editProfilePopup.helpers({
  40. allowEmailChange() {
  41. return AccountSettings.findOne('accounts-allowEmailChange').booleanValue;
  42. },
  43. allowUserNameChange() {
  44. return AccountSettings.findOne('accounts-allowUserNameChange').booleanValue;
  45. },
  46. allowUserDelete() {
  47. return AccountSettings.findOne('accounts-allowUserDelete').booleanValue;
  48. },
  49. });
  50. Template.editProfilePopup.events({
  51. submit(event, templateInstance) {
  52. event.preventDefault();
  53. const fullname = templateInstance.find('.js-profile-fullname').value.trim();
  54. const username = templateInstance.find('.js-profile-username').value.trim();
  55. const initials = templateInstance.find('.js-profile-initials').value.trim();
  56. const email = templateInstance.find('.js-profile-email').value.trim();
  57. let isChangeUserName = false;
  58. let isChangeEmail = false;
  59. Users.update(Meteor.userId(), {
  60. $set: {
  61. 'profile.fullname': fullname,
  62. 'profile.initials': initials,
  63. },
  64. });
  65. isChangeUserName = username !== Meteor.user().username;
  66. isChangeEmail =
  67. email.toLowerCase() !== Meteor.user().emails[0].address.toLowerCase();
  68. if (isChangeUserName && isChangeEmail) {
  69. Meteor.call(
  70. 'setUsernameAndEmail',
  71. username,
  72. email.toLowerCase(),
  73. Meteor.userId(),
  74. function(error) {
  75. const usernameMessageElement = templateInstance.$('.username-taken');
  76. const emailMessageElement = templateInstance.$('.email-taken');
  77. if (error) {
  78. const errorElement = error.error;
  79. if (errorElement === 'username-already-taken') {
  80. usernameMessageElement.show();
  81. emailMessageElement.hide();
  82. } else if (errorElement === 'email-already-taken') {
  83. usernameMessageElement.hide();
  84. emailMessageElement.show();
  85. }
  86. } else {
  87. usernameMessageElement.hide();
  88. emailMessageElement.hide();
  89. Popup.back();
  90. }
  91. },
  92. );
  93. } else if (isChangeUserName) {
  94. Meteor.call('setUsername', username, Meteor.userId(), function(error) {
  95. const messageElement = templateInstance.$('.username-taken');
  96. if (error) {
  97. messageElement.show();
  98. } else {
  99. messageElement.hide();
  100. Popup.back();
  101. }
  102. });
  103. } else if (isChangeEmail) {
  104. Meteor.call('setEmail', email.toLowerCase(), Meteor.userId(), function(
  105. error,
  106. ) {
  107. const messageElement = templateInstance.$('.email-taken');
  108. if (error) {
  109. messageElement.show();
  110. } else {
  111. messageElement.hide();
  112. Popup.back();
  113. }
  114. });
  115. } else Popup.back();
  116. },
  117. 'click #deleteButton': Popup.afterConfirm('userDelete', function() {
  118. Popup.close();
  119. Users.remove(Meteor.userId());
  120. AccountsTemplates.logout();
  121. }),
  122. });
  123. // XXX For some reason the useraccounts autofocus isnt working in this case.
  124. // See https://github.com/meteor-useraccounts/core/issues/384
  125. Template.changePasswordPopup.onRendered(function() {
  126. this.find('#at-field-current_password').focus();
  127. });
  128. Template.changeLanguagePopup.helpers({
  129. languages() {
  130. return _.map(TAPi18n.getLanguages(), (lang, code) => {
  131. // Same code in /client/components/main/layouts.js
  132. // TODO : Make code reusable
  133. const tag = code;
  134. let name = lang.name;
  135. if (lang.name === 'br') {
  136. name = 'Brezhoneg';
  137. } else if (lang.name === 'ig') {
  138. name = 'Igbo';
  139. } else if (lang.name === 'oc') {
  140. name = 'Occitan';
  141. }
  142. return { tag, name };
  143. }).sort(function(a, b) {
  144. if (a.name === b.name) {
  145. return 0;
  146. } else {
  147. return a.name > b.name ? 1 : -1;
  148. }
  149. });
  150. },
  151. isCurrentLanguage() {
  152. return this.tag === TAPi18n.getLanguage();
  153. },
  154. });
  155. Template.changeLanguagePopup.events({
  156. 'click .js-set-language'(event) {
  157. Users.update(Meteor.userId(), {
  158. $set: {
  159. 'profile.language': this.tag,
  160. },
  161. });
  162. event.preventDefault();
  163. },
  164. });
  165. Template.changeSettingsPopup.helpers({
  166. showDesktopDragHandles() {
  167. currentUser = Meteor.user();
  168. if (currentUser) {
  169. return (currentUser.profile || {}).showDesktopDragHandles;
  170. } else {
  171. import { Cookies } from 'meteor/ostrio:cookies';
  172. const cookies = new Cookies();
  173. if (cookies.has('showDesktopDragHandles')) {
  174. return true;
  175. } else {
  176. return false;
  177. }
  178. }
  179. },
  180. hiddenSystemMessages() {
  181. currentUser = Meteor.user();
  182. if (currentUser) {
  183. return (currentUser.profile || {}).hasHiddenSystemMessages;
  184. } else {
  185. import { Cookies } from 'meteor/ostrio:cookies';
  186. const cookies = new Cookies();
  187. if (cookies.has('hasHiddenSystemMessages')) {
  188. return true;
  189. } else {
  190. return false;
  191. }
  192. }
  193. },
  194. showCardsCountAt() {
  195. currentUser = Meteor.user();
  196. if (currentUser) {
  197. return Meteor.user().getLimitToShowCardsCount();
  198. } else {
  199. import { Cookies } from 'meteor/ostrio:cookies';
  200. const cookies = new Cookies();
  201. return cookies.get('limitToShowCardsCount');
  202. }
  203. },
  204. });
  205. Template.changeSettingsPopup.events({
  206. 'click .js-toggle-desktop-drag-handles'() {
  207. currentUser = Meteor.user();
  208. if (currentUser) {
  209. Meteor.call('toggleDesktopDragHandles');
  210. } else {
  211. import { Cookies } from 'meteor/ostrio:cookies';
  212. const cookies = new Cookies();
  213. if (cookies.has('showDesktopDragHandles')) {
  214. cookies.remove('showDesktopDragHandles');
  215. } else {
  216. cookies.set('showDesktopDragHandles', 'true');
  217. }
  218. }
  219. },
  220. 'click .js-toggle-system-messages'() {
  221. currentUser = Meteor.user();
  222. if (currentUser) {
  223. Meteor.call('toggleSystemMessages');
  224. } else {
  225. import { Cookies } from 'meteor/ostrio:cookies';
  226. const cookies = new Cookies();
  227. if (cookies.has('hasHiddenSystemMessages')) {
  228. cookies.remove('hasHiddenSystemMessages');
  229. } else {
  230. cookies.set('hasHiddenSystemMessages', 'true');
  231. }
  232. }
  233. },
  234. 'click .js-apply-show-cards-at'(event, templateInstance) {
  235. event.preventDefault();
  236. const minLimit = parseInt(
  237. templateInstance.$('#show-cards-count-at').val(),
  238. 10,
  239. );
  240. if (!isNaN(minLimit)) {
  241. currentUser = Meteor.user();
  242. if (currentUser) {
  243. Meteor.call('changeLimitToShowCardsCount', minLimit);
  244. } else {
  245. import { Cookies } from 'meteor/ostrio:cookies';
  246. const cookies = new Cookies();
  247. cookies.set('limitToShowCardsCount', minLimit);
  248. }
  249. Popup.back();
  250. }
  251. },
  252. });