userHeader.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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-my-cards'() {
  27. Popup.close();
  28. },
  29. 'click .js-open-archived-board'() {
  30. Modal.open('archivedBoards');
  31. },
  32. 'click .js-edit-profile': Popup.open('editProfile'),
  33. 'click .js-change-settings': Popup.open('changeSettings'),
  34. 'click .js-change-avatar': Popup.open('changeAvatar'),
  35. 'click .js-change-password': Popup.open('changePassword'),
  36. 'click .js-change-language': Popup.open('changeLanguage'),
  37. 'click .js-logout'(event) {
  38. event.preventDefault();
  39. AccountsTemplates.logout();
  40. },
  41. 'click .js-go-setting'() {
  42. Popup.close();
  43. },
  44. });
  45. Template.editProfilePopup.helpers({
  46. allowEmailChange() {
  47. Meteor.call('AccountSettings.allowEmailChange', (_, result) => {
  48. if (result) {
  49. return true;
  50. } else {
  51. return false;
  52. }
  53. });
  54. },
  55. allowUserNameChange() {
  56. Meteor.call('AccountSettings.allowUserNameChange', (_, result) => {
  57. if (result) {
  58. return true;
  59. } else {
  60. return false;
  61. }
  62. });
  63. },
  64. allowUserDelete() {
  65. Meteor.call('AccountSettings.allowUserDelete', (_, result) => {
  66. if (result) {
  67. return true;
  68. } else {
  69. return false;
  70. }
  71. });
  72. },
  73. });
  74. Template.editProfilePopup.events({
  75. submit(event, templateInstance) {
  76. event.preventDefault();
  77. const fullname = templateInstance.find('.js-profile-fullname').value.trim();
  78. const username = templateInstance.find('.js-profile-username').value.trim();
  79. const initials = templateInstance.find('.js-profile-initials').value.trim();
  80. const email = templateInstance.find('.js-profile-email').value.trim();
  81. let isChangeUserName = false;
  82. let isChangeEmail = false;
  83. Users.update(Meteor.userId(), {
  84. $set: {
  85. 'profile.fullname': fullname,
  86. 'profile.initials': initials,
  87. },
  88. });
  89. isChangeUserName = username !== Meteor.user().username;
  90. isChangeEmail =
  91. email.toLowerCase() !== Meteor.user().emails[0].address.toLowerCase();
  92. if (isChangeUserName && isChangeEmail) {
  93. Meteor.call(
  94. 'setUsernameAndEmail',
  95. username,
  96. email.toLowerCase(),
  97. Meteor.userId(),
  98. function(error) {
  99. const usernameMessageElement = templateInstance.$('.username-taken');
  100. const emailMessageElement = templateInstance.$('.email-taken');
  101. if (error) {
  102. const errorElement = error.error;
  103. if (errorElement === 'username-already-taken') {
  104. usernameMessageElement.show();
  105. emailMessageElement.hide();
  106. } else if (errorElement === 'email-already-taken') {
  107. usernameMessageElement.hide();
  108. emailMessageElement.show();
  109. }
  110. } else {
  111. usernameMessageElement.hide();
  112. emailMessageElement.hide();
  113. Popup.back();
  114. }
  115. },
  116. );
  117. } else if (isChangeUserName) {
  118. Meteor.call('setUsername', username, Meteor.userId(), function(error) {
  119. const messageElement = templateInstance.$('.username-taken');
  120. if (error) {
  121. messageElement.show();
  122. } else {
  123. messageElement.hide();
  124. Popup.back();
  125. }
  126. });
  127. } else if (isChangeEmail) {
  128. Meteor.call('setEmail', email.toLowerCase(), Meteor.userId(), function(
  129. error,
  130. ) {
  131. const messageElement = templateInstance.$('.email-taken');
  132. if (error) {
  133. messageElement.show();
  134. } else {
  135. messageElement.hide();
  136. Popup.back();
  137. }
  138. });
  139. } else Popup.back();
  140. },
  141. 'click #deleteButton': Popup.afterConfirm('userDelete', function() {
  142. Popup.close();
  143. Users.remove(Meteor.userId());
  144. AccountsTemplates.logout();
  145. }),
  146. });
  147. // XXX For some reason the useraccounts autofocus isnt working in this case.
  148. // See https://github.com/meteor-useraccounts/core/issues/384
  149. Template.changePasswordPopup.onRendered(function() {
  150. this.find('#at-field-current_password').focus();
  151. });
  152. Template.changeLanguagePopup.helpers({
  153. languages() {
  154. return _.map(TAPi18n.getLanguages(), (lang, code) => {
  155. // Same code in /client/components/main/layouts.js
  156. // TODO : Make code reusable
  157. const tag = code;
  158. let name = lang.name;
  159. if (lang.name === 'br') {
  160. name = 'Brezhoneg';
  161. } else if (lang.name === 'ig') {
  162. name = 'Igbo';
  163. } else if (lang.name === 'oc') {
  164. name = 'Occitan';
  165. } else if (lang.name === '繁体中文(台湾)') {
  166. name = '繁體中文(台灣)';
  167. }
  168. return { tag, name };
  169. }).sort(function(a, b) {
  170. if (a.name === b.name) {
  171. return 0;
  172. } else {
  173. return a.name > b.name ? 1 : -1;
  174. }
  175. });
  176. },
  177. isCurrentLanguage() {
  178. return this.tag === TAPi18n.getLanguage();
  179. },
  180. });
  181. Template.changeLanguagePopup.events({
  182. 'click .js-set-language'(event) {
  183. Users.update(Meteor.userId(), {
  184. $set: {
  185. 'profile.language': this.tag,
  186. },
  187. });
  188. event.preventDefault();
  189. },
  190. });
  191. Template.changeSettingsPopup.helpers({
  192. showDesktopDragHandles() {
  193. currentUser = Meteor.user();
  194. if (currentUser) {
  195. return (currentUser.profile || {}).showDesktopDragHandles;
  196. } else if (window.localStorage.getItem('showDesktopDragHandles')) {
  197. return true;
  198. } else {
  199. return false;
  200. }
  201. },
  202. hiddenSystemMessages() {
  203. currentUser = Meteor.user();
  204. if (currentUser) {
  205. return (currentUser.profile || {}).hasHiddenSystemMessages;
  206. } else if (window.localStorage.getItem('hasHiddenSystemMessages')) {
  207. return true;
  208. } else {
  209. return false;
  210. }
  211. },
  212. showCardsCountAt() {
  213. currentUser = Meteor.user();
  214. if (currentUser) {
  215. return Meteor.user().getLimitToShowCardsCount();
  216. } else {
  217. return window.localStorage.getItem('limitToShowCardsCount');
  218. }
  219. },
  220. weekDays(startDay) {
  221. return [
  222. TAPi18n.__('sunday'),
  223. TAPi18n.__('monday'),
  224. TAPi18n.__('tuesday'),
  225. TAPi18n.__('wednesday'),
  226. TAPi18n.__('thursday'),
  227. TAPi18n.__('friday'),
  228. TAPi18n.__('saturday'),
  229. ].map(function(day, index) {
  230. return { name: day, value: index, isSelected: index === startDay };
  231. });
  232. },
  233. startDayOfWeek() {
  234. currentUser = Meteor.user();
  235. if (currentUser) {
  236. return currentUser.getStartDayOfWeek();
  237. } else {
  238. return window.localStorage.getItem('startDayOfWeek');
  239. }
  240. },
  241. });
  242. Template.changeSettingsPopup.events({
  243. 'click .js-toggle-desktop-drag-handles'() {
  244. currentUser = Meteor.user();
  245. if (currentUser) {
  246. Meteor.call('toggleDesktopDragHandles');
  247. } else if (window.localStorage.getItem('showDesktopDragHandles')) {
  248. window.localStorage.removeItem('showDesktopDragHandles');
  249. } else {
  250. window.localStorage.setItem('showDesktopDragHandles', 'true');
  251. }
  252. },
  253. 'click .js-toggle-system-messages'() {
  254. currentUser = Meteor.user();
  255. if (currentUser) {
  256. Meteor.call('toggleSystemMessages');
  257. } else if (window.localStorage.getItem('hasHiddenSystemMessages')) {
  258. window.localStorage.removeItem('hasHiddenSystemMessages');
  259. } else {
  260. window.localStorage.setItem('hasHiddenSystemMessages', 'true');
  261. }
  262. },
  263. 'click .js-apply-user-settings'(event, templateInstance) {
  264. event.preventDefault();
  265. const minLimit = parseInt(
  266. templateInstance.$('#show-cards-count-at').val(),
  267. 10,
  268. );
  269. const startDay = parseInt(
  270. templateInstance.$('#start-day-of-week').val(),
  271. 10,
  272. );
  273. const currentUser = Meteor.user();
  274. if (!isNaN(minLimit)) {
  275. if (currentUser) {
  276. Meteor.call('changeLimitToShowCardsCount', minLimit);
  277. } else {
  278. window.localStorage.setItem('limitToShowCardsCount', minLimit);
  279. }
  280. }
  281. if (!isNaN(startDay)) {
  282. if (currentUser) {
  283. Meteor.call('changeStartDayOfWeek', startDay);
  284. } else {
  285. window.localStorage.setItem('startDayOfWeek', startDay);
  286. }
  287. }
  288. Popup.back();
  289. },
  290. });