forked from yamill/react-native-orientation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
100 lines (78 loc) · 2 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
92
93
94
95
96
97
98
99
100
var Orientation = require('react-native').NativeModules.Orientation;
var DeviceEventEmitter = require('react-native').DeviceEventEmitter;
var listeners = {};
var orientationDidChangeEvent = 'orientationDidChange';
var specificOrientationDidChangeEvent = 'specificOrientationDidChange';
var id = 0;
var META = '__listener_id';
function getKey(listener) {
if (!listener.hasOwnProperty(META)) {
if (!Object.isExtensible(listener)) {
return 'F';
}
Object.defineProperty(listener, META, {
value: 'L' + ++id,
});
}
return listener[META];
};
module.exports = {
getOrientation(cb) {
Orientation.getOrientation((error,orientation) =>{
cb(error, orientation);
});
},
getSpecificOrientation(cb) {
Orientation.getSpecificOrientation((error,orientation) =>{
cb(error, orientation);
});
},
lockToPortrait() {
Orientation.lockToPortrait();
},
lockToLandscape() {
Orientation.lockToLandscape();
},
lockToLandscapeRight() {
Orientation.lockToLandscapeRight();
},
lockToLandscapeLeft() {
Orientation.lockToLandscapeLeft();
},
unlockAllOrientations() {
Orientation.unlockAllOrientations();
},
addOrientationListener(cb) {
var key = getKey(cb);
listeners[key] = DeviceEventEmitter.addListener(orientationDidChangeEvent,
(body) => {
cb(body.orientation);
});
},
removeOrientationListener(cb) {
var key = getKey(cb);
if (!listeners[key]) {
return;
}
listeners[key].remove();
listeners[key] = null;
},
addSpecificOrientationListener(cb) {
var key = getKey(cb);
listeners[key] = DeviceEventEmitter.addListener(specificOrientationDidChangeEvent,
(body) => {
cb(body.specificOrientation);
});
},
removeSpecificOrientationListener(cb) {
var key = getKey(cb);
if (!listeners[key]) {
return;
}
listeners[key].remove();
listeners[key] = null;
},
getInitialOrientation() {
return Orientation.initialOrientation;
}
}