-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (72 loc) · 2.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const window = require('global/window')
var handlerCallback
var useOrientation
function setUseOrientation () {
useOrientation = true
}
function setUseWindowSize () {
useOrientation = false
}
function getScreenOrientation (orientationEvent) {
const orientation = orientationEvent !== undefined
? orientationEvent
: getOrientation()
const type = orientation.type
const angle = orientation.angle
const types = type.split('-')
const direction = types[0]
const version = types[1]
return {
direction: direction,
version: version,
angle: angle
}
}
function addEventOnOrientationChange (callback) {
handlerCallback = callback
if (shouldUseOrientation()) {
window.screen.orientation.addEventListener('change', handleOrientationChange, false)
} else {
window.addEventListener('resize', handleResize, false)
}
}
function removeEventOnOrientationChange () {
if (shouldUseOrientation()) {
window.screen.orientation.removeEventListener('change', handleOrientationChange)
} else {
window.removeEventListener('resize', handleResize)
}
}
function shouldUseOrientation () {
if (useOrientation === true) {
return true
} else if (useOrientation === false) {
return false
}
return (window.screen !== undefined && window.screen.orientation !== undefined)
}
function getOrientation () {
return shouldUseOrientation()
? window.screen.orientation
: detect(window)
}
function detect (windowtarget) {
const x = windowtarget.innerWidth
const y = windowtarget.innerHeight
return {
type: x >= y ? 'landscape-primary' : 'portrait-primary',
angle: 0
}
}
function handleOrientationChange (event) {
return handlerCallback(getScreenOrientation(event.target))
}
function handleResize (event) {
return handlerCallback(getScreenOrientation(detect(event.target)))
}
const screenOrientationModule = module.exports = getScreenOrientation
screenOrientationModule.setUseOrientation = setUseOrientation
screenOrientationModule.setUseWindowSize = setUseWindowSize
screenOrientationModule.getScreenOrientation = getScreenOrientation
screenOrientationModule.addEventOnOrientationChange = addEventOnOrientationChange
screenOrientationModule.removeEventOnOrientationChange = removeEventOnOrientationChange