Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Toolkit
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
digital blueprint
Web Component Framework
Toolkit
Commits
51044201
Commit
51044201
authored
4 years ago
by
Reiter, Christoph
Browse files
Options
Downloads
Patches
Plain Diff
Move device detection into functions
so they can be reused
parent
4bda8504
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
packages/qr-code-scanner/src/qr-code-scanner.js
+67
-52
67 additions, 52 deletions
packages/qr-code-scanner/src/qr-code-scanner.js
with
67 additions
and
52 deletions
packages/qr-code-scanner/src/qr-code-scanner.js
+
67
−
52
View file @
51044201
...
...
@@ -9,6 +9,62 @@ import {classMap} from 'lit-html/directives/class-map.js';
import
jsQR
from
"
jsqr
"
;
/**
* Returns the ID for the most important device
*
* @param {Map} devices
* @return string|null
*/
function
getPrimaryDevice
(
devices
)
{
if
(
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i
.
test
(
navigator
.
userAgent
))
{
if
(
devices
.
has
(
'
environment
'
))
return
'
environment
'
;
}
if
(
devices
.
size
)
{
return
Array
.
from
(
devices
)[
0
][
0
];
}
return
null
;
}
/**
* Returns a map of device IDs and translated names.
*
* Moreimportant devices first.
*
* @return Map<string,string>
*/
async
function
getVideoDevices
()
{
let
devices_map
=
new
Map
();
if
(
navigator
.
mediaDevices
&&
navigator
.
mediaDevices
.
enumerateDevices
&&
navigator
.
mediaDevices
.
getUserMedia
)
{
let
devices
;
try
{
devices
=
await
navigator
.
mediaDevices
.
enumerateDevices
()
}
catch
(
err
)
{
console
.
log
(
err
.
name
+
"
:
"
+
err
.
message
);
return
devices_map
;
}
for
(
let
device
of
devices
)
{
if
(
device
.
kind
===
'
videoinput
'
)
{
let
id
=
device
.
deviceId
;
if
(
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i
.
test
(
navigator
.
userAgent
))
{
devices_map
.
set
(
'
environment
'
,
i18n
.
t
(
'
back-camera
'
));
devices_map
.
set
(
'
user
'
,
i18n
.
t
(
'
front-camera
'
));
}
else
{
devices_map
.
set
(
id
?
id
:
true
,
device
.
label
||
i18n
.
t
(
'
camera
'
)
+
(
devices_map
.
size
+
1
));
}
}
}
return
devices_map
;
}
else
{
return
devices_map
;
}
}
/**
* Notification web component
*/
...
...
@@ -65,7 +121,17 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) {
i18n
.
changeLanguage
(
this
.
lang
);
this
.
updateComplete
.
then
(
async
()
=>
{
this
.
notSupported
=
(
!
await
this
.
checkSupport
());
let
devices
=
await
getVideoDevices
();
this
.
notSupported
=
!
devices
.
size
;
this
.
activeCamera
=
getPrimaryDevice
(
devices
)
||
''
;
for
(
let
[
id
,
label
]
of
devices
)
{
let
opt
=
document
.
createElement
(
"
option
"
);
opt
.
value
=
id
;
opt
.
text
=
label
;
this
.
_
(
'
#videoSource
'
).
appendChild
(
opt
);
}
if
(
!
this
.
stopScan
)
{
this
.
qrCodeScannerInit
();
}
...
...
@@ -83,57 +149,6 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) {
}
};
/**
* Ckecks if browser support video recording
* Gets video devices of device
*
*/
async
checkSupport
()
{
let
devices_map
=
new
Map
();
if
(
navigator
.
mediaDevices
&&
navigator
.
mediaDevices
.
enumerateDevices
&&
navigator
.
mediaDevices
.
getUserMedia
)
{
let
devices
;
try
{
devices
=
await
navigator
.
mediaDevices
.
enumerateDevices
()
}
catch
(
err
)
{
console
.
log
(
err
.
name
+
"
:
"
+
err
.
message
);
return
false
;
}
for
(
let
device
of
devices
)
{
if
(
device
.
kind
===
'
videoinput
'
)
{
let
id
=
device
.
deviceId
;
if
(
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i
.
test
(
navigator
.
userAgent
))
{
devices_map
.
set
(
'
environment
'
,
i18n
.
t
(
'
back-camera
'
));
devices_map
.
set
(
'
user
'
,
i18n
.
t
(
'
front-camera
'
));
}
else
{
devices_map
.
set
(
id
?
id
:
true
,
device
.
label
||
i18n
.
t
(
'
camera
'
)
+
(
devices_map
.
size
+
1
));
}
}
}
for
(
let
[
id
,
label
]
of
devices_map
)
{
let
opt
=
document
.
createElement
(
"
option
"
);
opt
.
value
=
id
;
opt
.
text
=
label
;
this
.
_
(
'
#videoSource
'
).
appendChild
(
opt
);
}
if
(
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i
.
test
(
navigator
.
userAgent
))
{
this
.
activeCamera
=
'
environment
'
;
}
else
{
this
.
activeCamera
=
devices_map
.
size
?
Array
.
from
(
devices_map
)[
0
][
0
]
:
''
;
}
if
(
devices_map
.
size
<
1
)
{
return
false
;
}
return
true
;
}
else
{
return
false
;
}
}
/**
* Get a loading message
*
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment