userHeader.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 === 'ar-EG') {
  165. // ar-EG = Arabic (Egypt), simply Masri (مَصرى, [ˈmɑsˤɾi], Egyptian, Masr refers to Cairo)
  166. name = 'مَصرى';
  167. } else if (lang.name === 'es-PY') {
  168. name = 'Español de Paraguayo';
  169. } else if (lang.name === 'fa-IR') {
  170. // fa-IR = Persian (Iran)
  171. name = 'فارسی/پارسی (ایران‎)';
  172. } else if (lang.name === 'fr-BE') {
  173. name = 'Français (Belgique)';
  174. } else if (lang.name === 'fr-CA') {
  175. name = 'Français (Canada)';
  176. } else if (lang.name === 'ig') {
  177. name = 'Igbo';
  178. } else if (lang.name === 'lv') {
  179. name = 'Latviešu';
  180. } else if (lang.name === 'latviešu valoda') {
  181. name = 'Latviešu';
  182. } else if (lang.name === 'Español') {
  183. name = 'español';
  184. } else if (lang.name === 'Español de Argentina') {
  185. name = 'español de Argentina';
  186. } else if (lang.name === 'Español de Chile') {
  187. name = 'español de Chile';
  188. } else if (lang.name === 'Español de Colombia') {
  189. name = 'español de Colombia';
  190. } else if (lang.name === 'Español de México') {
  191. name = 'español de México';
  192. } else if (lang.name === 'es-PY') {
  193. name = 'español de Paraguayo';
  194. } else if (lang.name === 'Español de Perú') {
  195. name = 'español de Perú';
  196. } else if (lang.name === 'Español de Puerto Rico') {
  197. name = 'español de Puerto Rico';
  198. } else if (lang.name === 'oc') {
  199. name = 'Occitan';
  200. } else if (lang.name === 'st') {
  201. name = 'Sãotomense';
  202. } else if (lang.name === '繁体中文(台湾)') {
  203. name = '繁體中文(台灣)';
  204. }
  205. return { tag, name };
  206. }).sort(function(a, b) {
  207. if (a.name === b.name) {
  208. return 0;
  209. } else {
  210. return a.name > b.name ? 1 : -1;
  211. }
  212. });
  213. },
  214. isCurrentLanguage() {
  215. return this.tag === TAPi18n.getLanguage();
  216. },
  217. });
  218. Template.changeLanguagePopup.events({
  219. 'click .js-set-language'(event) {
  220. Users.update(Meteor.userId(), {
  221. $set: {
  222. 'profile.language': this.tag,
  223. },
  224. });
  225. TAPi18n.setLanguage(this.tag);
  226. event.preventDefault();
  227. },
  228. });
  229. Template.changeSettingsPopup.helpers({
  230. showDesktopDragHandles() {
  231. currentUser = Meteor.user();
  232. if (currentUser) {
  233. return (currentUser.profile || {}).showDesktopDragHandles;
  234. } else if (window.localStorage.getItem('showDesktopDragHandles')) {
  235. return true;
  236. } else {
  237. return false;
  238. }
  239. },
  240. hiddenSystemMessages() {
  241. currentUser = Meteor.user();
  242. if (currentUser) {
  243. return (currentUser.profile || {}).hasHiddenSystemMessages;
  244. } else if (window.localStorage.getItem('hasHiddenSystemMessages')) {
  245. return true;
  246. } else {
  247. return false;
  248. }
  249. },
  250. showCardsCountAt() {
  251. currentUser = Meteor.user();
  252. if (currentUser) {
  253. return Meteor.user().getLimitToShowCardsCount();
  254. } else {
  255. return window.localStorage.getItem('limitToShowCardsCount');
  256. }
  257. },
  258. weekDays(startDay) {
  259. return [
  260. TAPi18n.__('sunday'),
  261. TAPi18n.__('monday'),
  262. TAPi18n.__('tuesday'),
  263. TAPi18n.__('wednesday'),
  264. TAPi18n.__('thursday'),
  265. TAPi18n.__('friday'),
  266. TAPi18n.__('saturday'),
  267. ].map(function(day, index) {
  268. return { name: day, value: index, isSelected: index === startDay };
  269. });
  270. },
  271. startDayOfWeek() {
  272. currentUser = Meteor.user();
  273. if (currentUser) {
  274. return currentUser.getStartDayOfWeek();
  275. } else {
  276. return window.localStorage.getItem('startDayOfWeek');
  277. }
  278. },
  279. });
  280. Template.changeSettingsPopup.events({
  281. 'keypress/paste #show-cards-count-at'() {
  282. let keyCode = event.keyCode;
  283. let charCode = String.fromCharCode(keyCode);
  284. let regex = new RegExp('[-0-9]');
  285. let ret = regex.test(charCode);
  286. return ret;
  287. },
  288. 'click .js-toggle-desktop-drag-handles'() {
  289. currentUser = Meteor.user();
  290. if (currentUser) {
  291. Meteor.call('toggleDesktopDragHandles');
  292. } else if (window.localStorage.getItem('showDesktopDragHandles')) {
  293. window.localStorage.removeItem('showDesktopDragHandles');
  294. } else {
  295. window.localStorage.setItem('showDesktopDragHandles', 'true');
  296. }
  297. },
  298. 'click .js-toggle-system-messages'() {
  299. currentUser = Meteor.user();
  300. if (currentUser) {
  301. Meteor.call('toggleSystemMessages');
  302. } else if (window.localStorage.getItem('hasHiddenSystemMessages')) {
  303. window.localStorage.removeItem('hasHiddenSystemMessages');
  304. } else {
  305. window.localStorage.setItem('hasHiddenSystemMessages', 'true');
  306. }
  307. },
  308. 'click .js-apply-user-settings'(event, templateInstance) {
  309. event.preventDefault();
  310. let minLimit = parseInt(
  311. templateInstance.$('#show-cards-count-at').val(),
  312. 10,
  313. );
  314. const startDay = parseInt(
  315. templateInstance.$('#start-day-of-week').val(),
  316. 10,
  317. );
  318. const currentUser = Meteor.user();
  319. if (isNaN(minLimit) || minLimit < -1) {
  320. minLimit = -1;
  321. }
  322. if (!isNaN(minLimit)) {
  323. if (currentUser) {
  324. Meteor.call('changeLimitToShowCardsCount', minLimit);
  325. } else {
  326. window.localStorage.setItem('limitToShowCardsCount', minLimit);
  327. }
  328. }
  329. if (!isNaN(startDay)) {
  330. if (currentUser) {
  331. Meteor.call('changeStartDayOfWeek', startDay);
  332. } else {
  333. window.localStorage.setItem('startDayOfWeek', startDay);
  334. }
  335. }
  336. Popup.back();
  337. },
  338. });