userHeader.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 = Settings.findOne();
  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.invitePeoplePopup.helpers({
  136. currentSetting() {
  137. return Settings.findOne();
  138. },
  139. });
  140. Template.editProfilePopup.helpers({
  141. allowEmailChange() {
  142. Meteor.call('AccountSettings.allowEmailChange', (_, result) => {
  143. if (result) {
  144. return true;
  145. } else {
  146. return false;
  147. }
  148. });
  149. },
  150. allowUserNameChange() {
  151. Meteor.call('AccountSettings.allowUserNameChange', (_, result) => {
  152. if (result) {
  153. return true;
  154. } else {
  155. return false;
  156. }
  157. });
  158. },
  159. allowUserDelete() {
  160. Meteor.call('AccountSettings.allowUserDelete', (_, result) => {
  161. if (result) {
  162. return true;
  163. } else {
  164. return false;
  165. }
  166. });
  167. },
  168. });
  169. Template.editProfilePopup.events({
  170. submit(event, templateInstance) {
  171. event.preventDefault();
  172. const fullname = templateInstance.find('.js-profile-fullname').value.trim();
  173. const username = templateInstance.find('.js-profile-username').value.trim();
  174. const initials = templateInstance.find('.js-profile-initials').value.trim();
  175. const email = templateInstance.find('.js-profile-email').value.trim();
  176. let isChangeUserName = false;
  177. let isChangeEmail = false;
  178. Users.update(Meteor.userId(), {
  179. $set: {
  180. 'profile.fullname': fullname,
  181. 'profile.initials': initials,
  182. },
  183. });
  184. isChangeUserName = username !== Meteor.user().username;
  185. isChangeEmail =
  186. email.toLowerCase() !== Meteor.user().emails[0].address.toLowerCase();
  187. if (isChangeUserName && isChangeEmail) {
  188. Meteor.call(
  189. 'setUsernameAndEmail',
  190. username,
  191. email.toLowerCase(),
  192. Meteor.userId(),
  193. function(error) {
  194. const usernameMessageElement = templateInstance.$('.username-taken');
  195. const emailMessageElement = templateInstance.$('.email-taken');
  196. if (error) {
  197. const errorElement = error.error;
  198. if (errorElement === 'username-already-taken') {
  199. usernameMessageElement.show();
  200. emailMessageElement.hide();
  201. } else if (errorElement === 'email-already-taken') {
  202. usernameMessageElement.hide();
  203. emailMessageElement.show();
  204. }
  205. } else {
  206. usernameMessageElement.hide();
  207. emailMessageElement.hide();
  208. Popup.back();
  209. }
  210. },
  211. );
  212. } else if (isChangeUserName) {
  213. Meteor.call('setUsername', username, Meteor.userId(), function(error) {
  214. const messageElement = templateInstance.$('.username-taken');
  215. if (error) {
  216. messageElement.show();
  217. } else {
  218. messageElement.hide();
  219. Popup.back();
  220. }
  221. });
  222. } else if (isChangeEmail) {
  223. Meteor.call('setEmail', email.toLowerCase(), Meteor.userId(), function(
  224. error,
  225. ) {
  226. const messageElement = templateInstance.$('.email-taken');
  227. if (error) {
  228. messageElement.show();
  229. } else {
  230. messageElement.hide();
  231. Popup.back();
  232. }
  233. });
  234. } else Popup.back();
  235. },
  236. 'click #deleteButton': Popup.afterConfirm('userDelete', function() {
  237. Popup.back();
  238. Users.remove(Meteor.userId());
  239. AccountsTemplates.logout();
  240. }),
  241. });
  242. // XXX For some reason the useraccounts autofocus isnt working in this case.
  243. // See https://github.com/meteor-useraccounts/core/issues/384
  244. Template.changePasswordPopup.onRendered(function() {
  245. this.find('#at-field-current_password').focus();
  246. });
  247. Template.changeLanguagePopup.helpers({
  248. languages() {
  249. return TAPi18n.getSupportedLanguages()
  250. .map(({ isoCode, name }) => ({ tag: isoCode, name }))
  251. .sort((a, b) => {
  252. if (a.name === b.name) {
  253. return 0;
  254. } else {
  255. return a.name > b.name ? 1 : -1;
  256. }
  257. });
  258. },
  259. isCurrentLanguage() {
  260. return this.tag === TAPi18n.getLanguage();
  261. },
  262. });
  263. Template.changeLanguagePopup.events({
  264. 'click .js-set-language'(event) {
  265. Users.update(Meteor.userId(), {
  266. $set: {
  267. 'profile.language': this.tag,
  268. },
  269. });
  270. TAPi18n.setLanguage(this.tag);
  271. event.preventDefault();
  272. },
  273. });
  274. Template.changeSettingsPopup.helpers({
  275. hiddenSystemMessages() {
  276. currentUser = Meteor.user();
  277. if (currentUser) {
  278. return (currentUser.profile || {}).hasHiddenSystemMessages;
  279. } else if (window.localStorage.getItem('hasHiddenSystemMessages')) {
  280. return true;
  281. } else {
  282. return false;
  283. }
  284. },
  285. showCardsCountAt() {
  286. currentUser = Meteor.user();
  287. if (currentUser) {
  288. return Meteor.user().getLimitToShowCardsCount();
  289. } else {
  290. return window.localStorage.getItem('limitToShowCardsCount');
  291. }
  292. },
  293. weekDays(startDay) {
  294. return [
  295. TAPi18n.__('sunday'),
  296. TAPi18n.__('monday'),
  297. TAPi18n.__('tuesday'),
  298. TAPi18n.__('wednesday'),
  299. TAPi18n.__('thursday'),
  300. TAPi18n.__('friday'),
  301. TAPi18n.__('saturday'),
  302. ].map(function(day, index) {
  303. return { name: day, value: index, isSelected: index === startDay };
  304. });
  305. },
  306. startDayOfWeek() {
  307. currentUser = Meteor.user();
  308. if (currentUser) {
  309. return currentUser.getStartDayOfWeek();
  310. } else {
  311. return window.localStorage.getItem('startDayOfWeek');
  312. }
  313. },
  314. });
  315. Template.changeSettingsPopup.events({
  316. 'keypress/paste #show-cards-count-at'() {
  317. let keyCode = event.keyCode;
  318. let charCode = String.fromCharCode(keyCode);
  319. let regex = new RegExp('[-0-9]');
  320. let ret = regex.test(charCode);
  321. return ret;
  322. },
  323. 'click .js-toggle-desktop-drag-handles'() {
  324. currentUser = Meteor.user();
  325. if (currentUser) {
  326. Meteor.call('toggleDesktopDragHandles');
  327. } else if (window.localStorage.getItem('showDesktopDragHandles')) {
  328. window.localStorage.removeItem('showDesktopDragHandles');
  329. } else {
  330. window.localStorage.setItem('showDesktopDragHandles', 'true');
  331. }
  332. },
  333. 'click .js-toggle-system-messages'() {
  334. currentUser = Meteor.user();
  335. if (currentUser) {
  336. Meteor.call('toggleSystemMessages');
  337. } else if (window.localStorage.getItem('hasHiddenSystemMessages')) {
  338. window.localStorage.removeItem('hasHiddenSystemMessages');
  339. } else {
  340. window.localStorage.setItem('hasHiddenSystemMessages', 'true');
  341. }
  342. },
  343. 'click .js-apply-user-settings'(event, templateInstance) {
  344. event.preventDefault();
  345. let minLimit = parseInt(
  346. templateInstance.$('#show-cards-count-at').val(),
  347. 10,
  348. );
  349. const startDay = parseInt(
  350. templateInstance.$('#start-day-of-week').val(),
  351. 10,
  352. );
  353. const currentUser = Meteor.user();
  354. if (isNaN(minLimit) || minLimit < -1) {
  355. minLimit = -1;
  356. }
  357. if (!isNaN(minLimit)) {
  358. if (currentUser) {
  359. Meteor.call('changeLimitToShowCardsCount', minLimit);
  360. } else {
  361. window.localStorage.setItem('limitToShowCardsCount', minLimit);
  362. }
  363. }
  364. if (!isNaN(startDay)) {
  365. if (currentUser) {
  366. Meteor.call('changeStartDayOfWeek', startDay);
  367. } else {
  368. window.localStorage.setItem('startDayOfWeek', startDay);
  369. }
  370. }
  371. Popup.back();
  372. },
  373. });