-
Notifications
You must be signed in to change notification settings - Fork 268
/
ImageActions.svelte
156 lines (139 loc) · 4.59 KB
/
ImageActions.svelte
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<script lang="ts">
import {
faArrowUp,
faEllipsisVertical,
faLayerGroup,
faPlay,
faTrash,
faEdit,
} from '@fortawesome/free-solid-svg-icons';
import type { ImageInfoUI } from './ImageInfoUI';
import { router } from 'tinro';
import ListItemButtonIcon from '../ui/ListItemButtonIcon.svelte';
import DropdownMenu from '../ui/DropdownMenu.svelte';
import FlatMenu from '../ui/FlatMenu.svelte';
import { runImageInfo } from '../../stores/run-image-store';
import type { Menu } from '../../../../main/src/plugin/menu-registry';
import { ImageUtils } from './image-utils';
export let onPushImage: (imageInfo: ImageInfoUI) => void;
export let onRenameImage: (imageInfo: ImageInfoUI) => void;
export let image: ImageInfoUI;
export let dropdownMenu = false;
export let detailed = false;
export let contributions: Menu[] = [];
let errorTitle: string | undefined = undefined;
let errorMessage: string | undefined = undefined;
let isAuthenticatedForThisImage = false;
const imageUtils = new ImageUtils();
async function runImage(imageInfo: ImageInfoUI) {
runImageInfo.set(imageInfo);
router.goto('/images/run/basic');
}
$: window.hasAuthconfigForImage(image.name).then(result => (isAuthenticatedForThisImage = result));
async function deleteImage(): Promise<void> {
try {
await imageUtils.deleteImage(image);
} catch (error) {
errorTitle = 'Error while deleting image';
errorMessage = String(error);
}
}
async function renameImage(imageInfo: ImageInfoUI): Promise<void> {
onRenameImage(imageInfo);
}
async function pushImage(imageInfo: ImageInfoUI): Promise<void> {
onPushImage(imageInfo);
}
async function showLayersImage(): Promise<void> {
router.goto(`/images/${image.id}/${image.engineId}/${image.base64RepoTag}/history`);
}
async function executeContribution(menu: Menu): Promise<void> {
try {
await window.executeCommand(menu.command, image);
} catch (err) {
errorTitle = `Error while executing ${menu.title}`;
errorMessage = String(err);
}
}
// If dropdownMenu = true, we'll change style to the imported dropdownMenu style
// otherwise, leave blank.
let actionsStyle: typeof DropdownMenu | typeof FlatMenu;
if (dropdownMenu) {
actionsStyle = DropdownMenu;
} else {
actionsStyle = FlatMenu;
}
</script>
<ListItemButtonIcon title="Run Image" onClick="{() => runImage(image)}" detailed="{detailed}" icon="{faPlay}" />
<ListItemButtonIcon
title="Delete Image"
onClick="{() => deleteImage()}"
detailed="{detailed}"
icon="{faTrash}"
enabled="{!image.inUse}" />
<!-- If dropdownMenu is true, use it, otherwise just show the regular buttons -->
<svelte:component this="{actionsStyle}">
{#if isAuthenticatedForThisImage}
<ListItemButtonIcon
title="Push Image"
onClick="{() => pushImage(image)}"
menu="{dropdownMenu}"
detailed="{detailed}"
icon="{faArrowUp}" />
{/if}
<ListItemButtonIcon
title="Edit Image"
onClick="{() => renameImage(image)}"
menu="{dropdownMenu}"
detailed="{detailed}"
icon="{faEdit}" />
{#if !detailed}
<ListItemButtonIcon
title="Show History"
onClick="{() => showLayersImage()}"
menu="{dropdownMenu}"
detailed="{detailed}"
icon="{faLayerGroup}" />
{/if}
{#each contributions as menu}
<ListItemButtonIcon
title="{menu.title}"
onClick="{() => executeContribution(menu)}"
menu="{dropdownMenu}"
icon="{faEllipsisVertical}" />
{/each}
{#if errorMessage}
<div class="modal fixed w-full h-full top-0 left-0 flex items-center justify-center p-8 lg:p-0 z-50" tabindex="-1">
<div class="pf-c-alert pf-m-danger pf-m-inline" aria-label="Success alert">
<div class="pf-c-alert__icon">
<i class="fas fa-fw fa-exclamation-circle" aria-hidden="true"></i>
</div>
<p class="pf-c-alert__title">
<span class="pf-screen-reader">Error:</span>
{errorTitle}
</p>
<div class="pf-c-alert__action">
<button
class="pf-c-button pf-m-plain"
type="button"
on:click="{() => {
errorMessage = undefined;
}}">
<i class="fas fa-times" aria-hidden="true"></i>
</button>
</div>
<div class="pf-c-alert__description">
<p class="flex break-words whitespace-normal">{errorMessage}</p>
</div>
<div class="pf-c-alert__action-group">
<button
class="pf-c-button pf-m-link pf-m-inline"
type="button"
on:click="{() => {
errorMessage = undefined;
}}">Ignore</button>
</div>
</div>
</div>
{/if}
</svelte:component>