Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 51 additions & 7 deletions app/static/css/dashboard.css
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ summary {
display: flex;
align-items: center;
justify-content: space-between;
gap: var(--space-3);
flex-wrap: wrap;
gap: var(--space-2) var(--space-3);
margin: calc(-1 * var(--space-4));
margin-bottom: var(--space-3);
padding: var(--space-3) var(--space-4);
Expand All @@ -113,43 +114,86 @@ summary {

.device-card-header .device-card-title {
min-width: 0;
flex: 1;
flex: 1 1 auto;
max-width: 100%;
overflow: hidden;
}

.device-card-header .device-title-rename {
display: block;
width: 100%;
max-width: 100%;
margin: 0;
padding: 0;
background: transparent;
border: 0;
border-radius: 0;
box-shadow: none;
color: inherit;
font: inherit;
font-size: 1.0625rem;
font-weight: 600;
line-height: 1.3;
text-align: left;
cursor: pointer;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}

.device-card-header .device-title-rename:hover,
.device-card-header .device-title-rename:focus-visible {
background: transparent;
text-decoration: underline;
}

.device-card-title-cluster {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: var(--space-2);
flex: 1 1 0;
min-width: 0;
flex: 1;
max-width: 100%;
overflow: hidden;
}

.device-rename-form {
.device-card-header-actions {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
align-items: center;
gap: var(--space-2);
flex: 0 0 auto;
max-width: 100%;
margin-left: auto;
}

.device-rename-form {
display: flex;
align-items: center;
width: 100%;
min-width: 0;
flex: 1;
max-width: 100%;
overflow: hidden;
}

.device-rename-form[hidden] {
display: none;
}

.device-rename-form input {
width: 100%;
min-width: 0;
flex: 1 1 8rem;
max-width: 100%;
box-sizing: border-box;
}

.device-card-header .device-rename-error {
flex-basis: 100%;
margin: 0;
color: var(--color-nav-text);
color: var(--color-danger);
}

.header-open-webui {
Expand Down
130 changes: 82 additions & 48 deletions app/static/js/components/device-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,17 @@ class DeviceCard extends HTMLElement {
<section class="panel device-card">
<header class="device-card-header">
<div class="device-card-title-cluster">
<h3 class="device-card-title">${escapeHtml(device.friendly_name)}</h3>
<button type="button" class="device-rename" data-rename="start">Rename</button>
<h3 class="device-card-title">
<button type="button" class="device-title-rename" data-rename="start" aria-label="Rename ${escapeHtml(device.friendly_name)}">${escapeHtml(device.friendly_name)}</button>
</h3>
<form class="device-rename-form" hidden>
<input name="friendly_name" maxlength="60" autocomplete="off" aria-label="Friendly name">
<button type="submit">Save</button>
<button type="button" data-rename="cancel">Cancel</button>
<p class="device-rename-error" hidden></p>
</form>
<p class="device-rename-error" role="alert" hidden></p>
</div>
<div class="device-card-header-actions">
${renderHeaderOpenWebUi(device)}
</div>
${renderHeaderOpenWebUi(device)}
</header>
<section class="device-section connected-system">
<div class="connected-system-header">
Expand Down Expand Up @@ -257,47 +258,49 @@ class DeviceCard extends HTMLElement {
const start = header.querySelector('[data-rename="start"]');
const form = header.querySelector('.device-rename-form');
const input = form.querySelector('input[name="friendly_name"]');
const error = form.querySelector('.device-rename-error');
const error = header.querySelector('.device-rename-error');
let suppressBlur = false;
const showError = (text) => {
error.hidden = !text;
error.textContent = text || '';
};
const closeEditor = () => {
// Bump the seq so an in-flight save cannot apply after Cancel.
this._renameSeq += 1;
this._renameSaving = false;
const showTitle = () => {
form.hidden = true;
title.hidden = false;
start.hidden = false;
showError('');
};
start.addEventListener('click', () => {
input.value = this._device.friendly_name;
showError('');
title.hidden = true;
start.hidden = true;
form.hidden = false;
input.focus();
});
form.querySelector('[data-rename="cancel"]').addEventListener('click', closeEditor);
input.addEventListener('keydown', (event) => {
if (event.key === 'Escape') {
event.preventDefault();
closeEditor();
const labelName = (name) => {
start.textContent = name;
start.setAttribute('aria-label', `Rename ${name}`);
};
const applyName = (name) => {
// ponytail: write the name in place. Assigning this.device calls
// _render() and drops the preview. The page list holds this object.
this._device.friendly_name = name;
labelName(name);
const link = this.querySelector(`#connected-screenshot-link-${this._device.id}`);
const image = this.querySelector(`#connected-screenshot-${this._device.id}`);
if (link) {
link.setAttribute('aria-label', `Control ${name}`);
}
});
form.addEventListener('submit', async (event) => {
event.preventDefault();
if (image) {
image.alt = `Latest screenshot for ${name}`;
}
};
const commit = async () => {
if (this._renameSaving) {
return;
}
const typed = input.value.trim();
if (!typed) {
showError('Enter a name.');
const previous = this._device.friendly_name;
if (!typed || typed === previous) {
labelName(previous);
showTitle();
return;
}
this._renameSaving = true;
const seq = this._renameSeq;
labelName(typed);
showTitle();
const result = await window.dashboardApi.putJson(
`/api/devices/${this._device.id}`,
{ friendly_name: typed },
Expand All @@ -306,28 +309,59 @@ class DeviceCard extends HTMLElement {
return;
}
this._renameSaving = false;
if (result.error) {
showError(result.error);
return;
}
const saved = result.device && result.device.friendly_name;
if (typeof saved !== 'string' || !saved.trim()) {
showError('Could not rename this device.');
if (result.error || typeof saved !== 'string' || !saved.trim()) {
labelName(previous);
showError(result.error || 'Could not rename this device.');
return;
}
// ponytail: write the name in place. Assigning this.device calls
// _render() and drops the preview. The page list holds this object.
this._device.friendly_name = saved;
title.textContent = saved;
const link = this.querySelector(`#connected-screenshot-link-${this._device.id}`);
const image = this.querySelector(`#connected-screenshot-${this._device.id}`);
if (link) {
link.setAttribute('aria-label', `Control ${saved}`);
applyName(saved);
showError('');
};
const cancel = () => {
suppressBlur = true;
this._renameSeq += 1;
this._renameSaving = false;
labelName(this._device.friendly_name);
showTitle();
showError('');
};
start.addEventListener('click', () => {
input.value = this._device.friendly_name;
showError('');
title.hidden = true;
form.hidden = false;
input.focus();
input.select();
});
input.addEventListener('keydown', (event) => {
if (event.key === 'Escape') {
event.preventDefault();
suppressBlur = true;
cancel();
input.blur();
start.focus();
} else if (event.key === 'Enter') {
event.preventDefault();
suppressBlur = true;
commit();
start.focus();
}
if (image) {
image.alt = `Latest screenshot for ${saved}`;
setTimeout(() => {
suppressBlur = false;
}, 0);
});
input.addEventListener('blur', () => {
if (suppressBlur) {
suppressBlur = false;
return;
}
closeEditor();
commit();
});
form.addEventListener('submit', (event) => {
event.preventDefault();
suppressBlur = true;
commit();
});
}

Expand Down
21 changes: 19 additions & 2 deletions tests/test_remove_device_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,30 @@ def test_circleci_runs_core_and_security_jobs():

def test_device_card_renames_from_the_title():
source = CARD.read_text(encoding='utf-8')
assert 'data-rename="start"' in source
assert 'class="device-title-rename" data-rename="start"' in source
assert 'aria-label="Rename ${escapeHtml(device.friendly_name)}"' in source
assert '>Rename</button>' not in source
assert 'maxlength="60"' in source
assert 'maxlength="60"' in APP.read_text(encoding='utf-8')
assert 'Enter a name.' in source
assert 'data-rename="cancel"' not in source
assert 'Could not rename this device.' in source
assert 'role="alert"' in source
assert 'data-action="rename"' not in source
assert 'window.prompt(' not in APP.read_text(encoding='utf-8')
assert "start.setAttribute('aria-label', `Rename ${name}`)" in source
blur = source.split("addEventListener('blur'", 1)[1]
blur = blur.split('addEventListener', 1)[0]
assert '.focus(' not in blur
escape = source.split("event.key === 'Escape'", 1)[1]
escape = escape.split("event.key === 'Enter'", 1)[0]
assert 'start.focus()' in escape
enter = source.split("event.key === 'Enter'", 1)[1]
enter = enter.split('setTimeout', 1)[0]
assert 'start.focus()' in enter
css = (ROOT / 'app/static/css/dashboard.css').read_text(encoding='utf-8')
rule = css.split('.device-card-header .device-rename-error', 1)[1]
rule = rule.split('}', 1)[0]
assert 'var(--color-danger)' in rule


def test_screenshot_click_opens_device_webui():
Expand Down
Loading