Skip to content

Commit

Permalink
Fixes an issue where CP Clear Cache would disable submit buttons in t…
Browse files Browse the repository at this point in the history
…he Utilities section inside Crafts Control Panel. Bump to 1.1.0
  • Loading branch information
mmikkel committed Jan 21, 2021
1 parent 3389bf3 commit b2bc57d
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 12 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -22,7 +22,7 @@
}
],
"require": {
"craftcms/cms": "^3.0.0-RC1"
"craftcms/cms": "^3.5.0"
},
"autoload": {
"psr-4": {
Expand Down
51 changes: 43 additions & 8 deletions src/CpClearCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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 = "<div>$html</div>";
$view = Craft::$app->getView();
$view->registerAssetBundle(UtilitiesAsset::class);
Expand All @@ -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,
]);
}

}
92 changes: 90 additions & 2 deletions src/resources/cpclearcache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = $('<div class="alldone" data-icon="done" />').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 = {

Expand Down Expand Up @@ -44,7 +132,7 @@
onShow: function () {
Garnish.requestAnimationFrame(function () {
$html.find('form').each(function () {
new Craft.ClearCachesUtility(this.id);
new Craft.CpClearCachesUtility(this.id);
});
});
}
Expand Down Expand Up @@ -80,4 +168,4 @@

};

} (window));
}(window));

0 comments on commit b2bc57d

Please sign in to comment.