|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | | -$(document).ready(function () { |
| 3 | +document.addEventListener('DOMContentLoaded', function () { |
4 | 4 | Yoda.call('token_load').then((data) => { |
5 | 5 | const container = document.getElementById('tokens') |
6 | 6 |
|
7 | 7 | 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> |
14 | 14 | ` |
| 15 | + container.appendChild(div) |
15 | 16 | }) |
16 | 17 | }) |
17 | 18 |
|
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( |
47 | 23 | (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() |
52 | 25 | }, |
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 | + ) |
63 | 31 | } |
64 | 32 | ) |
65 | | - }) |
66 | | - }) |
| 33 | + } |
67 | 34 |
|
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 | + } |
75 | 87 | }) |
76 | 88 |
|
77 | 89 | const passwordModal = document.getElementById('dataAccessPassword') |
78 | 90 | passwordModal.addEventListener('hidden.bs.modal', function (event) { |
79 | | - $(this).find('form').trigger('reset') |
| 91 | + this.querySelector('form').reset() |
80 | 92 | window.location.reload() |
81 | 93 | }) |
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 | | - }) |
89 | 94 | }) |
0 commit comments