Skip to content

Commit

Permalink
Closes #13962: Add a copy-to-clipboard button to the key field of the…
Browse files Browse the repository at this point in the history
… API token creation form
  • Loading branch information
jeremystretch committed Oct 17, 2023
1 parent 7983c25 commit 51ef4fb
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion netbox/project-static/dist/netbox.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion netbox/project-static/dist/netbox.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion netbox/project-static/src/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Clipboard from 'clipboard';
import { getElements } from './util';

export function initClipboard(): void {
for (const element of getElements('a.copy-content')) {
for (const element of getElements('.copy-content')) {
new Clipboard(element);
}
}
5 changes: 0 additions & 5 deletions netbox/templates/account/token.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
{% block content %}
<div class="row">
<div class="col col-md-12">
{% if key and not settings.ALLOW_TOKEN_RETRIEVAL %}
<div class="alert alert-danger" role="alert">
<i class="mdi mdi-alert"></i> Tokens cannot be retrieved at a later time. You must <a href="#" class="copy-content" data-clipboard-target="#token_id" title="Copy to clipboard">copy the token value</a> below and store it securely.
</div>
{% endif %}
<div class="card">
<h5 class="card-header">{% trans "Token" %}</h5>
<div class="card-body">
Expand Down
3 changes: 3 additions & 0 deletions netbox/users/forms/model_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ class UserTokenForm(BootstrapMixin, forms.ModelForm):
help_text=_(
'Keys must be at least 40 characters in length. <strong>Be sure to record your key</strong> prior to '
'submitting this form, as it may no longer be accessible once the token has been created.'
),
widget=forms.TextInput(
attrs={'data-clipboard': 'true'}
)
)
allowed_ips = SimpleArrayField(
Expand Down
8 changes: 8 additions & 0 deletions netbox/utilities/templates/form_helpers/render_field.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@
{{ label }}
</label>
</div>
{# Include a copy-to-clipboard button #}

This comment has been minimized.

Copy link
@jsenecal

jsenecal Oct 17, 2023

Contributor

Shouldn't this behavior documented somewhere for reuse?

{% elif 'data-clipboard' in field.field.widget.attrs %}
<div class="input-group">
{{ field }}
<button type="button" title="{% trans "Copy to clipboard" %}" class="btn btn-outline-dark border-input copy-content" data-clipboard-target="#{{ field.id_for_label }}">
<i class="mdi mdi-content-copy"></i>
</button>
</div>
{# Default field rendering #}
{% else %}
{{ field }}
Expand Down

0 comments on commit 51ef4fb

Please sign in to comment.