cordova-plugin-screen-orien.../www/screenorientation.js

121 lines
3.7 KiB
JavaScript
Raw Normal View History

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
2016-12-14 07:39:58 +08:00
var screenOrientation = {},
Orientations = [
'portrait-primary',
'portrait-secondary',
'landscape-primary',
'landscape-secondary',
'portrait', // either portrait-primary or portrait-secondary.
'landscape', // either landscape-primary or landscape-secondary.
'any' // All orientations are supported (unlocked orientation)
];
2014-05-06 14:53:23 +08:00
2016-12-14 07:39:58 +08:00
screenOrientation.Orientations = Orientations;
screenOrientation.currOrientation = 'any';
var orientationMask = 0;
screenOrientation.setOrientation = function(orientation) {
if(orientation == Orientations[0]){
orientationMask = 1;
}
else if(orientation == Orientations[1]){
orientationMask = 2;
}
else if(orientation == Orientations[2]){
orientationMask = 4;
}
else if(orientation == Orientations[3]){
orientationMask = 8;
}
else if(orientation == Orientations[4]){
orientationMask = 3;
}
else if(orientation == Orientations[5]){
orientationMask = 12;
}
else if(orientation == Orientations[6]){
orientationMask = 15;
}
2014-05-06 14:53:23 +08:00
2016-12-14 07:39:58 +08:00
cordova.exec(null, null, "CDVOrientation", "screenOrientation", [orientationMask, orientation]);
//console.log('setOrientation not supported on device');
};
2016-12-14 07:39:58 +08:00
function addScreenOrientationApi(screenObject) {
if (screenObject.unlockOrientation || screenObject.lockOrientation) {
return;
}
2016-12-14 07:39:58 +08:00
screenObject.lockOrientation = function(orientation) {
2016-12-14 07:39:58 +08:00
var p = new Promise(function(resolve,reject){
if (Orientations.indexOf(orientation) == -1) {
var err = new Error();
err.name = "NotSupportedError";
reject(err);//"cannot change orientation");
}
else {
screenOrientation.currOrientation = screenObject.orientation = orientation;
screenOrientation.setOrientation(orientation);
resolve("Orientation set"); // orientation change successful
}
});
return p;
};
2014-05-06 14:53:23 +08:00
2016-12-14 07:39:58 +08:00
screenObject.unlockOrientation = function() {
screenOrientation.currOrientation = screenObject.orientation = 'any';
screenOrientation.setOrientation('any');
};
}
2016-12-14 07:39:58 +08:00
addScreenOrientationApi(screen);
orientationChange();
2014-05-06 14:53:23 +08:00
2016-12-14 07:39:58 +08:00
function orientationChange() {
var orientation;
2016-12-14 07:39:58 +08:00
switch (window.orientation) {
case 0:
orientation = 'portrait-primary';
break;
case 90:
orientation = 'landscape-primary';
break;
case 180:
orientation = 'portrait-secondary';
break;
case -90:
orientation = 'landscape-secondary';
break;
default:
orientation = 'any';
}
2016-12-14 07:39:58 +08:00
screen.orientation = orientation;
}
2016-12-14 07:39:58 +08:00
window.addEventListener("orientationchange", orientationChange, true);
2016-09-21 01:36:36 +08:00
2016-12-14 07:39:58 +08:00
module.exports = screenOrientation;