d89a5c859beadfbdaf8e01595536e34678b63710
[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
14 /**
15 * Module: TYPO3/CMS/Backend/Login
16 * JavaScript module for the backend login form
17 */
18 define(['jquery', 'TYPO3/CMS/Backend/jquery.clearable', 'bootstrap'], function($) {
19 'use strict';
20
21 /**
22 *
23 * @type {{options: {loginForm: string, interfaceField: string, useridentField: string, submitButton: string, error: string, errorNoCookies: string, formFields: string, submitHandler: null}}}
24 * @exports TYPO3/CMS/Backend/Login
25 */
26 var BackendLogin = {
27 options: {
28 loginForm: '#typo3-login-form',
29 interfaceField: '.t3js-login-interface-field',
30 useridentField: '.t3js-login-userident-field',
31 submitButton: '.t3js-login-submit',
32 error: '.t3js-login-error',
33 errorNoCookies: '.t3js-login-error-nocookies',
34 formFields: '.t3js-login-formfields',
35 submitHandler: null
36 }
37 },
38 options = BackendLogin.options;
39
40 /**
41 * Hide all form fields and show a progress message and icon
42 */
43 BackendLogin.showLoginProcess = function() {
44 $(options.submitButton).button('loading');
45 $(options.error).addClass('hidden');
46 $(options.errorNoCookies).addClass('hidden');
47 };
48
49 /**
50 * Pass on to registered submit handler
51 *
52 * @param {Event} event
53 */
54 BackendLogin.handleSubmit = function(event) {
55 BackendLogin.showLoginProcess();
56
57 if (BackendLogin.options.submitHandler) {
58 BackendLogin.options.submitHandler(event);
59 }
60 };
61
62 /**
63 * Store the new selected Interface in a cookie to save it for future visits
64 */
65 BackendLogin.interfaceSelectorChanged = function() {
66 var now = new Date();
67 var expires = new Date(now.getTime() + 1000*60*60*24*365); // cookie expires in one year
68 document.cookie = 'typo3-login-interface=' + $(options.interfaceField).val() + '; expires=' + expires.toGMTString() + ';';
69 };
70
71 /**
72 * Check if an interface was stored in a cookie and preselect it in the select box
73 */
74 BackendLogin.checkForInterfaceCookie = function() {
75 if ($(options.interfaceField).length) {
76 var posStart = document.cookie.indexOf('typo3-login-interface=');
77 if (posStart !== -1) {
78 var selectedInterface = document.cookie.substr(posStart + 22);
79 selectedInterface = selectedInterface.substr(0, selectedInterface.indexOf(';'));
80 $(options.interfaceField).val(selectedInterface);
81 }
82 }
83 };
84
85 /**
86 * Hides input fields and shows cookie warning
87 */
88 BackendLogin.showCookieWarning = function() {
89 $(options.formFields).addClass('hidden');
90 $(options.errorNoCookies).removeClass('hidden');
91 };
92
93 /**
94 * Hides cookie warning and shows input fields
95 */
96 BackendLogin.hideCookieWarning = function() {
97 $(options.formFields).removeClass('hidden');
98 $(options.errorNoCookies).addClass('hidden');
99 };
100
101 /**
102 * Checks browser's cookie support
103 * see http://stackoverflow.com/questions/8112634/jquery-detecting-cookies-enabled
104 */
105 BackendLogin.checkCookieSupport = function() {
106 var cookieEnabled = navigator.cookieEnabled;
107
108 // when cookieEnabled flag is present and false then cookies are disabled.
109 if (cookieEnabled === false) {
110 BackendLogin.showCookieWarning();
111 } else {
112 // try to set a test cookie if we can't see any cookies and we're using
113 // either a browser that doesn't support navigator.cookieEnabled
114 // or IE (which always returns true for navigator.cookieEnabled)
115 if (!document.cookie && (cookieEnabled === null || /*@cc_on!@*/false)) {
116 document.cookie = 'typo3-login-cookiecheck=1';
117
118 if (!document.cookie) {
119 BackendLogin.showCookieWarning();
120 } else {
121 // unset the cookie again
122 document.cookie = 'typo3-login-cookiecheck=; expires=' + new Date(0).toUTCString();
123 }
124 }
125 }
126 };
127
128 /**
129 * Registers listeners for the Login Interface
130 */
131 BackendLogin.initializeEvents = function() {
132 $(options.loginForm).on('submit', BackendLogin.handleSubmit);
133
134 // The Interface selector is not always present, so this check is needed
135 if ($(options.interfaceField).length > 0) {
136 $(document).on('change blur', options.interfaceField, BackendLogin.interfaceSelectorChanged);
137 }
138
139 $('.t3js-clearable').clearable();
140
141 // carousel news height transition
142 $('.t3js-login-news-carousel').on('slide.bs.carousel', function(e) {
143 var nextH = $(e.relatedTarget).height();
144 $(this).find('div.active').parent().animate({ height: nextH }, 500);
145 });
146 };
147
148 // initialize and return the BackendLogin object
149 $(function() {
150 BackendLogin.checkCookieSupport();
151 BackendLogin.checkForInterfaceCookie();
152 BackendLogin.initializeEvents();
153 });
154
155 // prevent opening the login form in the backend frameset
156 if (top.location.href !== location.href) {
157 top.location.href = location.href;
158 }
159
160 return BackendLogin;
161 });