-
Notifications
You must be signed in to change notification settings - Fork 690
Expand file tree
/
Copy pathDeleteAssets.php
More file actions
61 lines (54 loc) · 1.71 KB
/
DeleteAssets.php
File metadata and controls
61 lines (54 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
declare(strict_types=1);
namespace CraftCms\Cms\Asset\Actions;
use CraftCms\Cms\Element\Actions\Delete;
use CraftCms\Cms\Support\Facades\HtmlStack;
class DeleteAssets extends Delete
{
#[\Override]
public function getTriggerHtml(): ?string
{
// Only enable for deletable elements, per canDelete()
HtmlStack::jsWithVars(fn ($type) => <<<JS
(() => {
const trigger = new Craft.ElementActionTrigger({
type: $type,
requireId: false,
validateSelection: (selectedItems, elementIndex) => {
for (let i = 0; i < selectedItems.length; i++) {
const element = selectedItems.eq(i).find('.element');
if (Garnish.hasAttr(element, 'data-is-folder')) {
if (selectedItems.length !== 1) {
// only one folder at a time
return false;
}
const sourcePath = element.data('source-path') || [];
if (!sourcePath.length || !sourcePath[sourcePath.length - 1].canDelete) {
return false;
}
} else {
if (!Garnish.hasAttr(element, 'data-deletable')) {
return false;
}
}
}
return true;
},
activate: (selectedItems, elementIndex) => {
const element = selectedItems.find('.element:first');
if (Garnish.hasAttr(element, 'data-is-folder')) {
const sourcePath = element.data('source-path');
elementIndex.deleteFolder(sourcePath[sourcePath.length - 1])
.then(() => {
elementIndex.updateElements();
});
} else {
elementIndex.submitAction(trigger.\$trigger.data('action'), Garnish.getPostData(trigger.\$trigger));
}
},
});
})();
JS, [static::class]);
return null;
}
}