-
Notifications
You must be signed in to change notification settings - Fork 638
/
ClearTypo3tempFiles.ts
117 lines (109 loc) · 4.52 KB
/
ClearTypo3tempFiles.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
import * as $ from 'jquery';
import {AjaxResponse} from 'TYPO3/CMS/Core/Ajax/AjaxResponse';
import {ResponseError} from 'TYPO3/CMS/Core/Ajax/ResponseError';
import {AbstractInteractableModule} from '../AbstractInteractableModule';
import Modal = require('TYPO3/CMS/Backend/Modal');
import Notification = require('TYPO3/CMS/Backend/Notification');
import AjaxRequest = require('TYPO3/CMS/Core/Ajax/AjaxRequest');
import Router = require('../../Router');
/**
* Module: TYPO3/CMS/Install/Module/ClearTypo3tempFiles
*/
class ClearTypo3tempFiles extends AbstractInteractableModule {
private selectorDeleteTrigger: string = '.t3js-clearTypo3temp-delete';
private selectorOutputContainer: string = '.t3js-clearTypo3temp-output';
private selectorStatContainer: string = '.t3js-clearTypo3temp-stat-container';
private selectorStatsTrigger: string = '.t3js-clearTypo3temp-stats';
private selectorStatTemplate: string = '.t3js-clearTypo3temp-stat-template';
private selectorStatNumberOfFiles: string = '.t3js-clearTypo3temp-stat-numberOfFiles';
private selectorStatDirectory: string = '.t3js-clearTypo3temp-stat-directory';
public initialize(currentModal: JQuery): void {
this.currentModal = currentModal;
this.getStats();
currentModal.on('click', this.selectorStatsTrigger, (e: JQueryEventObject): void => {
e.preventDefault();
$(this.selectorOutputContainer).empty();
this.getStats();
});
currentModal.on('click', this.selectorDeleteTrigger, (e: JQueryEventObject): void => {
const folder = $(e.currentTarget).data('folder');
const storageUid = $(e.currentTarget).data('storage-uid');
e.preventDefault();
this.delete(folder, storageUid);
});
}
private getStats(): void {
this.setModalButtonsState(false);
const modalContent = this.getModalBody();
(new AjaxRequest(Router.getUrl('clearTypo3tempFilesStats')))
.get({cache: 'no-cache'})
.then(
async (response: AjaxResponse): Promise<any> => {
const data = await response.resolve();
if (data.success === true) {
modalContent.empty().append(data.html);
Modal.setButtons(data.buttons);
if (Array.isArray(data.stats) && data.stats.length > 0) {
data.stats.forEach((element: any): void => {
if (element.numberOfFiles > 0) {
const aStat = modalContent.find(this.selectorStatTemplate).clone();
aStat.find(this.selectorStatNumberOfFiles).text(element.numberOfFiles);
aStat.find(this.selectorStatDirectory).text(element.directory);
aStat.find(this.selectorDeleteTrigger).attr('data-folder', element.directory);
aStat.find(this.selectorDeleteTrigger).attr('data-storage-uid', element.storageUid);
modalContent.find(this.selectorStatContainer).append(aStat.html());
}
});
}
} else {
Notification.error('Something went wrong');
}
},
(error: ResponseError): void => {
Router.handleAjaxError(error, modalContent);
}
);
}
private delete(folder: string, storageUid: number): void {
const modalContent = this.getModalBody();
const executeToken = this.getModuleContent().data('clear-typo3temp-delete-token');
(new AjaxRequest(Router.getUrl()))
.post({
install: {
action: 'clearTypo3tempFiles',
token: executeToken,
folder: folder,
storageUid: storageUid,
},
})
.then(
async (response: AjaxResponse): Promise<any> => {
const data = await response.resolve();
if (data.success === true && Array.isArray(data.status)) {
data.status.forEach((element: any): void => {
Notification.success(element.message);
});
this.getStats();
} else {
Notification.error('Something went wrong');
}
},
(error: ResponseError): void => {
Router.handleAjaxError(error, modalContent);
}
);
}
}
export = new ClearTypo3tempFiles();