The plugin now follows W 3 specs ; tests for screen.orientation.onchange pending

This commit is contained in:
maverickmishra
2017-03-08 17:45:24 -08:00
committed by Jesse MacFadyen
parent f8207c4164
commit bef03769de
5 changed files with 110 additions and 35 deletions
+18
View File
@@ -1,3 +1,21 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.cordova.screenorientationdemo" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>ScreenOrientationDemo</name>
+2
View File
@@ -51,6 +51,8 @@
<button id="btnLandPrimary">Landscape - Primary</button>
</div>
<button id="btnAny">Unlock</button>
<button id="btnOnchange">Onchange</button>
<button id="btnAddEvtListener">Add Event</button>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
+14 -1
View File
@@ -66,8 +66,13 @@ var app = {
btnAny.addEventListener("click", function() {
screen.orientation.unlock();
});
btnOnchange.addEventListener("click", function() {
screen.orientation.onchange = myFunction1();
});
btnAddEvtListener.addEventListener("click", function() {
screen.orientation.addEventListener('change', myFunction2(), true);
});
},
@@ -84,4 +89,12 @@ var app = {
}
};
function myFunction1() {
alert('This change listener uses screen.orientation.onchange');
}
function myFunction2() {
alert('This change listener uses screen.orientation.addEventListener');
}
app.initialize();
+9 -11
View File
@@ -17,10 +17,8 @@
* specific language governing permissions and limitations
* under the License.
*
*/
*/
/* jshint jasmine: true */
exports.defineAutoTests = function() {
describe('window.screen', function() {
@@ -51,14 +49,13 @@ exports.defineAutoTests = function() {
expect(typeof window.screen.orientation.type).toBe('string');
});
xit('should have an `angle` property (number)', function() {
it('should have an `angle` property (number)', function() {
expect(window.screen.orientation.angle).toBeDefined();
expect(typeof window.screen.orientation.angle).toBe('number');
});
xit('should have an `onchange` property (function)', function() {
expect(window.screen.orientation.onchange).toBeDefined();
expect(typeof window.screen.orientation.onchange).toBe('function');
});
});
@@ -68,7 +65,7 @@ exports.defineAutoTests = function() {
expect(window.OrientationType).toBeDefined();
});
xit("should have defined types", function(){
it("should have defined types", function() {
expect(window.OrientationType['portrait-primary']).toBeDefined();
expect(window.OrientationType['portrait-secondary']).toBeDefined();
expect(window.OrientationType['landscape-primary']).toBeDefined();
@@ -82,7 +79,7 @@ exports.defineAutoTests = function() {
expect(window.OrientationLockType).toBeDefined();
});
it("should have defined types", function(){
it("should have defined types", function() {
expect(window.OrientationLockType['portrait-primary']).toBeDefined();
expect(window.OrientationLockType['portrait-secondary']).toBeDefined();
expect(window.OrientationLockType['landscape-primary']).toBeDefined();
@@ -99,12 +96,12 @@ exports.defineAutoTests = function() {
// test onchange works
describe('window.screen.orientation', function() {
xit('should successfully lock and unlock screen orientation, lock should return a promise', function(done) {
it('should successfully lock and unlock screen orientation, lock should return a promise', function(done) {
try {
var promise = window.screen.orientation.lock('landscape');
expect(promise).toBeDefined();
expect(typeof promise).toBe('promise');
expect(typeof promise.then).toBe('function');
promise.then(function() {
expect(window.screen.orientation.unlock).not.toThrow();
done();
@@ -117,6 +114,7 @@ exports.defineAutoTests = function() {
fail(err);
done();
}
});
});
};
};
+67 -23
View File
@@ -18,15 +18,13 @@
* under the License.
*
*/
var screenOrientation = {};
if (!window.OrientationType) {
window.OrientationType = {
'0': 'portrait-primary',
'180': 'portrait-secondary',
'90': 'landscape-primary',
'-90': 'landscape-secondary'
'portrait-primary': 0,
'portrait-secondary': 180,
'landscape-primary': 90,
'landscape-secondary': -90
};
}
if (!window.OrientationLockType) {
@@ -35,9 +33,9 @@ if (!window.OrientationLockType) {
'portrait-secondary': 2,
'landscape-primary': 4,
'landscape-secondary': 8,
'portrait': (1 & 2), // either portrait-primary or portrait-secondary.
'landscape': ( 4 & 8 ), // either landscape-primary or landscape-secondary.
'any': ( 1 & 2 & 4 & 8 ) // All orientations are supported (unlocked orientation)
'portrait': 3, // either portrait-primary or portrait-secondary.
'landscape': 12, // either landscape-primary or landscape-secondary.
'any': 15 // All orientations are supported (unlocked orientation)
};
}
var orientationMask = 1;
@@ -46,7 +44,14 @@ screenOrientation.setOrientation = function(orientation) {
cordova.exec(null, null, "CDVOrientation", "screenOrientation", [orientationMask, orientation]);
};
if (!screen.orientation) {
screen.orientation = {};
}
setOrientationProperties();
function addScreenOrientationApi(screenObject) {
if (screenObject.unlock || screenObject.lock) {
screenObject.nativeLock = screenObject.lock;
}
@@ -57,11 +62,11 @@ function addScreenOrientationApi(screenObject) {
if (screenObject.nativeLock != null) {
promiseLock = screenObject.nativeLock(orientation);
promiseLock.then(function success(res) {
resolve();
}, function error(err) {
screenObject.nativeLock = null;
resolveOrientation(orientation, resolve, reject);
});
resolve();
}, function error(err) {
screenObject.nativeLock = null;
resolveOrientation(orientation, resolve, reject);
});
} else {
resolveOrientation(orientation, resolve, reject);
}
@@ -74,27 +79,66 @@ function addScreenOrientationApi(screenObject) {
};
}
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
}
}
if (!screen.orientation) {
screen.orientation = {};
}
addScreenOrientationApi(screen.orientation);
orientationChange();
function orientationChange() {
screen.orientation.type = window.OrientationType[window.orientation];
var onChangeListener = null;
Object.defineProperty(screen.orientation, 'onchange', {
set: function(listener) {
if (onChangeListener != null) {
screen.orienation.removeEventListener('change', onChangeListener);
}
onChangeListener = listener;
if (onChangeListener != null) {
screen.orientation.addEventListener('change', onChangeListener);
}
},
enumerable: true,
});
var orientationchange = function() {
setOrientationProperties();
var event = document.createEvent('Events');
event.initEvent("change", false, false);
document.dispatchEvent(event);
};
function setOrientationProperties() {
switch (window.orientation) {
case 0:
screen.orientation.type = 'portrait-primary';
break;
case 90:
screen.orientation.type = 'landscape-primary';
break;
case 180:
screen.orientation.type = 'portrait-secondary';
break;
case -90:
screen.orientation.type = 'landscape-secondary';
break;
}
screen.orientation.angle = window.orientation;
}
window.addEventListener("orientationchange", orientationChange, true);
module.exports = screenOrientation;
window.addEventListener("orientationchange", orientationchange, true);
screen.orientation.addEventListener = document.addEventListener;
module.exports = screenOrientation;