forked from wonday/react-native-orientation-locker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (69 loc) · 2.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
//
// 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, DeviceEventEmitter } = require('react-native');
const LocalEventEmitter = new NativeEventEmitter(OrientationNative);
var listeners = {};
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];
};
export default class Orientation {
static getOrientation = (cb) => {
OrientationNative.getOrientation((orientation) =>{
cb(orientation);
});
};
static lockToPortrait = () => {
OrientationNative.lockToPortrait();
};
static lockToPortraitUpsideDown = () => {
OrientationNative.lockToPortraitUpsideDown();
};
static lockToLandscape = () => {
OrientationNative.lockToLandscape();
};
static lockToLandscapeRight = () => {
OrientationNative.lockToLandscapeRight();
};
static lockToLandscapeLeft = () => {
OrientationNative.lockToLandscapeLeft();
};
static unlockAllOrientations = () => {
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 getInitialOrientation = () => {
return OrientationNative.initialOrientation;
};
};