settingBody.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. const customHTMLafterBodyStart = $('#customHTMLafterBodyStart').val().trim();
  131. const customHTMLbeforeBodyEnd = $('#customHTMLbeforeBodyEnd').val().trim();
  132. try {
  133. Settings.update(Settings.findOne()._id, {
  134. $set: {
  135. productName,
  136. hideLogo: hideLogoChange,
  137. customHTMLafterBodyStart,
  138. customHTMLbeforeBodyEnd,
  139. },
  140. });
  141. } catch (e) {
  142. return;
  143. } finally {
  144. this.setLoading(false);
  145. }
  146. DocHead.setTitle(productName);
  147. },
  148. sendSMTPTestEmail() {
  149. Meteor.call('sendSMTPTestEmail', (err, ret) => {
  150. if (!err && ret) { /* eslint-disable no-console */
  151. const message = `${TAPi18n.__(ret.message)}: ${ret.email}`;
  152. console.log(message);
  153. alert(message);
  154. } else {
  155. const reason = err.reason || '';
  156. const message = `${TAPi18n.__(err.error)}\n${reason}`;
  157. console.log(message, err);
  158. alert(message);
  159. }
  160. /* eslint-enable no-console */
  161. });
  162. },
  163. events() {
  164. return [{
  165. 'click a.js-toggle-registration': this.toggleRegistration,
  166. 'click a.js-toggle-tls': this.toggleTLS,
  167. 'click a.js-setting-menu': this.switchMenu,
  168. 'click a.js-toggle-board-choose': this.checkBoard,
  169. 'click button.js-email-invite': this.inviteThroughEmail,
  170. 'click button.js-save': this.saveMailServerInfo,
  171. 'click button.js-send-smtp-test-email': this.sendSMTPTestEmail,
  172. 'click a.js-toggle-hide-logo': this.toggleHideLogo,
  173. 'click button.js-save-layout': this.saveLayout,
  174. }];
  175. },
  176. }).register('setting');
  177. BlazeComponent.extendComponent({
  178. saveAccountsChange() {
  179. const allowEmailChange = ($('input[name=allowEmailChange]:checked').val() === 'true');
  180. const allowUserNameChange = ($('input[name=allowUserNameChange]:checked').val() === 'true');
  181. AccountSettings.update('accounts-allowEmailChange', {
  182. $set: {'booleanValue': allowEmailChange},
  183. });
  184. AccountSettings.update('accounts-allowUserNameChange', {
  185. $set: {'booleanValue': allowUserNameChange},
  186. });
  187. },
  188. allowEmailChange() {
  189. return AccountSettings.findOne('accounts-allowEmailChange').booleanValue;
  190. },
  191. allowUserNameChange() {
  192. return AccountSettings.findOne('accounts-allowUserNameChange').booleanValue;
  193. },
  194. events() {
  195. return [{
  196. 'click button.js-accounts-save': this.saveAccountsChange,
  197. }];
  198. },
  199. }).register('accountSettings');
  200. BlazeComponent.extendComponent({
  201. onCreated() {
  202. this.loading = new ReactiveVar(false);
  203. },
  204. setLoading(w) {
  205. this.loading.set(w);
  206. },
  207. currentSetting() {
  208. return Announcements.findOne();
  209. },
  210. saveMessage() {
  211. const message = $('#admin-announcement').val().trim();
  212. Announcements.update(Announcements.findOne()._id, {
  213. $set: {'body': message},
  214. });
  215. },
  216. toggleActive() {
  217. this.setLoading(true);
  218. const isActive = this.currentSetting().enabled;
  219. Announcements.update(Announcements.findOne()._id, {
  220. $set: {'enabled': !isActive},
  221. });
  222. this.setLoading(false);
  223. if (isActive) {
  224. $('.admin-announcement').slideUp();
  225. } else {
  226. $('.admin-announcement').slideDown();
  227. }
  228. },
  229. events() {
  230. return [{
  231. 'click a.js-toggle-activemessage': this.toggleActive,
  232. 'click button.js-announcement-save': this.saveMessage,
  233. }];
  234. },
  235. }).register('announcementSettings');