c9480c6cfb86696a56dee7026fb94dd10642b737
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Resources / Public / JavaScript / Modules / DatabaseAnalyzer.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/DatabaseAnalyzer
16 */
17 define([
18 'jquery',
19 'TYPO3/CMS/Install/Router',
20 'TYPO3/CMS/Install/FlashMessage',
21 'TYPO3/CMS/Install/ProgressBar',
22 'TYPO3/CMS/Install/InfoBox',
23 'TYPO3/CMS/Install/Severity',
24 'TYPO3/CMS/Backend/Notification'
25 ], function($, Router, FlashMessage, ProgressBar, InfoBox, Severity, Notification) {
26 'use strict';
27
28 return {
29
30 selectorModalBody: '.t3js-modal-body',
31 selectorAnalyzeTrigger: '.t3js-databaseAnalyzer-analyze',
32 selectorExecuteTrigger: '.t3js-databaseAnalyzer-execute',
33 selectorOutputContainer: '.t3js-databaseAnalyzer-output',
34 selectorSuggestionBlock: '.t3js-databaseAnalyzer-suggestion-block',
35 selectorSuggestionLine: '.t3js-databaseAnalyzer-suggestion-line',
36 currentModal: {},
37
38 initialize: function(currentModal) {
39 var self = this;
40 this.currentModal = currentModal;
41 self.analyzeAjax();
42
43 // Select / deselect all checkboxes
44 currentModal.on('click', '.t3js-databaseAnalyzer-suggestion-block-checkbox', function(e) {
45 $(this).closest('fieldset').find(':checkbox').prop('checked', this.checked);
46 });
47 currentModal.on('click', this.selectorAnalyzeTrigger, function(e) {
48 e.preventDefault();
49 self.analyze();
50 });
51 currentModal.on('click', this.selectorExecuteTrigger, function(e) {
52 e.preventDefault();
53 self.execute();
54 });
55
56 },
57
58 analyze: function() {
59 this.currentModal.find(this.selectorOutputContainer).empty();
60 this.analyzeAjax();
61 },
62
63 analyzeAjax: function() {
64 var self = this;
65 var modalContent = this.currentModal.find(self.selectorModalBody);
66 var message = ProgressBar.render(Severity.loading, 'Loading...', '');
67 modalContent.find(self.selectorOutputContainer).append(message);
68 $(this.selectorExecuteTrigger).prop('disabled', true);
69 $(this.selectorAnalyzeTrigger).prop('disabled', true);
70
71 $.ajax({
72 url: Router.getUrl('databaseAnalyzerAnalyze'),
73 cache: false,
74 success: function(data) {
75 if (data.success === true) {
76 if (Array.isArray(data.status)) {
77 data.status.forEach(function(element) {
78 var message = InfoBox.render(element.severity, element.title, element.message);
79 modalContent.find(self.selectorOutputContainer).find('.alert-loading').remove();
80 modalContent.find(self.selectorOutputContainer).append(message);
81 });
82 }
83 modalContent.empty().append(data.html);
84 if (Array.isArray(data.suggestions)) {
85 data.suggestions.forEach(function(element) {
86 var aBlock = modalContent.find(self.selectorSuggestionBlock).clone();
87 aBlock.removeClass(self.selectorSuggestionBlock.substr(1));
88 var key = element.key;
89 aBlock.find('.t3js-databaseAnalyzer-suggestion-block-legend').text(element.label);
90 aBlock.find('.t3js-databaseAnalyzer-suggestion-block-checkbox').attr('id', 't3-install-' + key + '-checkbox');
91 if (element.enabled) {
92 aBlock.find('.t3js-databaseAnalyzer-suggestion-block-checkbox').attr('checked', 'checked');
93 }
94 aBlock.find('.t3js-databaseAnalyzer-suggestion-block-label').attr('for', 't3-install-' + key + '-checkbox');
95 element.children.forEach(function(line) {
96 var aLine = modalContent.find(self.selectorSuggestionLine).clone();
97 aLine.removeClass(self.selectorSuggestionLine.substr(1));
98 var hash = line.hash;
99 var $checkbox = aLine.find('.t3js-databaseAnalyzer-suggestion-line-checkbox');
100 $checkbox.attr('id', 't3-install-db-' + hash).attr('data-hash', hash);
101 if (element.enabled) {
102 $checkbox.attr('checked', 'checked');
103 }
104 aLine.find('.t3js-databaseAnalyzer-suggestion-line-label').attr('for', 't3-install-db-' + hash);
105 aLine.find('.t3js-databaseAnalyzer-suggestion-line-statement').text(line.statement);
106 if (typeof line.current !== 'undefined') {
107 aLine.find('.t3js-databaseAnalyzer-suggestion-line-current-value').text(line.current);
108 aLine.find('.t3js-databaseAnalyzer-suggestion-line-current').show();
109 }
110 if (typeof line.rowCount !== 'undefined') {
111 aLine.find('.t3js-databaseAnalyzer-suggestion-line-count-value').text(line.rowCount);
112 aLine.find('.t3js-databaseAnalyzer-suggestion-line-count').show();
113 }
114 aBlock.find('.t3js-databaseAnalyzer-suggestion-block-line').append(aLine);
115 });
116 modalContent.find(self.selectorOutputContainer).append(aBlock.html());
117 });
118 self.currentModal.find(self.selectorExecuteTrigger).prop('disabled', false);
119 self.currentModal.find(self.selectorAnalyzeTrigger).prop('disabled', false);
120 }
121 } else {
122 Notification.error('Something went wrong');
123 }
124 },
125 error: function(xhr) {
126 Router.handleAjaxError(xhr);
127 }
128 });
129 },
130
131 execute: function() {
132 var self = this;
133 var executeToken = self.currentModal.find('#t3js-databaseAnalyzer-execute-token').text();
134 var selectedHashes = [];
135 self.currentModal.find('.t3js-databaseAnalyzer-output .t3js-databaseAnalyzer-suggestion-block-line input:checked').each(function() {
136 selectedHashes.push($(this).data('hash'));
137 });
138 $(this.selectorExecuteTrigger).prop('disabled', true);
139 $(this.selectorAnalyzeTrigger).prop('disabled', true);
140 $.ajax({
141 url: Router.getUrl(),
142 method: 'POST',
143 data: {
144 'install': {
145 'action': 'databaseAnalyzerExecute',
146 'token': executeToken,
147 'hashes': selectedHashes
148 }
149 },
150 cache: false,
151 success: function(data) {
152 if (data.success === true) {
153 if (Array.isArray(data.status)) {
154 data.status.forEach(function(element) {
155 Notification.showMessage(element.title, element.message, element.severity);
156 });
157 }
158 }
159 self.analyzeAjax();
160 },
161 error: function(xhr) {
162 Router.handleAjaxError(xhr);
163 }
164 });
165 }
166 };
167 });