mirror of
https://github.com/apache/cordova-plugin-screen-orientation.git
synced 2025-02-23 00:22:51 +08:00
fix(android, ios): use clobbers to overwrite screen.orientation (#116)
This commit is contained in:
parent
cc71a9dd7e
commit
601400627e
@ -29,6 +29,7 @@
|
|||||||
|
|
||||||
<js-module src="www/screenorientation.js" name="screenorientation">
|
<js-module src="www/screenorientation.js" name="screenorientation">
|
||||||
<clobbers target="cordova.plugins.screenorientation" />
|
<clobbers target="cordova.plugins.screenorientation" />
|
||||||
|
<clobbers target="screen.orientation" />
|
||||||
</js-module>
|
</js-module>
|
||||||
|
|
||||||
<platform name="ios">
|
<platform name="ios">
|
||||||
|
@ -47,42 +47,19 @@ screenOrientation.setOrientation = function (orientation) {
|
|||||||
cordova.exec(null, null, 'CDVOrientation', 'screenOrientation', [orientationMask, orientation]);
|
cordova.exec(null, null, 'CDVOrientation', 'screenOrientation', [orientationMask, orientation]);
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!screen.orientation) {
|
screenOrientation.lock = function (orientation) {
|
||||||
screen.orientation = {};
|
var p = new Promise(function (resolve, reject) {
|
||||||
}
|
resolveOrientation(orientation, resolve, reject);
|
||||||
|
});
|
||||||
|
return p;
|
||||||
|
};
|
||||||
|
|
||||||
|
screenOrientation.unlock = function () {
|
||||||
|
screenOrientation.setOrientation('any');
|
||||||
|
};
|
||||||
|
|
||||||
setOrientationProperties();
|
setOrientationProperties();
|
||||||
|
|
||||||
function addScreenOrientationApi (screenObject) {
|
|
||||||
if (screenObject.unlock || screenObject.lock) {
|
|
||||||
screenObject.nativeLock = screenObject.lock;
|
|
||||||
}
|
|
||||||
|
|
||||||
screenObject.lock = function (orientation) {
|
|
||||||
var promiseLock;
|
|
||||||
var p = new Promise(function (resolve, reject) {
|
|
||||||
if (screenObject.nativeLock) {
|
|
||||||
promiseLock = screenObject.nativeLock(orientation);
|
|
||||||
promiseLock.then(
|
|
||||||
function success (_) {
|
|
||||||
resolve();
|
|
||||||
},
|
|
||||||
function error (_) {
|
|
||||||
screenObject.nativeLock = null;
|
|
||||||
resolveOrientation(orientation, resolve, reject);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
resolveOrientation(orientation, resolve, reject);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return p;
|
|
||||||
};
|
|
||||||
screenObject.unlock = function () {
|
|
||||||
screenOrientation.setOrientation('any');
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolveOrientation (orientation, resolve, reject) {
|
function resolveOrientation (orientation, resolve, reject) {
|
||||||
if (!Object.prototype.hasOwnProperty.call(OrientationLockType, orientation)) {
|
if (!Object.prototype.hasOwnProperty.call(OrientationLockType, orientation)) {
|
||||||
var err = new Error();
|
var err = new Error();
|
||||||
@ -94,18 +71,16 @@ function resolveOrientation (orientation, resolve, reject) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
addScreenOrientationApi(screen.orientation);
|
|
||||||
|
|
||||||
var onChangeListener = null;
|
var onChangeListener = null;
|
||||||
|
|
||||||
Object.defineProperty(screen.orientation, 'onchange', {
|
Object.defineProperty(screenOrientation, 'onchange', {
|
||||||
set: function (listener) {
|
set: function (listener) {
|
||||||
if (onChangeListener) {
|
if (onChangeListener) {
|
||||||
screen.orientation.removeEventListener('change', onChangeListener);
|
screenOrientation.removeEventListener('change', onChangeListener);
|
||||||
}
|
}
|
||||||
onChangeListener = listener;
|
onChangeListener = listener;
|
||||||
if (onChangeListener) {
|
if (onChangeListener) {
|
||||||
screen.orientation.addEventListener('change', onChangeListener);
|
screenOrientation.addEventListener('change', onChangeListener);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
get: function () {
|
get: function () {
|
||||||
@ -122,33 +97,33 @@ var orientationchange = function () {
|
|||||||
evtTarget.dispatchEvent(event);
|
evtTarget.dispatchEvent(event);
|
||||||
};
|
};
|
||||||
|
|
||||||
screen.orientation.addEventListener = function (a, b, c) {
|
screenOrientation.addEventListener = function (a, b, c) {
|
||||||
return evtTarget.addEventListener(a, b, c);
|
return evtTarget.addEventListener(a, b, c);
|
||||||
};
|
};
|
||||||
|
|
||||||
screen.orientation.removeEventListener = function (a, b, c) {
|
screenOrientation.removeEventListener = function (a, b, c) {
|
||||||
return evtTarget.removeEventListener(a, b, c);
|
return evtTarget.removeEventListener(a, b, c);
|
||||||
};
|
};
|
||||||
|
|
||||||
function setOrientationProperties () {
|
function setOrientationProperties () {
|
||||||
switch (window.orientation) {
|
switch (window.orientation) {
|
||||||
case 0:
|
case 0:
|
||||||
screen.orientation.type = 'portrait-primary';
|
screenOrientation.type = 'portrait-primary';
|
||||||
break;
|
break;
|
||||||
case 90:
|
case 90:
|
||||||
screen.orientation.type = 'landscape-primary';
|
screenOrientation.type = 'landscape-primary';
|
||||||
break;
|
break;
|
||||||
case 180:
|
case 180:
|
||||||
screen.orientation.type = 'portrait-secondary';
|
screenOrientation.type = 'portrait-secondary';
|
||||||
break;
|
break;
|
||||||
case -90:
|
case -90:
|
||||||
screen.orientation.type = 'landscape-secondary';
|
screenOrientation.type = 'landscape-secondary';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
screen.orientation.type = 'portrait-primary';
|
screenOrientation.type = 'portrait-primary';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
screen.orientation.angle = window.orientation || 0;
|
screenOrientation.angle = window.orientation || 0;
|
||||||
}
|
}
|
||||||
window.addEventListener('orientationchange', orientationchange, true);
|
window.addEventListener('orientationchange', orientationchange, true);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user