mirror of
https://github.com/apache/cordova-plugin-screen-orientation.git
synced 2025-02-23 08:32:51 +08:00
getter for onchange, add/remove event listeners, this closes #12
This commit is contained in:
parent
90128f42d7
commit
9d6f2d271d
@ -54,8 +54,29 @@ exports.defineAutoTests = function() {
|
||||
expect(typeof window.screen.orientation.angle).toBe('number');
|
||||
});
|
||||
|
||||
xit('should have an `onchange` property (function)', function() {
|
||||
it('should have an `onchange` settable function', function() {
|
||||
// it should be null to start
|
||||
expect(window.screen.orientation.onchange).toBe(null);
|
||||
// then we set it
|
||||
var funk = function(){};
|
||||
window.screen.orientation.onchange = funk;
|
||||
// now it should exist
|
||||
expect(window.screen.orientation.onchange).toBeDefined();
|
||||
expect(window.screen.orientation.onchange).toBe(funk);
|
||||
// clear it
|
||||
window.screen.orientation.onchange = null;
|
||||
// it should be null again
|
||||
expect(window.screen.orientation.onchange).toBe(null);
|
||||
});
|
||||
|
||||
it('should have an eventListener interface',function() {
|
||||
expect(window.screen.orientation.addEventListener).toBeDefined();
|
||||
expect(typeof window.screen.orientation.addEventListener).toBe('function');
|
||||
|
||||
expect(window.screen.orientation.removeEventListener).toBeDefined();
|
||||
expect(typeof window.screen.orientation.removeEventListener).toBe('function');
|
||||
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -95,32 +95,41 @@ function resolveOrientation(orientation, resolve, reject) {
|
||||
addScreenOrientationApi(screen.orientation);
|
||||
|
||||
var onChangeListener = null;
|
||||
|
||||
Object.defineProperty(screen.orientation, 'onchange', {
|
||||
|
||||
set: function(listener) {
|
||||
console.log("setting onchange to : " + listener);
|
||||
|
||||
if (onChangeListener != null) {
|
||||
screen.orienation.removeEventListener('change', onChangeListener);
|
||||
screen.orientation.removeEventListener('change', onChangeListener);
|
||||
}
|
||||
|
||||
onChangeListener = listener;
|
||||
|
||||
if (onChangeListener != null) {
|
||||
screen.orientation.addEventListener('change', onChangeListener);
|
||||
}
|
||||
},
|
||||
get: function() {
|
||||
return (onChangeListener ? onChangeListener : null);
|
||||
},
|
||||
enumerable: true,
|
||||
});
|
||||
|
||||
|
||||
|
||||
var evtTarget = new XMLHttpRequest(); //document.createElement('div');
|
||||
var orientationchange = function() {
|
||||
setOrientationProperties();
|
||||
var event = document.createEvent('Events');
|
||||
event.initEvent("change", false, false);
|
||||
document.dispatchEvent(event);
|
||||
|
||||
evtTarget.dispatchEvent(event);
|
||||
};
|
||||
|
||||
screen.orientation.addEventListener = function(a,b,c) {
|
||||
return evtTarget.addEventListener(a,b,c);
|
||||
}
|
||||
screen.orientation.removeEventListener = function(a,b,c) {
|
||||
return evtTarget.removeEventListener(a,b,c);
|
||||
}
|
||||
|
||||
function setOrientationProperties() {
|
||||
switch (window.orientation) {
|
||||
case 0:
|
||||
@ -140,5 +149,5 @@ function setOrientationProperties() {
|
||||
|
||||
}
|
||||
window.addEventListener("orientationchange", orientationchange, true);
|
||||
screen.orientation.addEventListener = document.addEventListener;
|
||||
|
||||
module.exports = screenOrientation;
|
||||
|
Loading…
Reference in New Issue
Block a user