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