forked from wonday/react-native-orientation-locker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
170 lines (145 loc) · 4.05 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// react-native-orientation-locker
//
//
// Created by Wonday on 17/5/12.
// Copyright (c) wonday.org All rights reserved.
//
"use strict";
import React, {Component} from "react";
const OrientationNative = require("react-native").NativeModules.Orientation;
const {NativeEventEmitter, Platform} = require("react-native");
const LocalEventEmitter = new NativeEventEmitter(OrientationNative);
var listeners = {};
var id = 0;
var META = "__listener_id";
var locked = false;
function getKey(listener) {
if (!listener.hasOwnProperty(META)) {
if (!Object.isExtensible(listener)) {
return "F";
}
Object.defineProperty(listener, META, {
value: "L" + ++id
});
}
return listener[META];
}
export default class Orientation {
static getOrientation = cb => {
OrientationNative.getOrientation(orientation => {
cb(orientation);
});
};
static getDeviceOrientation = cb => {
OrientationNative.getDeviceOrientation(deviceOrientation => {
cb(deviceOrientation);
});
};
static isLocked = () => {
return locked;
}
static lockToPortrait = () => {
locked = true;
OrientationNative.lockToPortrait();
};
static lockToPortraitUpsideDown = () => {
locked = true;
OrientationNative.lockToPortraitUpsideDown();
};
static lockToLandscape = () => {
locked = true;
OrientationNative.lockToLandscape();
};
static lockToLandscapeRight = () => {
locked = true;
OrientationNative.lockToLandscapeRight();
};
static lockToLandscapeLeft = () => {
locked = true;
OrientationNative.lockToLandscapeLeft();
};
// OrientationMaskAllButUpsideDown
static lockToAllOrientationsButUpsideDown = () => {
locked = true;
if (Platform.OS === "ios") {
OrientationNative.lockToAllOrientationsButUpsideDown();
}
};
static unlockAllOrientations = () => {
locked = false;
OrientationNative.unlockAllOrientations();
};
static addOrientationListener = cb => {
var key = getKey(cb);
listeners[key] = LocalEventEmitter.addListener(
"orientationDidChange",
body => {
cb(body.orientation);
}
);
};
static removeOrientationListener = cb => {
var key = getKey(cb);
if (!listeners[key]) {
return;
}
listeners[key].remove();
listeners[key] = null;
};
static addDeviceOrientationListener = cb => {
var key = getKey(cb);
listeners[key] = LocalEventEmitter.addListener(
"deviceOrientationDidChange",
body => {
cb(body.deviceOrientation);
}
);
};
static removeDeviceOrientationListener = cb => {
var key = getKey(cb);
if (!listeners[key]) {
return;
}
listeners[key].remove();
listeners[key] = null;
};
static addLockListener = cb => {
var key = getKey(cb);
listeners[key] = LocalEventEmitter.addListener(
"lockDidChange",
body => {
cb(body.orientation);
}
);
};
static removeLockListener = cb => {
var key = getKey(cb);
if (!listeners[key]) {
return;
}
listeners[key].remove();
listeners[key] = null;
};
static removeAllListeners = () => {
for(var key in listeners){
if (!listeners[key]) {
continue;
}
listeners[key].remove();
listeners[key] = null;
}
};
static getInitialOrientation = () => {
return OrientationNative.initialOrientation;
};
static getAutoRotateState = cb => {
if (Platform.OS === "android") {
OrientationNative.getAutoRotateState(state => {
cb(state);
});
} else {
cb(true); // iOS not implement
}
};
}