settingBody.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. BlazeComponent.extendComponent({
  2. onCreated() {
  3. this.error = new ReactiveVar('');
  4. this.loading = new ReactiveVar(false);
  5. this.generalSetting = new ReactiveVar(true);
  6. this.emailSetting = new ReactiveVar(false);
  7. this.accountSetting = new ReactiveVar(false);
  8. this.announcementSetting = new ReactiveVar(false);
  9. this.layoutSetting = new ReactiveVar(false);
  10. Meteor.subscribe('setting');
  11. Meteor.subscribe('mailServer');
  12. Meteor.subscribe('accountSettings');
  13. Meteor.subscribe('announcements');
  14. },
  15. setError(error) {
  16. this.error.set(error);
  17. },
  18. setLoading(w) {
  19. this.loading.set(w);
  20. },
  21. checkField(selector) {
  22. const value = $(selector).val();
  23. if (!value || value.trim() === '') {
  24. $(selector).parents('li.smtp-form').addClass('has-error');
  25. throw Error('blank field');
  26. } else {
  27. return value;
  28. }
  29. },
  30. currentSetting() {
  31. return Settings.findOne();
  32. },
  33. boards() {
  34. return Boards.find({
  35. archived: false,
  36. 'members.userId': Meteor.userId(),
  37. 'members.isAdmin': true,
  38. }, {
  39. sort: ['title'],
  40. });
  41. },
  42. toggleRegistration() {
  43. this.setLoading(true);
  44. const registrationClosed = this.currentSetting().disableRegistration;
  45. Settings.update(Settings.findOne()._id, {$set: {disableRegistration: !registrationClosed}});
  46. this.setLoading(false);
  47. if (registrationClosed) {
  48. $('.invite-people').slideUp();
  49. } else {
  50. $('.invite-people').slideDown();
  51. }
  52. },
  53. toggleTLS() {
  54. $('#mail-server-tls').toggleClass('is-checked');
  55. },
  56. switchMenu(event) {
  57. const target = $(event.target);
  58. if (!target.hasClass('active')) {
  59. $('.side-menu li.active').removeClass('active');
  60. target.parent().addClass('active');
  61. const targetID = target.data('id');
  62. this.generalSetting.set('registration-setting' === targetID);
  63. this.emailSetting.set('email-setting' === targetID);
  64. this.accountSetting.set('account-setting' === targetID);
  65. this.announcementSetting.set('announcement-setting' === targetID);
  66. this.layoutSetting.set('layout-setting' === targetID);
  67. }
  68. },
  69. checkBoard(event) {
  70. let target = $(event.target);
  71. if (!target.hasClass('js-toggle-board-choose')) {
  72. target = target.parent();
  73. }
  74. const checkboxId = target.attr('id');
  75. $(`#${checkboxId} .materialCheckBox`).toggleClass('is-checked');
  76. $(`#${checkboxId}`).toggleClass('is-checked');
  77. },
  78. inviteThroughEmail() {
  79. const emails = $('#email-to-invite').val().trim().split('\n').join(',').split(',');
  80. const boardsToInvite = [];
  81. $('.js-toggle-board-choose .materialCheckBox.is-checked').each(function () {
  82. boardsToInvite.push($(this).data('id'));
  83. });
  84. const validEmails = [];
  85. emails.forEach((email) => {
  86. if (email && SimpleSchema.RegEx.Email.test(email.trim())) {
  87. validEmails.push(email.trim());
  88. }
  89. });
  90. if (validEmails.length) {
  91. this.setLoading(true);
  92. Meteor.call('sendInvitation', validEmails, boardsToInvite, () => {
  93. // if (!err) {
  94. // TODO - show more info to user
  95. // }
  96. this.setLoading(false);
  97. });
  98. }
  99. },
  100. saveMailServerInfo() {
  101. this.setLoading(true);
  102. $('li').removeClass('has-error');
  103. try {
  104. const host = this.checkField('#mail-server-host');
  105. const port = this.checkField('#mail-server-port');
  106. const username = $('#mail-server-username').val().trim();
  107. const password = $('#mail-server-password').val().trim();
  108. const from = this.checkField('#mail-server-from');
  109. const tls = $('#mail-server-tls.is-checked').length > 0;
  110. Settings.update(Settings.findOne()._id, {
  111. $set: {
  112. 'mailServer.host': host, 'mailServer.port': port, 'mailServer.username': username,
  113. 'mailServer.password': password, 'mailServer.enableTLS': tls, 'mailServer.from': from,
  114. },
  115. });
  116. } catch (e) {
  117. return;
  118. } finally {
  119. this.setLoading(false);
  120. }
  121. },
  122. saveLayout() {
  123. this.setLoading(true);
  124. $('li').removeClass('has-error');
  125. try {
  126. const productName = $('#product-name').val().trim();
  127. Settings.update(Settings.findOne()._id, {
  128. $set: {
  129. productName,
  130. },
  131. });
  132. } catch (e) {
  133. return;
  134. } finally {
  135. this.setLoading(false);
  136. }
  137. },
  138. sendSMTPTestEmail() {
  139. Meteor.call('sendSMTPTestEmail', (err, ret) => {
  140. if (!err && ret) { /* eslint-disable no-console */
  141. const message = `${TAPi18n.__(ret.message)}: ${ret.email}`;
  142. console.log(message);
  143. alert(message);
  144. } else {
  145. const reason = err.reason || '';
  146. const message = `${TAPi18n.__(err.error)}\n${reason}`;
  147. console.log(message, err);
  148. alert(message);
  149. }
  150. /* eslint-enable no-console */
  151. });
  152. },
  153. events() {
  154. return [{
  155. 'click a.js-toggle-registration': this.toggleRegistration,
  156. 'click a.js-toggle-tls': this.toggleTLS,
  157. 'click a.js-setting-menu': this.switchMenu,
  158. 'click a.js-toggle-board-choose': this.checkBoard,
  159. 'click button.js-email-invite': this.inviteThroughEmail,
  160. 'click button.js-save': this.saveMailServerInfo,
  161. 'click button.js-send-smtp-test-email': this.sendSMTPTestEmail,
  162. 'click button.js-save-layout': this.saveLayout,
  163. }];
  164. },
  165. }).register('setting');
  166. BlazeComponent.extendComponent({
  167. saveAccountsChange() {
  168. const allowEmailChange = ($('input[name=allowEmailChange]:checked').val() === 'true');
  169. const allowUserNameChange = ($('input[name=allowUserNameChange]:checked').val() === 'true');
  170. AccountSettings.update('accounts-allowEmailChange', {
  171. $set: {'booleanValue': allowEmailChange},
  172. });
  173. AccountSettings.update('accounts-allowUserNameChange', {
  174. $set: {'booleanValue': allowUserNameChange},
  175. });
  176. },
  177. allowEmailChange() {
  178. return AccountSettings.findOne('accounts-allowEmailChange').booleanValue;
  179. },
  180. allowUserNameChange() {
  181. return AccountSettings.findOne('accounts-allowUserNameChange').booleanValue;
  182. },
  183. events() {
  184. return [{
  185. 'click button.js-accounts-save': this.saveAccountsChange,
  186. }];
  187. },
  188. }).register('accountSettings');
  189. BlazeComponent.extendComponent({
  190. onCreated() {
  191. this.loading = new ReactiveVar(false);
  192. },
  193. setLoading(w) {
  194. this.loading.set(w);
  195. },
  196. currentSetting() {
  197. return Announcements.findOne();
  198. },
  199. saveMessage() {
  200. const message = $('#admin-announcement').val().trim();
  201. Announcements.update(Announcements.findOne()._id, {
  202. $set: {'body': message},
  203. });
  204. },
  205. toggleActive() {
  206. this.setLoading(true);
  207. const isActive = this.currentSetting().enabled;
  208. Announcements.update(Announcements.findOne()._id, {
  209. $set: {'enabled': !isActive},
  210. });
  211. this.setLoading(false);
  212. if (isActive) {
  213. $('.admin-announcement').slideUp();
  214. } else {
  215. $('.admin-announcement').slideDown();
  216. }
  217. },
  218. events() {
  219. return [{
  220. 'click a.js-toggle-activemessage': this.toggleActive,
  221. 'click button.js-announcement-save': this.saveMessage,
  222. }];
  223. },
  224. }).register('announcementSettings');