@smileid/web-components 1.5.0 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,290 +1,290 @@
1
- import './selfie-capture';
2
- import './selfie-capture-instructions';
3
- import './selfie-capture-review';
4
- import SmartCamera from '../../../domain/camera/src/SmartCamera';
5
- import styles from '../../../styles/src/styles';
6
- import packageJson from '../../../package.json';
7
-
8
- const COMPONENTS_VERSION = packageJson.version;
9
-
10
- const smartCameraWeb = document.querySelector('smart-camera-web');
11
-
12
- async function getPermissions(captureScreen, facingMode = 'user') {
13
- try {
14
- const stream = await SmartCamera.getMedia({
15
- audio: false,
16
- video: { facingMode },
17
- });
18
- const devices = await navigator.mediaDevices.enumerateDevices();
19
- const videoDevice = devices.find(
20
- (device) =>
21
- device.kind === 'videoinput' &&
22
- stream.getVideoTracks()[0].getSettings().deviceId === device.deviceId,
23
- );
24
- smartCameraWeb?.dispatchEvent(
25
- new CustomEvent('metadata.camera-name', {
26
- detail: { cameraName: videoDevice?.label },
27
- }),
28
- );
29
- captureScreen.removeAttribute('data-camera-error');
30
- captureScreen.setAttribute('data-camera-ready', true);
31
- } catch (error) {
32
- captureScreen.removeAttribute('data-camera-ready');
33
- captureScreen.setAttribute(
34
- 'data-camera-error',
35
- SmartCamera.handleCameraError(error),
36
- );
37
- }
38
- }
39
-
40
- class SelfieCaptureScreens extends HTMLElement {
41
- constructor() {
42
- super();
43
- this.activeScreen = null;
44
- smartCameraWeb?.dispatchEvent(new CustomEvent('metadata.initialize'));
45
- }
46
-
47
- connectedCallback() {
48
- this.innerHTML = `
49
- ${styles(this.themeColor)}
50
- <div>
51
- <selfie-capture-instructions theme-color='${this.themeColor}' ${this.showNavigation} ${this.hideAttribution} ${this.hideBack} hidden></selfie-capture-instructions>
52
- <selfie-capture theme-color='${this.themeColor}' ${this.showNavigation} ${this.allowAgentMode} ${this.allowAgentModeTests} ${this.hideAttribution} ${this.disableImageTests} hidden></selfie-capture>
53
- <selfie-capture-review theme-color='${this.themeColor}' ${this.showNavigation} ${this.hideAttribution} hidden></selfie-capture-review>
54
- </div>
55
- `;
56
-
57
- this._data = {
58
- images: [],
59
- meta: {
60
- libraryVersion: COMPONENTS_VERSION,
61
- },
62
- };
63
-
64
- this.selfieInstruction = this.querySelector('selfie-capture-instructions');
65
- this.selfieCapture = this.querySelector('selfie-capture');
66
- this.selfieReview = this.querySelector('selfie-capture-review');
67
-
68
- if (this.hideInstructions && !this.hasAttribute('hidden')) {
69
- getPermissions(this.selfieCapture, this.getAgentMode());
70
- }
71
-
72
- // If the initial screen is selfie-capture, we need to get permissions
73
- if (this.getAttribute('initial-screen') === 'selfie-capture') {
74
- getPermissions(this.selfieCapture, this.getAgentMode()).then(() =>
75
- this.setActiveScreen(this.selfieCapture),
76
- );
77
- } else if (this.hideInstructions) {
78
- this.setActiveScreen(this.selfieCapture);
79
- } else {
80
- this.setActiveScreen(this.selfieInstruction);
81
- }
82
-
83
- this.setUpEventListeners();
84
- }
85
-
86
- getAgentMode() {
87
- return this.inAgentMode ? 'environment' : 'user';
88
- }
89
-
90
- disconnectedCallback() {
91
- SmartCamera.stopMedia();
92
- if (this.activeScreen) {
93
- this.activeScreen.removeAttribute('hidden');
94
- }
95
- this.activeScreen = null;
96
- this.innerHTML = '';
97
- }
98
-
99
- setUpEventListeners() {
100
- this.selfieInstruction.addEventListener(
101
- 'selfie-capture-instructions.capture',
102
- async () => {
103
- await getPermissions(this.selfieCapture, this.getAgentMode()).then(() =>
104
- this.setActiveScreen(this.selfieCapture),
105
- );
106
- smartCameraWeb?.dispatchEvent(
107
- new CustomEvent('metadata.selfie-capture-start'),
108
- );
109
- smartCameraWeb?.dispatchEvent(
110
- new CustomEvent('metadata.selfie-origin', {
111
- detail: {
112
- imageOrigin: {
113
- environment: 'back_camera',
114
- user: 'front_camera',
115
- }[this.getAgentMode()],
116
- },
117
- }),
118
- );
119
- },
120
- );
121
- this.selfieInstruction.addEventListener(
122
- 'selfie-capture-instructions.cancelled',
123
- () => {
124
- this.handleBackEvents();
125
- },
126
- );
127
-
128
- this.selfieCapture.addEventListener('selfie-capture.cancelled', () => {
129
- if (this.hideInstructions) {
130
- this.dispatchEvent(new CustomEvent('selfie-capture-screens.cancelled'));
131
- } else {
132
- this.setActiveScreen(this.selfieInstruction);
133
- }
134
- });
135
-
136
- this.selfieCapture.addEventListener('selfie-capture.publish', (event) => {
137
- smartCameraWeb?.dispatchEvent(
138
- new CustomEvent('metadata.selfie-capture-end'),
139
- );
140
- this.selfieReview.setAttribute('data-image', event.detail.referenceImage);
141
- this._data.images = event.detail.images;
142
- SmartCamera.stopMedia();
143
- this.setActiveScreen(this.selfieReview);
144
- });
145
-
146
- this.selfieCapture.addEventListener('selfie-capture.cancelled', () => {
147
- this.selfieCapture.reset();
148
- SmartCamera.stopMedia();
149
- if (this.hideInstructions) {
150
- this.handleBackEvents();
151
- return;
152
- }
153
-
154
- this.setActiveScreen(this.selfieInstruction);
155
- });
156
-
157
- this.selfieReview.addEventListener(
158
- 'selfie-capture-review.rejected',
159
- async () => {
160
- smartCameraWeb?.dispatchEvent(
161
- new CustomEvent('metadata.selfie-capture-retry'),
162
- );
163
- this.selfieReview.removeAttribute('data-image');
164
- this._data.images = [];
165
- if (this.hideInstructions) {
166
- this.setActiveScreen(this.selfieCapture);
167
- await getPermissions(this.selfieCapture, this.getAgentMode());
168
- } else {
169
- this.setActiveScreen(this.selfieInstruction);
170
- }
171
- },
172
- );
173
-
174
- this.selfieReview.addEventListener(
175
- 'selfie-capture-review.accepted',
176
- async () => {
177
- this._publishSelectedImages();
178
- },
179
- );
180
-
181
- [this.selfieInstruction, this.selfieCapture, this.selfieReview].forEach(
182
- (screen) => {
183
- screen.addEventListener(
184
- `${screen.nodeName.toLowerCase()}.close`,
185
- () => {
186
- this.handleCloseEvent();
187
- },
188
- );
189
- },
190
- );
191
- }
192
-
193
- _publishSelectedImages() {
194
- this.dispatchEvent(
195
- new CustomEvent('selfie-capture-screens.publish', { detail: this._data }),
196
- );
197
- }
198
-
199
- get hideInstructions() {
200
- return this.hasAttribute('hide-instructions');
201
- }
202
-
203
- get hideAttribution() {
204
- return this.hasAttribute('hide-attribution') ? 'hide-attribution' : '';
205
- }
206
-
207
- get hideBackOfId() {
208
- return this.hasAttribute('hide-back-of-id');
209
- }
210
-
211
- get showNavigation() {
212
- return this.hasAttribute('show-navigation') ? 'show-navigation' : '';
213
- }
214
-
215
- get inAgentMode() {
216
- return this.getAttribute('allow-agent-mode') === 'true';
217
- }
218
-
219
- get allowAgentMode() {
220
- return this.inAgentMode ? "allow-agent-mode='true'" : '';
221
- }
222
-
223
- get allowAgentModeTests() {
224
- return this.hasAttribute('show-agent-mode-for-tests')
225
- ? 'show-agent-mode-for-tests'
226
- : '';
227
- }
228
-
229
- get hideBack() {
230
- return this.hasAttribute('hide-back-to-host') ||
231
- this.hasAttribute('hide-back')
232
- ? 'hide-back'
233
- : '';
234
- }
235
-
236
- get disableImageTests() {
237
- return this.hasAttribute('disable-image-tests')
238
- ? 'disable-image-tests'
239
- : '';
240
- }
241
-
242
- get themeColor() {
243
- return this.getAttribute('theme-color') || '#001096';
244
- }
245
-
246
- setActiveScreen(screen) {
247
- this.activeScreen?.setAttribute('hidden', '');
248
- screen.removeAttribute('hidden');
249
- this.activeScreen = screen;
250
- }
251
-
252
- handleBackEvents() {
253
- this.dispatchEvent(new CustomEvent('selfie-capture-screens.cancelled'));
254
- }
255
-
256
- handleCloseEvent() {
257
- this.dispatchEvent(new CustomEvent('selfie-capture-screens.close'));
258
- }
259
-
260
- static get observedAttributes() {
261
- return [
262
- 'title',
263
- 'hidden',
264
- 'show-navigation',
265
- 'hide-back-to-host',
266
- 'initial-screen',
267
- ];
268
- }
269
-
270
- attributeChangedCallback(name) {
271
- switch (name) {
272
- case 'title':
273
- case 'hidden':
274
- case 'initial-screen':
275
- this.connectedCallback();
276
- break;
277
- default:
278
- break;
279
- }
280
- }
281
- }
282
-
283
- if (
284
- 'customElements' in window &&
285
- !customElements.get('selfie-capture-screens')
286
- ) {
287
- customElements.define('selfie-capture-screens', SelfieCaptureScreens);
288
- }
289
-
290
- export default SelfieCaptureScreens;
1
+ import './selfie-capture';
2
+ import './selfie-capture-instructions';
3
+ import './selfie-capture-review';
4
+ import SmartCamera from '../../../domain/camera/src/SmartCamera';
5
+ import styles from '../../../styles/src/styles';
6
+ import packageJson from '../../../package.json';
7
+
8
+ const COMPONENTS_VERSION = packageJson.version;
9
+
10
+ const smartCameraWeb = document.querySelector('smart-camera-web');
11
+
12
+ async function getPermissions(captureScreen, facingMode = 'user') {
13
+ try {
14
+ const stream = await SmartCamera.getMedia({
15
+ audio: false,
16
+ video: { facingMode },
17
+ });
18
+ const devices = await navigator.mediaDevices.enumerateDevices();
19
+ const videoDevice = devices.find(
20
+ (device) =>
21
+ device.kind === 'videoinput' &&
22
+ stream.getVideoTracks()[0].getSettings().deviceId === device.deviceId,
23
+ );
24
+ smartCameraWeb?.dispatchEvent(
25
+ new CustomEvent('metadata.camera-name', {
26
+ detail: { cameraName: videoDevice?.label },
27
+ }),
28
+ );
29
+ captureScreen.removeAttribute('data-camera-error');
30
+ captureScreen.setAttribute('data-camera-ready', true);
31
+ } catch (error) {
32
+ captureScreen.removeAttribute('data-camera-ready');
33
+ captureScreen.setAttribute(
34
+ 'data-camera-error',
35
+ SmartCamera.handleCameraError(error),
36
+ );
37
+ }
38
+ }
39
+
40
+ class SelfieCaptureScreens extends HTMLElement {
41
+ constructor() {
42
+ super();
43
+ this.activeScreen = null;
44
+ smartCameraWeb?.dispatchEvent(new CustomEvent('metadata.initialize'));
45
+ }
46
+
47
+ connectedCallback() {
48
+ this.innerHTML = `
49
+ ${styles(this.themeColor)}
50
+ <div>
51
+ <selfie-capture-instructions theme-color='${this.themeColor}' ${this.showNavigation} ${this.hideAttribution} ${this.hideBack} hidden></selfie-capture-instructions>
52
+ <selfie-capture theme-color='${this.themeColor}' ${this.showNavigation} ${this.allowAgentMode} ${this.allowAgentModeTests} ${this.hideAttribution} ${this.disableImageTests} hidden></selfie-capture>
53
+ <selfie-capture-review theme-color='${this.themeColor}' ${this.showNavigation} ${this.hideAttribution} hidden></selfie-capture-review>
54
+ </div>
55
+ `;
56
+
57
+ this._data = {
58
+ images: [],
59
+ meta: {
60
+ libraryVersion: COMPONENTS_VERSION,
61
+ },
62
+ };
63
+
64
+ this.selfieInstruction = this.querySelector('selfie-capture-instructions');
65
+ this.selfieCapture = this.querySelector('selfie-capture');
66
+ this.selfieReview = this.querySelector('selfie-capture-review');
67
+
68
+ if (this.hideInstructions && !this.hasAttribute('hidden')) {
69
+ getPermissions(this.selfieCapture, this.getAgentMode());
70
+ }
71
+
72
+ // If the initial screen is selfie-capture, we need to get permissions
73
+ if (this.getAttribute('initial-screen') === 'selfie-capture') {
74
+ getPermissions(this.selfieCapture, this.getAgentMode()).then(() =>
75
+ this.setActiveScreen(this.selfieCapture),
76
+ );
77
+ } else if (this.hideInstructions) {
78
+ this.setActiveScreen(this.selfieCapture);
79
+ } else {
80
+ this.setActiveScreen(this.selfieInstruction);
81
+ }
82
+
83
+ this.setUpEventListeners();
84
+ }
85
+
86
+ getAgentMode() {
87
+ return this.inAgentMode ? 'environment' : 'user';
88
+ }
89
+
90
+ disconnectedCallback() {
91
+ SmartCamera.stopMedia();
92
+ if (this.activeScreen) {
93
+ this.activeScreen.removeAttribute('hidden');
94
+ }
95
+ this.activeScreen = null;
96
+ this.innerHTML = '';
97
+ }
98
+
99
+ setUpEventListeners() {
100
+ this.selfieInstruction.addEventListener(
101
+ 'selfie-capture-instructions.capture',
102
+ async () => {
103
+ await getPermissions(this.selfieCapture, this.getAgentMode()).then(() =>
104
+ this.setActiveScreen(this.selfieCapture),
105
+ );
106
+ smartCameraWeb?.dispatchEvent(
107
+ new CustomEvent('metadata.selfie-capture-start'),
108
+ );
109
+ smartCameraWeb?.dispatchEvent(
110
+ new CustomEvent('metadata.selfie-origin', {
111
+ detail: {
112
+ imageOrigin: {
113
+ environment: 'back_camera',
114
+ user: 'front_camera',
115
+ }[this.getAgentMode()],
116
+ },
117
+ }),
118
+ );
119
+ },
120
+ );
121
+ this.selfieInstruction.addEventListener(
122
+ 'selfie-capture-instructions.cancelled',
123
+ () => {
124
+ this.handleBackEvents();
125
+ },
126
+ );
127
+
128
+ this.selfieCapture.addEventListener('selfie-capture.cancelled', () => {
129
+ if (this.hideInstructions) {
130
+ this.dispatchEvent(new CustomEvent('selfie-capture-screens.cancelled'));
131
+ } else {
132
+ this.setActiveScreen(this.selfieInstruction);
133
+ }
134
+ });
135
+
136
+ this.selfieCapture.addEventListener('selfie-capture.publish', (event) => {
137
+ smartCameraWeb?.dispatchEvent(
138
+ new CustomEvent('metadata.selfie-capture-end'),
139
+ );
140
+ this.selfieReview.setAttribute('data-image', event.detail.referenceImage);
141
+ this._data.images = event.detail.images;
142
+ SmartCamera.stopMedia();
143
+ this.setActiveScreen(this.selfieReview);
144
+ });
145
+
146
+ this.selfieCapture.addEventListener('selfie-capture.cancelled', () => {
147
+ this.selfieCapture.reset();
148
+ SmartCamera.stopMedia();
149
+ if (this.hideInstructions) {
150
+ this.handleBackEvents();
151
+ return;
152
+ }
153
+
154
+ this.setActiveScreen(this.selfieInstruction);
155
+ });
156
+
157
+ this.selfieReview.addEventListener(
158
+ 'selfie-capture-review.rejected',
159
+ async () => {
160
+ smartCameraWeb?.dispatchEvent(
161
+ new CustomEvent('metadata.selfie-capture-retry'),
162
+ );
163
+ this.selfieReview.removeAttribute('data-image');
164
+ this._data.images = [];
165
+ if (this.hideInstructions) {
166
+ this.setActiveScreen(this.selfieCapture);
167
+ await getPermissions(this.selfieCapture, this.getAgentMode());
168
+ } else {
169
+ this.setActiveScreen(this.selfieInstruction);
170
+ }
171
+ },
172
+ );
173
+
174
+ this.selfieReview.addEventListener(
175
+ 'selfie-capture-review.accepted',
176
+ async () => {
177
+ this._publishSelectedImages();
178
+ },
179
+ );
180
+
181
+ [this.selfieInstruction, this.selfieCapture, this.selfieReview].forEach(
182
+ (screen) => {
183
+ screen.addEventListener(
184
+ `${screen.nodeName.toLowerCase()}.close`,
185
+ () => {
186
+ this.handleCloseEvent();
187
+ },
188
+ );
189
+ },
190
+ );
191
+ }
192
+
193
+ _publishSelectedImages() {
194
+ this.dispatchEvent(
195
+ new CustomEvent('selfie-capture-screens.publish', { detail: this._data }),
196
+ );
197
+ }
198
+
199
+ get hideInstructions() {
200
+ return this.hasAttribute('hide-instructions');
201
+ }
202
+
203
+ get hideAttribution() {
204
+ return this.hasAttribute('hide-attribution') ? 'hide-attribution' : '';
205
+ }
206
+
207
+ get hideBackOfId() {
208
+ return this.hasAttribute('hide-back-of-id');
209
+ }
210
+
211
+ get showNavigation() {
212
+ return this.hasAttribute('show-navigation') ? 'show-navigation' : '';
213
+ }
214
+
215
+ get inAgentMode() {
216
+ return this.getAttribute('allow-agent-mode') === 'true';
217
+ }
218
+
219
+ get allowAgentMode() {
220
+ return this.inAgentMode ? "allow-agent-mode='true'" : '';
221
+ }
222
+
223
+ get allowAgentModeTests() {
224
+ return this.hasAttribute('show-agent-mode-for-tests')
225
+ ? 'show-agent-mode-for-tests'
226
+ : '';
227
+ }
228
+
229
+ get hideBack() {
230
+ return this.hasAttribute('hide-back-to-host') ||
231
+ this.hasAttribute('hide-back')
232
+ ? 'hide-back'
233
+ : '';
234
+ }
235
+
236
+ get disableImageTests() {
237
+ return this.hasAttribute('disable-image-tests')
238
+ ? 'disable-image-tests'
239
+ : '';
240
+ }
241
+
242
+ get themeColor() {
243
+ return this.getAttribute('theme-color') || '#001096';
244
+ }
245
+
246
+ setActiveScreen(screen) {
247
+ this.activeScreen?.setAttribute('hidden', '');
248
+ screen.removeAttribute('hidden');
249
+ this.activeScreen = screen;
250
+ }
251
+
252
+ handleBackEvents() {
253
+ this.dispatchEvent(new CustomEvent('selfie-capture-screens.cancelled'));
254
+ }
255
+
256
+ handleCloseEvent() {
257
+ this.dispatchEvent(new CustomEvent('selfie-capture-screens.close'));
258
+ }
259
+
260
+ static get observedAttributes() {
261
+ return [
262
+ 'title',
263
+ 'hidden',
264
+ 'show-navigation',
265
+ 'hide-back-to-host',
266
+ 'initial-screen',
267
+ ];
268
+ }
269
+
270
+ attributeChangedCallback(name) {
271
+ switch (name) {
272
+ case 'title':
273
+ case 'hidden':
274
+ case 'initial-screen':
275
+ this.connectedCallback();
276
+ break;
277
+ default:
278
+ break;
279
+ }
280
+ }
281
+ }
282
+
283
+ if (
284
+ 'customElements' in window &&
285
+ !customElements.get('selfie-capture-screens')
286
+ ) {
287
+ customElements.define('selfie-capture-screens', SelfieCaptureScreens);
288
+ }
289
+
290
+ export default SelfieCaptureScreens;