Skip to content
Snippets Groups Projects
Commit 76dd0d78 authored by Andreas Kienast's avatar Andreas Kienast Committed by Benni Mack
Browse files

[TASK] Migrate Toolbar/ClearCacheMenu to TypeScript

Resolves: #84127
Releases: master
Change-Id: I60ee74644a04354e6939aa8e5acc452004e1f582
Reviewed-on: https://review.typo3.org/56002


Tested-by: default avatarTYPO3com <no-reply@typo3.com>
Reviewed-by: default avatarMathias Schreiber <mathias.schreiber@typo3.com>
Tested-by: default avatarMathias Schreiber <mathias.schreiber@typo3.com>
Reviewed-by: default avatarBenni Mack <benni@typo3.org>
Tested-by: default avatarBenni Mack <benni@typo3.org>
parent 3b39a1a9
No related branches found
No related tags found
No related merge requests found
/*
* 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 Icons = require('../Icons');
import Notification = require('../Notification');
import Viewport = require('../Viewport');
enum Identifiers {
containerSelector = '#typo3-cms-backend-backend-toolbaritems-clearcachetoolbaritem',
menuItemSelector = 'a.toolbar-cache-flush-action',
toolbarIconSelector = '.toolbar-item-icon .t3js-icon'
}
/**
* Module: TYPO3/CMS/Backend/Toolbar/ClearCacheMenu
* main functionality for clearing caches via the top bar
* reloading the clear cache icon
*/
class ClearCacheMenu {
constructor() {
Viewport.Topbar.Toolbar.registerEvent(this.initializeEvents);
}
/**
* Registers listeners for the icons inside the dropdown to trigger
* the clear cache call
*/
private initializeEvents = (): void => {
$(Identifiers.containerSelector).on('click', Identifiers.menuItemSelector, (evt: JQueryEventObject): void => {
evt.preventDefault();
const ajaxUrl = $(evt.currentTarget).attr('href');
if (ajaxUrl) {
this.clearCache(ajaxUrl);
}
});
}
/**
* Calls TYPO3 to clear a cache, then changes the topbar icon
* to a spinner. Restores the original topbar icon when the request completed.
*
* @param {string} ajaxUrl The URL to load
*/
private clearCache(ajaxUrl: string): void {
// Close clear cache menu
$(Identifiers.containerSelector).removeClass('open');
const $toolbarItemIcon = $(Identifiers.toolbarIconSelector, Identifiers.containerSelector);
const $existingIcon = $toolbarItemIcon.clone();
Icons.getIcon('spinner-circle-light', Icons.sizes.small).done((spinner: string): void => {
$toolbarItemIcon.replaceWith(spinner);
});
$.ajax({
url: ajaxUrl,
type: 'post',
cache: false,
complete: (jqXHRObject: JQueryXHR, status: string): void => {
$(Identifiers.toolbarIconSelector, Identifiers.containerSelector).replaceWith($existingIcon);
if (status !== 'success' || jqXHRObject.responseText !== '') {
Notification.error(
'An error occurred',
'An error occurred while clearing the cache. It is likely not all caches were cleared as expected.'
);
}
}
});
}
}
export = new ClearCacheMenu();
......@@ -10,78 +10,4 @@
*
* The TYPO3 project - inspiring people to share!
*/
/**
* Module: TYPO3/CMS/Backend/Toolbar/ClearCacheMenu
* main functionality for clearing caches via the top bar
* reloading the clear cache icon
*/
define([
'jquery',
'TYPO3/CMS/Backend/Icons',
'TYPO3/CMS/Backend/Notification',
'TYPO3/CMS/Backend/Viewport'
], function($, Icons, Notification, Viewport) {
'use strict';
/**
*
* @type {{options: {containerSelector: string, menuItemSelector: string, toolbarIconSelector: string}}}
* @exports TYPO3/CMS/Backend/Toolbar/ClearCacheMenu
*/
var ClearCacheMenu = {
options: {
containerSelector: '#typo3-cms-backend-backend-toolbaritems-clearcachetoolbaritem',
menuItemSelector: 'a.toolbar-cache-flush-action',
toolbarIconSelector: '.toolbar-item-icon .t3js-icon'
}
};
/**
* Registers listeners for the icons inside the dropdown to trigger
* the clear cache call
*/
ClearCacheMenu.initializeEvents = function() {
$(ClearCacheMenu.options.containerSelector).on('click', ClearCacheMenu.options.menuItemSelector, function(evt) {
evt.preventDefault();
var ajaxUrl = $(this).attr('href');
if (ajaxUrl) {
ClearCacheMenu.clearCache(ajaxUrl);
}
});
};
/**
* calls TYPO3 to clear a cache, then changes the topbar icon
* to a spinner. Restores the original topbar icon when the request completed.
*
* @param {String} ajaxUrl the URL to load
*/
ClearCacheMenu.clearCache = function(ajaxUrl) {
// Close clear cache menu
$(ClearCacheMenu.options.containerSelector).removeClass('open');
var $toolbarItemIcon = $(ClearCacheMenu.options.toolbarIconSelector, ClearCacheMenu.options.containerSelector),
$existingIcon = $toolbarItemIcon.clone();
Icons.getIcon('spinner-circle-light', Icons.sizes.small).done(function(spinner) {
$toolbarItemIcon.replaceWith(spinner);
});
$.ajax({
url: ajaxUrl,
type: 'post',
cache: false,
complete: function(jqXHRObject, status) {
$(ClearCacheMenu.options.toolbarIconSelector, ClearCacheMenu.options.containerSelector).replaceWith($existingIcon);
if (status !== 'success' || jqXHRObject.responseText !== '') {
Notification.error('An error occurs', 'An error occurred while clearing the cache. It is likely not all caches were cleared as expected.', 0);
}
}
});
};
Viewport.Topbar.Toolbar.registerEvent(ClearCacheMenu.initializeEvents);
return ClearCacheMenu;
});
define(["require","exports","jquery","../Icons","../Notification","../Viewport"],function(e,t,r,c,o,n){"use strict";var i,a;return(a=i||(i={})).containerSelector="#typo3-cms-backend-backend-toolbaritems-clearcachetoolbaritem",a.menuItemSelector="a.toolbar-cache-flush-action",a.toolbarIconSelector=".toolbar-item-icon .t3js-icon",new(function(){function e(){var e=this;this.initializeEvents=function(){r(i.containerSelector).on("click",i.menuItemSelector,function(t){t.preventDefault();var c=r(t.currentTarget).attr("href");c&&e.clearCache(c)})},n.Topbar.Toolbar.registerEvent(this.initializeEvents)}return e.prototype.clearCache=function(e){r(i.containerSelector).removeClass("open");var t=r(i.toolbarIconSelector,i.containerSelector),n=t.clone();c.getIcon("spinner-circle-light",c.sizes.small).done(function(e){t.replaceWith(e)}),r.ajax({url:e,type:"post",cache:!1,complete:function(e,t){r(i.toolbarIconSelector,i.containerSelector).replaceWith(n),"success"===t&&""===e.responseText||o.error("An error occurred","An error occurred while clearing the cache. It is likely not all caches were cleared as expected.")}})},e}())});
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment