userHeader.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. Meteor.call('AccountSettings.allowEmailChange', (_, result) => {
  42. if (result) {
  43. return true;
  44. } else {
  45. return false;
  46. }
  47. });
  48. },
  49. allowUserNameChange() {
  50. Meteor.call('AccountSettings.allowUserNameChange', (_, result) => {
  51. if (result) {
  52. return true;
  53. } else {
  54. return false;
  55. }
  56. });
  57. },
  58. allowUserDelete() {
  59. Meteor.call('AccountSettings.allowUserDelete', (_, result) => {
  60. if (result) {
  61. return true;
  62. } else {
  63. return false;
  64. }
  65. });
  66. },
  67. });
  68. Template.editProfilePopup.events({
  69. submit(event, templateInstance) {
  70. event.preventDefault();
  71. const fullname = templateInstance.find('.js-profile-fullname').value.trim();
  72. const username = templateInstance.find('.js-profile-username').value.trim();
  73. const initials = templateInstance.find('.js-profile-initials').value.trim();
  74. const email = templateInstance.find('.js-profile-email').value.trim();
  75. let isChangeUserName = false;
  76. let isChangeEmail = false;
  77. Users.update(Meteor.userId(), {
  78. $set: {
  79. 'profile.fullname': fullname,
  80. 'profile.initials': initials,
  81. },
  82. });
  83. isChangeUserName = username !== Meteor.user().username;
  84. isChangeEmail =
  85. email.toLowerCase() !== Meteor.user().emails[0].address.toLowerCase();
  86. if (isChangeUserName && isChangeEmail) {
  87. Meteor.call(
  88. 'setUsernameAndEmail',
  89. username,
  90. email.toLowerCase(),
  91. Meteor.userId(),
  92. function(error) {
  93. const usernameMessageElement = templateInstance.$('.username-taken');
  94. const emailMessageElement = templateInstance.$('.email-taken');
  95. if (error) {
  96. const errorElement = error.error;
  97. if (errorElement === 'username-already-taken') {
  98. usernameMessageElement.show();
  99. emailMessageElement.hide();
  100. } else if (errorElement === 'email-already-taken') {
  101. usernameMessageElement.hide();
  102. emailMessageElement.show();
  103. }
  104. } else {
  105. usernameMessageElement.hide();
  106. emailMessageElement.hide();
  107. Popup.back();
  108. }
  109. },
  110. );
  111. } else if (isChangeUserName) {
  112. Meteor.call('setUsername', username, Meteor.userId(), function(error) {
  113. const messageElement = templateInstance.$('.username-taken');
  114. if (error) {
  115. messageElement.show();
  116. } else {
  117. messageElement.hide();
  118. Popup.back();
  119. }
  120. });
  121. } else if (isChangeEmail) {
  122. Meteor.call('setEmail', email.toLowerCase(), Meteor.userId(), function(
  123. error,
  124. ) {
  125. const messageElement = templateInstance.$('.email-taken');
  126. if (error) {
  127. messageElement.show();
  128. } else {
  129. messageElement.hide();
  130. Popup.back();
  131. }
  132. });
  133. } else Popup.back();
  134. },
  135. 'click #deleteButton': Popup.afterConfirm('userDelete', function() {
  136. Popup.close();
  137. Users.remove(Meteor.userId());
  138. AccountsTemplates.logout();
  139. }),
  140. });
  141. // XXX For some reason the useraccounts autofocus isnt working in this case.
  142. // See https://github.com/meteor-useraccounts/core/issues/384
  143. Template.changePasswordPopup.onRendered(function() {
  144. this.find('#at-field-current_password').focus();
  145. });
  146. Template.changeLanguagePopup.helpers({
  147. languages() {
  148. return _.map(TAPi18n.getLanguages(), (lang, code) => {
  149. // Same code in /client/components/main/layouts.js
  150. // TODO : Make code reusable
  151. const tag = code;
  152. let name = lang.name;
  153. if (lang.name === 'br') {
  154. name = 'Brezhoneg';
  155. } else if (lang.name === 'ig') {
  156. name = 'Igbo';
  157. } else if (lang.name === 'oc') {
  158. name = 'Occitan';
  159. } else if (lang.name === '繁体中文(台湾)') {
  160. name = '繁體中文(台灣)';
  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 (window.localStorage.getItem('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 (window.localStorage.getItem('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 window.localStorage.getItem('limitToShowCardsCount');
  212. }
  213. },
  214. weekDays(startDay) {
  215. return [
  216. TAPi18n.__('sunday'),
  217. TAPi18n.__('monday'),
  218. TAPi18n.__('tuesday'),
  219. TAPi18n.__('wednesday'),
  220. TAPi18n.__('thursday'),
  221. TAPi18n.__('friday'),
  222. TAPi18n.__('saturday'),
  223. ].map(function(day, index) {
  224. return { name: day, value: index, isSelected: index === startDay };
  225. });
  226. },
  227. startDayOfWeek() {
  228. currentUser = Meteor.user();
  229. if (currentUser) {
  230. return currentUser.getStartDayOfWeek();
  231. } else {
  232. return window.localStorage.getItem('startDayOfWeek');
  233. }
  234. },
  235. });
  236. Template.changeSettingsPopup.events({
  237. 'click .js-toggle-desktop-drag-handles'() {
  238. currentUser = Meteor.user();
  239. if (currentUser) {
  240. Meteor.call('toggleDesktopDragHandles');
  241. } else if (window.localStorage.getItem('showDesktopDragHandles')) {
  242. window.localStorage.removeItem('showDesktopDragHandles');
  243. } else {
  244. window.localStorage.setItem('showDesktopDragHandles', 'true');
  245. }
  246. },
  247. 'click .js-toggle-system-messages'() {
  248. currentUser = Meteor.user();
  249. if (currentUser) {
  250. Meteor.call('toggleSystemMessages');
  251. } else if (window.localStorage.getItem('hasHiddenSystemMessages')) {
  252. window.localStorage.removeItem('hasHiddenSystemMessages');
  253. } else {
  254. window.localStorage.setItem('hasHiddenSystemMessages', 'true');
  255. }
  256. },
  257. 'click .js-apply-user-settings'(event, templateInstance) {
  258. event.preventDefault();
  259. const minLimit = parseInt(
  260. templateInstance.$('#show-cards-count-at').val(),
  261. 10,
  262. );
  263. const startDay = parseInt(
  264. templateInstance.$('#start-day-of-week').val(),
  265. 10,
  266. );
  267. const currentUser = Meteor.user();
  268. if (!isNaN(minLimit)) {
  269. if (currentUser) {
  270. Meteor.call('changeLimitToShowCardsCount', minLimit);
  271. } else {
  272. window.localStorage.setItem('limitToShowCardsCount', minLimit);
  273. }
  274. }
  275. if (!isNaN(startDay)) {
  276. if (currentUser) {
  277. Meteor.call('changeStartDayOfWeek', startDay);
  278. } else {
  279. window.localStorage.setItem('startDayOfWeek', startDay);
  280. }
  281. }
  282. Popup.back();
  283. },
  284. });