mirror of
https://github.com/apache/cordova-plugin-screen-orientation.git
synced 2025-02-23 16:42:50 +08:00
working version of screen orientation plugin adhering to w3c specs
This commit is contained in:
parent
912b6b199b
commit
1f536ec80e
@ -50,7 +50,7 @@
|
||||
<button id="btnPortPrimary">Portrait - Primary</button>
|
||||
<button id="btnLandPrimary">Landscape - Primary</button>
|
||||
</div>
|
||||
<button id="btnAny">Any</button>
|
||||
<button id="btnAny">Unlock</button>
|
||||
</div>
|
||||
<script type="text/javascript" src="cordova.js"></script>
|
||||
<script type="text/javascript" src="js/index.js"></script>
|
||||
|
@ -36,44 +36,35 @@ var app = {
|
||||
onDeviceReady: function() {
|
||||
app.receivedEvent('deviceready');
|
||||
btnPortrait.addEventListener("click", function() {
|
||||
// alert('Orientation is ' + screen.orientation);
|
||||
screen.lockOrientation('portrait').then(function(obj) {
|
||||
screen.orientation.lock('portrait').then(function(obj) {
|
||||
console.log(obj);
|
||||
}).catch(function(obj) {
|
||||
}, function(obj) {
|
||||
console.log(obj);
|
||||
});
|
||||
});
|
||||
btnLandscape.addEventListener("click", function() {
|
||||
// alert('Orientation is ' + screen.orientation);
|
||||
screen.lockOrientation('landscape').then(function(obj) {
|
||||
screen.orientation.lock('landscape').then(function(obj) {
|
||||
console.log(obj);
|
||||
}).catch(function(obj) {
|
||||
}, function(obj) {
|
||||
console.log(obj);
|
||||
});
|
||||
});
|
||||
btnPortPrimary.addEventListener("click", function() {
|
||||
// alert('Orientation is ' + screen.orientation);
|
||||
screen.lockOrientation('portrait-primary').then(function(obj) {
|
||||
screen.orientation.lock('portrait-primary').then(function(obj) {
|
||||
console.log(obj);
|
||||
}).catch(function(obj) {
|
||||
}, function(obj) {
|
||||
console.log(obj);
|
||||
});
|
||||
});
|
||||
btnLandPrimary.addEventListener("click", function() {
|
||||
// alert('Orientation is ' + screen.orientation);
|
||||
screen.lockOrientation('landscape-primary').then(function(obj) {
|
||||
screen.orientation.lock('landscape-primary').then(function(obj) {
|
||||
console.log(obj);
|
||||
}).catch(function(obj) {
|
||||
}, function(obj) {
|
||||
console.log(obj);
|
||||
});
|
||||
});
|
||||
btnAny.addEventListener("click", function() {
|
||||
// alert('Orientation is ' + screen.orientation);
|
||||
screen.lockOrientation('any').then(function(obj) {
|
||||
console.log(obj);
|
||||
}).catch(function(obj) {
|
||||
console.log(obj);
|
||||
});
|
||||
screen.orientation.unlock();
|
||||
});
|
||||
|
||||
|
||||
|
@ -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();
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
};
|
||||
|
@ -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;
|
||||
screenOrientation.currOrientation = 'any';
|
||||
var orientationMask = 0;
|
||||
screenOrientation.setOrientation = function(orientation) {
|
||||
if(orientation == Orientations[0]){
|
||||
orientationMask = 1;
|
||||
var screenOrientation = {};
|
||||
if (!window.OrientationType) {
|
||||
window.OrientationType = {
|
||||
'0': 'portrait-primary',
|
||||
'180': 'portrait-secondary',
|
||||
'90': 'landscape-primary',
|
||||
'-90': 'landscape-secondary'
|
||||
};
|
||||
}
|
||||
else if(orientation == Orientations[1]){
|
||||
orientationMask = 2;
|
||||
if (!window.OrientationLockType) {
|
||||
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]){
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
var orientationMask = 1;
|
||||
screenOrientation.setOrientation = function(orientation) {
|
||||
orientationMask = window.OrientationLockType[orientation];
|
||||
cordova.exec(null, null, "CDVOrientation", "screenOrientation", [orientationMask, orientation]);
|
||||
//console.log('setOrientation not supported on device');
|
||||
};
|
||||
};
|
||||
|
||||
function addScreenOrientationApi(screenObject) {
|
||||
if (screenObject.unlockOrientation || screenObject.lockOrientation) {
|
||||
return;
|
||||
function addScreenOrientationApi(screenObject) {
|
||||
if (screenObject.unlock || screenObject.lock) {
|
||||
screenObject.nativeLock = screenObject.lock;
|
||||
}
|
||||
|
||||
screenObject.lockOrientation = function(orientation) {
|
||||
screenObject.lock = function(orientation) {
|
||||
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;
|
||||
}
|
||||
|
||||
var p = new Promise(function(resolve,reject){
|
||||
if (Orientations.indexOf(orientation) == -1) {
|
||||
screenObject.unlock = function() {
|
||||
screenOrientation.setOrientation('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;
|
||||
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() {
|
||||
screenOrientation.currOrientation = screenObject.orientation = 'any';
|
||||
screenOrientation.setOrientation('any');
|
||||
};
|
||||
}
|
||||
|
||||
addScreenOrientationApi(screen);
|
||||
orientationChange();
|
||||
|
||||
function orientationChange() {
|
||||
var orientation;
|
||||
}
|
||||
if (!screen.orientation) {
|
||||
screen.orientation = {};
|
||||
}
|
||||
addScreenOrientationApi(screen.orientation);
|
||||
orientationChange();
|
||||
|
||||
function orientationChange() {
|
||||
switch (window.orientation) {
|
||||
case 0:
|
||||
orientation = 'portrait-primary';
|
||||
screen.orientation.type = window.OrientationType['0']; //append angle here ?
|
||||
break;
|
||||
case 90:
|
||||
orientation = 'landscape-primary';
|
||||
screen.orientation.type = window.OrientationType['90'];
|
||||
break;
|
||||
case 180:
|
||||
orientation = 'portrait-secondary';
|
||||
screen.orientation.type = window.OrientationType['180'];
|
||||
break;
|
||||
case -90:
|
||||
orientation = 'landscape-secondary';
|
||||
screen.orientation.type = window.OrientationType['-90'];
|
||||
break;
|
||||
default:
|
||||
orientation = 'any';
|
||||
screen.orientation.type = window.OrientationType['0'];
|
||||
}
|
||||
|
||||
screen.orientation = orientation;
|
||||
}
|
||||
|
||||
window.addEventListener("orientationchange", orientationChange, true);
|
||||
|
||||
module.exports = screenOrientation;
|
||||
}
|
||||
window.addEventListener("orientationchange", orientationChange, true);
|
||||
module.exports = screenOrientation;
|
Loading…
Reference in New Issue
Block a user