userHeader.js 8.6 KB

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