1 | /*
|
2 | The MIT License (MIT)
|
3 |
|
4 | Copyright (c) 2014
|
5 |
|
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | of this software and associated documentation files (the "Software"), to deal
|
8 | in the Software without restriction, including without limitation the rights
|
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | copies of the Software, and to permit persons to whom the Software is
|
11 | furnished to do so, subject to the following conditions:
|
12 |
|
13 | The above copyright notice and this permission notice shall be included in all
|
14 | copies or substantial portions of the Software.
|
15 |
|
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22 | SOFTWARE.
|
23 | */
|
24 | var 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 |
|
40 | screenOrientation.Orientations = Orientations;
|
41 | screenOrientation.currOrientation = 'unlocked';
|
42 |
|
43 | screenOrientation.setOrientation = function(orientation) {
|
44 | //platform specific files override this function
|
45 | console.log('setOrientation not supported on device');
|
46 | };
|
47 |
|
48 | function 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 |
|
68 | addScreenOrientationApi(screen);
|
69 | orientationChange();
|
70 |
|
71 | function 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 |
|
94 | window.addEventListener("orientationchange", orientationChange, true);
|
95 |
|
96 | module.exports = screenOrientation; |
\ | No newline at end of file |