userHeader.js 14 KB

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