settingBody.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. Meteor.subscribe('setting');
  2. Meteor.subscribe('mailServer');
  3. BlazeComponent.extendComponent({
  4. onCreated() {
  5. this.error = new ReactiveVar('');
  6. this.loading = new ReactiveVar(false);
  7. this.generalSetting = new ReactiveVar(true);
  8. this.emailSetting = new ReactiveVar(false);
  9. },
  10. setError(error) {
  11. this.error.set(error);
  12. },
  13. setLoading(w) {
  14. this.loading.set(w);
  15. },
  16. checkField(selector) {
  17. const value = $(selector).val();
  18. if(!value || value.trim() === ''){
  19. $(selector).parents('li.smtp-form').addClass('has-error');
  20. throw Error('blank field');
  21. } else {
  22. return value;
  23. }
  24. },
  25. currentSetting(){
  26. return Settings.findOne();
  27. },
  28. boards() {
  29. return Boards.find({
  30. archived: false,
  31. 'members.userId': Meteor.userId(),
  32. 'members.isAdmin': true,
  33. }, {
  34. sort: ['title'],
  35. });
  36. },
  37. toggleRegistration(){
  38. this.setLoading(true);
  39. const registrationClosed = this.currentSetting().disableRegistration;
  40. Settings.update(Settings.findOne()._id, {$set:{disableRegistration: !registrationClosed}});
  41. this.setLoading(false);
  42. if(registrationClosed){
  43. $('.invite-people').slideUp();
  44. }else{
  45. $('.invite-people').slideDown();
  46. }
  47. },
  48. toggleTLS(){
  49. $('#mail-server-tls').toggleClass('is-checked');
  50. },
  51. switchMenu(event){
  52. const target = $(event.target);
  53. if(!target.hasClass('active')){
  54. $('.side-menu li.active').removeClass('active');
  55. target.parent().addClass('active');
  56. const targetID = target.data('id');
  57. this.generalSetting.set('registration-setting' === targetID);
  58. this.emailSetting.set('email-setting' === targetID);
  59. }
  60. },
  61. checkBoard(event){
  62. let target = $(event.target);
  63. if(!target.hasClass('js-toggle-board-choose')){
  64. target = target.parent();
  65. }
  66. const checkboxId = target.attr('id');
  67. $(`#${checkboxId} .materialCheckBox`).toggleClass('is-checked');
  68. $(`#${checkboxId}`).toggleClass('is-checked');
  69. },
  70. inviteThroughEmail(){
  71. const emails = $('#email-to-invite').val().trim().split('\n').join(',').split(',');
  72. const boardsToInvite = [];
  73. $('.js-toggle-board-choose .materialCheckBox.is-checked').each(function () {
  74. boardsToInvite.push($(this).data('id'));
  75. });
  76. const validEmails = [];
  77. emails.forEach((email) => {
  78. if (email && SimpleSchema.RegEx.Email.test(email.trim())) {
  79. validEmails.push(email.trim());
  80. }
  81. });
  82. if (validEmails.length) {
  83. this.setLoading(true);
  84. Meteor.call('sendInvitation', validEmails, boardsToInvite, () => {
  85. // if (!err) {
  86. // TODO - show more info to user
  87. // }
  88. this.setLoading(false);
  89. });
  90. }
  91. },
  92. saveMailServerInfo(){
  93. this.setLoading(true);
  94. $('li').removeClass('has-error');
  95. try{
  96. const host = this.checkField('#mail-server-host');
  97. const port = this.checkField('#mail-server-port');
  98. const username = $('#mail-server-username').val().trim();
  99. const password = $('#mail-server-password').val().trim();
  100. const from = this.checkField('#mail-server-from');
  101. const tls = $('#mail-server-tls.is-checked').length > 0;
  102. Settings.update(Settings.findOne()._id, {$set:{'mailServer.host':host, 'mailServer.port': port, 'mailServer.username': username,
  103. 'mailServer.password': password, 'mailServer.enableTLS': tls, 'mailServer.from': from}});
  104. } catch (e) {
  105. return;
  106. } finally {
  107. this.setLoading(false);
  108. }
  109. },
  110. events(){
  111. return [{
  112. 'click a.js-toggle-registration': this.toggleRegistration,
  113. 'click a.js-toggle-tls': this.toggleTLS,
  114. 'click a.js-setting-menu': this.switchMenu,
  115. 'click a.js-toggle-board-choose': this.checkBoard,
  116. 'click button.js-email-invite': this.inviteThroughEmail,
  117. 'click button.js-save': this.saveMailServerInfo,
  118. }];
  119. },
  120. }).register('setting');