Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 48 additions & 6 deletions hardware/static/js/hw_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ let hw_admin = ((hw)=>{
}
let obj = {}
let cams = []
obj.qrScanner = null

obj.initTypeaheads = ()=>{
if($("#qr-result"))
if($("#qr-result").length)
$("#qr-result").typeahead({
hint:true,
highlight:true,
Expand All @@ -33,6 +34,49 @@ let hw_admin = ((hw)=>{
}
});
}

obj.destroyQrScanner = ()=>{
if (obj.qrScanner) {
try {
obj.qrScanner.hide()
} catch (e) {
try {
obj.qrScanner.stop()
} catch (err) {
console.warn(err)
}
}
obj.qrScanner = null
}
$('#veil').remove()
}

obj.initQrScanner = ()=>{
let inputElem = document.getElementById('qr-result')
let videoElem = document.getElementById('qr-video')
let qrButton = document.querySelector('.qr-btn')

if (!inputElem || !videoElem || !qrButton) return
if (inputElem.dataset.qrScannerInitialized === 'true') return

inputElem.dataset.qrScannerInitialized = 'true'
obj.destroyQrScanner()

obj.qrScanner = new Scanner('qr-video', (content) => {
let qrContent = content && content.data ? content.data : content.toString()
let input = $('#qr-result')
obj.qrScanner.hide()
input.val('')
input.focus().typeahead('val', qrContent).focus()
}, {
popup: true,
popup_title: 'QR scanner',
})

qrButton.onclick = function () {
obj.qrScanner.show()
}
}
//-Updates the content
//-Shows a toast if there's a message
obj.processResponse = (data)=>{
Expand All @@ -42,16 +86,13 @@ let hw_admin = ((hw)=>{
}

if(data.content){
obj.destroyQrScanner()
$('#hw-container').fadeTo(200, 0, ()=>{
$('#hw-container').html(data.content)
obj.initListeners()
obj.initTypeaheads()
obj.initQrScanner()
$('#hw-container').fadeTo(200, 1)

window.document.dispatchEvent(new Event("DOMContentLoaded", {
bubbles: true,
cancelable: true
}));
})
}
}
Expand Down Expand Up @@ -124,4 +165,5 @@ let hw_admin = ((hw)=>{
document.addEventListener("DOMContentLoaded", ()=>{
hw_admin.initListeners()
hw_admin.initTypeaheads()
hw_admin.initQrScanner()
})
3 changes: 1 addition & 2 deletions hardware/templates/hardware_admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
<link rel="stylesheet" href="{% static 'css/hw.css' %}">
<script > window.CSRF_TOKEN = "{{ csrf_token }}"</script>
<script src="{% static 'lib/typeahead.jquery.min.js' %}"></script>
<script type="text/javascript" src="https://rawgit.com/schmich/instascan-builds/master/instascan.min.js"></script>
<script type="text/javascript" src="{% static 'js/scanner_old.js' %}"></script>
{% include 'include/import_qr_scanner.html' %}
<script type="text/javascript" src="{% static 'js/hw.js' %}"></script>
<script type="text/javascript" src="{% static 'js/hw_admin.js' %}"></script>

Expand Down
79 changes: 1 addition & 78 deletions hardware/templates/include/hardware_user_email.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,84 +3,7 @@
<div class='hw-centered' id="form-container-id">
<label>Hacker:</label>
<input id="qr-result" class="typeahead" style="color: #0f0f0f" name="email" type="text"/><i id='{{ id }}-qr' class='fa-solid fa-qrcode fa-2x qr-btn'></i>
<div id="popup-scan" style="display: none;">
<label class="control-label" for="qr-camara-select" > Select Camera</label>
<select id="qr-camara-select" class="form-control">
<option value="null">---</option>
</select>
<video id="qr-video" style="margin-top: 10px; border-radius: 5px"></video>
<button id="qr-close-button" class="btn btn-block btn-info">Close</button>
</div>

<script>
document.addEventListener("DOMContentLoaded", ()=>{
if (!document.getElementById('qr-result')) return
function stop() {
$('#popup-scan').hide();
$('#veil').hide();
scanner.stop()
}
$(document).on('keyup', function(e) {
if (e.key === "Escape") stop()
});
const div = document.createElement('div')
div.style = 'display: none;'
div.className = 'veil'
div.id = 'veil'
div.onclick = stop

document.body.appendChild(div)

let scanner = new Scanner('qr-video', 'qr-camara-select', (content) => {
let input = $('#qr-result')
stop()
input.val('')
input.focus().typeahead('val',content.toString()).focus();
})
let qrButton = document.getElementById("{{ id }}-qr");
qrButton.onclick = function () {
$('#popup-scan').show();
$('#veil').show();
scanner.start()
}
let closeButton = document.getElementById("qr-close-button");
closeButton.onclick = stop
})
</script>
{# <script type="module">#}
{# import QrScanner from '/static/lib/qr-scanner.min.js';#}
{# QrScanner.WORKER_PATH = '/static/lib/qr-scanner-worker.min.js';#}
{# // Video stream#}
{# const video = document.getElementById('qr-video');#}
{# // Result elements and set function#}
{# const camQrResult = document.getElementById('qr-result');#}
{# function setResult(result) {#}
{# camQrResult.focus();#}
{# camQrResult.value = result;#}
{# hideQR();#}
{# $(".typeahead").typeahead('val', '')#}
{# $(".typeahead").focus().typeahead('val',result.toString()).focus();#}
{# }#}
{# // Scan#}
{# const scanner = new QrScanner(video, result => setResult(result));#}
{# $("#{{ id }}-qr").click(function () {#}
{# let veil = document.createElement("div");#}
{# document.body.appendChild(veil);#}
{# veil.classList.add('veil');#}
{# veil.id = 'veil';#}
{# $("#popup-scan").show();#}
{# document.getElementById("veil").addEventListener("click", function(){#}
{# hideQR();#}
{# });#}
{# scanner.start();#}
{# });#}
{# function hideQR() {#}
{# $("#popup-scan").hide();#}
{# var el = document.getElementById("veil");#}
{# el.parentNode.removeChild(el);#}
{# scanner.stop();#}
{# }#}
{# </script>#}
<video id="qr-video" style="margin-top: 10px; border-radius: 5px; display: none; width: 100%"></video>
<div class='hw-centered'>
{% if item_id %}
<button id="hw-user-send-noreq" data-item-id="{{item_id}}" class="btn btn-success">Send</button>
Expand Down