diff --git a/CHANGELOG.md b/CHANGELOG.md index b70199b..b651eec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## 1.1.0 - 2021-01-21 + +### Fixed +- Fixes an issue where CP Clear Cache would disable submit buttons in the Utilities section inside Craft's Control Panel + +### Changed +- CP Clear Cache now requires Craft 3.5.0+ + ## 1.0.7 - 2020-07-31 ### Improved diff --git a/composer.json b/composer.json index 4eb9a07..6af1e11 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "mmikkel/cp-clearcache", "description": "Less clickin’ to get clearin’", "type": "craft-plugin", - "version": "1.0.7", + "version": "1.1.0", "keywords": [ "craft", "cms", @@ -22,7 +22,7 @@ } ], "require": { - "craftcms/cms": "^3.0.0-RC1" + "craftcms/cms": "^3.5.0" }, "autoload": { "psr-4": { diff --git a/src/CpClearCache.php b/src/CpClearCache.php index ae60a08..2f4d561 100644 --- a/src/CpClearCache.php +++ b/src/CpClearCache.php @@ -14,7 +14,9 @@ use craft\base\Plugin; use craft\base\UtilityInterface; use craft\events\RegisterCpNavItemsEvent; +use craft\helpers\ArrayHelper; use craft\services\Plugins; +use craft\utilities\ClearCaches; use craft\web\Application; use craft\web\assets\utilities\UtilitiesAsset; use craft\web\twig\variables\Cp; @@ -63,6 +65,9 @@ public function init() parent::init(); self::$plugin = $this; + // Register alias + Craft::setAlias('@mmikkel/cpclearcache', __DIR__); + $request = Craft::$app->getRequest(); if (!$request->getIsCpRequest() || $request->getIsConsoleRequest()) { @@ -99,19 +104,13 @@ protected function doIt() return; } - // Register alias - Craft::setAlias('@mmikkel/cpclearcache', __DIR__); - // Register asset bundle Event::on( View::class, View::EVENT_BEGIN_BODY, - function () use ($clearCachesUtility) { + function () { try { - $html = $clearCachesUtility::contentHtml(); - if (!$html) { - return; - } + $html = $this->getClearCachesUtilityHtml(); $html = "
$html
"; $view = Craft::$app->getView(); $view->registerAssetBundle(UtilitiesAsset::class); @@ -136,4 +135,40 @@ function () use ($clearCachesUtility) { }); } + /** + * @return string + * @throws \Twig\Error\LoaderError + * @throws \Twig\Error\RuntimeError + * @throws \Twig\Error\SyntaxError + * @throws \yii\base\Exception + */ + protected function getClearCachesUtilityHtml(): string + { + $cacheOptions = []; + $tagOptions = []; + + foreach (ClearCaches::cacheOptions() as $cacheOption) { + $cacheOptions[] = [ + 'label' => $cacheOption['label'], + 'value' => $cacheOption['key'], + 'info' => $cacheOption['info'] ?? null, + ]; + } + + foreach (ClearCaches::tagOptions() as $tagOption) { + $tagOptions[] = [ + 'label' => $tagOption['label'], + 'value' => $tagOption['tag'], + ]; + } + + ArrayHelper::multisort($cacheOptions, 'label'); + $view = Craft::$app->getView(); + + return $view->renderTemplate('_components/utilities/ClearCaches', [ + 'cacheOptions' => $cacheOptions, + 'tagOptions' => $tagOptions, + ]); + } + } diff --git a/src/resources/cpclearcache.js b/src/resources/cpclearcache.js index 7977b1f..730355b 100644 --- a/src/resources/cpclearcache.js +++ b/src/resources/cpclearcache.js @@ -3,6 +3,94 @@ if (!window.Craft || !window.jQuery) { return false; } + + Craft.CpClearCachesUtility = Garnish.Base.extend( + { + init: function (formId) { + let $form = $('#' + formId); + let $checkboxes = $form.find('input[type=checkbox]'); + let $btn = $form.find('.btn'); + let checkInputs = function () { + if ($checkboxes.filter(':checked').length) { + $btn.removeClass('disabled'); + } else { + $btn.addClass('disabled'); + } + }; + $checkboxes.on('change', checkInputs); + checkInputs(); + this.addListener($form, 'submit', ev => { + ev.preventDefault(); + if (!$btn.hasClass('disabled')) { + this.onSubmit(ev); + } + }); + }, + + onSubmit: function (ev) { + let $form = $(ev.currentTarget); + let $trigger = $form.find('button.submit'); + let $status = $form.find('.utility-status'); + + if ($trigger.hasClass('disabled')) { + return; + } + + let progressBar, $allDone; + if (!$form.data('progressBar')) { + progressBar = new Craft.ProgressBar($status); + $form.data('progressBar', progressBar); + } else { + progressBar = $form.data('progressBar'); + progressBar.resetProgressBar(); + $allDone = $form.data('allDone'); + } + + progressBar.$progressBar.removeClass('hidden'); + + progressBar.$progressBar.velocity('stop').velocity({ + opacity: 1 + }, { + complete: $.proxy(function () { + let postData = Garnish.getPostData($form); + let params = Craft.expandPostArray(postData); + + Craft.postActionRequest(params.action, params, (response, textStatus) => { + if (response && response.error) { + alert(response.error); + } + + progressBar.setProgressPercentage(100); + + setTimeout(() => { + if (!$allDone) { + $allDone = $('
').appendTo($status); + $allDone.css('opacity', 0); + $form.data('allDone', $allDone); + } + + progressBar.$progressBar.velocity({ opacity: 0 }, { + duration: 'fast', complete: () => { + $allDone.velocity({ opacity: 1 }, { duration: 'fast' }); + $trigger.removeClass('disabled'); + $trigger.trigger('focus'); + }, + }); + }, 300); + }, { + complete: $.noop + }); + }, this) + }); + + if ($allDone) { + $allDone.css('opacity', 0); + } + + $trigger.addClass('disabled'); + $trigger.trigger('blur'); + }, + }); Craft.CpClearCachePlugin = { @@ -44,7 +132,7 @@ onShow: function () { Garnish.requestAnimationFrame(function () { $html.find('form').each(function () { - new Craft.ClearCachesUtility(this.id); + new Craft.CpClearCachesUtility(this.id); }); }); } @@ -80,4 +168,4 @@ }; -} (window)); +}(window));