-
Notifications
You must be signed in to change notification settings - Fork 611
/
RenameFile.php
99 lines (86 loc) · 2.77 KB
/
RenameFile.php
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\elements\actions;
use Craft;
use craft\base\ElementAction;
/**
* RenameFile represents a Rename File element action.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class RenameFile extends ElementAction
{
/**
* @inheritdoc
*/
public function getTriggerLabel(): string
{
return Craft::t('app', 'Rename file');
}
/**
* @inheritdoc
*/
public function getTriggerHtml(): ?string
{
Craft::$app->getView()->registerJsWithVars(
fn($type, $prompt) => <<<JS
(() => {
new Craft.ElementActionTrigger({
type: $type,
bulk: false,
validateSelection: (selectedItems, elementIndex) => Garnish.hasAttr(selectedItems.find('.element'), 'data-movable'),
activate: (selectedItems, elementIndex) => {
const \$element = selectedItems.find('.element')
const assetId = \$element.data('id');
let oldName = \$element.data('filename');
const newName = prompt($prompt, oldName);
if (!newName || newName == oldName)
{
return;
}
elementIndex.setIndexBusy();
let currentFolderId = elementIndex.\$source.data('folder-id');
const currentFolder = elementIndex.sourcePath[elementIndex.sourcePath.length - 1];
if (currentFolder && currentFolder.folderId) {
currentFolderId = currentFolder.folderId;
}
const data = {
assetId: assetId,
folderId: currentFolderId,
filename: newName
};
Craft.sendActionRequest('POST', 'assets/move-asset', {data})
.then(response => {
if (response.data.conflict) {
alert(response.data.conflict);
this.activate(selectedItems);
return;
}
if (response.data.success) {
elementIndex.updateElements();
// If assets were just merged we should get the reference tags updated right away
Craft.cp.runQueue();
}
})
.catch(({response}) => {
alert(response.data.message)
})
.finally(() => {
elementIndex.setIndexAvailable();
});
},
});
})();
JS,
[
static::class,
Craft::t('app', 'Enter the new filename'),
]);
return null;
}
}