8426368ac083a32c8ac474d97b97ac75c9665b2e
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Resources / Public / JavaScript / Modules / LocalConfiguration.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/Install/LocalConfiguration
16 */
17 define(['jquery', 'TYPO3/CMS/Install/FlashMessage', 'TYPO3/CMS/Install/ProgressBar', 'TYPO3/CMS/Install/InfoBox', 'TYPO3/CMS/Install/Severity', 'bootstrap'], function($, FlashMessage, ProgressBar, InfoBox, Severity) {
18 'use strict';
19
20 return {
21 selectorToggleAllTrigger: '.gridder-show .t3js-localConfiguration-toggleAll',
22 selectorWriteTrigger: '.t3js-localConfiguration-write',
23 selectorSearchTrigger: '.t3js-localConfiguration-search',
24 selectorWriteToken: '#t3js-localConfiguration-write-token',
25 selectorOutputContainer: '.t3js-localConfiguration-output',
26
27 initialize: function() {
28 var self = this;
29
30 $(document).on('click', this.selectorWriteTrigger, function() {
31 self.write();
32 });
33
34 // Expand / collapse "Toggle all" button
35 $(document).on('click', this.selectorToggleAllTrigger, function() {
36 var $panels = $('.panel-collapse', '.gridder-show .t3js-localConfiguration');
37 var action = ($panels.eq(0).hasClass('in')) ? 'hide' : 'show';
38 $panels.collapse(action);
39 });
40
41 // Make jquerys "contains" work case-insensitive
42 jQuery.expr[':'].contains = jQuery.expr.createPseudo(function(arg) {
43 return function (elem) {
44 return jQuery(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
45 };
46 });
47
48 // Focus search field on certain user interactions
49 $(document).on('keydown', function(e) {
50 var $searchInput = $(self.selectorSearchTrigger);
51 if (e.ctrlKey || e.metaKey) {
52 // Focus search field on ctrl-f
53 switch (String.fromCharCode(e.which).toLowerCase()) {
54 case 'f':
55 e.preventDefault();
56 $searchInput.focus();
57 break;
58 }
59 } else if (e.keyCode === 27) {
60 // Clear search on ESC key
61 e.preventDefault();
62 $searchInput.val('').focus();
63 }
64 });
65
66 // Perform expand collapse on search matches
67 $(document).on('keyup', this.selectorSearchTrigger, function() {
68 var typedQuery = $(this).val();
69 var $searchInput = $(self.selectorSearchTrigger);
70 $('div.item').each(function() {
71 var $item = $(this);
72 if ($(':contains(' + typedQuery + ')', $item).length > 0 || $('input[value*="' + typedQuery + '"]', $item).length > 0) {
73 $item.removeClass('hidden').addClass('searchhit');
74 } else {
75 $item.removeClass('searchhit').addClass('hidden');
76 }
77 });
78 $('.searchhit').parent().collapse('show');
79 self.handleButtonScrolling();
80 // Make search field clearable
81 require(['jquery.clearable'], function() {
82 var searchResultShown = ('' !== $searchInput.first().val());
83 $searchInput.clearable().focus();
84 });
85 });
86
87 // Trigger fixed button calculation on collapse / expand
88 $(document).on('shown.bs.collapse', '.gridder-show .collapse', function() {
89 self.handleButtonScrolling();
90 });
91 $(document).on('hidden.bs.collapse', '.gridder-show .collapse', function() {
92 self.handleButtonScrolling();
93 });
94 },
95
96 write: function() {
97 var url = location.href + '&install[controller]=ajax';
98 var configurationValues = {};
99 $('.gridder-show .t3js-localConfiguration-pathValue').each(function(i, element) {
100 var $element = $(element);
101 if ($element.attr('type') === 'checkbox') {
102 if (element.checked) {
103 configurationValues[$element.data('path')] = '1';
104 } else {
105 configurationValues[$element.data('path')] = '0';
106 }
107 } else {
108 configurationValues[$element.data('path')] = $element.val();
109 }
110 });
111 var postData = {
112 'install': {
113 'action': 'localConfigurationWrite',
114 'token': $(this.selectorWriteToken).text(),
115 'configurationValues': configurationValues
116 }
117 };
118 var $outputContainer = $(this.selectorOutputContainer);
119 var message = ProgressBar.render(Severity.loading, 'Loading...', '');
120 $outputContainer.empty().html(message);
121 $.ajax({
122 method: 'POST',
123 data: postData,
124 url: url,
125 cache: false,
126 success: function(data) {
127 $outputContainer.empty();
128 if (data.success === true && Array.isArray(data.status)) {
129 data.status.forEach(function(element) {
130 var message = InfoBox.render(element.severity, element.title, element.message);
131 $outputContainer.append(message);
132 });
133 }
134 },
135 error: function() {
136 var message = FlashMessage.render(Severity.error, 'Something went wrong', '');
137 $outputContainer.empty().html(message);
138 }
139 });
140 },
141
142 /**
143 * Fix or unfix the "Write configuration" / "Toggle all" buttons at browser window
144 * bottom if a scrollbar is shown
145 */
146 handleButtonScrolling: function() {
147 var $fixedFooterHandler = $('#fixed-footer-handler');
148 var $fixedFooter = $('#fixed-footer');
149 if (!this.isScrolledIntoView($fixedFooterHandler)) {
150 $fixedFooter.addClass('fixed');
151 $fixedFooter.width($('.t3js-localConfiguration .panel-group').width());
152 } else {
153 $fixedFooter.removeClass('fixed');
154 }
155 },
156
157 /**
158 * Helper of handleButtonScrolling()
159 * See if an element is within current viewport.
160 *
161 * @param element
162 * @returns {boolean}
163 */
164 isScrolledIntoView: function(element) {
165 var $window = $(window);
166 var docViewTop = $window.scrollTop();
167 var docViewBottom = docViewTop + $window.height();
168 var $elem = $(element);
169 var elemTop = $elem.offset().top;
170 var elemBottom = elemTop + $elem.height();
171 return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
172 }
173 };
174 });