layouts.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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(() => {
  21. Meteor.subscribe('setting');
  22. });
  23. Template.userFormsLayout.onRendered(() => {
  24. AccountsTemplates.state.form.keys = new Proxy(AccountsTemplates.state.form.keys, validator);
  25. const i18nTag = navigator.language;
  26. if (i18nTag) {
  27. T9n.setLanguage(i18nTagToT9n(i18nTag));
  28. }
  29. EscapeActions.executeAll();
  30. });
  31. Template.userFormsLayout.helpers({
  32. currentSetting() {
  33. return Settings.findOne();
  34. },
  35. languages() {
  36. return _.map(TAPi18n.getLanguages(), (lang, code) => {
  37. const tag = code;
  38. let name = lang.name;
  39. if (lang.name === 'br') {
  40. name = 'Brezhoneg';
  41. } else if (lang.name === 'ig') {
  42. name = 'Igbo';
  43. }
  44. return { tag, name };
  45. }).sort(function(a, b) {
  46. if (a.name === b.name) {
  47. return 0;
  48. } else {
  49. return a.name > b.name ? 1 : -1;
  50. }
  51. });
  52. },
  53. isCurrentLanguage() {
  54. const t9nTag = i18nTagToT9n(this.tag);
  55. const curLang = T9n.getLanguage() || 'en';
  56. return t9nTag === curLang;
  57. },
  58. /*
  59. isCas() {
  60. return Meteor.settings.public &&
  61. Meteor.settings.public.cas &&
  62. Meteor.settings.public.cas.loginUrl;
  63. },
  64. casSignInLabel() {
  65. return TAPi18n.__('casSignIn', {}, T9n.getLanguage() || 'en');
  66. },
  67. */
  68. });
  69. Template.userFormsLayout.events({
  70. 'change .js-userform-set-language'(evt) {
  71. const i18nTag = $(evt.currentTarget).val();
  72. T9n.setLanguage(i18nTagToT9n(i18nTag));
  73. evt.preventDefault();
  74. },
  75. 'click button#cas'() {
  76. Meteor.loginWithCas(function() {
  77. if (FlowRouter.getRouteName() === 'atSignIn') {
  78. FlowRouter.go('/');
  79. }
  80. });
  81. },
  82. 'click #at-btn'(event) {
  83. /* All authentication method can be managed/called here.
  84. !! DON'T FORGET to correctly fill the fields of the user during its creation if necessary authenticationMethod : String !!
  85. */
  86. const authenticationMethodSelected = $('.select-authentication').val();
  87. // Local account
  88. if (authenticationMethodSelected === 'password') {
  89. return;
  90. }
  91. // Stop submit #at-pwd-form
  92. event.preventDefault();
  93. event.stopImmediatePropagation();
  94. const email = $('#at-field-username_and_email').val();
  95. const password = $('#at-field-password').val();
  96. // Ldap account
  97. if (authenticationMethodSelected === 'ldap') {
  98. // Check if the user can use the ldap connection
  99. Meteor.subscribe('user-authenticationMethod', email, {
  100. onReady() {
  101. const user = Users.findOne();
  102. if (user === undefined || user.authenticationMethod === 'ldap') {
  103. // Use the ldap connection package
  104. Meteor.loginWithLDAP(email, password, function(error) {
  105. if (!error) {
  106. // Connection
  107. return FlowRouter.go('/');
  108. }
  109. return error;
  110. });
  111. }
  112. return this.stop();
  113. },
  114. });
  115. }
  116. },
  117. });
  118. Template.defaultLayout.events({
  119. 'click .js-close-modal': () => {
  120. Modal.close();
  121. },
  122. });