settingBody.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. toggleStrictMode(){
  38. this.setLoading(true);
  39. const isStrictMode = this.currentSetting().strict;
  40. Settings.update(Settings.findOne()._id, {$set:{strict: !isStrictMode}});
  41. this.setLoading(false);
  42. if(isStrictMode){
  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('general-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. this.setLoading(true);
  69. const emails = $('#email-to-invite').val().trim().split('\n').join(',').split(',');
  70. const boardsToInvite = [];
  71. $('.js-toggle-board-choose .materialCheckBox.is-checked').each(function () {
  72. boardsToInvite.push($(this).data('id'));
  73. });
  74. const validEmails = [];
  75. emails.forEach((email) => {
  76. if (email && SimpleSchema.RegEx.Email.test(email.trim())) {
  77. validEmails.push(email.trim());
  78. }
  79. });
  80. Meteor.call('sendInvitation', validEmails, boardsToInvite, () => {
  81. // if (!err) {
  82. // TODO - show more info to user
  83. // }
  84. this.setLoading(false);
  85. });
  86. },
  87. saveMailServerInfo(){
  88. this.setLoading(true);
  89. $('li').removeClass('has-error');
  90. try{
  91. const host = this.checkField('#mail-server-host');
  92. const port = this.checkField('#mail-server-port');
  93. const username = this.checkField('#mail-server-username');
  94. const password = this.checkField('#mail-server-password');
  95. const from = this.checkField('#mail-server-from');
  96. Settings.update(Settings.findOne()._id, {$set:{'mailServer.host':host, 'mailServer.port': port, 'mailServer.username': username,
  97. 'mailServer.password': password, 'mailServer.from': from}});
  98. } catch (e) {
  99. return;
  100. } finally {
  101. this.setLoading(false);
  102. }
  103. },
  104. events(){
  105. return [{
  106. 'click a.js-toggle-strict-mode': this.toggleStrictMode,
  107. 'click a.js-setting-menu': this.switchMenu,
  108. 'click a.js-toggle-board-choose': this.checkBoard,
  109. 'click button.js-email-invite': this.inviteThroughEmail,
  110. 'click button.js-save': this.saveMailServerInfo,
  111. }];
  112. },
  113. }).register('setting');