layouts.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. Template.userFormsLayout.onCreated(function() {
  8. Meteor.call('getDefaultAuthenticationMethod', (error, result) => {
  9. this.data.defaultAuthenticationMethod = new ReactiveVar(error ? undefined : result);
  10. });
  11. Meteor.subscribe('setting');
  12. });
  13. Template.userFormsLayout.onRendered(() => {
  14. const i18nTag = navigator.language;
  15. if (i18nTag) {
  16. T9n.setLanguage(i18nTagToT9n(i18nTag));
  17. }
  18. EscapeActions.executeAll();
  19. });
  20. Template.userFormsLayout.helpers({
  21. currentSetting() {
  22. return Settings.findOne();
  23. },
  24. afterBodyStart() {
  25. return currentSetting.customHTMLafterBodyStart;
  26. },
  27. beforeBodyEnd() {
  28. return currentSetting.customHTMLbeforeBodyEnd;
  29. },
  30. languages() {
  31. return _.map(TAPi18n.getLanguages(), (lang, code) => {
  32. const tag = code;
  33. let name = lang.name;
  34. if (lang.name === 'br') {
  35. name = 'Brezhoneg';
  36. } else if (lang.name === 'ig') {
  37. name = 'Igbo';
  38. }
  39. return { tag, name };
  40. }).sort(function(a, b) {
  41. if (a.name === b.name) {
  42. return 0;
  43. } else {
  44. return a.name > b.name ? 1 : -1;
  45. }
  46. });
  47. },
  48. isCurrentLanguage() {
  49. const t9nTag = i18nTagToT9n(this.tag);
  50. const curLang = T9n.getLanguage() || 'en';
  51. return t9nTag === curLang;
  52. },
  53. /*
  54. isCas() {
  55. return Meteor.settings.public &&
  56. Meteor.settings.public.cas &&
  57. Meteor.settings.public.cas.loginUrl;
  58. },
  59. casSignInLabel() {
  60. return TAPi18n.__('casSignIn', {}, T9n.getLanguage() || 'en');
  61. },
  62. */
  63. });
  64. Template.userFormsLayout.events({
  65. 'change .js-userform-set-language'(evt) {
  66. const i18nTag = $(evt.currentTarget).val();
  67. T9n.setLanguage(i18nTagToT9n(i18nTag));
  68. evt.preventDefault();
  69. },
  70. 'click button#cas'() {
  71. Meteor.loginWithCas(function() {
  72. if (FlowRouter.getRouteName() === 'atSignIn') {
  73. FlowRouter.go('/');
  74. }
  75. });
  76. },
  77. 'click #at-btn'(event, instance) {
  78. const email = $('#at-field-username_and_email').val();
  79. const password = $('#at-field-password').val();
  80. if (FlowRouter.getRouteName() !== 'atSignIn' || password === '' || email === '') {
  81. return;
  82. }
  83. // Stop submit #at-pwd-form
  84. event.preventDefault();
  85. event.stopImmediatePropagation();
  86. Meteor.subscribe('user-authenticationMethod', email, {
  87. onReady() {
  88. return authentication.call(this, instance, email, password);
  89. },
  90. });
  91. },
  92. });
  93. Template.defaultLayout.events({
  94. 'click .js-close-modal': () => {
  95. Modal.close();
  96. },
  97. });
  98. function authentication(instance, email, password) {
  99. const user = Users.findOne();
  100. // Authentication with password
  101. if (user && user.authenticationMethod === 'password') {
  102. $('#at-pwd-form').submit();
  103. return this.stop();
  104. }
  105. const authenticationMethod = user
  106. ? user.authenticationMethod
  107. : instance.data.defaultAuthenticationMethod.get();
  108. switch (authenticationMethod) {
  109. case 'ldap':
  110. // Use the ldap connection package
  111. Meteor.loginWithLDAP(email, password, function(error) {
  112. if (error) {
  113. displayError('error-ldap-login');
  114. return this.stop();
  115. } else {
  116. return FlowRouter.go('/');
  117. }
  118. });
  119. break;
  120. default:
  121. displayError('error-undefined');
  122. }
  123. return this.stop();
  124. }
  125. function displayError(code) {
  126. const translated = TAPi18n.__(code);
  127. if (translated === code) {
  128. return;
  129. }
  130. if(!$('.at-error').length) {
  131. $('.at-pwd-form').before('<div class="at-error"><p></p></div>');
  132. }
  133. $('.at-error p').text(translated);
  134. }