/**
* javascript/images.js
*
* Images panel: table + a "Pull Image" action, backed by ajax/images.php.
*/
(function () {
'use strict';
const P = window.Podman;
let images = [];
function rowHtml(img) {
const created = img.createdAt ? P.formatRelativeTime(img.createdAt) : '—';
return '' +
'
' +
'| ' + P.escapeHtml(img.repository) + ' | ' +
'' + P.escapeHtml(img.tag) + ' | ' +
'' + P.escapeHtml(img.shortId) + ' | ' +
'' + P.escapeHtml(img.sizeFormatted) + ' | ' +
'' + created + ' | ' +
'' + img.usedBy + ' | ' +
' | ' +
'
';
}
function render() {
const tbody = P.el('images-tbody');
tbody.innerHTML = images.length
? images.map(rowHtml).join('')
: '| No images. |
';
}
function load() {
const tbody = P.el('images-tbody');
tbody.innerHTML = P.loadingRow(7);
return P.get('images', 'list').then(function (data) {
images = data;
render();
}).catch(function (err) {
tbody.innerHTML = P.errorRow(7, err.message);
});
}
function init() {
P.el('images-pull-btn').addEventListener('click', function () {
P.openFormModal({
title: 'Pull Image',
submitLabel: 'Pull',
fields: [
{ name: 'reference', label: 'Image reference', required: true, placeholder: 'docker.io/library/postgres:16' },
],
onSubmit: function (values) {
return P.post('images', 'pull', { reference: values.reference }).then(load);
},
});
});
P.el('images-tbody').addEventListener('click', function (e) {
const btn = e.target.closest('button[data-action="remove"]');
if (!btn || btn.disabled) return;
const id = btn.closest('tr').dataset.id;
if (!confirm('Remove this image?')) return;
btn.disabled = true;
P.post('images', 'remove', { id: id }).then(load).catch(function (err) {
alert('Remove failed: ' + err.message);
btn.disabled = false;
});
});
return load();
}
P.registerPanel('images', { init: init, refresh: load });
})();