bfde65a7bf2e3ff9f66e035749f23923429aea52
[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 $(options.usernameField).val('openid_url');
65 $(options.passwordField).val('');
66
67 BackendLogin.setLogintypeCookie('openid');
68 };
69
70 /**
71 * Change to Interface for default login and save the selection to a cookie
72 */
73 BackendLogin.switchToDefault = function() {
74 $('#t3-login-openIdLogo').hide();
75
76 $(options.openIdField).val('');
77 $(options.usernameField).val('');
78
79
80 $('#t3-login-form-footer-default').show();
81 $('#t3-login-form-footer-openId').hide();
82 $('#t3-login-username-section, #t3-login-password-section').show();
83 $('#t3-login-openid_url-section').hide();
84 $('#t3-login-interface-section').show();
85
86 $(options.usernameField).trigger('focus');
87
88 BackendLogin.setLogintypeCookie('username');
89 };
90
91 /**
92 * Hide all form fields and show a progress message and icon
93 */
94 BackendLogin.showLoginProcess = function() {
95 // setting a fixed height (based on the current, calculated height of the browser) for
96 // the box with the login form, so it doesn't jump around when the spinner is shown
97 var loginBoxHeight = $('#t3-login-form-fields').height();
98 $('#t3-login-process').height(loginBoxHeight).show();
99 $('#t3-login-error').hide();
100 $('#t3-login-form-fields').hide();
101 $('#t3-nocookies-error').hide();
102 };
103
104 /**
105 * Store a login type in a cookie to save it for future visits
106 * Login type means whether you login by username/password or OpenID
107 */
108 BackendLogin.setLogintypeCookie = function(type) {
109 var now = new Date();
110 var expires = new Date(now.getTime() + 1000*60*60*24*365); // cookie expires in one year
111 document.cookie = 'typo3-login-method=' + type + '; expires=' + expires.toGMTString() + ';';
112 };
113
114 /**
115 * Check if a login type was stored in a cookie and change the Interface accordingly
116 */
117 BackendLogin.checkForLogintypeCookie = function() {
118 if (document.cookie.indexOf('typo3-login-method=openid') >- 1) {
119 BackendLogin.switchToOpenId();
120 }
121 };
122
123 /**
124 * Store the new selected Interface in a cookie to save it for future visits
125 */
126 BackendLogin.interfaceSelectorChanged = function() {
127 var now = new Date();
128 var expires = new Date(now.getTime() + 1000*60*60*24*365); // cookie expires in one year
129 document.cookie = 'typo3-login-interface=' + $(options.interfaceSelector).val() + '; expires=' + expires.toGMTString() + ';';
130 };
131
132 /**
133 * Shows up the clear icon for a field which is not empty, and hides it otherwise
134 */
135 BackendLogin.setVisibilityOfClearIcon = function($formFieldElement) {
136 if ($formFieldElement.val().length > 0) {
137 $formFieldElement.next(options.clearIconSelector).find('a').show();
138 } else {
139 $formFieldElement.next(options.clearIconSelector).find('a').hide();
140 }
141 };
142
143 /**
144 * Clears an input field and sets focus to it
145 */
146 BackendLogin.clearInputField = function($formFieldElement) {
147 $formFieldElement.val('').focus();
148 };
149
150 /**
151 * Check if an interface was stored in a cookie and preselect it in the select box
152 */
153 BackendLogin.checkForInterfaceCookie = function() {
154 if ($(options.interfaceSelector).length) {
155 var posStart = document.cookie.indexOf('typo3-login-interface=');
156 if (posStart != -1) {
157 var selectedInterface = document.cookie.substr(posStart + 22);
158 selectedInterface = selectedInterface.substr(0, selectedInterface.indexOf(';'));
159 }
160 $(options.interfaceSelector).val(selectedInterface);
161 }
162 };
163
164 /**
165 * To prevent its unintented use when typing the password, the user is warned when Capslock is on
166 */
167 BackendLogin.showCapsLockWarning = function($alertIconElement, event) {
168 if (BackendLogin.isCapslockEnabled(event) === true) {
169 $alertIconElement.show();
170 } else {
171 $alertIconElement.hide();
172 }
173 };
174
175 /**
176 * Hides input fields and shows cookie warning
177 */
178 BackendLogin.showCookieWarning = function() {
179 $('#t3-login-form-fields').hide();
180 $('#t3-nocookies-error').show();
181 };
182
183 /**
184 * Hides cookie warning and shows input fields
185 */
186 BackendLogin.hideCookieWarning = function() {
187 $('#t3-nocookies-error').hide();
188 $('#t3-login-form-fields').show();
189 };
190
191 /**
192 * Checks browser's cookie support
193 * see http://stackoverflow.com/questions/8112634/jquery-detecting-cookies-enabled
194 */
195 BackendLogin.checkCookieSupport = function() {
196 var cookieEnabled = navigator.cookieEnabled;
197
198 // when cookieEnabled flag is present and false then cookies are disabled.
199 if (cookieEnabled === false) {
200 BackendLogin.showCookieWarning();
201 } else {
202 // try to set a test cookie if we can't see any cookies and we're using
203 // either a browser that doesn't support navigator.cookieEnabled
204 // or IE (which always returns true for navigator.cookieEnabled)
205 if (!document.cookie && (cookieEnabled === null || /*@cc_on!@*/false)) {
206 document.cookie = 'typo3-login-cookiecheck=1';
207
208 if (!document.cookie) {
209 BackendLogin.showCookieWarning();
210 } else {
211 // unset the cookie again
212 document.cookie = 'typo3-login-cookiecheck=; expires=' + new Date(0).toUTCString();
213 }
214 }
215 }
216 };
217
218 /**
219 * Registers listeners for the Login Interface (e.g. to toggle OpenID and Default login)
220 */
221 BackendLogin.initializeEvents = function() {
222 $(document).on('click', '#t3-login-switchToOpenId', BackendLogin.switchToOpenId);
223 $(document).on('click', '#t3-login-switchToDefault', BackendLogin.switchToDefault);
224 $(document).on('click', options.submitButton, BackendLogin.showLoginProcess);
225
226 // The Interface selector is not always present, so this check is needed
227 if ($(options.interfaceSelector).length > 0) {
228 $(document).on('change blur', options.interfaceSelector, BackendLogin.interfaceSelectorChanged);
229 }
230
231 $(document).on('click', options.clearIconSelector, function() {
232 BackendLogin.clearInputField($(this).prev());
233 });
234 $(document).on('focus blur keypress', options.usernameField + ', ' + options.passwordField + ', ' + options.openIdField, function() {
235 BackendLogin.setVisibilityOfClearIcon($(this));
236 });
237 $(document).on('keypress', options.usernameField + ', ' + options.passwordField + ', ' + options.openIdField, function(evt) {
238 BackendLogin.showCapsLockWarning($(this).siblings('.t3-login-alert-capslock'), evt);
239 });
240 };
241 // initialize and return the BackendLogin object
242 return function() {
243
244 $(document).ready(function() {
245 BackendLogin.checkForInterfaceCookie();
246 BackendLogin.checkForLogintypeCookie();
247 BackendLogin.checkCookieSupport();
248 BackendLogin.initializeEvents();
249
250 // previously named "startUp"
251
252 // If the login screen is shown in the login_frameset window for re-login,
253 // then try to get the username of the current/former login from opening windows main frame:
254 try {
255 if (parent.opener && parent.opener.TS && parent.opener.TS.username) {
256 $(options.usernameField).val(parent.opener.TS.username);
257 }
258 } catch(error) {} // continue
259
260 BackendLogin.setVisibilityOfClearIcon($(options.usernameField));
261 BackendLogin.setVisibilityOfClearIcon($(options.passwordField));
262
263 // previously named "check focus"
264 if ($(options.usernameField).val() == '') {
265 $(options.usernameField).focus();
266 } else if ($(options.passwordField).attr('type') !== 'hidden') {
267 $(options.passwordField).focus();
268 }
269
270 });
271
272 // prevent opening the login form in the backend frameset
273 if (top.location.href != self.location.href) {
274 top.location.href = self.location.href;
275 }
276
277 TYPO3.BackendLogin = BackendLogin;
278 return BackendLogin;
279 }();
280 });