2014-05-06 14:53:23 +08:00
|
|
|
/*
|
2016-05-06 02:46:35 +08:00
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
*/
|
2014-05-06 14:53:23 +08:00
|
|
|
|
2014-07-30 06:49:28 +08:00
|
|
|
var screenOrientation = {},
|
2014-07-13 07:26:44 +08:00
|
|
|
Orientations = [
|
|
|
|
'portrait-primary',
|
|
|
|
// The orientation is in the primary portrait mode.
|
|
|
|
'portrait-secondary',
|
|
|
|
// The orientation is in the secondary portrait mode.
|
|
|
|
'landscape-primary',
|
|
|
|
// The orientation is in the primary landscape mode.
|
|
|
|
'landscape-secondary',
|
|
|
|
// The orientation is in the secondary landscape mode.
|
|
|
|
'portrait',
|
|
|
|
// The orientation is either portrait-primary or portrait-secondary.
|
|
|
|
'landscape'
|
2014-07-13 14:50:16 +08:00
|
|
|
// The orientation is either landscape-primary or landscape-secondary.
|
2014-07-27 11:51:20 +08:00
|
|
|
];
|
2014-05-06 14:53:23 +08:00
|
|
|
|
2014-07-27 11:51:20 +08:00
|
|
|
screenOrientation.Orientations = Orientations;
|
|
|
|
screenOrientation.currOrientation = 'unlocked';
|
2014-05-06 14:53:23 +08:00
|
|
|
|
2014-07-27 11:51:20 +08:00
|
|
|
screenOrientation.setOrientation = function(orientation) {
|
2014-07-30 06:49:28 +08:00
|
|
|
//platform specific files override this function
|
|
|
|
console.log('setOrientation not supported on device');
|
2014-07-27 11:51:20 +08:00
|
|
|
};
|
2014-07-12 14:05:30 +08:00
|
|
|
|
2015-08-10 07:37:38 +08:00
|
|
|
function addScreenOrientationApi(screenObject) {
|
|
|
|
if (screenObject.unlockOrientation || screenObject.lockOrientation) {
|
2014-07-12 14:05:30 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-10 07:37:38 +08:00
|
|
|
screenObject.lockOrientation = function(orientation) {
|
2014-07-13 07:26:44 +08:00
|
|
|
if (Orientations.indexOf(orientation) == -1) {
|
|
|
|
console.log('INVALID ORIENTATION', orientation);
|
|
|
|
return;
|
|
|
|
}
|
2015-08-10 07:37:38 +08:00
|
|
|
screenOrientation.currOrientation = screenObject.orientation = orientation;
|
2014-07-27 11:51:20 +08:00
|
|
|
screenOrientation.setOrientation(orientation);
|
2014-07-12 14:05:30 +08:00
|
|
|
};
|
|
|
|
|
2015-08-10 07:37:38 +08:00
|
|
|
screenObject.unlockOrientation = function() {
|
|
|
|
screenOrientation.currOrientation = screenObject.orientation = 'unlocked';
|
2014-07-27 11:51:20 +08:00
|
|
|
screenOrientation.setOrientation('unlocked');
|
2014-07-12 14:05:30 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
addScreenOrientationApi(screen);
|
2014-07-27 11:51:20 +08:00
|
|
|
orientationChange();
|
2014-05-06 14:53:23 +08:00
|
|
|
|
2014-07-27 11:51:20 +08:00
|
|
|
function orientationChange() {
|
|
|
|
var orientation;
|
|
|
|
|
|
|
|
switch (window.orientation) {
|
|
|
|
case 0:
|
|
|
|
orientation = 'portrait-primary';
|
|
|
|
break;
|
|
|
|
case 90:
|
2015-07-20 20:37:52 +08:00
|
|
|
orientation = 'landscape-primary';
|
2014-07-27 11:51:20 +08:00
|
|
|
break;
|
|
|
|
case 180:
|
|
|
|
orientation = 'portrait-secondary';
|
|
|
|
break;
|
|
|
|
case -90:
|
2015-07-20 20:37:52 +08:00
|
|
|
orientation = 'landscape-secondary';
|
2014-07-27 11:51:20 +08:00
|
|
|
break;
|
2014-07-13 07:26:44 +08:00
|
|
|
default:
|
2014-07-27 11:51:20 +08:00
|
|
|
orientation = 'unknown';
|
2014-05-08 05:56:40 +08:00
|
|
|
}
|
2014-05-06 14:53:23 +08:00
|
|
|
|
2014-07-27 11:51:20 +08:00
|
|
|
screen.orientation = orientation;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener("orientationchange", orientationChange, true);
|
|
|
|
|
|
|
|
module.exports = screenOrientation;
|