UNPKG

3.25 kBJavaScriptView Raw
1/*
2The MIT License (MIT)
3
4Copyright (c) 2014
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23 */
24var screenOrientation = {},
25 Orientations = [
26 'portrait-primary',
27 // The orientation is in the primary portrait mode.
28 'portrait-secondary',
29 // The orientation is in the secondary portrait mode.
30 'landscape-primary',
31 // The orientation is in the primary landscape mode.
32 'landscape-secondary',
33 // The orientation is in the secondary landscape mode.
34 'portrait',
35 // The orientation is either portrait-primary or portrait-secondary.
36 'landscape'
37 // The orientation is either landscape-primary or landscape-secondary.
38 ];
39
40screenOrientation.Orientations = Orientations;
41screenOrientation.currOrientation = 'unlocked';
42
43screenOrientation.setOrientation = function(orientation) {
44 //platform specific files override this function
45 console.log('setOrientation not supported on device');
46};
47
48function addScreenOrientationApi(obj) {
49 if (obj.unlockOrientation || obj.lockOrientation) {
50 return;
51 }
52
53 obj.lockOrientation = function(orientation) {
54 if (Orientations.indexOf(orientation) == -1) {
55 console.log('INVALID ORIENTATION', orientation);
56 return;
57 }
58 screenOrientation.currOrientation = orientation;
59 screenOrientation.setOrientation(orientation);
60 };
61
62 obj.unlockOrientation = function() {
63 screenOrientation.currOrientation = 'unlocked';
64 screenOrientation.setOrientation('unlocked');
65 };
66}
67
68addScreenOrientationApi(screen);
69orientationChange();
70
71function orientationChange() {
72 var orientation;
73
74 switch (window.orientation) {
75 case 0:
76 orientation = 'portrait-primary';
77 break;
78 case 90:
79 orientation = 'landscape-secondary';
80 break;
81 case 180:
82 orientation = 'portrait-secondary';
83 break;
84 case -90:
85 orientation = 'landscape-primary';
86 break;
87 default:
88 orientation = 'unknown';
89 }
90
91 screen.orientation = orientation;
92}
93
94window.addEventListener("orientationchange", orientationChange, true);
95
96module.exports = screenOrientation;
\No newline at end of file