working version of screen orientation plugin adhering to w3c specs

This commit is contained in:
maverickmishra 2017-01-09 16:42:53 -08:00
parent 912b6b199b
commit 1f536ec80e
4 changed files with 127 additions and 107 deletions

View File

@ -50,7 +50,7 @@
<button id="btnPortPrimary">Portrait - Primary</button> <button id="btnPortPrimary">Portrait - Primary</button>
<button id="btnLandPrimary">Landscape - Primary</button> <button id="btnLandPrimary">Landscape - Primary</button>
</div> </div>
<button id="btnAny">Any</button> <button id="btnAny">Unlock</button>
</div> </div>
<script type="text/javascript" src="cordova.js"></script> <script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script> <script type="text/javascript" src="js/index.js"></script>

View File

@ -36,44 +36,35 @@ var app = {
onDeviceReady: function() { onDeviceReady: function() {
app.receivedEvent('deviceready'); app.receivedEvent('deviceready');
btnPortrait.addEventListener("click", function() { btnPortrait.addEventListener("click", function() {
// alert('Orientation is ' + screen.orientation); screen.orientation.lock('portrait').then(function(obj) {
screen.lockOrientation('portrait').then(function(obj) {
console.log(obj); console.log(obj);
}).catch(function(obj) { }, function(obj) {
console.log(obj); console.log(obj);
}); });
}); });
btnLandscape.addEventListener("click", function() { btnLandscape.addEventListener("click", function() {
// alert('Orientation is ' + screen.orientation); screen.orientation.lock('landscape').then(function(obj) {
screen.lockOrientation('landscape').then(function(obj) {
console.log(obj); console.log(obj);
}).catch(function(obj) { }, function(obj) {
console.log(obj); console.log(obj);
}); });
}); });
btnPortPrimary.addEventListener("click", function() { btnPortPrimary.addEventListener("click", function() {
// alert('Orientation is ' + screen.orientation); screen.orientation.lock('portrait-primary').then(function(obj) {
screen.lockOrientation('portrait-primary').then(function(obj) {
console.log(obj); console.log(obj);
}).catch(function(obj) { }, function(obj) {
console.log(obj); console.log(obj);
}); });
}); });
btnLandPrimary.addEventListener("click", function() { btnLandPrimary.addEventListener("click", function() {
// alert('Orientation is ' + screen.orientation); screen.orientation.lock('landscape-primary').then(function(obj) {
screen.lockOrientation('landscape-primary').then(function(obj) {
console.log(obj); console.log(obj);
}).catch(function(obj) { }, function(obj) {
console.log(obj); console.log(obj);
}); });
}); });
btnAny.addEventListener("click", function() { btnAny.addEventListener("click", function() {
// alert('Orientation is ' + screen.orientation); screen.orientation.unlock();
screen.lockOrientation('any').then(function(obj) {
console.log(obj);
}).catch(function(obj) {
console.log(obj);
});
}); });

View File

@ -56,4 +56,38 @@ exports.defineAutoTests = function() {
} }
}); });
}); });
describe('OrientationLockType should have one of seven values', function () {
it("should have one of seven orientation lock types", function(){
expect(window.OrientationLockType['portrait-primary']).toBe(1);
expect(window.OrientationLockType['portrait-secondary']).toBe(2);
expect(window.OrientationLockType['landscape-primary']).toBe(4);
expect(window.OrientationLockType['landscape-secondary']).toBe(8);
expect(window.OrientationLockType['portrait']).toBe(3);
expect(window.OrientationLockType['landscape']).toBe(12);
expect(window.OrientationLockType['any']).toBe(15);
alert(window.screen.orientation.angle);
});
});
describe('Screen object should exist', function () {
it("should exist", function() {
expect(window.screen).toBeDefined();
});
it(" screen should contain an attribute called ScreenOrientation", function() {
expect(window.screen.orientation).toBeDefined();
});
it(" Screenorientation object should contain methods called lock and unlock", function() {
expect(window.screen.orientation.lock).toEqual(jasmine.any(Function));
expect(window.screen.orientation.unlock).toEqual(jasmine.any(Function));
});
it(" Screenorientation object should contain properties called type and angle", function() {
expect(window.screen.orientation.type).toBeDefined();
expect(window.screen.orientation.angle).toBeDefined();
});
});
}; };

