settingBody.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. switchMenu(event){
  49. const target = $(event.target);
  50. if(!target.hasClass('active')){
  51. $('.side-menu li.active').removeClass('active');
  52. target.parent().addClass('active');
  53. const targetID = target.data('id');
  54. this.generalSetting.set('registration-setting' === targetID);
  55. this.emailSetting.set('email-setting' === targetID);
  56. }
  57. },
  58. checkBoard(event){
  59. let target = $(event.target);
  60. if(!target.hasClass('js-toggle-board-choose')){
  61. target = target.parent();
  62. }
  63. const checkboxId = target.attr('id');
  64. $(`#${checkboxId} .materialCheckBox`).toggleClass('is-checked');
  65. $(`#${checkboxId}`).toggleClass('is-checked');
  66. },
  67. inviteThroughEmail(){
  68. const emails = $('#email-to-invite').val().trim().split('\n').join(',').split(',');
  69. const boardsToInvite = [];
  70. $('.js-toggle-board-choose .materialCheckBox.is-checked').each(function () {
  71. boardsToInvite.push($(this).data('id'));
  72. });
  73. const validEmails = [];
  74. emails.forEach((email) => {
  75. if (email && SimpleSchema.RegEx.Email.test(email.trim())) {
  76. validEmails.push(email.trim());
  77. }
  78. });
  79. if (validEmails.length) {
  80. this.setLoading(true);
  81. Meteor.call('sendInvitation', validEmails, boardsToInvite, () => {
  82. // if (!err) {
  83. // TODO - show more info to user
  84. // }
  85. this.setLoading(false);
  86. });
  87. }
  88. },
  89. saveMailServerInfo(){
  90. this.setLoading(true);
  91. $('li').removeClass('has-error');
  92. try{
  93. const host = this.checkField('#mail-server-host');
  94. const port = this.checkField('#mail-server-port');
  95. const username = $('#mail-server-username').val().trim();
  96. const password = $('#mail-server-password').val().trim();
  97. const from = this.checkField('#mail-server-from');
  98. Settings.update(Settings.findOne()._id, {$set:{'mailServer.host':host, 'mailServer.port': port, 'mailServer.username': username,
  99. 'mailServer.password': password, 'mailServer.from': from}});
  100. } catch (e) {
  101. return;
  102. } finally {
  103. this.setLoading(false);
  104. }
  105. },
  106. events(){
  107. return [{
  108. 'click a.js-toggle-registration': this.toggleRegistration,
  109. 'click a.js-setting-menu': this.switchMenu,
  110. 'click a.js-toggle-board-choose': this.checkBoard,
  111. 'click button.js-email-invite': this.inviteThroughEmail,
  112. 'click button.js-save': this.saveMailServerInfo,
  113. }];
  114. },
  115. }).register('setting');