c2e76e3306df02b25c389a4f81065f36263dda41
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / Login.js
1 /**
2 * This file is part of the TYPO3 CMS project.
3 *
4 * It is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License, either version 2
6 * of the License, or any later version.
7 *
8 * For the full copyright and license information, please read the
9 * LICENSE.txt file that was distributed with this source code.
10 *
11 * The TYPO3 project - inspiring people to share!
12 */
13 define('TYPO3/CMS/Backend/Login', ['jquery'], function($) {
14 var BackendLogin = {
15 options: {
16 usernameField: '#t3-username',
17 passwordField: '#t3-password',
18 useridentField: '#t3-field-userident',
19 openIdField: '#openid_url',
20 submitButton: '#t3-login-submit',
21 clearIconSelector: '.t3-login-clearInputField',
22 interfaceSelector: '#t3-interfaceselector'
23 }
24 },
25 options = BackendLogin.options;
26
27 // Checks weather capslock is enabled (returns TRUE if enabled, false otherwise)
28 // thanks to http://24ways.org/2007/capturing-caps-lock
29 BackendLogin.isCapslockEnabled = function(e) {
30 var ev = e ? e : window.event;
31 if (!ev) {
32 return;
33 }
34 // get key pressed
35 var which = -1;
36 if (ev.which) {
37 which = ev.which;
38 } else if (ev.keyCode) {
39 which = ev.keyCode;
40 }
41 // get shift status
42 var shift_status = false;
43 if (ev.shiftKey) {
44 shift_status = ev.shiftKey;
45 } else if (ev.modifiers) {
46 shift_status = !!(ev.modifiers & 4);
47 }
48 return (((which >= 65 && which <= 90) && !shift_status) ||
49 ((which >= 97 && which <= 122) && shift_status));
50 };
51
52 /**
53 * Change to Interface for OpenId login and save the selection to a cookie
54 */
55 BackendLogin.switchToOpenId = function() {
56 $('#t3-login-form-footer-default').hide();
57 $('#t3-login-form-footer-openId').show();
58
59 $('#t3-login-username-section, #t3-login-password-section').hide();
60 $('#t3-login-openid_url-section').show();
61 $('#t3-login-interface-section').hide();
62
63 $(options.openIdField).trigger('focus');
64 if ($(options.usernameField).val() == '') {
65 $(options.usernameField).val('openid_url');
66 }
67
68 setLogintypeCookie('openid');
69 };
70
71 /**
72 * Change to Interface for default login and save the selection to a cookie
73 */
74 BackendLogin.switchToDefault = function() {
75 $('#t3-login-openIdLogo').hide();
76
77 if ($(options.usernameField).val() == 'openid_url') {
78 $(options.usernameField).val('');
79 }
80
81 $('#t3-login-form-footer-default').show();
82 $('#t3-login-form-footer-openId').hide();
83 $('#t3-login-username-section, #t3-login-password-section').show();
84 $('#t3-login-openid_url-section').hide();
85 $('#t3-login-interface-section').show();
86
87 $(options.usernameField).trigger('focus');
88
89 setLogintypeCookie('username');
90 };
91
92 /**
93 * Hide all form fields and show a progress message and icon
94 */
95 BackendLogin.showLoginProcess = function() {
96 // setting a fixed height (based on the current, calculated height of the browser) for
97 // the box with the login form, so it doesn't jump around when the spinner is shown
98 var loginBoxHeight = $('#t3-login-form-fields').height();
99 $('#t3-login-process').height(loginBoxHeight).show();
100 $('#t3-login-error').hide();
101 $('#t3-login-form-fields').hide();
102 $('#t3-nocookies-error').hide();
103 };
104
105 /**
106 * Store a login type in a cookie to save it for future visits
107 * Login type means whether you login by username/password or OpenID
108 */
109 BackendLogin.setLogintypeCookie = function(type) {
110 var now = new Date();
111 var expires = new Date(now.getTime() + 1000*60*60*24*365); // cookie expires in one year
112 document.cookie = 'typo3-login-method=' + type + '; expires=' + expires.toGMTString() + ';';
113 };
114
115 /**
116 * Check if a login type was stored in a cookie and change the Interface accordingly
117 */
118 BackendLogin.checkForLogintypeCookie = function() {
119 if (document.cookie.indexOf('typo3-login-method=openid') >- 1) {
120 BackendLogin.switchToOpenId();
121 }
122 };
123
124 /**
125 * Store the new selected Interface in a cookie to save it for future visits
126 */
127 BackendLogin.interfaceSelectorChanged = function() {
128 var now = new Date();
129 var expires = new Date(now.getTime() + 1000*60*60*24*365); // cookie expires in one year
130 document.cookie = 'typo3-login-interface=' + $(options.interfaceSelector).val() + '; expires=' + expires.toGMTString() + ';';
131 };
132
133 /**
134 * Shows up the clear icon for a field which is not empty, and hides it otherwise
135 */
136 BackendLogin.setVisibilityOfClearIcon = function($formFieldElement) {
137 if ($formFieldElement.val().length > 0) {
138 $formFieldElement.next(options.clearIconSelector).find('a').show();
139 } else {
140 $formFieldElement.next(options.clearIconSelector).find('a').hide();
141 }
142 };
143
144 /**
145 * Clears an input field and sets focus to it
146 */
147 BackendLogin.clearInputField = function($formFieldElement) {
148 $formFieldElement.val('').focus();
149 };
150
151 /**
152 * Check if an interface was stored in a cookie and preselect it in the select box
153 */
154 BackendLogin.checkForInterfaceCookie = function() {
155 if ($(options.interfaceSelector).length) {
156 var posStart = document.cookie.indexOf('typo3-login-interface=');
157 if (posStart != -1) {
158 var selectedInterface = document.cookie.substr(posStart + 22);
159 selectedInterface = selectedInterface.substr(0, selectedInterface.indexOf(';'));
160 }
161 $(options.interfaceSelector).val(selectedInterface);
162 }
163 };
164
165 /**
166 * To prevent its unintented use when typing the password, the user is warned when Capslock is on
167 */
168 BackendLogin.showCapsLockWarning = function($alertIconElement, event) {
169 if (BackendLogin.isCapslockEnabled(event) === true) {
170 $alertIconElement.show();
171 } else {
172 $alertIconElement.hide();
173 }
174 };
175
176 /**
177 * Hides input fields and shows cookie warning
178 */
179 BackendLogin.showCookieWarning = function() {
180 $('#t3-login-form-fields').hide();
181 $('#t3-nocookies-error').show();
182 };
183
184 /**
185 * Hides cookie warning and shows input fields
186 */
187 BackendLogin.hideCookieWarning = function() {
188 $('#t3-nocookies-error').hide();
189 $('#t3-login-form-fields').show();
190 };
191
192 /**
193 * Checks browser's cookie support
194 * see http://stackoverflow.com/questions/8112634/jquery-detecting-cookies-enabled
195 */
196 BackendLogin.checkCookieSupport = function() {
197 var cookieEnabled = navigator.cookieEnabled;
198
199 // when cookieEnabled flag is present and false then cookies are disabled.
200 if (cookieEnabled === false) {
201 BackendLogin.showCookieWarning();
202 } else {
203 // try to set a test cookie if we can't see any cookies and we're using
204 // either a browser that doesn't support navigator.cookieEnabled
205 // or IE (which always returns true for navigator.cookieEnabled)
206 if (!document.cookie && (cookieEnabled === null || /*@cc_on!@*/false)) {
207 document.cookie = 'typo3-login-cookiecheck=1';
208
209 if (!document.cookie) {
210 BackendLogin.showCookieWarning();
211 } else {
212 // unset the cookie again
213 document.cookie = 'typo3-login-cookiecheck=; expires=' + new Date(0).toUTCString();
214 }
215 }
216 }
217 };
218
219 /**
220 * Registers listeners for the Login Interface (e.g. to toggle OpenID and Default login)
221 */
222 BackendLogin.initializeEvents = function() {
223 $(document).on('click', '#t3-login-switchToOpenId', BackendLogin.switchToOpenId);
224 $(document).on('click', '#t3-login-switchToDefault', BackendLogin.switchToDefault);
225 $(document).on('click', options.submitButton, BackendLogin.showLoginProcess);
226
227 // The Interface selector is not always present, so this check is needed
228 if ($(options.interfaceSelector).length > 0) {
229 $(document).on('change blur', options.interfaceSelector, BackendLogin.interfaceSelectorChanged);
230 }
231
232 $(document).on('click', options.clearIconSelector, function() {
233 BackendLogin.clearInputField($(this).prev());
234 });
235 $(document).on('focus blur keypress', options.usernameField + ', ' + options.passwordField + ', ' + options.openIdField, function() {
236 BackendLogin.setVisibilityOfClearIcon($(this));
237 });
238 $(document).on('keypress', options.usernameField + ', ' + options.passwordField + ', ' + options.openIdField, function(evt) {
239 BackendLogin.showCapsLockWarning($(this).siblings('.t3-login-alert-capslock'), evt);
240 });
241 };
242 // initialize and return the BackendLogin object
243 return function() {
244
245 $(document).ready(function() {
246 BackendLogin.checkForInterfaceCookie();
247 BackendLogin.checkForLogintypeCookie();
248 BackendLogin.checkCookieSupport();
249 BackendLogin.initializeEvents();
250
251 // previously named "startUp"
252
253 // If the login screen is shown in the login_frameset window for re-login,
254 // then try to get the username of the current/former login from opening windows main frame:
255 try {
256 if (parent.opener && parent.opener.TS && parent.opener.TS.username) {
257 $(options.usernameField).val(parent.opener.TS.username);
258 }
259 } catch(error) {} // continue
260
261 BackendLogin.setVisibilityOfClearIcon($(options.usernameField));
262 BackendLogin.setVisibilityOfClearIcon($(options.passwordField));
263
264 // previously named "check focus"
265 if ($(options.usernameField).val() == '') {
266 $(options.usernameField).focus();
267 } else if ($(options.passwordField).attr('type') !== 'hidden') {
268 $(options.passwordField).focus();
269 }
270
271 });
272
273 // prevent opening the login form in the backend frameset
274 if (top.location.href != self.location.href) {
275 top.location.href = self.location.href;
276 }
277
278 TYPO3.BackendLogin = BackendLogin;
279 return BackendLogin;
280 }();
281 });