userHeader.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. isNotOAuth2AuthenticationMethod(){
  25. currentUser = Meteor.user();
  26. if (currentUser) {
  27. return currentUser.authenticationMethod.toLowerCase() != 'oauth2';
  28. } else {
  29. return true;
  30. }
  31. }
  32. });
  33. Template.memberMenuPopup.events({
  34. 'click .js-my-cards'() {
  35. Popup.close();
  36. },
  37. 'click .js-due-cards'() {
  38. Popup.close();
  39. },
  40. 'click .js-open-archived-board'() {
  41. Modal.open('archivedBoards');
  42. },
  43. 'click .js-edit-profile': Popup.open('editProfile'),
  44. 'click .js-change-settings': Popup.open('changeSettings'),
  45. 'click .js-change-avatar': Popup.open('changeAvatar'),
  46. 'click .js-change-password': Popup.open('changePassword'),
  47. 'click .js-change-language': Popup.open('changeLanguage'),
  48. 'click .js-logout'(event) {
  49. event.preventDefault();
  50. AccountsTemplates.logout();
  51. },
  52. 'click .js-go-setting'() {
  53. Popup.close();
  54. },
  55. });
  56. Template.editProfilePopup.helpers({
  57. allowEmailChange() {
  58. Meteor.call('AccountSettings.allowEmailChange', (_, result) => {
  59. if (result) {
  60. return true;
  61. } else {
  62. return false;
  63. }
  64. });
  65. },
  66. allowUserNameChange() {
  67. Meteor.call('AccountSettings.allowUserNameChange', (_, result) => {
  68. if (result) {
  69. return true;
  70. } else {
  71. return false;
  72. }
  73. });
  74. },
  75. allowUserDelete() {
  76. Meteor.call('AccountSettings.allowUserDelete', (_, result) => {
  77. if (result) {
  78. return true;
  79. } else {
  80. return false;
  81. }
  82. });
  83. },
  84. });
  85. Template.editProfilePopup.events({
  86. submit(event, templateInstance) {
  87. event.preventDefault();
  88. const fullname = templateInstance.find('.js-profile-fullname').value.trim();
  89. const username = templateInstance.find('.js-profile-username').value.trim();
  90. const initials = templateInstance.find('.js-profile-initials').value.trim();
  91. const email = templateInstance.find('.js-profile-email').value.trim();
  92. let isChangeUserName = false;
  93. let isChangeEmail = false;
  94. Users.update(Meteor.userId(), {
  95. $set: {
  96. 'profile.fullname': fullname,
  97. 'profile.initials': initials,
  98. },
  99. });
  100. isChangeUserName = username !== Meteor.user().username;
  101. isChangeEmail =
  102. email.toLowerCase() !== Meteor.user().emails[0].address.toLowerCase();
  103. if (isChangeUserName && isChangeEmail) {
  104. Meteor.call(
  105. 'setUsernameAndEmail',
  106. username,
  107. email.toLowerCase(),
  108. Meteor.userId(),
  109. function(error) {
  110. const usernameMessageElement = templateInstance.$('.username-taken');
  111. const emailMessageElement = templateInstance.$('.email-taken');
  112. if (error) {
  113. const errorElement = error.error;
  114. if (errorElement === 'username-already-taken') {
  115. usernameMessageElement.show();
  116. emailMessageElement.hide();
  117. } else if (errorElement === 'email-already-taken') {
  118. usernameMessageElement.hide();
  119. emailMessageElement.show();
  120. }
  121. } else {
  122. usernameMessageElement.hide();
  123. emailMessageElement.hide();
  124. Popup.back();
  125. }
  126. },
  127. );
  128. } else if (isChangeUserName) {
  129. Meteor.call('setUsername', username, Meteor.userId(), function(error) {
  130. const messageElement = templateInstance.$('.username-taken');
  131. if (error) {
  132. messageElement.show();
  133. } else {
  134. messageElement.hide();
  135. Popup.back();
  136. }
  137. });
  138. } else if (isChangeEmail) {
  139. Meteor.call('setEmail', email.toLowerCase(), Meteor.userId(), function(
  140. error,
  141. ) {
  142. const messageElement = templateInstance.$('.email-taken');
  143. if (error) {
  144. messageElement.show();
  145. } else {
  146. messageElement.hide();
  147. Popup.back();
  148. }
  149. });
  150. } else Popup.back();
  151. },
  152. 'click #deleteButton': Popup.afterConfirm('userDelete', function() {
  153. Popup.close();
  154. Users.remove(Meteor.userId());
  155. AccountsTemplates.logout();
  156. }),
  157. });
  158. // XXX For some reason the useraccounts autofocus isnt working in this case.
  159. // See https://github.com/meteor-useraccounts/core/issues/384
  160. Template.changePasswordPopup.onRendered(function() {
  161. this.find('#at-field-current_password').focus();
  162. });
  163. Template.changeLanguagePopup.helpers({
  164. languages() {
  165. return _.map(TAPi18n.getLanguages(), (lang, code) => {
  166. // Same code in /client/components/main/layouts.js
  167. // TODO : Make code reusable
  168. const tag = code;
  169. let name = lang.name;
  170. if (lang.name === 'br') {
  171. name = 'Brezhoneg';
  172. } else if (lang.name === 'ar-EG') {
  173. // ar-EG = Arabic (Egypt), simply Masri (مَصرى, [ˈmɑsˤɾi], Egyptian, Masr refers to Cairo)
  174. name = 'مَصرى';
  175. } else if (lang.name === 'fa-IR') {
  176. // fa-IR = Persian (Iran)
  177. name = 'فارسی/پارسی (ایران‎)';
  178. } else if (lang.name === 'de-CH') {
  179. name = 'Deutsch (Schweiz)';
  180. } else if (lang.name === 'fr-BE') {
  181. name = 'Français (Belgique)';
  182. } else if (lang.name === 'fr-CA') {
  183. name = 'Français (Canada)';
  184. } else if (lang.name === 'fr-CH') {
  185. name = 'Français (Schweiz)';
  186. } else if (lang.name === 'ig') {
  187. name = 'Igbo';
  188. } else if (lang.name === 'lv') {
  189. name = 'Latviešu';
  190. } else if (lang.name === 'latviešu valoda') {
  191. name = 'Latviešu';
  192. } else if (lang.name === 'en-IT') {
  193. name = 'English (Italy)';
  194. } else if (lang.name === 'Español') {
  195. name = 'español';
  196. } else if (lang.name === 'es_419') {
  197. name = 'español de América Latina';
  198. } else if (lang.name === 'es-419') {
  199. name = 'español de América Latina';
  200. } else if (lang.name === 'Español de América Latina') {
  201. name = 'español de América Latina';
  202. } else if (lang.name === 'es-LA') {
  203. name = 'español de América Latina';
  204. } else if (lang.name === 'Español de Argentina') {
  205. name = 'español de Argentina';
  206. } else if (lang.name === 'Español de Chile') {
  207. name = 'español de Chile';
  208. } else if (lang.name === 'Español de Colombia') {
  209. name = 'español de Colombia';
  210. } else if (lang.name === 'Español de México') {
  211. name = 'español de México';
  212. } else if (lang.name === 'es-PY') {
  213. name = 'español de Paraguayo';
  214. } else if (lang.name === 'Español de Paraguayo') {
  215. name = 'español de Paraguayo';
  216. } else if (lang.name === 'Español de Perú') {
  217. name = 'español de Perú';
  218. } else if (lang.name === 'Español de Puerto Rico') {
  219. name = 'español de Puerto Rico';
  220. } else if (lang.name === 'oc') {
  221. name = 'Occitan';
  222. } else if (lang.name === 'st') {
  223. name = 'Sãotomense';
  224. } else if (lang.name === '繁体中文(台湾)') {
  225. name = '繁體中文(台灣)';
  226. }
  227. return { tag, name };
  228. }).sort(function(a, b) {
  229. if (a.name === b.name) {
  230. return 0;
  231. } else {
  232. return a.name > b.name ? 1 : -1;
  233. }
  234. });
  235. },
  236. isCurrentLanguage() {
  237. return this.tag === TAPi18n.getLanguage();
  238. },
  239. });
  240. Template.changeLanguagePopup.events({
  241. 'click .js-set-language'(event) {
  242. Users.update(Meteor.userId(), {
  243. $set: {
  244. 'profile.language': this.tag,
  245. },
  246. });
  247. TAPi18n.setLanguage(this.tag);
  248. event.preventDefault();
  249. },
  250. });
  251. Template.changeSettingsPopup.helpers({
  252. hiddenSystemMessages() {
  253. currentUser = Meteor.user();
  254. if (currentUser) {
  255. return (currentUser.profile || {}).hasHiddenSystemMessages;
  256. } else if (window.localStorage.getItem('hasHiddenSystemMessages')) {
  257. return true;
  258. } else {
  259. return false;
  260. }
  261. },
  262. showCardsCountAt() {
  263. currentUser = Meteor.user();
  264. if (currentUser) {
  265. return Meteor.user().getLimitToShowCardsCount();
  266. } else {
  267. return window.localStorage.getItem('limitToShowCardsCount');
  268. }
  269. },
  270. weekDays(startDay) {
  271. return [
  272. TAPi18n.__('sunday'),
  273. TAPi18n.__('monday'),
  274. TAPi18n.__('tuesday'),
  275. TAPi18n.__('wednesday'),
  276. TAPi18n.__('thursday'),
  277. TAPi18n.__('friday'),
  278. TAPi18n.__('saturday'),
  279. ].map(function(day, index) {
  280. return { name: day, value: index, isSelected: index === startDay };
  281. });
  282. },
  283. startDayOfWeek() {
  284. currentUser = Meteor.user();
  285. if (currentUser) {
  286. return currentUser.getStartDayOfWeek();
  287. } else {
  288. return window.localStorage.getItem('startDayOfWeek');
  289. }
  290. },
  291. });
  292. Template.changeSettingsPopup.events({
  293. 'keypress/paste #show-cards-count-at'() {
  294. let keyCode = event.keyCode;
  295. let charCode = String.fromCharCode(keyCode);
  296. let regex = new RegExp('[-0-9]');
  297. let ret = regex.test(charCode);
  298. return ret;
  299. },
  300. 'click .js-toggle-desktop-drag-handles'() {
  301. currentUser = Meteor.user();
  302. if (currentUser) {
  303. Meteor.call('toggleDesktopDragHandles');
  304. } else if (window.localStorage.getItem('showDesktopDragHandles')) {
  305. window.localStorage.removeItem('showDesktopDragHandles');
  306. } else {
  307. window.localStorage.setItem('showDesktopDragHandles', 'true');
  308. }
  309. },
  310. 'click .js-toggle-system-messages'() {
  311. currentUser = Meteor.user();
  312. if (currentUser) {
  313. Meteor.call('toggleSystemMessages');
  314. } else if (window.localStorage.getItem('hasHiddenSystemMessages')) {
  315. window.localStorage.removeItem('hasHiddenSystemMessages');
  316. } else {
  317. window.localStorage.setItem('hasHiddenSystemMessages', 'true');
  318. }
  319. },
  320. 'click .js-apply-user-settings'(event, templateInstance) {
  321. event.preventDefault();
  322. let minLimit = parseInt(
  323. templateInstance.$('#show-cards-count-at').val(),
  324. 10,
  325. );
  326. const startDay = parseInt(
  327. templateInstance.$('#start-day-of-week').val(),
  328. 10,
  329. );
  330. const currentUser = Meteor.user();
  331. if (isNaN(minLimit) || minLimit < -1) {
  332. minLimit = -1;
  333. }
  334. if (!isNaN(minLimit)) {
  335. if (currentUser) {
  336. Meteor.call('changeLimitToShowCardsCount', minLimit);
  337. } else {
  338. window.localStorage.setItem('limitToShowCardsCount', minLimit);
  339. }
  340. }
  341. if (!isNaN(startDay)) {
  342. if (currentUser) {
  343. Meteor.call('changeStartDayOfWeek', startDay);
  344. } else {
  345. window.localStorage.setItem('startDayOfWeek', startDay);
  346. }
  347. }
  348. Popup.back();
  349. },
  350. });