/
LoginBox.js
190 lines (168 loc) · 5.26 KB
/
LoginBox.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Copyright (c) 2010-2022 BSI Business Systems Integration AG.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* BSI Business Systems Integration AG - initial API and implementation
*/
import {Box, Device, strings, TextMap} from '../index';
import $ from 'jquery';
export default class LoginBox extends Box {
constructor() {
super();
this.ajaxOptions = {
type: 'POST'
};
this.authUrl = 'auth';
this.onPostDoneFunc = this.redirect.bind(this);
this.redirectUrl = null;
this.logoUrl = 'logo.png';
this.userDataKey = 'user';
this.passwordDataKey = 'password';
this.additionalData = {};
this.prepareRedirectUrlFunc = LoginBox.prepareRedirectUrl;
this.messageKey = null;
this.texts = {
'ui.Login': 'Login',
'ui.LoginFailed': 'Login failed',
'ui.User': 'Username',
'ui.Password': 'Password'
};
}
init(options) {
options = options || {};
options.texts = new TextMap($.extend(this.texts, options.texts));
options.ajaxOptions = $.extend(this.ajaxOptions, options.ajaxOptions);
$.extend(this, options);
}
_render() {
super._render();
this.$container.addClass('login-box');
this.$content.addClass('login-box-content ');
this.$form = $('<form>')
.attr('action', this.authUrl)
.attr('method', 'post')
.submit(this._onLoginFormSubmit.bind(this))
.appendTo(this.$content);
if (this.messageKey) {
this.$message = $('<div>')
.attr('id', 'message-box')
.addClass('message-box')
.html(strings.nl2br(this.texts.get(this.messageKey)))
.appendTo(this.$form);
}
this.$user = $('<input>')
.attr('type', 'text')
.attr('autocapitalize', 'off')
.attr('autocorrect', 'off')
.placeholder(this.texts.get('ui.User'))
.appendTo(this.$form);
this.$password = $('<input>')
.attr('type', 'password')
.placeholder(this.texts.get('ui.Password'))
.appendTo(this.$form);
this.$button = $('<button>')
.attr('type', 'submit')
.addClass('login-button button default')
.text(this.texts.get('ui.Login'))
.appendTo(this.$form);
this.$user.focus();
}
_resetButtonText() {
this.$button
.text(this.texts.get('ui.Login'))
.removeClass('login-error');
}
data() {
let data = {};
data[this.userDataKey] = this.$user.val();
data[this.passwordDataKey] = this.$password.val();
$.extend(data, this.additionalData);
return data;
}
_onLoginFormSubmit(event) {
// Prevent default submit action
event.preventDefault();
let url = this.$form.attr('action');
let data = this.data();
this.$button
.removeClass('login-error')
.setEnabled(false);
this.$user.off('input.resetLoginError');
this.$password.off('input.resetLoginError');
if (Device.get().supportsCssAnimation()) {
this.$button
.html('')
.append($('<div>').addClass('login-button-loading'));
}
let options = $.extend({}, this.ajaxOptions, {
url: url,
data: data
});
$.ajax(options)
.done(this._onPostDone.bind(this))
.fail(this._onPostFail.bind(this));
}
redirect(data) {
this.$backgroundElements.addClass('box-background-elements-fadeout');
if (Device.get().supportsCssAnimation()) {
this.$backgroundElements.oneAnimationEnd(() => {
this._redirect(data);
});
} else {
// fallback for old browsers that do not support the animation-end event
this._redirect(data);
}
}
_redirect(data) {
// Calculate target URL
let url = this.redirectUrl;
if (!url) {
url = (window.location.href || '').trim();
// Remove login and everything after it from the URL
url = this.prepareRedirectUrlFunc(url);
}
// Go to target URL
if (url) {
window.location.href = url;
} else {
window.location.reload();
}
}
_onPostDone(data) {
this.remove();
this.onPostDoneFunc.call(this, data);
}
_onPostFail(jqXHR, textStatus, errorThrown) {
// execute delayed to make sure loading animation is visible, otherwise (if it is very fast), it flickers
setTimeout(this._onPostFailImpl.bind(this, jqXHR, textStatus, errorThrown), 300);
}
_onPostFailImpl(jqXHR, textStatus, errorThrown) {
this.$button
.setEnabled(true)
.html('')
.text(this.texts.get('ui.LoginFailed'))
.addClass('login-error');
this.$user
.val('')
.focus();
this.$password
.val('');
this.$user
.one('input.resetLoginError', this._resetButtonText.bind(this));
this.$password
.one('input.resetLoginError', this._resetButtonText.bind(this));
}
// ----- Helper functions -----
static prepareRedirectUrl(url) {
let urlParts = /^([^?#]*)(\?[^#]*)?(#.*)?$/.exec(url || ''); // $1 = baseUrl, $2 = queryPart, $3 = hashPart
let filteredBaseUrl = urlParts[1]
.replace(/login.html$/, '')
.replace(/login$/, '')
.replace(/logout$/, '');
return filteredBaseUrl + (urlParts[2] ? urlParts[2] : '') + (urlParts[3] ? urlParts[3] : '');
}
}