layouts.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. BlazeLayout.setRoot('body');
  2. const i18nTagToT9n = i18nTag => {
  3. // t9n/i18n tags are same now, see: https://github.com/softwarerero/meteor-accounts-t9n/pull/129
  4. // but we keep this conversion function here, to be aware that that they are different system.
  5. return i18nTag;
  6. };
  7. const validator = {
  8. set(obj, prop, value) {
  9. if (prop === 'state' && value !== 'signIn') {
  10. $('.at-form-authentication').hide();
  11. } else if (prop === 'state' && value === 'signIn') {
  12. $('.at-form-authentication').show();
  13. }
  14. // The default behavior to store the value
  15. obj[prop] = value;
  16. // Indicate success
  17. return true;
  18. },
  19. };
  20. Template.userFormsLayout.onCreated(function() {
  21. const templateInstance = this;
  22. templateInstance.currentSetting = new ReactiveVar();
  23. templateInstance.isLoading = new ReactiveVar(false);
  24. Meteor.subscribe('setting', {
  25. onReady() {
  26. templateInstance.currentSetting.set(Settings.findOne());
  27. return this.stop();
  28. },
  29. });
  30. });
  31. Template.userFormsLayout.onRendered(() => {
  32. AccountsTemplates.state.form.keys = new Proxy(
  33. AccountsTemplates.state.form.keys,
  34. validator,
  35. );
  36. const i18nTag = navigator.language;
  37. if (i18nTag) {
  38. T9n.setLanguage(i18nTagToT9n(i18nTag));
  39. }
  40. EscapeActions.executeAll();
  41. });
  42. Template.userFormsLayout.helpers({
  43. currentSetting() {
  44. return Template.instance().currentSetting.get();
  45. },
  46. isLoading() {
  47. return Template.instance().isLoading.get();
  48. },
  49. afterBodyStart() {
  50. return currentSetting.customHTMLafterBodyStart;
  51. },
  52. beforeBodyEnd() {
  53. return currentSetting.customHTMLbeforeBodyEnd;
  54. },
  55. languages() {
  56. return _.map(TAPi18n.getLanguages(), (lang, code) => {
  57. const tag = code;
  58. let name = lang.name;
  59. if (lang.name === 'br') {
  60. name = 'Brezhoneg';
  61. } else if (lang.name === 'ig') {
  62. name = 'Igbo';
  63. } else if (lang.name === 'oc') {
  64. name = 'Occitan';
  65. } else if (lang.name === 'zh-TW') {
  66. name = '繁體中文(台灣)';
  67. }
  68. return { tag, name };
  69. }).sort(function(a, b) {
  70. if (a.name === b.name) {
  71. return 0;
  72. } else {
  73. return a.name > b.name ? 1 : -1;
  74. }
  75. });
  76. },
  77. isCurrentLanguage() {
  78. const t9nTag = i18nTagToT9n(this.tag);
  79. const curLang = T9n.getLanguage() || 'en';
  80. return t9nTag === curLang;
  81. },
  82. });
  83. Template.userFormsLayout.events({
  84. 'change .js-userform-set-language'(event) {
  85. const i18nTag = $(event.currentTarget).val();
  86. T9n.setLanguage(i18nTagToT9n(i18nTag));
  87. event.preventDefault();
  88. },
  89. 'click #at-btn'(event, templateInstance) {
  90. if (FlowRouter.getRouteName() === 'atSignIn') {
  91. templateInstance.isLoading.set(true);
  92. authentication(event, templateInstance).then(() => {
  93. templateInstance.isLoading.set(false);
  94. });
  95. }
  96. },
  97. });
  98. Template.defaultLayout.events({
  99. 'click .js-close-modal': () => {
  100. Modal.close();
  101. },
  102. });
  103. async function authentication(event, templateInstance) {
  104. const match = $('#at-field-username_and_email').val();
  105. const password = $('#at-field-password').val();
  106. if (!match || !password) return undefined;
  107. const result = await getAuthenticationMethod(
  108. templateInstance.currentSetting.get(),
  109. match,
  110. );
  111. if (result === 'password') return undefined;
  112. // Stop submit #at-pwd-form
  113. event.preventDefault();
  114. event.stopImmediatePropagation();
  115. switch (result) {
  116. case 'ldap':
  117. return new Promise(resolve => {
  118. Meteor.loginWithLDAP(match, password, function() {
  119. resolve(FlowRouter.go('/'));
  120. });
  121. });
  122. case 'cas':
  123. return new Promise(resolve => {
  124. Meteor.loginWithCas(match, password, function() {
  125. resolve(FlowRouter.go('/'));
  126. });
  127. });
  128. default:
  129. return undefined;
  130. }
  131. }
  132. function getAuthenticationMethod(
  133. { displayAuthenticationMethod, defaultAuthenticationMethod },
  134. match,
  135. ) {
  136. if (displayAuthenticationMethod) {
  137. return $('.select-authentication').val();
  138. }
  139. return getUserAuthenticationMethod(defaultAuthenticationMethod, match);
  140. }
  141. function getUserAuthenticationMethod(defaultAuthenticationMethod, match) {
  142. return new Promise(resolve => {
  143. try {
  144. Meteor.subscribe('user-authenticationMethod', match, {
  145. onReady() {
  146. const user = Users.findOne();
  147. const authenticationMethod = user
  148. ? user.authenticationMethod
  149. : defaultAuthenticationMethod;
  150. resolve(authenticationMethod);
  151. },
  152. });
  153. } catch (error) {
  154. resolve(defaultAuthenticationMethod);
  155. }
  156. });
  157. }