mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-10 03:49:24 +00:00
Create an NFC button for browser supporting NFC API
This commit is contained in:
33
core/templates/core/widgets/nfc.jinja
Normal file
33
core/templates/core/widgets/nfc.jinja
Normal file
@ -0,0 +1,33 @@
|
||||
<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 fa-tag"></i></button>
|
||||
</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>
|
@ -66,7 +66,7 @@ class SelectDate(DateInput):
|
||||
|
||||
|
||||
class MarkdownInput(Textarea):
|
||||
template_name = "core/markdown_textarea.jinja"
|
||||
template_name = "core/widgets/markdown_textarea.jinja"
|
||||
|
||||
def get_context(self, name, value, attrs):
|
||||
context = super().get_context(name, value, attrs)
|
||||
@ -100,6 +100,15 @@ class MarkdownInput(Textarea):
|
||||
return context
|
||||
|
||||
|
||||
class NFCTextInput(TextInput):
|
||||
template_name = "core/widgets/nfc.jinja"
|
||||
|
||||
def get_context(self, name, value, attrs):
|
||||
context = super().get_context(name, value, attrs)
|
||||
context["translations"] = {"unsupported": _("Unsupported NFC card")}
|
||||
return context
|
||||
|
||||
|
||||
class SelectFile(TextInput):
|
||||
def render(self, name, value, attrs=None, renderer=None):
|
||||
if attrs:
|
||||
|
Reference in New Issue
Block a user