userHeader.js 15 KB

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