Updated to attach functions directly to screen object and constant cleanup

This commit is contained in:
Grant Benvenuti 2014-07-13 09:26:44 +10:00
parent 2934bea321
commit 1e4a7b18c4

View File

@ -23,45 +23,26 @@ SOFTWARE.
*/ */
var argscheck = require('cordova/argscheck'), var argscheck = require('cordova/argscheck'),
exec = require('cordova/exec'), exec = require('cordova/exec'),
Orientations = [
Constants = { 'portrait-primary',
Orientation: { // The orientation is in the primary portrait mode.
PORTRAIT_PRIMARY: 'portrait-primary', 'portrait-secondary',
// The orientation is in the primary portrait mode. // The orientation is in the secondary portrait mode.
PORTRAIT_SECONDARY: 'portrait-secondary', 'landscape-primary',
// The orientation is in the secondary portrait mode. // The orientation is in the primary landscape mode.
LANDSCAPE_PRIMARY: 'landscape-primary', 'landscape-secondary',
// The orientation is in the primary landscape mode. // The orientation is in the secondary landscape mode.
LANDSCAPE_SECONDARY: 'landscape-secondary', 'portrait',
// The orientation is in the secondary landscape mode. // The orientation is either portrait-primary or portrait-secondary.
PORAIT: 'portrait', 'landscape'
// The orientation is either portrait-primary or portrait-secondary. ],
LANDSCAPE: 'landscape', currOrientation = 'unlocked';
// The orientation is either landscape-primary or landscape-secondary.
UNLOCKED: 'unlocked'
}
},
currOrientation = Constants.Orientation.UNLOCKED;
var orientationExports = {}; var orientationExports = {};
// Tack on the orientation Constants to the base plugin. function setOrientation(orientation) {
for (var key in Constants) { currOrientation = orientation ? orientation : 'unlocked';
orientationExports[key] = Constants[key]; exec(null, null, "YoikScreenOrientation", "screenOrientation", ['set', currOrientation]);
}
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) { function addScreenOrientationApi(obj) {
@ -69,21 +50,16 @@ function addScreenOrientationApi(obj) {
return; return;
} }
var successCallback = function() {
fireEvent(obj);
},
errorCallback = function(){};
obj.lockOrientation = function(orientation) { obj.lockOrientation = function(orientation) {
// if (Object.keys(Constants.Orientation).indexOf(orientation) == -1) { if (Orientations.indexOf(orientation) == -1) {
// return; console.log('INVALID ORIENTATION', orientation);
// } return;
}
setOrientation(successCallback, errorCallback, orientation); setOrientation(orientation);
}; };
obj.unlockOrientation = function() { obj.unlockOrientation = function() {
setOrientation(successCallback, errorCallback, Constants.Orientation.UNLOCKED); setOrientation('unlocked');
}; };
} }
@ -91,29 +67,28 @@ addScreenOrientationApi(screen);
// ios orientation callback/hook // ios orientation callback/hook
window.shouldRotateToOrientation = function(orientation) { window.shouldRotateToOrientation = function(orientation) {
var o = Constants.Orientation;
switch (currOrientation) { switch (currOrientation) {
case o.PORTRAIT: case 'portrait':
case o.PORTRAIT_PRIMARY: case 'portrait-primary':
if (orientation === 0) return true; if (orientation === 0) return true;
break; break;
case o.LANDSCAPE: case 'landscape':
case o.LANDSCAPE_PRIMARY: case 'landscape-primary':
if (orientation === -90) return true; if (orientation === -90) return true;
break; break;
case o.LANDSCAPE_SECONDARY: case 'landscape':
case o.LANDSCAPE: case 'landscape-secondary':
if (orientation === 90) return true; if (orientation === 90) return true;
break; break;
case o.PORTRAIT: case 'portrait':
case o.PORTRAIT_SECONDARY: case 'portrait-secondary':
if (orientation === 180) return true; if (orientation === 180) return true;
break; break;
case o.UNLOCKED: default:
if (orientation === -90 || orientation === 90 || orientation === 0) return true; if (orientation === -90 || orientation === 90 || orientation === 0) return true;
break; break;
} }
return false; return false;
}; };
module.exports = orientationExports; module.exports = {};