Skip to content

Commit 771eceb

Browse files
committed
Data Access Passwords: remove dependency on jQuery
1 parent f832f1b commit 771eceb

File tree

3 files changed

+72
-69
lines changed

3 files changed

+72
-69
lines changed

user/static/user/js/data_access.js

Lines changed: 72 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,94 @@
11
'use strict'
22

3-
$(document).ready(function () {
3+
document.addEventListener('DOMContentLoaded', function () {
44
Yoda.call('token_load').then((data) => {
55
const container = document.getElementById('tokens')
66

77
data.forEach(token => {
8-
container.innerHTML += `
9-
<div class="list-group-item d-inline-flex">
10-
<label class="col-sm-7">${token.label}</label>
11-
<span class="col-sm-3">${token.exp_time}</span>
12-
<button type="button" class="btn btn-danger col-sm-2 delete-token">Delete</button>
13-
</div>
8+
const div = document.createElement('div')
9+
div.className = 'list-group-item d-inline-flex'
10+
div.innerHTML = `
11+
<label class="col-sm-7">${token.label}</label>
12+
<span class="col-sm-3">${token.exp_time}</span>
13+
<button type="button" class="btn btn-danger col-sm-2 delete-token">Delete</button>
1414
`
15+
container.appendChild(div)
1516
})
1617
})
1718

18-
$('body').on('click', '.delete-token', function (e) {
19-
const label = $(this).siblings('label').text()
20-
Yoda.call('token_delete', { label }, { quiet: true }).then(
21-
(data) => {
22-
$(this).parent().remove()
23-
},
24-
() => {
25-
Yoda.set_message(
26-
'error',
27-
'An error occurred while deleting the data access password. If the issue persists, contact your administrator.')
28-
})
29-
})
30-
31-
$('body').on('click', '#generateButton', function (e) {
32-
// Reset error messages.
33-
const passwordGenerateError = document.getElementById('passwordGenerateError')
34-
passwordGenerateError.setAttribute('hidden', true)
35-
const passwordLabelError = document.getElementById('passwordLabelError')
36-
passwordLabelError.setAttribute('hidden', true)
37-
38-
const labelInput = document.getElementById('f-token-label')
39-
const label = document.getElementById('f-token-label').value
40-
const button = document.getElementById('generateButton')
41-
const token = document.getElementById('tokenField')
42-
labelInput.setAttribute('disabled', true)
43-
button.setAttribute('hidden', true)
44-
45-
Yoda.call('token_delete_expired', {}).then(response => {
46-
return Yoda.call('token_generate', { label }, { quiet: true }).then(
19+
document.body.addEventListener('click', function (e) {
20+
if (e.target.classList.contains('delete-token')) {
21+
const label = e.target.previousElementSibling.textContent
22+
Yoda.call('token_delete', { label }, { quiet: true }).then(
4723
(data) => {
48-
$('#f-token').val(data)
49-
const p = document.getElementById('passwordOk')
50-
p.removeAttribute('hidden')
51-
token.removeAttribute('hidden')
24+
e.target.parentElement.remove()
5225
},
53-
(error) => {
54-
let errorId = 'passwordGenerateError'
55-
if (error.status === 'error_TokenExistsError') {
56-
errorId = 'passwordLabelError'
57-
}
58-
const p = document.getElementById(errorId)
59-
p.removeAttribute('hidden')
60-
button.removeAttribute('hidden')
61-
token.setAttribute('hidden', true)
62-
labelInput.removeAttribute('disabled')
26+
() => {
27+
Yoda.set_message(
28+
'error',
29+
'An error occurred while deleting the data access password. If the issue persists, contact your administrator.'
30+
)
6331
}
6432
)
65-
})
66-
})
33+
}
6734

68-
$('.btn-copy-to-clipboard').on('click', function (event) {
69-
const token = document.getElementById('f-token')
70-
token.removeAttribute('disabled')
71-
$('#f-token').select()
72-
document.execCommand('copy')
73-
event.preventDefault()
74-
token.setAttribute('disabled', true)
35+
if (e.target.id === 'generateButton') {
36+
// Reset error messages.
37+
const passwordGenerateError = document.getElementById('passwordGenerateError')
38+
passwordGenerateError.setAttribute('hidden', true)
39+
const passwordLabelError = document.getElementById('passwordLabelError')
40+
passwordLabelError.setAttribute('hidden', true)
41+
42+
const labelInput = document.getElementById('f-token-label')
43+
const label = labelInput.value
44+
const button = document.getElementById('generateButton')
45+
const token = document.getElementById('tokenField')
46+
labelInput.setAttribute('disabled', true)
47+
button.setAttribute('hidden', true)
48+
49+
Yoda.call('token_delete_expired', {}).then(response => {
50+
return Yoda.call('token_generate', { label }, { quiet: true }).then(
51+
(data) => {
52+
document.getElementById('f-token').value = data
53+
const p = document.getElementById('passwordOk')
54+
p.removeAttribute('hidden')
55+
token.removeAttribute('hidden')
56+
},
57+
(error) => {
58+
let errorId = 'passwordGenerateError'
59+
if (error.status === 'error_TokenExistsError') {
60+
errorId = 'passwordLabelError'
61+
}
62+
const p = document.getElementById(errorId)
63+
p.removeAttribute('hidden')
64+
button.removeAttribute('hidden')
65+
token.setAttribute('hidden', true)
66+
labelInput.removeAttribute('disabled')
67+
}
68+
)
69+
})
70+
}
71+
72+
if (e.target.classList.contains('btn-copy-to-clipboard')) {
73+
const token = document.getElementById('f-token')
74+
token.removeAttribute('disabled')
75+
token.select()
76+
document.execCommand('copy')
77+
e.preventDefault()
78+
token.setAttribute('disabled', true)
79+
}
80+
81+
if (e.target.classList.contains('btn-generate-dap')) {
82+
const now = new Date()
83+
const date = now.toLocaleDateString('en-GB', { year: 'numeric', month: 'long', day: 'numeric' })
84+
const time = now.toTimeString().split(' ')[0]
85+
document.getElementById('f-token-label').value = `${date} ${time}`
86+
}
7587
})
7688

7789
const passwordModal = document.getElementById('dataAccessPassword')
7890
passwordModal.addEventListener('hidden.bs.modal', function (event) {
79-
$(this).find('form').trigger('reset')
91+
this.querySelector('form').reset()
8092
window.location.reload()
8193
})
82-
83-
$('.btn-generate-dap').on('click', function (event) {
84-
const now = new Date()
85-
const date = now.toLocaleDateString('en-GB', { year: 'numeric', month: 'long', day: 'numeric' })
86-
const time = now.toTimeString().split(' ')[0]
87-
$('#f-token-label').val(`${date} ${time}`)
88-
})
8994
})

user/templates/user/data_access.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
{% block title %}{{ super() }} - Data Access Tokens{% endblock %}
44

55
{% block scripts %}
6-
<script src="{{ url_for('static', filename='lib/jquery-3.7.1/jquery-3.7.1.min.js') }}"></script>
76
<script src="{{ url_for('user_bp.static', filename='js/data_access.js') }}"></script>
87
{% endblock %}
98

user/templates/user/notifications.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
{% block title %}{{ super() }} - Notifications{% endblock title %}
44

55
{% block scripts %}
6-
<script src="{{ url_for('static', filename='lib/jquery-3.7.1/jquery-3.7.1.min.js') }}"></script>
76
<script src="{{ url_for('user_bp.static', filename='js/notifications.js') }}"></script>
87
{% endblock scripts %}
98

0 commit comments

Comments
 (0)