@operato/board 0.2.15
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.
- package/.storybook/main.js +3 -0
- package/.storybook/server.mjs +8 -0
- package/CHANGELOG.md +22 -0
- package/LICENSE +21 -0
- package/README.md +95 -0
- package/custom-elements.json +1377 -0
- package/demo/index-player.html +101 -0
- package/demo/index-viewer.html +101 -0
- package/demo/index.html +101 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +3 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/ox-board-player copy.d.ts +39 -0
- package/dist/src/ox-board-player copy.js +258 -0
- package/dist/src/ox-board-player copy.js.map +1 -0
- package/dist/src/ox-board-player-control.d.ts +39 -0
- package/dist/src/ox-board-player-control.js +390 -0
- package/dist/src/ox-board-player-control.js.map +1 -0
- package/dist/src/ox-board-player-style.d.ts +1 -0
- package/dist/src/ox-board-player-style.js +200 -0
- package/dist/src/ox-board-player-style.js.map +1 -0
- package/dist/src/ox-board-player.d.ts +39 -0
- package/dist/src/ox-board-player.js +284 -0
- package/dist/src/ox-board-player.js.map +1 -0
- package/dist/src/ox-board-viewer.d.ts +45 -0
- package/dist/src/ox-board-viewer.js +491 -0
- package/dist/src/ox-board-viewer.js.map +1 -0
- package/dist/src/ox-board-wrapper.d.ts +1 -0
- package/dist/src/ox-board-wrapper.js +88 -0
- package/dist/src/ox-board-wrapper.js.map +1 -0
- package/dist/src/player/board-player-carousel.d.ts +1 -0
- package/dist/src/player/board-player-carousel.js +205 -0
- package/dist/src/player/board-player-carousel.js.map +1 -0
- package/dist/src/player/board-player-grid.d.ts +1 -0
- package/dist/src/player/board-player-grid.js +78 -0
- package/dist/src/player/board-player-grid.js.map +1 -0
- package/dist/src/utils/fullscreen.d.ts +14 -0
- package/dist/src/utils/fullscreen.js +69 -0
- package/dist/src/utils/fullscreen.js.map +1 -0
- package/dist/src/utils/os.d.ts +20 -0
- package/dist/src/utils/os.js +41 -0
- package/dist/src/utils/os.js.map +1 -0
- package/dist/src/utils/swipe-listener.d.ts +10 -0
- package/dist/src/utils/swipe-listener.js +257 -0
- package/dist/src/utils/swipe-listener.js.map +1 -0
- package/dist/stories/index.stories.d.ts +33 -0
- package/dist/stories/index.stories.js +33 -0
- package/dist/stories/index.stories.js.map +1 -0
- package/dist/test/board-viewer.test.d.ts +1 -0
- package/dist/test/board-viewer.test.js +24 -0
- package/dist/test/board-viewer.test.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +76 -0
- package/src/index.ts +2 -0
- package/src/ox-board-player-style.ts +200 -0
- package/src/ox-board-player.ts +292 -0
- package/src/ox-board-viewer.ts +534 -0
- package/src/ox-board-wrapper.ts +93 -0
- package/src/player/board-player-carousel.ts +197 -0
- package/src/player/board-player-grid.ts +78 -0
- package/src/utils/fullscreen.ts +82 -0
- package/src/utils/os.ts +41 -0
- package/src/utils/swipe-listener.ts +290 -0
- package/src/utils/things-scene.d.ts +1 -0
- package/stories/index.stories.ts +52 -0
- package/test/board-viewer.test.ts +35 -0
- package/tsconfig.json +22 -0
- package/web-dev-server.config.mjs +28 -0
- package/web-test-runner.config.mjs +29 -0
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import './board-player-grid'
|
|
2
|
+
|
|
3
|
+
import { LitElement, PropertyValues, css, html } from 'lit'
|
|
4
|
+
import { customElement, property, query, state } from 'lit/decorators.js'
|
|
5
|
+
|
|
6
|
+
@customElement('ox-board-player-carousel')
|
|
7
|
+
class BoardPlayerCarousel extends LitElement {
|
|
8
|
+
static styles = [
|
|
9
|
+
css`
|
|
10
|
+
:host {
|
|
11
|
+
display: block;
|
|
12
|
+
width: 100%;
|
|
13
|
+
height: 100%;
|
|
14
|
+
position: relative;
|
|
15
|
+
margin: 0 auto 0px;
|
|
16
|
+
-webkit-perspective: 1200px;
|
|
17
|
+
-moz-perspective: 1200px;
|
|
18
|
+
-o-perspective: 1200px;
|
|
19
|
+
perspective: 1200px;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
#carousel {
|
|
23
|
+
width: 100%;
|
|
24
|
+
height: 100%;
|
|
25
|
+
margin: 0px auto 0px;
|
|
26
|
+
|
|
27
|
+
position: absolute;
|
|
28
|
+
-webkit-transform-style: preserve-3d;
|
|
29
|
+
-moz-transform-style: preserve-3d;
|
|
30
|
+
-o-transform-style: preserve-3d;
|
|
31
|
+
transform-style: preserve-3d;
|
|
32
|
+
-webkit-transition: -webkit-transform 1.5s;
|
|
33
|
+
-moz-transition: -moz-transform 1.5s;
|
|
34
|
+
-o-transition: -o-transform 1.5s;
|
|
35
|
+
transition: transform 1.5s;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
#carousel > * {
|
|
39
|
+
position: absolute;
|
|
40
|
+
width: 100%;
|
|
41
|
+
height: 100%;
|
|
42
|
+
font-weight: bold;
|
|
43
|
+
-webkit-transition: opacity 1.5s, -webkit-transform 1.5s;
|
|
44
|
+
-moz-transition: opacity 1.5s, -moz-transform 1.5s;
|
|
45
|
+
-o-transition: opacity 1.5s, -o-transform 1.5s;
|
|
46
|
+
transition: opacity 1.5s, transform 1.5s;
|
|
47
|
+
|
|
48
|
+
-webkit-backface-visibility: hidden;
|
|
49
|
+
-moz-backface-visibility: hidden;
|
|
50
|
+
-o-backface-visibility: hidden;
|
|
51
|
+
backface-visibility: hidden;
|
|
52
|
+
}
|
|
53
|
+
`
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
@property({ type: String }) axis: string = 'y'
|
|
57
|
+
@property({ type: Number }) rows: number = 1
|
|
58
|
+
@property({ type: Number }) columns: number = 1
|
|
59
|
+
|
|
60
|
+
@state() _slotObserver?: MutationObserver
|
|
61
|
+
@state() _boundResize?: () => void
|
|
62
|
+
@state() _rotation: number = 0
|
|
63
|
+
@state() _theta: number = 0
|
|
64
|
+
@state() _radius: number = 0
|
|
65
|
+
@state() _rotateFn?: string
|
|
66
|
+
|
|
67
|
+
@state() _panelCount: number = 0
|
|
68
|
+
@state() _panelSize: number = 0
|
|
69
|
+
@state() _isHorizontal: boolean = true
|
|
70
|
+
|
|
71
|
+
@query('#slot') _slot!: HTMLSlotElement
|
|
72
|
+
@query('#carousel') _carousel!: HTMLElement
|
|
73
|
+
|
|
74
|
+
render() {
|
|
75
|
+
return html`
|
|
76
|
+
<slot id="slot" select="[page]"></slot>
|
|
77
|
+
|
|
78
|
+
<div id="carousel"></div>
|
|
79
|
+
`
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async connectedCallback() {
|
|
83
|
+
await super.connectedCallback()
|
|
84
|
+
|
|
85
|
+
this._boundResize = this.build.bind(this)
|
|
86
|
+
window.addEventListener('resize', this._boundResize)
|
|
87
|
+
|
|
88
|
+
this._slotObserver = new MutationObserver(mutations => {
|
|
89
|
+
this.build()
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
await this.updateComplete
|
|
93
|
+
|
|
94
|
+
this._slotObserver?.observe(this._slot, { childList: true })
|
|
95
|
+
this.build()
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
disconnectedCallback() {
|
|
99
|
+
super.disconnectedCallback()
|
|
100
|
+
|
|
101
|
+
this._slotObserver?.disconnect()
|
|
102
|
+
delete this._slotObserver
|
|
103
|
+
|
|
104
|
+
if (this._boundResize) {
|
|
105
|
+
window.removeEventListener('resize', this._boundResize)
|
|
106
|
+
delete this._boundResize
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
firstUpdated() {
|
|
111
|
+
this._rotation = 0
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
updated(changes: PropertyValues<this>) {
|
|
115
|
+
changes.has('axis') && this._onAxisChanged()
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
build() {
|
|
119
|
+
var pages = Array.from(this.querySelectorAll('[page]'))
|
|
120
|
+
var panel = Array.from(this._carousel.querySelectorAll('ox-board-player-grid')).pop()
|
|
121
|
+
|
|
122
|
+
var rows = this.rows || 1
|
|
123
|
+
var columns = this.columns || 1
|
|
124
|
+
|
|
125
|
+
var i = panel ? panel.querySelectorAll('[page]').length : 0
|
|
126
|
+
var page = pages.shift()
|
|
127
|
+
|
|
128
|
+
while (page) {
|
|
129
|
+
if (!(i++ % (rows * columns))) {
|
|
130
|
+
panel = document.createElement('ox-board-player-grid')
|
|
131
|
+
;(panel as any).rows = rows
|
|
132
|
+
;(panel as any).columns = columns
|
|
133
|
+
|
|
134
|
+
this._carousel.appendChild(panel)
|
|
135
|
+
}
|
|
136
|
+
panel?.appendChild(page)
|
|
137
|
+
|
|
138
|
+
page = pages.shift()
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
this.start()
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
start() {
|
|
145
|
+
var panels = this._carousel.querySelectorAll('ox-board-player-grid')
|
|
146
|
+
|
|
147
|
+
this._isHorizontal = this.axis === 'y'
|
|
148
|
+
|
|
149
|
+
this._panelCount = panels.length
|
|
150
|
+
|
|
151
|
+
this._panelSize = this._carousel[this._isHorizontal ? 'offsetWidth' : 'offsetHeight'] || 640
|
|
152
|
+
this._rotateFn = this._isHorizontal ? 'rotateY' : 'rotateX'
|
|
153
|
+
this._theta = 360 / (this._panelCount || 1)
|
|
154
|
+
|
|
155
|
+
// do some trig to figure out how big the carousel is in 3D space
|
|
156
|
+
this._radius = Math.round(this._panelSize / 2 / Math.tan(Math.PI / (this._panelCount < 2 ? 2 : this._panelCount)))
|
|
157
|
+
|
|
158
|
+
for (let i = 0; i < this._panelCount; i++) {
|
|
159
|
+
let panel = panels[i] as HTMLElement
|
|
160
|
+
let angle = this._theta * i
|
|
161
|
+
panel.style.opacity = '1'
|
|
162
|
+
|
|
163
|
+
panel.style.backgroundColor = 'white'
|
|
164
|
+
|
|
165
|
+
panel.style.transform = this._rotateFn + '(' + angle + 'deg) translateZ(' + this._radius + 'px)'
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// adjust rotation so panels are always flat
|
|
169
|
+
this._rotation = Math.round(this._rotation / this._theta) * this._theta
|
|
170
|
+
this._transform()
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
stop() {
|
|
174
|
+
this._carousel.innerHTML = ''
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
_onAxisChanged() {
|
|
178
|
+
this.start()
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
_transform() {
|
|
182
|
+
// push the carousel back in 3D space, and rotate it
|
|
183
|
+
this._carousel.style.transform =
|
|
184
|
+
'translateZ(-' + this._radius + 'px) ' + this._rotateFn + '(' + this._rotation + 'deg)'
|
|
185
|
+
this.dispatchEvent(new CustomEvent('transform', { bubbles: true, composed: true }))
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
previous() {
|
|
189
|
+
this._rotation += this._theta
|
|
190
|
+
this._transform()
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
next() {
|
|
194
|
+
this._rotation -= this._theta
|
|
195
|
+
this._transform()
|
|
196
|
+
}
|
|
197
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { LitElement, PropertyValues, css, html } from 'lit'
|
|
2
|
+
import { customElement, property, query, state } from 'lit/decorators.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 자식 컴포넌트들을 그리드형태로 화면에 배치하여 한꺼번에 디스플레이해주는 컴포넌트.
|
|
6
|
+
* Example:
|
|
7
|
+
<ox-board-player-grid rows="3" columns="3" tabindex="0" focus>
|
|
8
|
+
<div page>A</div>
|
|
9
|
+
<div page>B</div>
|
|
10
|
+
<div page>C</div>
|
|
11
|
+
<div page>D</div>
|
|
12
|
+
</ox-board-player-grid>
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
@customElement('ox-board-player-grid')
|
|
16
|
+
class BoardPlayerGrid extends LitElement {
|
|
17
|
+
static styles = [
|
|
18
|
+
css`
|
|
19
|
+
:host {
|
|
20
|
+
width: 100%;
|
|
21
|
+
height: 100%;
|
|
22
|
+
position: relative;
|
|
23
|
+
|
|
24
|
+
display: grid;
|
|
25
|
+
grid-gap: 0px;
|
|
26
|
+
grid-auto-flow: dense;
|
|
27
|
+
}
|
|
28
|
+
`
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
@property({ type: Number }) rows: number = 1
|
|
32
|
+
@property({ type: Number }) columns: number = 1
|
|
33
|
+
|
|
34
|
+
@state() _slotObserver?: MutationObserver
|
|
35
|
+
|
|
36
|
+
@query('#slot') _slot!: HTMLSlotElement
|
|
37
|
+
|
|
38
|
+
render() {
|
|
39
|
+
return html` <slot id="slot" select="[page]"></slot> `
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async connectedCallback() {
|
|
43
|
+
await super.connectedCallback()
|
|
44
|
+
|
|
45
|
+
this._slotObserver = new MutationObserver(mutations => {
|
|
46
|
+
var columns = this.columns || 1
|
|
47
|
+
var rows = this.rows || 1
|
|
48
|
+
|
|
49
|
+
;(this.style as any)['grid-template-columns'] = `repeat(${columns}, 1fr)`
|
|
50
|
+
;(this.style as any)['grid-template-rows'] = `repeat(${rows}, 1fr)`
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
await this.updateComplete
|
|
54
|
+
|
|
55
|
+
var columns = this.columns || 1
|
|
56
|
+
var rows = this.rows || 1
|
|
57
|
+
|
|
58
|
+
;(this.style as any)['grid-template-columns'] = `repeat(${columns}, 1fr)`
|
|
59
|
+
;(this.style as any)['grid-template-rows'] = `repeat(${rows}, 1fr)`
|
|
60
|
+
|
|
61
|
+
this._slotObserver?.observe(this._slot, { childList: true })
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
disconnectedCallback() {
|
|
65
|
+
super.disconnectedCallback()
|
|
66
|
+
|
|
67
|
+
this._slotObserver?.disconnect()
|
|
68
|
+
delete this._slotObserver
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
start() {}
|
|
72
|
+
|
|
73
|
+
stop() {}
|
|
74
|
+
|
|
75
|
+
next() {}
|
|
76
|
+
|
|
77
|
+
previous() {}
|
|
78
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 풀스크린 전환 이후와 풀스크린 해제 이후에 호출되는 콜백함수
|
|
3
|
+
* @callback FullscreenCallback
|
|
4
|
+
*/
|
|
5
|
+
export type FullscreenCallback = () => void
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 엘리먼트를 풀스크린으로 표시되도록 한다.
|
|
9
|
+
* @param {HTMLElement} element 대상 엘리먼트
|
|
10
|
+
* @param {FullscreenCallback} afterfull 풀스크린이 된 이후에 호출되는 콜백함수
|
|
11
|
+
* @param {FullscreenCallback} afterfinish 풀스크린이 해제된 이후에 호출되는 콜백함수
|
|
12
|
+
*/
|
|
13
|
+
export function fullscreen(element: HTMLElement, afterfull?: FullscreenCallback, afterfinish?: FullscreenCallback) {
|
|
14
|
+
var org_width = element.style.width
|
|
15
|
+
var org_height = element.style.height
|
|
16
|
+
|
|
17
|
+
function _fullscreen_callback(e: Event) {
|
|
18
|
+
if (
|
|
19
|
+
!document.fullscreenElement &&
|
|
20
|
+
//@ts-ignore
|
|
21
|
+
!document.mozFullScreen &&
|
|
22
|
+
//@ts-ignore
|
|
23
|
+
!document.webkitIsFullScreen &&
|
|
24
|
+
//@ts-ignore
|
|
25
|
+
!document.msFullscreenElement
|
|
26
|
+
) {
|
|
27
|
+
;['fullscreenchange', 'webkitfullscreenchange', 'MSFullscreenChange'].forEach(event =>
|
|
28
|
+
document.removeEventListener(event, _fullscreen_callback)
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
element.style.width = org_width
|
|
32
|
+
element.style.height = org_height
|
|
33
|
+
|
|
34
|
+
afterfinish && afterfinish()
|
|
35
|
+
} else {
|
|
36
|
+
element.style.width = '100%'
|
|
37
|
+
element.style.height = '100%'
|
|
38
|
+
|
|
39
|
+
afterfull && afterfull()
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
;['fullscreenchange', 'webkitfullscreenchange', 'MSFullscreenChange'].forEach(event =>
|
|
44
|
+
document.addEventListener(event, _fullscreen_callback)
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
if (element.requestFullscreen) element.requestFullscreen()
|
|
48
|
+
//@ts-ignore
|
|
49
|
+
else if (element.webkitRequestFullScreen) element.webkitRequestFullScreen()
|
|
50
|
+
//@ts-ignore
|
|
51
|
+
else if (element.mozRequestFullScreen) element.mozRequestFullScreen()
|
|
52
|
+
//@ts-ignore
|
|
53
|
+
else if (element.msRequestFullscreen) element.msRequestFullscreen()
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function exitfullscreen() {
|
|
57
|
+
if (document.exitFullscreen) document.exitFullscreen()
|
|
58
|
+
//@ts-ignore
|
|
59
|
+
else if (document.mozCancelFullScreen) document.mozCancelFullScreen()
|
|
60
|
+
//@ts-ignore
|
|
61
|
+
else if (document.webkitCancelFullScreen) document.webkitCancelFullScreen()
|
|
62
|
+
//@ts-ignore
|
|
63
|
+
else if (document.msExitFullscreen) document.msExitFullscreen()
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function togglefullscreen(
|
|
67
|
+
element: HTMLElement,
|
|
68
|
+
afterfull?: FullscreenCallback,
|
|
69
|
+
afterfinish?: FullscreenCallback
|
|
70
|
+
) {
|
|
71
|
+
if (
|
|
72
|
+
!document.fullscreenElement &&
|
|
73
|
+
//@ts-ignore
|
|
74
|
+
!document.mozFullScreen &&
|
|
75
|
+
//@ts-ignore
|
|
76
|
+
!document.webkitIsFullScreen &&
|
|
77
|
+
//@ts-ignore
|
|
78
|
+
!document.msFullscreenElement
|
|
79
|
+
)
|
|
80
|
+
fullscreen(element, afterfull, afterfinish)
|
|
81
|
+
else exitfullscreen()
|
|
82
|
+
}
|
package/src/utils/os.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
var os = 'Unknown OS'
|
|
2
|
+
|
|
3
|
+
if (navigator.userAgent.indexOf('Win') != -1) os = 'Windows'
|
|
4
|
+
else if (navigator.userAgent.indexOf('Mac') != -1) os = 'Mac'
|
|
5
|
+
else if (navigator.userAgent.indexOf('Linux') != -1) os = 'Linux'
|
|
6
|
+
else if (navigator.userAgent.indexOf('Android') != -1) os = 'Android'
|
|
7
|
+
else if (navigator.userAgent.indexOf('like Mac') != -1) os = 'iOS'
|
|
8
|
+
|
|
9
|
+
var mobile = os === 'iOS' || os === 'Android'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* method to get operating system of running browser
|
|
13
|
+
* @returns {string} name of OS : 'Windows | 'Mac' | 'Android' | 'iOS'
|
|
14
|
+
*/
|
|
15
|
+
export function getOS() {
|
|
16
|
+
return os
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* method to tell if platform of running browser is kind of mobile device
|
|
21
|
+
* @returns {boolean}
|
|
22
|
+
*/
|
|
23
|
+
export function isMobileDevice() {
|
|
24
|
+
return mobile
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* method to tell if operating system of running browser is iOS
|
|
29
|
+
* @returns {boolean}
|
|
30
|
+
*/
|
|
31
|
+
export function isIOS() {
|
|
32
|
+
return os === 'iOS'
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* method to tell if operating system of running browser is MacOS
|
|
37
|
+
* @returns {boolean}
|
|
38
|
+
*/
|
|
39
|
+
export function isMacOS() {
|
|
40
|
+
return os === 'Mac'
|
|
41
|
+
}
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Starts monitoring swipes on the given element and
|
|
3
|
+
* emits `swipe` event when a swipe gesture is performed.
|
|
4
|
+
* @param {DOMElement} element Element on which to listen for swipe gestures.
|
|
5
|
+
* @param {Object} options Optional: Options.
|
|
6
|
+
* @return {Object}
|
|
7
|
+
*/
|
|
8
|
+
export function SwipeListener(element: HTMLElement, options?: any) {
|
|
9
|
+
if (!element) return
|
|
10
|
+
|
|
11
|
+
// CustomEvent polyfill
|
|
12
|
+
if (typeof window !== 'undefined') {
|
|
13
|
+
;(function () {
|
|
14
|
+
if (typeof window.CustomEvent === 'function') return false
|
|
15
|
+
function CustomEvent(event: string, params: any) {
|
|
16
|
+
params = params || { bubbles: false, cancelable: false, detail: undefined }
|
|
17
|
+
var evt = document.createEvent('CustomEvent')
|
|
18
|
+
evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail)
|
|
19
|
+
return evt
|
|
20
|
+
}
|
|
21
|
+
CustomEvent.prototype = window.Event.prototype
|
|
22
|
+
;(window as any).CustomEvent = CustomEvent
|
|
23
|
+
})()
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let defaultOpts = {
|
|
27
|
+
minHorizontal: 10, // Minimum number of pixels traveled to count as a horizontal swipe.
|
|
28
|
+
minVertical: 10, // Minimum number of pixels traveled to count as a vertical swipe.
|
|
29
|
+
deltaHorizontal: 3, // Delta for horizontal swipe
|
|
30
|
+
deltaVertical: 5, // Delta for vertical swipe
|
|
31
|
+
preventScroll: false, // Prevents scrolling when swiping.
|
|
32
|
+
lockAxis: true, // Select only one axis to be true instead of multiple.
|
|
33
|
+
touch: true, // Listen for touch events
|
|
34
|
+
mouse: true // Listen for mouse events
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Set options
|
|
38
|
+
if (!options) {
|
|
39
|
+
options = {}
|
|
40
|
+
}
|
|
41
|
+
options = {
|
|
42
|
+
...defaultOpts,
|
|
43
|
+
...options
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Store the touches
|
|
47
|
+
let touches: Array<{ x: number; y: number }> = []
|
|
48
|
+
|
|
49
|
+
// Not dragging by default.
|
|
50
|
+
let dragging = false
|
|
51
|
+
|
|
52
|
+
// When mouse-click is started, make dragging true.
|
|
53
|
+
const _mousedown = function (e: MouseEvent) {
|
|
54
|
+
dragging = true
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// When mouse-click is released, make dragging false and signify end by imitating `touchend`.
|
|
58
|
+
const _mouseup = function (e: MouseEvent) {
|
|
59
|
+
dragging = false
|
|
60
|
+
_touchend(e as any)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// When mouse is moved while being clicked, imitate a `touchmove`.
|
|
64
|
+
const _mousemove = function (e: MouseEvent) {
|
|
65
|
+
if (dragging) {
|
|
66
|
+
;(e as any).changedTouches = [
|
|
67
|
+
{
|
|
68
|
+
clientX: e.clientX,
|
|
69
|
+
clientY: e.clientY
|
|
70
|
+
}
|
|
71
|
+
]
|
|
72
|
+
_touchmove(e as any)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (options.mouse) {
|
|
77
|
+
element.addEventListener('mousedown', _mousedown)
|
|
78
|
+
element.addEventListener('mouseup', _mouseup)
|
|
79
|
+
element.addEventListener('mousemove', _mousemove)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// When the swipe is completed, calculate the direction.
|
|
83
|
+
const _touchend = function (e: TouchEvent) {
|
|
84
|
+
if (!touches.length) return
|
|
85
|
+
|
|
86
|
+
const touch = typeof TouchEvent === 'function' && e instanceof TouchEvent
|
|
87
|
+
|
|
88
|
+
let x = [],
|
|
89
|
+
y = []
|
|
90
|
+
|
|
91
|
+
let directions = {
|
|
92
|
+
top: false,
|
|
93
|
+
right: false,
|
|
94
|
+
bottom: false,
|
|
95
|
+
left: false
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
for (let i = 0; i < touches.length; i++) {
|
|
99
|
+
x.push(touches[i].x)
|
|
100
|
+
y.push(touches[i].y)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const xs = x[0],
|
|
104
|
+
xe = x[x.length - 1], // Start and end x-coords
|
|
105
|
+
ys = y[0],
|
|
106
|
+
ye = y[y.length - 1] // Start and end y-coords
|
|
107
|
+
|
|
108
|
+
const eventCoords = {
|
|
109
|
+
x: [xs, xe],
|
|
110
|
+
y: [ys, ye]
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (touches.length > 1) {
|
|
114
|
+
const swipeReleaseEventData = {
|
|
115
|
+
detail: {
|
|
116
|
+
touch,
|
|
117
|
+
target: e.target,
|
|
118
|
+
...eventCoords
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
let swipeReleaseEvent = new CustomEvent('swiperelease', swipeReleaseEventData)
|
|
123
|
+
element.dispatchEvent(swipeReleaseEvent)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Determine left or right
|
|
127
|
+
let diff = x[0] - x[x.length - 1]
|
|
128
|
+
let swipe = 'none'
|
|
129
|
+
if (diff > 0) {
|
|
130
|
+
swipe = 'left'
|
|
131
|
+
} else {
|
|
132
|
+
swipe = 'right'
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
let min = Math.min(...x),
|
|
136
|
+
max = Math.max(...x),
|
|
137
|
+
_diff
|
|
138
|
+
|
|
139
|
+
// If minimum horizontal distance was travelled
|
|
140
|
+
if (Math.abs(diff) >= options.minHorizontal) {
|
|
141
|
+
switch (swipe) {
|
|
142
|
+
case 'left':
|
|
143
|
+
_diff = Math.abs(min - x[x.length - 1])
|
|
144
|
+
if (_diff <= options.deltaHorizontal) {
|
|
145
|
+
directions.left = true
|
|
146
|
+
}
|
|
147
|
+
break
|
|
148
|
+
case 'right':
|
|
149
|
+
_diff = Math.abs(max - x[x.length - 1])
|
|
150
|
+
if (_diff <= options.deltaHorizontal) {
|
|
151
|
+
directions.right = true
|
|
152
|
+
}
|
|
153
|
+
break
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Determine top or bottom
|
|
158
|
+
diff = y[0] - y[y.length - 1]
|
|
159
|
+
swipe = 'none'
|
|
160
|
+
if (diff > 0) {
|
|
161
|
+
swipe = 'top'
|
|
162
|
+
} else {
|
|
163
|
+
swipe = 'bottom'
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
min = Math.min(...y)
|
|
167
|
+
max = Math.max(...y)
|
|
168
|
+
|
|
169
|
+
// If minimum vertical distance was travelled
|
|
170
|
+
if (Math.abs(diff) >= options.minVertical) {
|
|
171
|
+
switch (swipe) {
|
|
172
|
+
case 'top':
|
|
173
|
+
_diff = Math.abs(min - y[y.length - 1])
|
|
174
|
+
if (_diff <= options.deltaVertical) {
|
|
175
|
+
directions.top = true
|
|
176
|
+
}
|
|
177
|
+
break
|
|
178
|
+
case 'bottom':
|
|
179
|
+
_diff = Math.abs(max - y[y.length - 1])
|
|
180
|
+
if (_diff <= options.deltaVertical) {
|
|
181
|
+
directions.bottom = true
|
|
182
|
+
}
|
|
183
|
+
break
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Clear touches array.
|
|
188
|
+
touches = []
|
|
189
|
+
|
|
190
|
+
// If there is a swipe direction, emit an event.
|
|
191
|
+
if (directions.top || directions.right || directions.bottom || directions.left) {
|
|
192
|
+
/**
|
|
193
|
+
* If lockAxis is true, determine which axis to select.
|
|
194
|
+
* The axis with the most travel is selected.
|
|
195
|
+
* TODO: Factor in for the orientation of the device
|
|
196
|
+
* and use it as a weight to determine the travel along an axis.
|
|
197
|
+
*/
|
|
198
|
+
if (options.lockAxis) {
|
|
199
|
+
if ((directions.left || directions.right) && Math.abs(xs - xe) > Math.abs(ys - ye)) {
|
|
200
|
+
directions.top = directions.bottom = false
|
|
201
|
+
} else if ((directions.top || directions.bottom) && Math.abs(xs - xe) < Math.abs(ys - ye)) {
|
|
202
|
+
directions.left = directions.right = false
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const eventData = {
|
|
207
|
+
detail: {
|
|
208
|
+
directions,
|
|
209
|
+
touch,
|
|
210
|
+
target: e.target,
|
|
211
|
+
...eventCoords
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
let event = new CustomEvent('swipe', eventData)
|
|
216
|
+
element.dispatchEvent(event)
|
|
217
|
+
} else {
|
|
218
|
+
let cancelEvent = new CustomEvent('swipecancel', {
|
|
219
|
+
detail: {
|
|
220
|
+
touch,
|
|
221
|
+
target: e.target,
|
|
222
|
+
...eventCoords
|
|
223
|
+
}
|
|
224
|
+
})
|
|
225
|
+
element.dispatchEvent(cancelEvent)
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// When a swipe is performed, store the coords.
|
|
230
|
+
const _touchmove = function (e: TouchEvent) {
|
|
231
|
+
let touch = e.changedTouches[0]
|
|
232
|
+
touches.push({
|
|
233
|
+
x: touch.clientX,
|
|
234
|
+
y: touch.clientY
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
// Emit a `swiping` event if there are more than one touch-points.
|
|
238
|
+
if (touches.length > 1) {
|
|
239
|
+
const xs = touches[0].x, // Start and end x-coords
|
|
240
|
+
xe = touches[touches.length - 1].x,
|
|
241
|
+
ys = touches[0].y, // Start and end y-coords
|
|
242
|
+
ye = touches[touches.length - 1].y,
|
|
243
|
+
eventData = {
|
|
244
|
+
detail: {
|
|
245
|
+
x: [xs, xe],
|
|
246
|
+
y: [ys, ye],
|
|
247
|
+
touch: typeof TouchEvent === 'function' && e instanceof TouchEvent,
|
|
248
|
+
target: e.target
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
let event = new CustomEvent('swiping', eventData)
|
|
252
|
+
|
|
253
|
+
const shouldPrevent =
|
|
254
|
+
options.preventScroll === true || (typeof options.preventScroll === 'function' && options.preventScroll(event))
|
|
255
|
+
|
|
256
|
+
if (shouldPrevent) {
|
|
257
|
+
e.preventDefault()
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
element.dispatchEvent(event)
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Test via a getter in the options object to see if the passive property is accessed
|
|
265
|
+
let passiveOptions: any = false
|
|
266
|
+
try {
|
|
267
|
+
const testOptions = Object.defineProperty({}, 'passive', {
|
|
268
|
+
get: function () {
|
|
269
|
+
passiveOptions = { passive: !options.preventScroll }
|
|
270
|
+
}
|
|
271
|
+
})
|
|
272
|
+
window.addEventListener('testPassive', null as any, testOptions)
|
|
273
|
+
window.removeEventListener('testPassive', null as any, testOptions)
|
|
274
|
+
} catch (e) {}
|
|
275
|
+
|
|
276
|
+
if (options.touch) {
|
|
277
|
+
element.addEventListener('touchmove', _touchmove, passiveOptions)
|
|
278
|
+
element.addEventListener('touchend', _touchend)
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
return {
|
|
282
|
+
off: function () {
|
|
283
|
+
element.removeEventListener('touchmove', _touchmove, passiveOptions)
|
|
284
|
+
element.removeEventListener('touchend', _touchend)
|
|
285
|
+
element.removeEventListener('mousedown', _mousedown)
|
|
286
|
+
element.removeEventListener('mouseup', _mouseup)
|
|
287
|
+
element.removeEventListener('mousemove', _mousemove)
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
declare module '@hatiolab/things-scene'
|