settingBody.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. toggleHideLogo() {
  57. $('#hide-logo').toggleClass('is-checked');
  58. },
  59. switchMenu(event) {
  60. const target = $(event.target);
  61. if (!target.hasClass('active')) {
  62. $('.side-menu li.active').removeClass('active');
  63. target.parent().addClass('active');
  64. const targetID = target.data('id');
  65. this.generalSetting.set('registration-setting' === targetID);
  66. this.emailSetting.set('email-setting' === targetID);
  67. this.accountSetting.set('account-setting' === targetID);
  68. this.announcementSetting.set('announcement-setting' === targetID);
  69. this.layoutSetting.set('layout-setting' === targetID);
  70. }
  71. },
  72. checkBoard(event) {
  73. let target = $(event.target);
  74. if (!target.hasClass('js-toggle-board-choose')) {
  75. target = target.parent();
  76. }
  77. const checkboxId = target.attr('id');
  78. $(`#${checkboxId} .materialCheckBox`).toggleClass('is-checked');
  79. $(`#${checkboxId}`).toggleClass('is-checked');
  80. },
  81. inviteThroughEmail() {
  82. const emails = $('#email-to-invite').val().trim().split('\n').join(',').split(',');
  83. const boardsToInvite = [];
  84. $('.js-toggle-board-choose .materialCheckBox.is-checked').each(function () {
  85. boardsToInvite.push($(this).data('id'));
  86. });
  87. const validEmails = [];
  88. emails.forEach((email) => {
  89. if (email && SimpleSchema.RegEx.Email.test(email.trim())) {
  90. validEmails.push(email.trim());
  91. }
  92. });
  93. if (validEmails.length) {
  94. this.setLoading(true);
  95. Meteor.call('sendInvitation', validEmails, boardsToInvite, () => {
  96. // if (!err) {
  97. // TODO - show more info to user
  98. // }
  99. this.setLoading(false);
  100. });
  101. }
  102. },
  103. saveMailServerInfo() {
  104. this.setLoading(true);
  105. $('li').removeClass('has-error');
  106. try {
  107. const host = this.checkField('#mail-server-host');
  108. const port = this.checkField('#mail-server-port');
  109. const username = $('#mail-server-username').val().trim();
  110. const password = $('#mail-server-password').val().trim();
  111. const from = this.checkField('#mail-server-from');
  112. const tls = $('#mail-server-tls.is-checked').length > 0;
  113. Settings.update(Settings.findOne()._id, {
  114. $set: {
  115. 'mailServer.host': host, 'mailServer.port': port, 'mailServer.username': username,
  116. 'mailServer.password': password, 'mailServer.enableTLS': tls, 'mailServer.from': from,
  117. },
  118. });
  119. } catch (e) {
  120. return;
  121. } finally {
  122. this.setLoading(false);
  123. }
  124. },
  125. saveLayout() {
  126. this.setLoading(true);
  127. $('li').removeClass('has-error');
  128. const productName = $('#product-name').val().trim();
  129. const hideLogoChange = ($('input[name=hideLogo]:checked').val() === 'true');
  130. try {
  131. Settings.update(Settings.findOne()._id, {
  132. $set: {
  133. productName,
  134. hideLogo: hideLogoChange,
  135. },
  136. });
  137. } catch (e) {
  138. return;
  139. } finally {
  140. this.setLoading(false);
  141. }
  142. DocHead.setTitle(productName);
  143. saveMailServerInfo();
  144. },
  145. sendSMTPTestEmail() {
  146. Meteor.call('sendSMTPTestEmail', (err, ret) => {
  147. if (!err && ret) { /* eslint-disable no-console */
  148. const message = `${TAPi18n.__(ret.message)}: ${ret.email}`;
  149. console.log(message);
  150. alert(message);
  151. } else {
  152. const reason = err.reason || '';
  153. const message = `${TAPi18n.__(err.error)}\n${reason}`;
  154. console.log(message, err);
  155. alert(message);
  156. }
  157. /* eslint-enable no-console */
  158. });
  159. },
  160. events() {
  161. return [{
  162. 'click a.js-toggle-registration': this.toggleRegistration,
  163. 'click a.js-toggle-tls': this.toggleTLS,
  164. 'click a.js-setting-menu': this.switchMenu,
  165. 'click a.js-toggle-board-choose': this.checkBoard,
  166. 'click button.js-email-invite': this.inviteThroughEmail,
  167. 'click button.js-save': this.saveMailServerInfo,
  168. 'click button.js-send-smtp-test-email': this.sendSMTPTestEmail,
  169. 'click a.js-toggle-hide-logo': this.toggleHideLogo,
  170. 'click button.js-save-layout': this.saveLayout,
  171. }];
  172. },
  173. }).register('setting');
  174. BlazeComponent.extendComponent({
  175. saveAccountsChange() {
  176. const allowEmailChange = ($('input[name=allowEmailChange]:checked').val() === 'true');
  177. const allowUserNameChange = ($('input[name=allowUserNameChange]:checked').val() === 'true');
  178. AccountSettings.update('accounts-allowEmailChange', {
  179. $set: {'booleanValue': allowEmailChange},
  180. });
  181. AccountSettings.update('accounts-allowUserNameChange', {
  182. $set: {'booleanValue': allowUserNameChange},
  183. });
  184. },
  185. allowEmailChange() {
  186. return AccountSettings.findOne('accounts-allowEmailChange').booleanValue;
  187. },
  188. allowUserNameChange() {
  189. return AccountSettings.findOne('accounts-allowUserNameChange').booleanValue;
  190. },
  191. events() {
  192. return [{
  193. 'click button.js-accounts-save': this.saveAccountsChange,
  194. }];
  195. },
  196. }).register('accountSettings');
  197. BlazeComponent.extendComponent({
  198. onCreated() {
  199. this.loading = new ReactiveVar(false);
  200. },
  201. setLoading(w) {
  202. this.loading.set(w);
  203. },
  204. currentSetting() {
  205. return Announcements.findOne();
  206. },
  207. saveMessage() {
  208. const message = $('#admin-announcement').val().trim();
  209. Announcements.update(Announcements.findOne()._id, {
  210. $set: {'body': message},
  211. });
  212. },
  213. toggleActive() {
  214. this.setLoading(true);
  215. const isActive = this.currentSetting().enabled;
  216. Announcements.update(Announcements.findOne()._id, {
  217. $set: {'enabled': !isActive},
  218. });
  219. this.setLoading(false);
  220. if (isActive) {
  221. $('.admin-announcement').slideUp();
  222. } else {
  223. $('.admin-announcement').slideDown();
  224. }
  225. },
  226. events() {
  227. return [{
  228. 'click a.js-toggle-activemessage': this.toggleActive,
  229. 'click button.js-announcement-save': this.saveMessage,
  230. }];
  231. },
  232. }).register('announcementSettings');