getter for onchange, add/remove event listeners, this closes #12

This commit is contained in:
Jesse MacFadyen 2017-03-09 17:57:00 -08:00
parent 90128f42d7
commit 9d6f2d271d
2 changed files with 39 additions and 9 deletions

View File

@ -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');
});
});

View File

@ -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;