userHeader.js 7.3 KB

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