html/confirm
< >

return function () {
    $self = url('/@object/@action/' . urlencode('html/confirm'));
    if (isset($_GET['frag']) && ! empty($_SERVER['HTTP_HX_REQUEST'])) {
        no_graphics(true);
        echo '<span class="docs-confirm-htmx-done">Loaded by htmx after the confirm.</span>';
        exit;
    }
    if (isset($_GET['del']) && main()->is_post() && is_ajax()) {
        no_graphics(true);
        echo (int) $_GET['del'];
        exit;
    }
    asset('jquery');
    asset('htmx');
    asset('yf_js_admin');
    _class('admin_ui')->confirm_assets();
    $ui = _class('admin_ui');
    $h = fn ($s) => _prepare_html((string) $s, 0);

    $state = [];
    foreach (['hit', 'go', 'q'] as $k) {
        if (isset($_GET[$k])) {
            $state[] = $k . '=' . $h($_GET[$k]);
        }
    }
    $intro = '<p class="docs-demo-intro">' . t('Confirm and prompt dialogs on a Bootstrap modal (asset <code>yf_confirm</code>, no jQuery, no Bootstrap JS needed): <code>yf.confirm(opts)</code> resolves to <code>true</code> / <code>false</code>, <code>yf.prompt(opts)</code> to the text or <code>null</code>. A link, a button or a form with <code>data-yf-confirm="..."</code> asks first, through one delegated listener, and htmx <code>hx-confirm</code> uses the same dialog. Esc cancels, Enter confirms, Tab stays in the dialog; a danger dialog starts on Cancel. The admin asks with it everywhere (<code>_class(\'admin_ui\')->confirm_attrs()</code>).') . '</p>';
    $btn = fn ($id, $label, $type = 'default') => '<button type="button" id="' . $id . '" class="' . $ui->btn_class($type) . '">' . $label . '</button>';
    $api = '<h4>' . t('JavaScript API') . '</h4><div class="yf-ui-toolbar">'
        . $btn('docs-confirm-basic', 'yf.confirm()') . $btn('docs-confirm-danger', t('Danger'), 'danger')
        . $btn('docs-confirm-typed', t('Typed word'), 'danger') . $btn('docs-confirm-prompt', 'yf.prompt()', 'primary')
        . $btn('docs-confirm-two', t('Two in a row')) . '</div>'
        . '<p>' . t('Answer:') . ' <output id="docs-confirm-out">' . t('none yet') . '</output></p>';
    $decl = '<h4>' . t('Declarative') . '</h4><div class="yf-ui-toolbar">'
        . '<a id="docs-confirm-link" class="' . $ui->btn_class() . '" href="' . $self . '?hit=link"' . $ui->confirm_html(t('Open the link?')) . '>' . t('Link') . '</a>'
        . '<form id="docs-confirm-form" method="get" action="' . $self . '"' . $ui->confirm_html(t('Send the form?'), ['on' => 'submit', 'danger' => true, 'ok' => t('Send')]) . '>'
        . '<input type="text" name="q" value="typed" class="' . $ui->input_class() . '" style="width:8em;display:inline-block">'
        . '<button type="submit" name="go" value="b1" class="' . $ui->btn_class() . '">' . t('Send 1') . '</button>'
        . '<button type="submit" name="go" value="b2" class="' . $ui->btn_class('primary') . '">' . t('Send 2') . '</button></form>'
        . '<button type="button" id="docs-confirm-htmx" class="' . $ui->btn_class() . '" hx-get="' . $self . '?frag=1" hx-target="#docs-confirm-htmx-out"'
        . ' hx-confirm="' . t('Load the fragment?') . '">htmx</button></div>'
        . '<p>' . t('Page state:') . ' <span id="docs-confirm-state">' . ($state ? implode(' ', $state) : t('none')) . '</span>;'
        . ' htmx: <span id="docs-confirm-htmx-out">' . t('nothing yet') . '</span></p>';
    $rows = '';
    foreach ([1 => 'Apples', 2 => 'Pears'] as $id => $name) {
        $rows .= '<tr data-id="' . $id . '"><td>' . $id . '</td><td>' . $name . '</td><td>' . $ui->btn('', $self . '?del=' . $id, ['kind' => 'delete', 'class' => 'ajax_delete']) . '</td></tr>';
    }
    $table = '<h4>' . t('Row delete (table2 btn_delete, yf_js_admin)') . '</h4><table class="' . $ui->table_class() . ' docs-confirm-table"><tbody>' . $rows . '</tbody></table>';
    js(<<<'JS'
(function () {
    var out = document.getElementById('docs-confirm-out');
    var show = function (v) { out.textContent = JSON.stringify(v); };
    var on = function (id, fn) { document.getElementById(id).addEventListener('click', fn); };
    on('docs-confirm-basic', function () { yf.confirm('Go on?').then(show); });
    on('docs-confirm-danger', function () { yf.confirm({ title: 'Delete', message: 'Delete the item?\nIt cannot be undone.', danger: true, ok: 'Delete' }).then(show); });
    on('docs-confirm-typed', function () { yf.confirm({ message: 'Purge every cache?', danger: true, type: 'purge', ok: 'Purge' }).then(show); });
    on('docs-confirm-prompt', function () {
        yf.prompt({ message: 'Number of days', value: '7', required: true, validate: function (v) { return /^\d+$/.test(v) || 'Digits only'; } }).then(show);
    });
    on('docs-confirm-two', function () {
        Promise.all([yf.confirm('First?'), yf.confirm('Second?')]).then(show);
    });
})();
JS);
    return $intro . $api . $decl . $table;
};

Діалоги підтвердження й запиту на модальному вікні Bootstrap (ресурс yf_confirm, без jQuery, JS Bootstrap не потрібен): yf.confirm(opts) повертає true / false, yf.prompt(opts) — текст або null. Посилання, кнопка чи форма з data-yf-confirm="..." спершу питає через один делегований обробник, а htmx hx-confirm використовує той самий діалог. Esc скасовує, Enter підтверджує, Tab лишається в діалозі; небезпечний діалог починає з кнопки Cancel. Адмінка питає ним усюди (_class('admin_ui')->confirm_attrs()).

JavaScript API

Відповідь: поки немає

Декларативно

Посилання

Стан сторінки: hit=link; htmx: поки нічого

Видалення рядка (table2 btn_delete, yf_js_admin)

1Apples
2Pears