userHeader.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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-my-cards'() {
  62. Popup.back();
  63. },
  64. 'click .js-due-cards'() {
  65. Popup.back();
  66. },
  67. 'click .js-open-archived-board'() {
  68. Modal.open('archivedBoards');
  69. },
  70. 'click .js-invite-people': Popup.open('invitePeople'),
  71. 'click .js-edit-profile': Popup.open('editProfile'),
  72. 'click .js-change-settings': Popup.open('changeSettings'),
  73. 'click .js-change-avatar': Popup.open('changeAvatar'),
  74. 'click .js-change-password': Popup.open('changePassword'),
  75. 'click .js-change-language': Popup.open('changeLanguage'),
  76. 'click .js-support': Popup.open('support'),
  77. 'click .js-logout'(event) {
  78. event.preventDefault();
  79. AccountsTemplates.logout();
  80. },
  81. 'click .js-go-setting'() {
  82. Popup.back();
  83. },
  84. });
  85. BlazeComponent.extendComponent({
  86. onCreated() {
  87. Meteor.subscribe('setting');
  88. },
  89. }).register('editProfilePopup');
  90. Template.invitePeoplePopup.events({
  91. 'click a.js-toggle-board-choose'(event){
  92. let target = $(event.target);
  93. if (!target.hasClass('js-toggle-board-choose')) {
  94. target = target.parent();
  95. }
  96. const checkboxId = target.attr('id');
  97. $(`#${checkboxId} .materialCheckBox`).toggleClass('is-checked');
  98. $(`#${checkboxId}`).toggleClass('is-checked');
  99. },
  100. 'click button.js-email-invite'(event){
  101. const emails = $('#email-to-invite')
  102. .val()
  103. .toLowerCase()
  104. .trim()
  105. .split('\n')
  106. .join(',')
  107. .split(',');
  108. const boardsToInvite = [];
  109. $('.js-toggle-board-choose .materialCheckBox.is-checked').each(function() {
  110. boardsToInvite.push($(this).data('id'));
  111. });
  112. const validEmails = [];
  113. emails.forEach(email => {
  114. if (email && SimpleSchema.RegEx.Email.test(email.trim())) {
  115. validEmails.push(email.trim());
  116. }
  117. });
  118. if (validEmails.length) {
  119. Meteor.call('sendInvitation', validEmails, boardsToInvite, (_, rc) => {
  120. if (rc == 0) {
  121. let divInfos = document.getElementById("invite-people-infos");
  122. if(divInfos && divInfos !== undefined){
  123. divInfos.innerHTML = "<span style='color: green'>" + TAPi18n.__('invite-people-success') + "</span>";
  124. }
  125. }
  126. else{
  127. let divInfos = document.getElementById("invite-people-infos");
  128. if(divInfos && divInfos !== undefined){
  129. divInfos.innerHTML = "<span style='color: red'>" + TAPi18n.__('invite-people-error') + "</span>";
  130. }
  131. }
  132. // Popup.close();
  133. });
  134. }
  135. },
  136. });
  137. Template.editProfilePopup.helpers({
  138. allowEmailChange() {
  139. Meteor.call('AccountSettings.allowEmailChange', (_, result) => {
  140. if (result) {
  141. return true;
  142. } else {
  143. return false;
  144. }
  145. });
  146. },
  147. allowUserNameChange() {
  148. Meteor.call('AccountSettings.allowUserNameChange', (_, result) => {
  149. if (result) {
  150. return true;
  151. } else {
  152. return false;
  153. }
  154. });
  155. },
  156. allowUserDelete() {
  157. Meteor.call('AccountSettings.allowUserDelete', (_, result) => {
  158. if (result) {
  159. return true;
  160. } else {
  161. return false;
  162. }
  163. });
  164. },
  165. });
  166. Template.editProfilePopup.events({
  167. submit(event, templateInstance) {
  168. event.preventDefault();
  169. const fullname = templateInstance.find('.js-profile-fullname').value.trim();
  170. const username = templateInstance.find('.js-profile-username').value.trim();
  171. const initials = templateInstance.find('.js-profile-initials').value.trim();
  172. const email = templateInstance.find('.js-profile-email').value.trim();
  173. let isChangeUserName = false;
  174. let isChangeEmail = false;
  175. Users.update(Meteor.userId(), {
  176. $set: {
  177. 'profile.fullname': fullname,
  178. 'profile.initials': initials,
  179. },
  180. });
  181. isChangeUserName = username !== ReactiveCache.getCurrentUser().username;
  182. isChangeEmail =
  183. email.toLowerCase() !== ReactiveCache.getCurrentUser().emails[0].address.toLowerCase();
  184. if (isChangeUserName && isChangeEmail) {
  185. Meteor.call(
  186. 'setUsernameAndEmail',
  187. username,
  188. email.toLowerCase(),
  189. Meteor.userId(),
  190. function(error) {
  191. const usernameMessageElement = templateInstance.$('.username-taken');
  192. const emailMessageElement = templateInstance.$('.email-taken');
  193. if (error) {
  194. const errorElement = error.error;
  195. if (errorElement === 'username-already-taken') {
  196. usernameMessageElement.show();
  197. emailMessageElement.hide();
  198. } else if (errorElement === 'email-already-taken') {
  199. usernameMessageElement.hide();
  200. emailMessageElement.show();
  201. }
  202. } else {
  203. usernameMessageElement.hide();
  204. emailMessageElement.hide();
  205. Popup.back();
  206. }
  207. },
  208. );
  209. } else if (isChangeUserName) {
  210. Meteor.call('setUsername', username, Meteor.userId(), function(error) {
  211. const messageElement = templateInstance.$('.username-taken');
  212. if (error) {
  213. messageElement.show();
  214. } else {
  215. messageElement.hide();
  216. Popup.back();
  217. }
  218. });
  219. } else if (isChangeEmail) {
  220. Meteor.call('setEmail', email.toLowerCase(), Meteor.userId(), function(
  221. error,
  222. ) {
  223. const messageElement = templateInstance.$('.email-taken');
  224. if (error) {
  225. messageElement.show();
  226. } else {
  227. messageElement.hide();
  228. Popup.back();
  229. }
  230. });
  231. } else Popup.back();
  232. },
  233. 'click #deleteButton': Popup.afterConfirm('userDelete', function() {
  234. Popup.back();
  235. // Use secure server method for self-deletion
  236. Meteor.call('removeUser', Meteor.userId(), (error, result) => {
  237. if (error) {
  238. if (process.env.DEBUG === 'true') {
  239. console.error('Error removing user:', error);
  240. }
  241. alert('Error deleting account: ' + error.reason);
  242. } else {
  243. if (process.env.DEBUG === 'true') {
  244. console.log('User deleted successfully:', result);
  245. }
  246. AccountsTemplates.logout();
  247. }
  248. });
  249. }),
  250. });
  251. // XXX For some reason the useraccounts autofocus isnt working in this case.
  252. // See https://github.com/meteor-useraccounts/core/issues/384
  253. Template.changePasswordPopup.onRendered(function() {
  254. $('.at-pwd-form').show();
  255. this.find('#at-field-current_password').focus();
  256. });
  257. Template.changeLanguagePopup.helpers({
  258. languages() {
  259. return TAPi18n.getSupportedLanguages()
  260. .map(({ tag, name }) => ({ tag: tag, name }))
  261. .sort((a, b) => {
  262. if (a.name === b.name) {
  263. return 0;
  264. } else {
  265. return a.name > b.name ? 1 : -1;
  266. }
  267. });
  268. },
  269. isCurrentLanguage() {
  270. return this.tag === TAPi18n.getLanguage();
  271. },
  272. });
  273. Template.changeLanguagePopup.events({
  274. 'click .js-set-language'(event) {
  275. Users.update(Meteor.userId(), {
  276. $set: {
  277. 'profile.language': this.tag,
  278. },
  279. });
  280. TAPi18n.setLanguage(this.tag);
  281. event.preventDefault();
  282. },
  283. });
  284. Template.changeSettingsPopup.helpers({
  285. rescueCardDescription() {
  286. const currentUser = ReactiveCache.getCurrentUser();
  287. if (currentUser) {
  288. return (currentUser.profile || {}).rescueCardDescription;
  289. } else if (window.localStorage.getItem('rescueCardDescription')) {
  290. return true;
  291. } else {
  292. return false;
  293. }
  294. },
  295. showCardsCountAt() {
  296. const currentUser = ReactiveCache.getCurrentUser();
  297. if (currentUser) {
  298. return currentUser.getLimitToShowCardsCount();
  299. } else {
  300. return window.localStorage.getItem('limitToShowCardsCount');
  301. }
  302. },
  303. weekDays(startDay) {
  304. return [
  305. TAPi18n.__('sunday'),
  306. TAPi18n.__('monday'),
  307. TAPi18n.__('tuesday'),
  308. TAPi18n.__('wednesday'),
  309. TAPi18n.__('thursday'),
  310. TAPi18n.__('friday'),
  311. TAPi18n.__('saturday'),
  312. ].map(function(day, index) {
  313. return { name: day, value: index, isSelected: index === startDay };
  314. });
  315. },
  316. startDayOfWeek() {
  317. currentUser = ReactiveCache.getCurrentUser();
  318. if (currentUser) {
  319. return currentUser.getStartDayOfWeek();
  320. } else {
  321. return window.localStorage.getItem('startDayOfWeek');
  322. }
  323. },
  324. });
  325. Template.changeSettingsPopup.events({
  326. 'keypress/paste #show-cards-count-at'() {
  327. let keyCode = event.keyCode;
  328. let charCode = String.fromCharCode(keyCode);
  329. let regex = new RegExp('[-0-9]');
  330. let ret = regex.test(charCode);
  331. return ret;
  332. },
  333. 'click .js-toggle-desktop-drag-handles'() {
  334. const currentUser = ReactiveCache.getCurrentUser();
  335. if (currentUser) {
  336. Meteor.call('toggleDesktopDragHandles');
  337. } else if (window.localStorage.getItem('showDesktopDragHandles')) {
  338. window.localStorage.removeItem('showDesktopDragHandles');
  339. } else {
  340. window.localStorage.setItem('showDesktopDragHandles', 'true');
  341. }
  342. },
  343. 'click .js-rescue-card-description'() {
  344. Meteor.call('toggleRescueCardDescription')
  345. },
  346. 'click .js-apply-user-settings'(event, templateInstance) {
  347. event.preventDefault();
  348. let minLimit = parseInt(
  349. templateInstance.$('#show-cards-count-at').val(),
  350. 10,
  351. );
  352. const startDay = parseInt(
  353. templateInstance.$('#start-day-of-week').val(),
  354. 10,
  355. );
  356. const currentUser = ReactiveCache.getCurrentUser();
  357. if (isNaN(minLimit) || minLimit < -1) {
  358. minLimit = -1;
  359. }
  360. if (!isNaN(minLimit)) {
  361. if (currentUser) {
  362. Meteor.call('changeLimitToShowCardsCount', minLimit);
  363. } else {
  364. window.localStorage.setItem('limitToShowCardsCount', minLimit);
  365. }
  366. }
  367. if (!isNaN(startDay)) {
  368. if (currentUser) {
  369. Meteor.call('changeStartDayOfWeek', startDay);
  370. } else {
  371. window.localStorage.setItem('startDayOfWeek', startDay);
  372. }
  373. }
  374. Popup.back();
  375. },
  376. });