userHeader.js 12 KB

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