userHeader.js 7.3 KB

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