mirror of
https://github.com/apache/cordova-plugin-screen-orientation.git
synced 2026-05-12 00:01:32 +08:00
New screenOrientation api, working minus checks and event firing
This commit is contained in:
+58
-32
@@ -23,23 +23,25 @@ SOFTWARE.
|
||||
*/
|
||||
var argscheck = require('cordova/argscheck'),
|
||||
exec = require('cordova/exec'),
|
||||
|
||||
Constants = {
|
||||
Orientation: {
|
||||
UNSPECIFIED: "unspecified",
|
||||
LANDSCAPE: "landscape",
|
||||
PORTRAIT: "portrait",
|
||||
USER: "user",
|
||||
BEHIND: "behind",
|
||||
SENSOR: "sensor",
|
||||
NOSENSOR: "nosensor",
|
||||
SENSOR_LANDSCAPE: "sensorLandscape",
|
||||
SENSOR_PORTRAIT: "sensorPortrait",
|
||||
REVERSE_LANDSCAPE: "reverseLandscape",
|
||||
REVERSE_PORTRAIT: "reversePortrait",
|
||||
FULL_SENSOR: "fullSensor"
|
||||
PORTRAIT_PRIMARY: 'portrait-primary',
|
||||
// The orientation is in the primary portrait mode.
|
||||
PORTRAIT_SECONDARY: 'portrait-secondary',
|
||||
// The orientation is in the secondary portrait mode.
|
||||
LANDSCAPE_PRIMARY: 'landscape-primary',
|
||||
// The orientation is in the primary landscape mode.
|
||||
LANDSCAPE_SECONDARY: 'landscape-secondary',
|
||||
// The orientation is in the secondary landscape mode.
|
||||
PORAIT: 'portrait',
|
||||
// The orientation is either portrait-primary or portrait-secondary.
|
||||
LANDSCAPE: 'landscape',
|
||||
// The orientation is either landscape-primary or landscape-secondary.
|
||||
UNLOCKED: 'unlocked'
|
||||
}
|
||||
},
|
||||
currOrientation = Constants.Orientation.UNSPECIFIED;
|
||||
currOrientation = Constants.Orientation.UNLOCKED;
|
||||
|
||||
var orientationExports = {};
|
||||
|
||||
@@ -48,46 +50,70 @@ for (var key in Constants) {
|
||||
orientationExports[key] = Constants[key];
|
||||
}
|
||||
|
||||
orientationExports.setOrientation = function(successCallback, errorCallback, orientation) {
|
||||
if (typeof successCallback == "string") {
|
||||
orientation = successCallback;
|
||||
successCallback = function(){};
|
||||
errorCallback = function(){};
|
||||
}
|
||||
|
||||
function setOrientation(successCallback, errorCallback, orientation) {
|
||||
currOrientation = orientation ? orientation : Constants.Orientation.UNSPECIFIED;
|
||||
|
||||
exec(successCallback, errorCallback, "YoikScreenOrientation", "screenOrientation", ['set', currOrientation]);
|
||||
};
|
||||
}
|
||||
|
||||
function fireEvent(obj) {
|
||||
var event = document.createEvent('HTMLEvents');
|
||||
|
||||
event.initEvent('orientationchange', true, true);
|
||||
event.eventName = 'orientationchange';
|
||||
// screen.dispatchEvent(event);
|
||||
}
|
||||
|
||||
function addScreenOrientationApi(obj) {
|
||||
if (obj.unlockOrientation || obj.lockOrientation) {
|
||||
return;
|
||||
}
|
||||
|
||||
var successCallback = function() {
|
||||
fireEvent(obj);
|
||||
},
|
||||
errorCallback = function(){};
|
||||
|
||||
obj.lockOrientation = function(orientation) {
|
||||
// if (Object.keys(Constants.Orientation).indexOf(orientation) == -1) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
setOrientation(successCallback, errorCallback, orientation);
|
||||
};
|
||||
|
||||
obj.unlockOrientation = function() {
|
||||
setOrientation(successCallback, errorCallback, Constants.Orientation.UNLOCKED);
|
||||
};
|
||||
}
|
||||
|
||||
addScreenOrientationApi(screen);
|
||||
|
||||
// ios orientation callback/hook
|
||||
window.shouldRotateToOrientation = function(orientation) {
|
||||
var o = Constants.Orientation;
|
||||
switch (currOrientation) {
|
||||
case o.PORTRAIT:
|
||||
case o.SENSOR_PORTRAIT:
|
||||
case o.PORTRAIT_PRIMARY:
|
||||
if (orientation === 0) return true;
|
||||
break;
|
||||
case o.LANDSCAPE:
|
||||
case o.SENSOR_LANDSCAPE:
|
||||
case o.LANDSCAPE_PRIMARY:
|
||||
if (orientation === -90) return true;
|
||||
break;
|
||||
case o.REVERSE_LANDSCAPE:
|
||||
case o.LANDSCAPE_SECONDARY:
|
||||
case o.LANDSCAPE:
|
||||
if (orientation === 90) return true;
|
||||
break;
|
||||
case o.REVERSE_PORTRAIT:
|
||||
case o.PORTRAIT:
|
||||
case o.PORTRAIT_SECONDARY:
|
||||
if (orientation === 180) return true;
|
||||
break;
|
||||
case o.FULL_SENSOR:
|
||||
return true;
|
||||
break;
|
||||
case o.SENSOR:
|
||||
case o.UNSPECIFIED:
|
||||
case o.UNLOCKED:
|
||||
if (orientation === -90 || orientation === 90 || orientation === 0) return true;
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = orientationExports;
|
||||
Reference in New Issue
Block a user