089245cd4cff899f12d60aa3bbf70458b7fca525
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Resources / Public / JavaScript / Modules / CoreUpdate.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/CoreUpdate
16 */
17 define([
18 'jquery',
19 'TYPO3/CMS/Install/Router',
20 'TYPO3/CMS/Install/FlashMessage',
21 'TYPO3/CMS/Install/Severity'
22 ], function($, Router, FlashMessage, Severity) {
23 'use strict';
24
25 return {
26 /**
27 * The action queue defines what actions are called in which order
28 */
29 actionQueue: {
30 coreUpdateUpdateVersionMatrix: {
31 loadingMessage: 'Fetching list of released versions from typo3.org',
32 finishMessage: 'Fetched list of released versions',
33 nextActionName: 'coreUpdateIsUpdateAvailable'
34 },
35 coreUpdateIsUpdateAvailable: {
36 loadingMessage: 'Checking for possible regular or security update',
37 finishMessage: undefined,
38 nextActionName: undefined
39 },
40 coreUpdateCheckPreConditions: {
41 loadingMessage: 'Checking if update is possible',
42 finishMessage: 'System can be updated',
43 nextActionName: 'coreUpdateDownload'
44 },
45 coreUpdateDownload: {
46 loadingMessage: 'Downloading new core',
47 finishMessage: undefined,
48 nextActionName: 'coreUpdateVerifyChecksum'
49 },
50 coreUpdateVerifyChecksum: {
51 loadingMessage: 'Verifying checksum of downloaded core',
52 finishMessage: undefined,
53 nextActionName: 'coreUpdateUnpack'
54 },
55 coreUpdateUnpack: {
56 loadingMessage: 'Unpacking core',
57 finishMessage: undefined,
58 nextActionName: 'coreUpdateMove'
59 },
60 coreUpdateMove: {
61 loadingMessage: 'Moving core',
62 finishMessage: undefined,
63 nextActionName: 'coreUpdateClearAllCache'
64 },
65 clearAllCache: {
66 loadingMessage: 'Clearing caches',
67 finishMessage: 'Caches cleared',
68 nextActionName: 'coreUpdateActivate'
69 },
70 coreUpdateActivate: {
71 loadingMessage: 'Activating core',
72 finishMessage: 'Core updated - please reload your browser',
73 nextActionName: undefined
74 }
75 },
76
77 selectorOutput: '.t3js-coreUpdate-output',
78 selectorTemplate: '.t3js-coreUpdate-buttonTemplate',
79
80 /**
81 * Clone of a DOM object acts as button template
82 */
83 buttonTemplate: null,
84
85 /**
86 * Fetching the templates out of the DOM
87 */
88 initialize: function() {
89 var self = this;
90 var buttonTemplateSection = $(self.selectorTemplate);
91 this.buttonTemplate = buttonTemplateSection.children().clone();
92
93 $(document).on('click', '.t3js-coreUpdate-init', function(e) {
94 e.preventDefault();
95 var action = $(e.target).data('action');
96 $(document).find(self.selectorOutput).empty();
97 self[action]();
98 });
99 },
100
101 /**
102 * Public method checkForUpdate
103 */
104 checkForUpdate: function() {
105 this.callAction('coreUpdateUpdateVersionMatrix');
106 },
107
108 /**
109 * Public method updateDevelopment
110 */
111 updateDevelopment: function() {
112 this.update('development');
113 },
114
115 updateRegular: function() {
116 this.update('regular');
117 },
118
119 /**
120 * Execute core update.
121 *
122 * @param type Either 'development' or 'regular'
123 */
124 update: function(type) {
125 if (type !== "development") {
126 type = 'regular';
127 }
128 this.callAction('coreUpdateCheckPreConditions', type);
129 },
130
131 /**
132 * Generic method to call actions from the queue
133 *
134 * @param actionName Name of the action to be called
135 * @param type Update type (optional)
136 */
137 callAction: function(actionName, type) {
138 var self = this;
139 var data = {
140 install: {
141 action: actionName
142 }
143 };
144 if (type !== undefined) {
145 data.install["type"] = type;
146 }
147 this.addLoadingMessage(this.actionQueue[actionName].loadingMessage);
148 $.ajax({
149 url: Router.getUrl(),
150 data: data,
151 cache: false,
152 success: function(result) {
153 var canContinue = self.handleResult(result, self.actionQueue[actionName].finishMessage);
154 if (canContinue === true && (self.actionQueue[actionName].nextActionName !== undefined)) {
155 self.callAction(self.actionQueue[actionName].nextActionName, type);
156 }
157 },
158 error: function(result) {
159 self.handleResult(result);
160 }
161 });
162 },
163
164 /**
165 * Handle ajax result of core update step.
166 *
167 * @param data
168 * @param successMessage Optional success message
169 */
170 handleResult: function(data, successMessage) {
171 var canContinue = false;
172 this.removeLoadingMessage();
173 if (data.success === true) {
174 canContinue = true;
175 if (data.status && typeof(data.status) === 'object') {
176 this.showStatusMessages(data.status);
177 }
178 if (data.action && typeof(data.action) === 'object') {
179 this.showActionButton(data.action);
180 }
181 if (successMessage) {
182 this.addMessage(Severity.ok, successMessage);
183 }
184 } else {
185 Router.handleAjaxError(xhr);
186 }
187 return canContinue;
188 },
189
190 /**
191 * Add a loading message with some text.
192 *
193 * @param messageTitle
194 */
195 addLoadingMessage: function(messageTitle) {
196 var domMessage = FlashMessage.render(Severity.loading, messageTitle);
197 $(this.selectorOutput).append(domMessage);
198 },
199
200 /**
201 * Remove an enabled loading message
202 */
203 removeLoadingMessage: function() {
204 $(this.selectorOutput).find('.alert-loading').remove();
205 },
206
207 /**
208 * Show a list of status messages
209 *
210 * @param messages
211 */
212 showStatusMessages: function(messages) {
213 var self = this;
214 $.each(messages, function(index, element) {
215 var title = false;
216 var message = false;
217 var severity = element.severity;
218 if (element.title) {
219 title = element.title;
220 }
221 if (element.message) {
222 message = element.message;
223 }
224 self.addMessage(severity, title, message);
225 });
226 },
227
228 /**
229 * Show an action button
230 *
231 * @param button
232 */
233 showActionButton: function(button) {
234 var title = false;
235 var action = false;
236 if (button.title) {
237 title = button.title;
238 }
239 if (button.action) {
240 action = button.action;
241 }
242 var domButton = this.buttonTemplate;
243 if (action) {
244 domButton.find('button').data('action', action);
245 }
246 if (title) {
247 domButton.find('button').text(title);
248 }
249 $(this.selectorOutput).append(domButton);
250 },
251
252 /**
253 * Show a status message
254 *
255 * @param severity
256 * @param title
257 * @param message
258 */
259 addMessage: function(severity, title, message) {
260 var domMessage = FlashMessage.render(severity, title, message);
261 $(this.selectorOutput).append(domMessage);
262 }
263 };
264 });