mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-09 19:40:19 +00:00
Convert nfc input to a web component
This commit is contained in:
@ -1,33 +1,5 @@
|
||||
<script-once src="{{ statics.js }}" defer></script-once>
|
||||
|
||||
<span>
|
||||
<input type="{{ widget.type }}" name="{{ widget.name }}"{% if widget.value != None %} value="{{ widget.value }}"{% endif %}{% include "django/forms/widgets/attrs.html" %}>
|
||||
<!-- NFC icon not available in fontawesome 4.7 -->
|
||||
<button type="button" id="{{ widget.attrs.id }}_button"><i class="fa-brands fa-nfc-symbol"></i></button>
|
||||
<nfc-input type="{{ widget.type }}" name="{{ widget.name }}"{% if widget.value != None %} value="{{ widget.value }}"{% endif %}{% include "django/forms/widgets/attrs.html" %}></nfc-input>
|
||||
</span>
|
||||
<script>
|
||||
document.addEventListener("DOMContentLoaded", function(event) {
|
||||
let button = document.getElementById("{{ widget.attrs.id }}_button");
|
||||
button.addEventListener("click", async () => {
|
||||
let input = document.getElementById("{{ widget.attrs.id }}");
|
||||
const ndef = new NDEFReader();
|
||||
await ndef.scan();
|
||||
|
||||
ndef.addEventListener("readingerror", () => {
|
||||
alert("{{ translations.unsupported }}")
|
||||
});
|
||||
|
||||
ndef.addEventListener("reading", ({ message, serialNumber }) => {
|
||||
input.value = serialNumber.replaceAll(":", "").toUpperCase();
|
||||
/* Auto submit form */
|
||||
b = document.createElement("button");
|
||||
input.appendChild(b)
|
||||
b.click()
|
||||
b.remove()
|
||||
});
|
||||
|
||||
});
|
||||
/* Disable feature if browser is not supported or if not HTTPS */
|
||||
if (typeof NDEFReader === "undefined") {
|
||||
button.remove();
|
||||
}
|
||||
});
|
||||
</script>
|
Reference in New Issue
Block a user