userHeader.js 11 KB

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