@operato/input 0.2.41 → 0.2.42

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.
@@ -0,0 +1,203 @@
1
+ import '@operato/popup'
2
+
3
+ import { css, html } from 'lit'
4
+ import { customElement, property, query, state } from 'lit/decorators.js'
5
+
6
+ import { OxPopup } from '@operato/popup'
7
+ import { BrowserMultiFormatReader } from '@zxing/library'
8
+
9
+ import { OxFormField } from './ox-formfield'
10
+
11
+ const barcodeIcon = `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAYBAMAAAAfR1CMAAADKGlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS42LWMxNDUgNzkuMTYzNDk5LCAyMDE4LzA4LzEzLTE2OjQwOjIyICAgICAgICAiPiA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPiA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtbG5zOnhtcE1NPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvbW0vIiB4bWxuczpzdFJlZj0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL3NUeXBlL1Jlc291cmNlUmVmIyIgeG1wOkNyZWF0b3JUb29sPSJBZG9iZSBQaG90b3Nob3AgQ0MgMjAxOSAoTWFjaW50b3NoKSIgeG1wTU06SW5zdGFuY2VJRD0ieG1wLmlpZDpFNjM4RURDQkQ1OUExMUU5QkExMkQ4NUY3NkMxNzBFOSIgeG1wTU06RG9jdW1lbnRJRD0ieG1wLmRpZDpFNjM4RURDQ0Q1OUExMUU5QkExMkQ4NUY3NkMxNzBFOSI+IDx4bXBNTTpEZXJpdmVkRnJvbSBzdFJlZjppbnN0YW5jZUlEPSJ4bXAuaWlkOkU2MzhFREM5RDU5QTExRTlCQTEyRDg1Rjc2QzE3MEU5IiBzdFJlZjpkb2N1bWVudElEPSJ4bXAuZGlkOkU2MzhFRENBRDU5QTExRTlCQTEyRDg1Rjc2QzE3MEU5Ii8+IDwvcmRmOkRlc2NyaXB0aW9uPiA8L3JkZjpSREY+IDwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8+55pr/QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAAkUExURQAAAEdwTAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEus/7UCWQwAAAALdFJOU9YAg3wKBFBDSz9PnvQNDgAAAE9JREFUGNNjEEQFDKLJSnCOklkgg9QUJFn3RgZhRyS+iCGDEIp2RSBfQICRkRGIgTSQL4jCF6ScvxsZYOFT2T50/6D7Fz080MMLPTzRwhsAHVspfelur08AAAAASUVORK5CYII=`
12
+
13
+ @customElement('ox-input-barcode')
14
+ export class OxInputBarcode extends OxFormField {
15
+ static styles = [
16
+ css`
17
+ :host {
18
+ display: flex;
19
+ align-items: center;
20
+ border: none;
21
+ overflow: hidden;
22
+ background-color: #fff;
23
+
24
+ padding: var(--custom-input-barcode-field-padding) !important;
25
+ }
26
+
27
+ * {
28
+ align-self: stretch;
29
+ }
30
+
31
+ *:focus {
32
+ outline: none;
33
+ }
34
+
35
+ input {
36
+ flex: 1 !important;
37
+ border: none;
38
+ font: var(--custom-input-barcode-field-font);
39
+ width: 10px;
40
+ flex-grow: 1;
41
+ }
42
+
43
+ #scan-button {
44
+ display: block;
45
+ width: 30px;
46
+ height: 100%;
47
+ min-height: 24px;
48
+ border: none;
49
+ background-color: transparent;
50
+ background-repeat: no-repeat;
51
+ background-position: center;
52
+ background-image: var(--barcodescan-input-button-icon);
53
+ }
54
+
55
+ #scan-button[hidden] {
56
+ display: none;
57
+ }
58
+
59
+ ox-popup {
60
+ position: fixed;
61
+
62
+ width: 80vw;
63
+ height: 80vh;
64
+ transform: translate(10%, 10%);
65
+ }
66
+
67
+ video {
68
+ width: 100%;
69
+ height: 100%;
70
+ }
71
+
72
+ @media screen and (max-width: 460px) {
73
+ ox-popup {
74
+ position: fixed;
75
+ left: 0;
76
+ top: 0;
77
+ width: 100vw;
78
+ height: 100vh;
79
+ transform: translate(0%, 0%);
80
+ }
81
+ }
82
+ `
83
+ ]
84
+
85
+ @property({ type: String, attribute: true }) name?: string
86
+ @property({ type: Boolean }) scannable?: boolean
87
+ @property({ attribute: 'without-enter', type: Boolean }) withoutEnter?: boolean
88
+ @property({ type: String }) value?: string
89
+
90
+ @state() stream?: MediaStream
91
+ @state() reader?: BrowserMultiFormatReader
92
+
93
+ @query('input') input!: HTMLInputElement
94
+ @query('ox-popup') popup!: OxPopup
95
+ @query('video') video!: HTMLVideoElement
96
+
97
+ connectedCallback() {
98
+ super.connectedCallback()
99
+
100
+ this.scannable = false
101
+
102
+ if (navigator.mediaDevices) {
103
+ ;(async () => {
104
+ try {
105
+ var stream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: 'environment' } })
106
+ if (stream) {
107
+ stream.getTracks().forEach(track => track.stop())
108
+ this.scannable = true
109
+ }
110
+ } catch (e) {
111
+ console.warn('this device not support camera for barcode scan', e)
112
+ }
113
+ })()
114
+ }
115
+ }
116
+
117
+ disconnectedCallback() {
118
+ this.stopScan()
119
+ }
120
+
121
+ render() {
122
+ this.style.setProperty('--barcodescan-input-button-icon', `url(${barcodeIcon})`)
123
+
124
+ return html`
125
+ <input type="text" .value=${this.value || ''} maxlength="50" @change=${(e: Event) => this.onChange(e)} />
126
+ <button
127
+ ?hidden=${!this.scannable}
128
+ id="scan-button"
129
+ @click=${(e: MouseEvent) => {
130
+ this.scan(e)
131
+ }}
132
+ ></button>
133
+
134
+ <ox-popup
135
+ @focusout=${() => {
136
+ this.stopScan()
137
+ }}
138
+ >
139
+ <video></video>
140
+ </ox-popup>
141
+ `
142
+ }
143
+
144
+ onChange(e: Event) {
145
+ this.value = (e.target as HTMLInputElement)?.value
146
+ //@ts-ignore
147
+ this.dispatchEvent(new e.constructor(e.type, e))
148
+ }
149
+
150
+ async scan(e: MouseEvent) {
151
+ try {
152
+ this.popup.open({})
153
+
154
+ /* template.video가 생성된 후에 접근하기 위해서, 한 프레임을 강제로 건너뛴다. */
155
+ await this.updateComplete
156
+
157
+ var constraints = { video: { facingMode: 'environment' } } /* backside camera first */
158
+ this.stream = await navigator.mediaDevices.getUserMedia(constraints)
159
+
160
+ this.reader = new BrowserMultiFormatReader()
161
+ if (getComputedStyle(this.popup).display !== 'none' /* popup not hidden */ && this.stream) {
162
+ var result = await this.reader.decodeOnceFromStream(this.stream, this.video)
163
+ var input = this.input
164
+ input.focus()
165
+ input.value = String(result)
166
+
167
+ if (!this.withoutEnter) {
168
+ input.dispatchEvent(new KeyboardEvent('keypress', { keyCode: 0x0d }))
169
+ input.dispatchEvent(
170
+ new CustomEvent('change', {
171
+ bubbles: true,
172
+ composed: true,
173
+ detail: String(result)
174
+ })
175
+ )
176
+ }
177
+ } else {
178
+ /* popup이 비동기 진행 중에 close된 경우라면, stopScan()을 처리하지 못하게 되므로, 다시한번 clear해준다. */
179
+ this.stopScan()
180
+ }
181
+ } catch (err) {
182
+ /*
183
+ * 1. stream device 문제로 예외 발생한 경우.
184
+ * 2. 뒤로가기 등으로 popup이 종료된 경우에도 NotFoundException: Video stream has ended before any code could be detected. 이 발생한다.
185
+ */
186
+ console.warn(err)
187
+ } finally {
188
+ this.popup.close()
189
+
190
+ this.stopScan()
191
+ }
192
+ }
193
+
194
+ stopScan() {
195
+ this.video.pause()
196
+
197
+ this.stream && this.stream.getTracks().forEach(track => track.stop())
198
+ this.reader && this.reader.reset()
199
+
200
+ delete this.stream
201
+ delete this.reader
202
+ }
203
+ }
@@ -72,7 +72,13 @@ export default class OxInputStack extends OxFormField {
72
72
  _notifyChange() {
73
73
  this.value = this.stack
74
74
 
75
- this.dispatchEvent(new CustomEvent('change', { detail: this.value, bubbles: true, composed: true }))
75
+ this.dispatchEvent(
76
+ new CustomEvent('change', {
77
+ detail: this.value,
78
+ bubbles: true,
79
+ composed: true
80
+ })
81
+ )
76
82
  }
77
83
 
78
84
  _onClickAddFloor(e: Event) {
package/src/ox-select.ts CHANGED
@@ -6,11 +6,12 @@ import '@material/mwc-icon'
6
6
  import '@operato/popup'
7
7
 
8
8
  import { css, html } from 'lit'
9
- import { customElement, property, query } from 'lit/decorators.js'
9
+ import { customElement, property } from 'lit/decorators.js'
10
10
 
11
- import { OxFormField } from './ox-formfield'
12
11
  import { OxPopupList } from '@operato/popup'
13
12
 
13
+ import { OxFormField } from './ox-formfield'
14
+
14
15
  @customElement('ox-select')
15
16
  export class Select extends OxFormField {
16
17
  static styles = [