userHeader.js 14 KB

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