88a00a236dc8f7489e31c05f15bf43324f000d85
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / Toolbar / ClearCacheMenu.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/Backend/Toolbar/ClearCacheMenu
16 * main functionality for clearing caches via the top bar
17 * reloading the clear cache icon
18 */
19 define([
20 'jquery',
21 'TYPO3/CMS/Backend/Icons',
22 'TYPO3/CMS/Backend/Notification',
23 'TYPO3/CMS/Backend/Viewport'
24 ], function($, Icons, Notification, Viewport) {
25 'use strict';
26
27 /**
28 *
29 * @type {{options: {containerSelector: string, menuItemSelector: string, toolbarIconSelector: string}}}
30 * @exports TYPO3/CMS/Backend/Toolbar/ClearCacheMenu
31 */
32 var ClearCacheMenu = {
33 options: {
34 containerSelector: '#typo3-cms-backend-backend-toolbaritems-clearcachetoolbaritem',
35 menuItemSelector: 'a.toolbar-cache-flush-action',
36 toolbarIconSelector: '.toolbar-item-icon .t3js-icon'
37 }
38 };
39
40 /**
41 * Registers listeners for the icons inside the dropdown to trigger
42 * the clear cache call
43 */
44 ClearCacheMenu.initializeEvents = function() {
45 $(ClearCacheMenu.options.containerSelector).on('click', ClearCacheMenu.options.menuItemSelector, function(evt) {
46 evt.preventDefault();
47 var ajaxUrl = $(this).attr('href');
48 if (ajaxUrl) {
49 ClearCacheMenu.clearCache(ajaxUrl);
50 }
51 });
52 };
53
54 /**
55 * calls TYPO3 to clear a cache, then changes the topbar icon
56 * to a spinner. Restores the original topbar icon when the request completed.
57 *
58 * @param {String} ajaxUrl the URL to load
59 */
60 ClearCacheMenu.clearCache = function(ajaxUrl) {
61 // Close clear cache menu
62 $(ClearCacheMenu.options.containerSelector).removeClass('open');
63
64 var $toolbarItemIcon = $(ClearCacheMenu.options.toolbarIconSelector, ClearCacheMenu.options.containerSelector),
65 $existingIcon = $toolbarItemIcon.clone();
66
67 Icons.getIcon('spinner-circle-light', Icons.sizes.small).done(function(spinner) {
68 $toolbarItemIcon.replaceWith(spinner);
69 });
70
71 $.ajax({
72 url: ajaxUrl,
73 type: 'post',
74 cache: false,
75 complete: function(jqXHRObject, status) {
76 $(ClearCacheMenu.options.toolbarIconSelector, ClearCacheMenu.options.containerSelector).replaceWith($existingIcon);
77 if (status !== 'success' || jqXHRObject.responseText !== '') {
78 Notification.error('An error occurs', 'An error occurred while clearing the cache. It is likely not all caches were cleared as expected.', 0);
79 }
80 }
81 });
82 };
83
84 Viewport.Topbar.Toolbar.registerEvent(ClearCacheMenu.initializeEvents);
85
86 return ClearCacheMenu;
87 });