userHeader.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import { TAPi18n } from '/imports/i18n';
  3. Template.headerUserBar.events({
  4. 'click .js-open-header-member-menu': Popup.open('memberMenu'),
  5. 'click .js-change-avatar': Popup.open('changeAvatar'),
  6. });
  7. BlazeComponent.extendComponent({
  8. onCreated() {
  9. Meteor.subscribe('setting');
  10. },
  11. }).register('memberMenuPopup');
  12. Template.memberMenuPopup.helpers({
  13. templatesBoardId() {
  14. const currentUser = ReactiveCache.getCurrentUser();
  15. if (currentUser) {
  16. return currentUser.getTemplatesBoardId();
  17. } else {
  18. // No need to getTemplatesBoardId on public board
  19. return false;
  20. }
  21. },
  22. templatesBoardSlug() {
  23. const currentUser = ReactiveCache.getCurrentUser();
  24. if (currentUser) {
  25. return currentUser.getTemplatesBoardSlug();
  26. } else {
  27. // No need to getTemplatesBoardSlug() on public board
  28. return false;
  29. }
  30. },
  31. isSameDomainNameSettingValue(){
  32. const currSett = ReactiveCache.getCurrentSetting();
  33. if(currSett && currSett != undefined && currSett.disableRegistration && currSett.mailDomainName !== undefined && currSett.mailDomainName != ""){
  34. currentUser = ReactiveCache.getCurrentUser();
  35. if (currentUser) {
  36. let found = false;
  37. for(let i = 0; i < currentUser.emails.length; i++) {
  38. if(currentUser.emails[i].address.endsWith(currSett.mailDomainName)){
  39. found = true;
  40. break;
  41. }
  42. }
  43. return found;
  44. } else {
  45. return true;
  46. }
  47. }
  48. else
  49. return false;
  50. },
  51. isNotOAuth2AuthenticationMethod(){
  52. const currentUser = ReactiveCache.getCurrentUser();
  53. if (currentUser) {
  54. return currentUser.authenticationMethod.toLowerCase() != 'oauth2';
  55. } else {
  56. return true;
  57. }
  58. }
  59. });
  60. Template.memberMenuPopup.events({
  61. 'click .js-open-bookmarks'(e) {
  62. e.preventDefault();
  63. if (Utils.isMiniScreen()) {
  64. FlowRouter.go('bookmarks');
  65. Popup.back();
  66. } else {
  67. Popup.open('bookmarksPopup')(e);
  68. }
  69. },
  70. 'click .js-my-cards'() {
  71. Popup.back();
  72. },
  73. 'click .js-due-cards'() {
  74. Popup.back();
  75. },
  76. 'click .js-open-archived-board'() {
  77. Modal.open('archivedBoards');
  78. },
  79. 'click .js-invite-people': Popup.open('invitePeople'),
  80. 'click .js-edit-profile': Popup.open('editProfile'),
  81. 'click .js-change-settings': Popup.open('changeSettings'),
  82. 'click .js-change-avatar': Popup.open('changeAvatar'),
  83. 'click .js-change-password': Popup.open('changePassword'),
  84. 'click .js-change-language': Popup.open('changeLanguage'),
  85. 'click .js-support': Popup.open('support'),
  86. 'click .js-notifications-drawer-toggle'() {
  87. Session.set('showNotificationsDrawer', !Session.get('showNotificationsDrawer'));
  88. },
  89. 'click .js-logout'(event) {
  90. event.preventDefault();
  91. AccountsTemplates.logout();
  92. },
  93. 'click .js-go-setting'() {
  94. Popup.back();
  95. },
  96. });
  97. BlazeComponent.extendComponent({
  98. onCreated() {
  99. Meteor.subscribe('setting');
  100. },
  101. }).register('editProfilePopup');
  102. Template.invitePeoplePopup.events({
  103. 'click a.js-toggle-board-choose'(event){
  104. let target = $(event.target);
  105. if (!target.hasClass('js-toggle-board-choose')) {
  106. target = target.parent();
  107. }
  108. const checkboxId = target.attr('id');
  109. $(`#${checkboxId} .materialCheckBox`).toggleClass('is-checked');
  110. $(`#${checkboxId}`).toggleClass('is-checked');
  111. },
  112. 'click button.js-email-invite'(event){
  113. const emails = $('#email-to-invite')
  114. .val()
  115. .toLowerCase()
  116. .trim()
  117. .split('\n')
  118. .join(',')
  119. .split(',');
  120. const boardsToInvite = [];
  121. $('.js-toggle-board-choose .materialCheckBox.is-checked').each(function() {
  122. boardsToInvite.push($(this).data('id'));
  123. });
  124. const validEmails = [];
  125. emails.forEach(email => {
  126. if (email && SimpleSchema.RegEx.Email.test(email.trim())) {
  127. validEmails.push(email.trim());
  128. }
  129. });
  130. if (validEmails.length) {
  131. Meteor.call('sendInvitation', validEmails, boardsToInvite, (_, rc) => {
  132. if (rc == 0) {
  133. let divInfos = document.getElementById("invite-people-infos");
  134. if(divInfos && divInfos !== undefined){
  135. divInfos.innerHTML = "<span style='color: green'>" + TAPi18n.__('invite-people-success') + "</span>";
  136. }
  137. }
  138. else{
  139. let divInfos = document.getElementById("invite-people-infos");
  140. if(divInfos && divInfos !== undefined){
  141. divInfos.innerHTML = "<span style='color: red'>" + TAPi18n.__('invite-people-error') + "</span>";
  142. }
  143. }
  144. // Popup.close();
  145. });
  146. }
  147. },
  148. });
  149. Template.editProfilePopup.helpers({
  150. allowEmailChange() {
  151. Meteor.call('AccountSettings.allowEmailChange', (_, result) => {
  152. if (result) {
  153. return true;
  154. } else {
  155. return false;
  156. }
  157. });
  158. },
  159. allowUserNameChange() {
  160. Meteor.call('AccountSettings.allowUserNameChange', (_, result) => {
  161. if (result) {
  162. return true;
  163. } else {
  164. return false;
  165. }
  166. });
  167. },
  168. allowUserDelete() {
  169. Meteor.call('AccountSettings.allowUserDelete', (_, result) => {
  170. if (result) {
  171. return true;
  172. } else {
  173. return false;
  174. }
  175. });
  176. },
  177. });
  178. Template.editProfilePopup.events({
  179. submit(event, templateInstance) {
  180. event.preventDefault();
  181. const fullname = templateInstance.find('.js-profile-fullname').value.trim();
  182. const username = templateInstance.find('.js-profile-username').value.trim();
  183. const initials = templateInstance.find('.js-profile-initials').value.trim();
  184. const email = templateInstance.find('.js-profile-email').value.trim();
  185. let isChangeUserName = false;
  186. let isChangeEmail = false;
  187. Users.update(Meteor.userId(), {
  188. $set: {
  189. 'profile.fullname': fullname,
  190. 'profile.initials': initials,
  191. },
  192. });
  193. isChangeUserName = username !== ReactiveCache.getCurrentUser().username;
  194. isChangeEmail =
  195. email.toLowerCase() !== ReactiveCache.getCurrentUser().emails[0].address.toLowerCase();
  196. if (isChangeUserName && isChangeEmail) {
  197. Meteor.call(
  198. 'setUsernameAndEmail',
  199. username,
  200. email.toLowerCase(),
  201. Meteor.userId(),
  202. function(error) {
  203. const usernameMessageElement = templateInstance.$('.username-taken');
  204. const emailMessageElement = templateInstance.$('.email-taken');
  205. if (error) {
  206. const errorElement = error.error;
  207. if (errorElement === 'username-already-taken') {
  208. usernameMessageElement.show();
  209. emailMessageElement.hide();
  210. } else if (errorElement === 'email-already-taken') {
  211. usernameMessageElement.hide();
  212. emailMessageElement.show();
  213. }
  214. } else {
  215. usernameMessageElement.hide();
  216. emailMessageElement.hide();
  217. Popup.back();
  218. }
  219. },
  220. );
  221. } else if (isChangeUserName) {
  222. Meteor.call('setUsername', username, Meteor.userId(), function(error) {
  223. const messageElement = templateInstance.$('.username-taken');
  224. if (error) {
  225. messageElement.show();
  226. } else {
  227. messageElement.hide();
  228. Popup.back();
  229. }
  230. });
  231. } else if (isChangeEmail) {
  232. Meteor.call('setEmail', email.toLowerCase(), Meteor.userId(), function(
  233. error,
  234. ) {
  235. const messageElement = templateInstance.$('.email-taken');
  236. if (error) {
  237. messageElement.show();
  238. } else {
  239. messageElement.hide();
  240. Popup.back();
  241. }
  242. });
  243. } else Popup.back();
  244. },
  245. 'click #deleteButton': Popup.afterConfirm('userDelete', function() {
  246. Popup.back();
  247. // Use secure server method for self-deletion
  248. Meteor.call('removeUser', Meteor.userId(), (error, result) => {
  249. if (error) {
  250. if (process.env.DEBUG === 'true') {
  251. console.error('Error removing user:', error);
  252. }
  253. alert('Error deleting account: ' + error.reason);
  254. } else {
  255. if (process.env.DEBUG === 'true') {
  256. console.log('User deleted successfully:', result);
  257. }
  258. AccountsTemplates.logout();
  259. }
  260. });
  261. }),
  262. });
  263. // XXX For some reason the useraccounts autofocus isnt working in this case.
  264. // See https://github.com/meteor-useraccounts/core/issues/384
  265. Template.changePasswordPopup.onRendered(function() {
  266. $('.at-pwd-form').show();
  267. this.find('#at-field-current_password').focus();
  268. });
  269. Template.changeLanguagePopup.helpers({
  270. languages() {
  271. return TAPi18n.getSupportedLanguages()
  272. .map(({ tag, name }) => ({ tag: tag, name }))
  273. .sort((a, b) => {
  274. if (a.name === b.name) {
  275. return 0;
  276. } else {
  277. return a.name > b.name ? 1 : -1;
  278. }
  279. });
  280. },
  281. isCurrentLanguage() {
  282. return this.tag === TAPi18n.getLanguage();
  283. },
  284. languageFlag() {
  285. const flagMap = {
  286. 'en': '🇺🇸', 'es': '🇪🇸', 'fr': '🇫🇷', 'de': '🇩🇪', 'it': '🇮🇹', 'pt': '🇵🇹', 'ru': '🇷🇺',
  287. 'ja': '🇯🇵', 'ko': '🇰🇷', 'zh': '🇨🇳', 'ar': '🇸🇦', 'hi': '🇮🇳', 'th': '🇹🇭', 'vi': '🇻🇳',
  288. 'tr': '🇹🇷', 'pl': '🇵🇱', 'nl': '🇳🇱', 'sv': '🇸🇪', 'da': '🇩🇰', 'no': '🇳🇴', 'fi': '🇫🇮',
  289. 'cs': '🇨🇿', 'hu': '🇭🇺', 'ro': '🇷🇴', 'bg': '🇧🇬', 'hr': '🇭🇷', 'sk': '🇸🇰', 'sl': '🇸🇮',
  290. 'et': '🇪🇪', 'lv': '🇱🇻', 'lt': '🇱🇹', 'el': '🇬🇷', 'he': '🇮🇱', 'uk': '🇺🇦', 'be': '🇧🇾',
  291. 'ca': '🇪🇸', 'eu': '🇪🇸', 'gl': '🇪🇸', 'cy': '🇬🇧', 'ga': '🇮🇪', 'mt': '🇲🇹', 'is': '🇮🇸',
  292. 'mk': '🇲🇰', 'sq': '🇦🇱', 'sr': '🇷🇸', 'bs': '🇧🇦', 'me': '🇲🇪', 'fa': '🇮🇷', 'ur': '🇵🇰',
  293. 'bn': '🇧🇩', 'ta': '🇮🇳', 'te': '🇮🇳', 'ml': '🇮🇳', 'kn': '🇮🇳', 'gu': '🇮🇳', 'pa': '🇮🇳',
  294. 'or': '🇮🇳', 'as': '🇮🇳', 'ne': '🇳🇵', 'si': '🇱🇰', 'my': '🇲🇲', 'km': '🇰🇭', 'lo': '🇱🇦',
  295. 'ka': '🇬🇪', 'hy': '🇦🇲', 'az': '🇦🇿', 'kk': '🇰🇿', 'ky': '🇰🇬', 'uz': '🇺🇿', 'mn': '🇲🇳',
  296. 'bo': '🇨🇳', 'dz': '🇧🇹', 'ug': '🇨🇳', 'ii': '🇨🇳', 'za': '🇨🇳', 'yue': '🇭🇰', 'zh-HK': '🇭🇰',
  297. 'zh-TW': '🇹🇼', 'zh-CN': '🇨🇳', 'id': '🇮🇩', 'ms': '🇲🇾', 'tl': '🇵🇭', 'ceb': '🇵🇭',
  298. 'haw': '🇺🇸', 'mi': '🇳🇿', 'sm': '🇼🇸', 'to': '🇹🇴', 'fj': '🇫🇯', 'ty': '🇵🇫', 'mg': '🇲🇬',
  299. 'sw': '🇹🇿', 'am': '🇪🇹', 'om': '🇪🇹', 'so': '🇸🇴', 'ti': '🇪🇷', 'ha': '🇳🇬', 'yo': '🇳🇬',
  300. 'ig': '🇳🇬', 'zu': '🇿🇦', 'xh': '🇿🇦', 'af': '🇿🇦', 'st': '🇿🇦', 'tn': '🇿🇦', 'ss': '🇿🇦',
  301. 've': '🇿🇦', 'ts': '🇿🇦', 'nr': '🇿🇦', 'nso': '🇿🇦', 'wo': '🇸🇳', 'ff': '🇸🇳', 'dy': '🇲🇱',
  302. 'bm': '🇲🇱', 'tw': '🇬🇭', 'ak': '🇬🇭', 'lg': '🇺🇬', 'rw': '🇷🇼', 'rn': '🇧🇮', 'ny': '🇲🇼',
  303. 'sn': '🇿🇼', 'nd': '🇿🇼'
  304. };
  305. return flagMap[this.tag] || '🌐';
  306. },
  307. });
  308. Template.changeLanguagePopup.events({
  309. 'click .js-set-language'(event) {
  310. Users.update(Meteor.userId(), {
  311. $set: {
  312. 'profile.language': this.tag,
  313. },
  314. });
  315. TAPi18n.setLanguage(this.tag);
  316. event.preventDefault();
  317. },
  318. });
  319. Template.changeSettingsPopup.helpers({
  320. rescueCardDescription() {
  321. const currentUser = ReactiveCache.getCurrentUser();
  322. if (currentUser) {
  323. return (currentUser.profile || {}).rescueCardDescription;
  324. } else if (window.localStorage.getItem('rescueCardDescription')) {
  325. return true;
  326. } else {
  327. return false;
  328. }
  329. },
  330. showCardsCountAt() {
  331. const currentUser = ReactiveCache.getCurrentUser();
  332. if (currentUser) {
  333. return currentUser.getLimitToShowCardsCount();
  334. } else {
  335. return window.localStorage.getItem('limitToShowCardsCount');
  336. }
  337. },
  338. weekDays(startDay) {
  339. return [
  340. TAPi18n.__('sunday'),
  341. TAPi18n.__('monday'),
  342. TAPi18n.__('tuesday'),
  343. TAPi18n.__('wednesday'),
  344. TAPi18n.__('thursday'),
  345. TAPi18n.__('friday'),
  346. TAPi18n.__('saturday'),
  347. ].map(function(day, index) {
  348. return { name: day, value: index, isSelected: index === startDay };
  349. });
  350. },
  351. startDayOfWeek() {
  352. currentUser = ReactiveCache.getCurrentUser();
  353. if (currentUser) {
  354. return currentUser.getStartDayOfWeek();
  355. } else {
  356. return window.localStorage.getItem('startDayOfWeek');
  357. }
  358. },
  359. });
  360. Template.changeSettingsPopup.events({
  361. 'keypress/paste #show-cards-count-at'() {
  362. let keyCode = event.keyCode;
  363. let charCode = String.fromCharCode(keyCode);
  364. let regex = new RegExp('[-0-9]');
  365. let ret = regex.test(charCode);
  366. return ret;
  367. },
  368. 'click .js-toggle-desktop-drag-handles'() {
  369. const currentUser = ReactiveCache.getCurrentUser();
  370. if (currentUser) {
  371. Meteor.call('toggleDesktopDragHandles');
  372. } else if (window.localStorage.getItem('showDesktopDragHandles')) {
  373. window.localStorage.removeItem('showDesktopDragHandles');
  374. } else {
  375. window.localStorage.setItem('showDesktopDragHandles', 'true');
  376. }
  377. },
  378. 'click .js-rescue-card-description'() {
  379. Meteor.call('toggleRescueCardDescription')
  380. },
  381. 'click .js-apply-user-settings'(event, templateInstance) {
  382. event.preventDefault();
  383. let minLimit = parseInt(
  384. templateInstance.$('#show-cards-count-at').val(),
  385. 10,
  386. );
  387. const startDay = parseInt(
  388. templateInstance.$('#start-day-of-week').val(),
  389. 10,
  390. );
  391. const currentUser = ReactiveCache.getCurrentUser();
  392. if (isNaN(minLimit) || minLimit < -1) {
  393. minLimit = -1;
  394. }
  395. if (!isNaN(minLimit)) {
  396. if (currentUser) {
  397. Meteor.call('changeLimitToShowCardsCount', minLimit);
  398. } else {
  399. window.localStorage.setItem('limitToShowCardsCount', minLimit);
  400. }
  401. }
  402. if (!isNaN(startDay)) {
  403. if (currentUser) {
  404. Meteor.call('changeStartDayOfWeek', startDay);
  405. } else {
  406. window.localStorage.setItem('startDayOfWeek', startDay);
  407. }
  408. }
  409. Popup.back();
  410. },
  411. });