userHeader.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. } else if (lang.name === '繁体中文(台湾)') {
  162. name = '繁體中文(台灣)';
  163. }
  164. return { tag, name };
  165. }).sort(function(a, b) {
  166. if (a.name === b.name) {
  167. return 0;
  168. } else {
  169. return a.name > b.name ? 1 : -1;
  170. }
  171. });
  172. },
  173. isCurrentLanguage() {
  174. return this.tag === TAPi18n.getLanguage();
  175. },
  176. });
  177. Template.changeLanguagePopup.events({
  178. 'click .js-set-language'(event) {
  179. Users.update(Meteor.userId(), {
  180. $set: {
  181. 'profile.language': this.tag,
  182. },
  183. });
  184. event.preventDefault();
  185. },
  186. });
  187. Template.changeSettingsPopup.helpers({
  188. showDesktopDragHandles() {
  189. currentUser = Meteor.user();
  190. if (currentUser) {
  191. return (currentUser.profile || {}).showDesktopDragHandles;
  192. } else if (cookies.has('showDesktopDragHandles')) {
  193. return true;
  194. } else {
  195. return false;
  196. }
  197. },
  198. hiddenSystemMessages() {
  199. currentUser = Meteor.user();
  200. if (currentUser) {
  201. return (currentUser.profile || {}).hasHiddenSystemMessages;
  202. } else if (cookies.has('hasHiddenSystemMessages')) {
  203. return true;
  204. } else {
  205. return false;
  206. }
  207. },
  208. showCardsCountAt() {
  209. currentUser = Meteor.user();
  210. if (currentUser) {
  211. return Meteor.user().getLimitToShowCardsCount();
  212. } else {
  213. return cookies.get('limitToShowCardsCount');
  214. }
  215. },
  216. });
  217. Template.changeSettingsPopup.events({
  218. 'click .js-toggle-desktop-drag-handles'() {
  219. currentUser = Meteor.user();
  220. if (currentUser) {
  221. Meteor.call('toggleDesktopDragHandles');
  222. } else if (cookies.has('showDesktopDragHandles')) {
  223. cookies.remove('showDesktopDragHandles');
  224. } else {
  225. cookies.set('showDesktopDragHandles', 'true');
  226. }
  227. },
  228. 'click .js-toggle-system-messages'() {
  229. currentUser = Meteor.user();
  230. if (currentUser) {
  231. Meteor.call('toggleSystemMessages');
  232. } else if (cookies.has('hasHiddenSystemMessages')) {
  233. cookies.remove('hasHiddenSystemMessages');
  234. } else {
  235. cookies.set('hasHiddenSystemMessages', 'true');
  236. }
  237. },
  238. 'click .js-apply-show-cards-at'(event, templateInstance) {
  239. event.preventDefault();
  240. const minLimit = parseInt(
  241. templateInstance.$('#show-cards-count-at').val(),
  242. 10,
  243. );
  244. if (!isNaN(minLimit)) {
  245. currentUser = Meteor.user();
  246. if (currentUser) {
  247. Meteor.call('changeLimitToShowCardsCount', minLimit);
  248. } else {
  249. cookies.set('limitToShowCardsCount', minLimit);
  250. }
  251. Popup.back();
  252. }
  253. },
  254. });