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