View File

@ -19,102 +19,97 @@
* *
*/ */
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)
];
screenOrientation.Orientations = Orientations; var screenOrientation = {};
screenOrientation.currOrientation = 'any'; if (!window.OrientationType) {
var orientationMask = 0; window.OrientationType = {
screenOrientation.setOrientation = function(orientation) { '0': 'portrait-primary',
if(orientation == Orientations[0]){ '180': 'portrait-secondary',
orientationMask = 1; '90': 'landscape-primary',
'-90': 'landscape-secondary'
};
} }
else if(orientation == Orientations[1]){ if (!window.OrientationLockType) {
orientationMask = 2; window.OrientationLockType = {
'portrait-primary': 1,
'portrait-secondary': 2,
'landscape-primary': 4,
'landscape-secondary': 8,
'portrait': 3, // either portrait-primary or portrait-secondary.
'landscape': 12, // either landscape-primary or landscape-secondary.
'any': 15 // All orientations are supported (unlocked orientation)
};
} }
else if(orientation == Orientations[2]){ var orientationMask = 1;
orientationMask = 4; screenOrientation.setOrientation = function(orientation) {
} orientationMask = window.OrientationLockType[orientation];
else if(orientation == Orientations[3]){ cordova.exec(null, null, "CDVOrientation", "screenOrientation", [orientationMask, orientation]);
orientationMask = 8;
}
else if(orientation == Orientations[4]){
orientationMask = 3;
}
else if(orientation == Orientations[5]){
orientationMask = 12;
}
else if(orientation == Orientations[6]){
orientationMask = 15;
}
cordova.exec(null, null, "CDVOrientation", "screenOrientation", [orientationMask, orientation]);
//console.log('setOrientation not supported on device');
};
function addScreenOrientationApi(screenObject) {
if (screenObject.unlockOrientation || screenObject.lockOrientation) {
return;
}
screenObject.lockOrientation = function(orientation) {
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;
}; };
screenObject.unlockOrientation = function() { function addScreenOrientationApi(screenObject) {
screenOrientation.currOrientation = screenObject.orientation = 'any'; if (screenObject.unlock || screenObject.lock) {
screenOrientation.setOrientation('any'); screenObject.nativeLock = screenObject.lock;
}; }
}
addScreenOrientationApi(screen); screenObject.lock = function(orientation) {
orientationChange(); var promiseLock;
var p = new Promise(function(resolve, reject) {
if (screenObject.nativeLock != null) {
promiseLock = screenObject.nativeLock(orientation);
flag = true;
} else {
resolveOrientation(orientation, resolve, reject);
}
promiseLock.then(function success(res) {
resolve();
}, function error(err) {
screenObject.nativeLock = null;
resolveOrientation(orientation, resolve, reject);
});
})
return p;
}
function orientationChange() { screenObject.unlock = function() {
var orientation; screenOrientation.setOrientation('any');
};
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';
} }
function resolveOrientation(orientation, resolve, reject) {
if (!OrientationLockType.hasOwnProperty(orientation)) {
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
}
screen.orientation = orientation; }
} if (!screen.orientation) {
screen.orientation = {};
}
addScreenOrientationApi(screen.orientation);
orientationChange();
window.addEventListener("orientationchange", orientationChange, true); function orientationChange() {
switch (window.orientation) {
module.exports = screenOrientation; case 0:
screen.orientation.type = window.OrientationType['0']; //append angle here ?
break;
case 90:
screen.orientation.type = window.OrientationType['90'];
break;
case 180:
screen.orientation.type = window.OrientationType['180'];
break;
case -90:
screen.orientation.type = window.OrientationType['-90'];
break;
default:
screen.orientation.type = window.OrientationType['0'];
}
}
window.addEventListener("orientationchange", orientationChange, true);
module.exports = screenOrientation;