Skip to content

Commit

Permalink
Merge pull request #2 from faix/master
Browse files Browse the repository at this point in the history
fixing QR
  • Loading branch information
faix authored Oct 5, 2019
2 parents 3c2fc2f + fc6d781 commit 5a3ed96
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 133 deletions.
27 changes: 27 additions & 0 deletions hardware/migrations/0003_rename_lending_to_borrowing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.21 on 2019-10-05 20:37
from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('hardware', '0002_auto_20180923_1311'),
]

operations = [
migrations.RenameField(
model_name='request',
old_name='lending',
new_name='borrowing',
),
migrations.AlterField(
model_name='borrowing',
name='borrowing_by',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='hardware_admin_borrowing', to=settings.AUTH_USER_MODEL),
),
]
2 changes: 1 addition & 1 deletion hardware/static/css/hw.css
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
border:thin solid black;
}
/*Popup QR scanner*/
#hw-qr-btn{
.qr-btn{
vertical-align: middle;
padding: 6px 8px 4px;
border: thin solid #ddd;
Expand Down
82 changes: 0 additions & 82 deletions hardware/static/js/hw_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,6 @@ let hw_admin = ((hw)=>{
}
});
}

obj.initScanner = ()=>{
Instascan.Camera.getCameras().then(function (cameras) {
if (cameras.length > 0) {
cams = cameras
console.log(cams)
} else {
console.error('No cameras found.');
}
}).catch(function (e) {
console.error(e);
});
}
//-Updates the content
//-Shows a toast if there's a message
obj.processResponse = (data)=>{
Expand Down Expand Up @@ -114,74 +101,6 @@ let hw_admin = ((hw)=>{
'item_id': ev.currentTarget.dataset.itemId
}, obj.processResponse)
})
$("#hw-qr-btn").on("click", (ev)=>{
obj.qrScan($("#id-email")[0])
})
}

//Opens a popup with a camera preview. If a QR is detected,
//it's value is set into 'inputElem'.
//Clicking the bg cancels the operation
//pre: call initScanner
obj.qrScan = (inputElem)=>{
if(!cams) console.error("I can't scan without a camera")
if(!localStorage.getItem("selectedCam"))
localStorage.setItem("selectedCam", 0)

let selectedCam = parseInt(localStorage.getItem("selectedCam"))
//Create video element for camera output
let videoElem = document.createElement('video')
//Create element to darken the rest of the page
let veil = document.createElement("div")
//Init scanner with this element
let scanner = new Instascan.Scanner({ video: videoElem });
//Once we scan a value, set the inputElem to this value and close the popup
scanner.addListener('scan', function (content) {
console.info("Read QR content: "+content)
inputElem.value = content
scanner.stop()
popup.parentNode.removeChild(popup)
veil.parentNode.removeChild(veil)
popup = ""
});
//Creating the popup
let popup = document.createElement("div")
popup.classList.add("hw-popup-scan")
//Append camera selector
let selectCam = document.createElement("select")
let optionsStr=""
for(let i =0; i < cams.length; i++)
optionsStr += "<option value='"+i+"'>" + (cams[i].name || "Cam"+i) + "</option>"
selectCam.innerHTML=optionsStr
popup.appendChild(selectCam)
selectCam.value = ""+selectedCam
//On selector change, we stop the scanner preview and change the camera
selectCam.addEventListener("change", ()=>{
let selectedCam = parseInt(this.value)
localStorage.setItem("selectedCam", selectedCam)
scanner.stop()
scanner.start(cams[seletedCam])
})
//Then we append the video preview
popup.appendChild(videoElem)
//Append popup to document
document.body.appendChild(popup)
//Darken the rest of the page
document.body.appendChild(veil)
veil.classList.add('veil')
//On click on the bg, cancel the operation
veil.addEventListener("click", ()=>{
if(popup){
scanner.stop()
popup.parentNode.removeChild(popup)
veil.parentNode.removeChild(veil)
popup = ""
}
})

//Start the scanner with the stored value
scanner.start(cams[selectedCam])

}

return obj
Expand All @@ -190,5 +109,4 @@ let hw_admin = ((hw)=>{
document.addEventListener("DOMContentLoaded", ()=>{
hw_admin.initListeners()
hw_admin.initTypeaheads()
hw_admin.initScanner()
})
48 changes: 0 additions & 48 deletions hardware/static/lib/instascan.min.js

This file was deleted.

Loading

0 comments on commit 5a3ed96

Please sign in to comment.