userHeader.js 11 KB

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