summaryrefslogtreecommitdiffstats
path: root/client/components/main/layouts.js
blob: 3fda11b7840f8d2f16ee48e7e63806a65736aa0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
BlazeLayout.setRoot('body');

const i18nTagToT9n = (i18nTag) => {
  // t9n/i18n tags are same now, see: https://github.com/softwarerero/meteor-accounts-t9n/pull/129
  // but we keep this conversion function here, to be aware that that they are different system.
  return i18nTag;
};

Template.userFormsLayout.onRendered(() => {
  const i18nTag = navigator.language;
  if (i18nTag) {
    T9n.setLanguage(i18nTagToT9n(i18nTag));
  }
  EscapeActions.executeAll();
});

Template.userFormsLayout.helpers({
  languages() {
    return _.map(TAPi18n.getLanguages(), (lang, code) => {
      const tag = code;
      let name = lang.name;
      if (lang.name === 'br') {
        name = 'Brezhoneg';
      } else if (lang.name === 'ig') {
        name = 'Igbo';
      }
      return { tag, name };
    }).sort(function(a, b) {
      if (a.name === b.name) {
        return 0;
      } else {
        return a.name > b.name ? 1 : -1;
      }
    });
  },

  isCurrentLanguage() {
    const t9nTag = i18nTagToT9n(this.tag);
    const curLang = T9n.getLanguage() || 'en';
    return t9nTag === curLang;
  },
/*
  isCas() {
    return Meteor.settings.public &&
      Meteor.settings.public.cas &&
      Meteor.settings.public.cas.loginUrl;
  },

  casSignInLabel() {
    return TAPi18n.__('casSignIn', {}, T9n.getLanguage() || 'en');
  },
*/
});

Template.userFormsLayout.events({
  'change .js-userform-set-language'(evt) {
    const i18nTag = $(evt.currentTarget).val();
    T9n.setLanguage(i18nTagToT9n(i18nTag));
    evt.preventDefault();
  },
  'click button#cas'() {
    Meteor.loginWithCas(function() {
      if (FlowRouter.getRouteName() === 'atSignIn') {
        FlowRouter.go('/');
      }
    });
  },
  'click #at-btn'(event) {
    /* All authentication method can be managed/called here.
       !! DON'T FORGET to correctly fill the fields of the user during its creation if necessary authenticationMethod : String !!
    */
    if (FlowRouter.getRouteName() !== 'atSignIn') {
      return;
    }

    const email = $('#at-field-username_and_email').val();

    Meteor.subscribe('user-authenticationMethod', email, {
      onReady() {
        const user = Users.findOne();

        if (user && user.authenticationMethod === 'password') {
          logoutWithTimer(user._id);
          return this.stop();
        }

        // Stop submit #at-pwd-form
        event.preventDefault();
        event.stopImmediatePropagation();

        const password = $('#at-field-password').val();

        if (user === undefined || user.authenticationMethod === 'ldap') {
          // Use the ldap connection package
          Meteor.loginWithLDAP(email, password, function(error) {
            if (!error) {
              logoutWithTimer(user._id);
              // Connection
              return FlowRouter.go('/');
            }
            return error;
          });
        }
        return this.stop();
      },
    });
  },
});

Template.defaultLayout.events({
  'click .js-close-modal': () => {
    Modal.close();
  },
});