mirror of
https://github.com/apache/cordova-plugin-screen-orientation.git
synced 2025-05-05 01:32:59 +08:00
Added bb10 support and orientation property to screen object
This commit is contained in:
parent
f3e89b7a07
commit
bb8b2c30c2
44
README.md
44
README.md
@ -1,7 +1,17 @@
|
|||||||
#cordova-yoik-screenorientation
|
#cordova-yoik-screenorientation
|
||||||
|
|
||||||
Cordova plugin to set/lock the screen orientation in a common way for both iOS and Android. From version 1.0.0 the
|
Cordova plugin to set/lock the screen orientation in a common way for iOS, Android and Blackberry 10. From version 1.0.0 the interface is based on the [Screen Orientation API](http://www.w3.org/TR/screen-orientation/).
|
||||||
interface is based on the [Screen Orientation API](http://www.w3.org/TR/screen-orientation/).
|
|
||||||
|
The plugin adds the following to the screen object:
|
||||||
|
|
||||||
|
__lockOrientation(ORIENTATION_STRING)__
|
||||||
|
lock the device orientation
|
||||||
|
|
||||||
|
__unlockOrientation()__
|
||||||
|
unlock the orientation
|
||||||
|
|
||||||
|
__orientation__
|
||||||
|
current orientation (ORIENTATION_STRING)
|
||||||
|
|
||||||
##Install
|
##Install
|
||||||
|
|
||||||
@ -11,7 +21,7 @@ cordova plugin add net.yoik.cordova.plugins.screenorientation
|
|||||||
https://github.com/yoik/cordova-yoik-screenorientation
|
https://github.com/yoik/cordova-yoik-screenorientation
|
||||||
|
|
||||||
|
|
||||||
##Orientations
|
##Supported Orientations
|
||||||
|
|
||||||
__portrait-primary__
|
__portrait-primary__
|
||||||
The orientation is in the primary portrait mode.
|
The orientation is in the primary portrait mode.
|
||||||
@ -33,31 +43,29 @@ The orientation is either landscape-primary or landscape-secondary (sensor).
|
|||||||
|
|
||||||
##Usage
|
##Usage
|
||||||
|
|
||||||
|
// set to either landscape
|
||||||
screen.lockOrientation('landscape');
|
screen.lockOrientation('landscape');
|
||||||
|
|
||||||
|
// allow user rotate
|
||||||
screen.unlockOrientation();
|
screen.unlockOrientation();
|
||||||
|
|
||||||
|
// access current orientation
|
||||||
|
console.log('Orientation is ' + screen.orientation);
|
||||||
|
|
||||||
##Events
|
##Events
|
||||||
|
|
||||||
Both android and iOS will fire the orientationchange event on the window object.
|
Both android and iOS will fire the orientationchange event on the window object.
|
||||||
For this version of the plugin use the window object if you require notification.
|
For this version of the plugin use the window object if you require notification.
|
||||||
|
|
||||||
i.e.
|
|
||||||
|
|
||||||
function init() {
|
For this plugin to follow the full API events should be fired on the screen object.
|
||||||
window.addEventListener("orientationchange", orientationChange, true);
|
iOS and BB10 do not currently support events on the _screen_ object so custom event
|
||||||
}
|
|
||||||
|
|
||||||
function orientationChange(e) {
|
|
||||||
var orientation="portrait";
|
|
||||||
if(window.orientation == -90 || window.orientation == 90) orientation = "landscape";
|
|
||||||
document.getElementById("status").innerHTML+=orientation+"<br>";
|
|
||||||
}
|
|
||||||
|
|
||||||
For this plugin to follow the API events should be fired on the screen object.
|
|
||||||
iOS does not currently support events on the _screen_ object so custom event
|
|
||||||
handling will need to be added (Suggestions welcome!).
|
handling will need to be added (Suggestions welcome!).
|
||||||
|
|
||||||
|
##Android Notes
|
||||||
|
|
||||||
|
The __screen.orientation__ property will not update when the phone is [rotated 180 degrees](http://www.quirksmode.org/dom/events/orientationchange.html).
|
||||||
|
|
||||||
##iOS Notes
|
##iOS Notes
|
||||||
|
|
||||||
The iOS version is a combination of the cordova JS callback _window.shouldRotateToOrientation_ and the workaround to recheck the orientation as implemented in https://github.com/Adlotto/cordova-plugin-recheck-screen-orientation.
|
The iOS version is a combination of the cordova JS callback _window.shouldRotateToOrientation_ and the workaround to recheck the orientation as implemented in https://github.com/Adlotto/cordova-plugin-recheck-screen-orientation.
|
||||||
@ -73,4 +81,8 @@ Issue [#1](https://github.com/yoik/cordova-yoik-screenorientation/issues/1) @dok
|
|||||||
>It seems to be related to having width=device-width, height=device-height in the meta viewport (which is part of the boilerplate phonegap/cordova app). It can be solved by updating the viewport with width=device-height, height=device-width or simply removing width and height altogether.
|
>It seems to be related to having width=device-width, height=device-height in the meta viewport (which is part of the boilerplate phonegap/cordova app). It can be solved by updating the viewport with width=device-height, height=device-width or simply removing width and height altogether.
|
||||||
|
|
||||||
|
|
||||||
|
##BB10 Notes
|
||||||
|
|
||||||
|
Wraps the com.blackberry.app plugin functions, auto installed as a dependancy.
|
||||||
|
|
||||||
Pull requests welcome.
|
Pull requests welcome.
|
||||||
|
19
plugin.xml
19
plugin.xml
@ -2,10 +2,10 @@
|
|||||||
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
|
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
id="net.yoik.cordova.plugins.screenorientation"
|
id="net.yoik.cordova.plugins.screenorientation"
|
||||||
version="1.0.1">
|
version="1.1.0">
|
||||||
|
|
||||||
<name>YoikScreenOrientation</name>
|
<name>Yoik Screen Orientation</name>
|
||||||
<description>Yoik Screen Orientation Plugin</description>
|
<description>The Yoik Screen Orientation Plugin adds Screen Orientation API lock and unlock functions to the global screen object in android, iOS and BB10.</description>
|
||||||
<license>MIT</license>
|
<license>MIT</license>
|
||||||
|
|
||||||
<engines>
|
<engines>
|
||||||
@ -22,6 +22,9 @@
|
|||||||
<param name="ios-package" value="YoikScreenOrientation" />
|
<param name="ios-package" value="YoikScreenOrientation" />
|
||||||
</feature>
|
</feature>
|
||||||
</config-file>
|
</config-file>
|
||||||
|
<js-module src="www/screenorientation.ios.js" name="screenorientation.ios">
|
||||||
|
<merges target="cordova.plugins.screenorientation" />
|
||||||
|
</js-module>
|
||||||
<header-file src="src/ios/YoikScreenOrientation.h" />
|
<header-file src="src/ios/YoikScreenOrientation.h" />
|
||||||
<source-file src="src/ios/YoikScreenOrientation.m" />
|
<source-file src="src/ios/YoikScreenOrientation.m" />
|
||||||
</platform>
|
</platform>
|
||||||
@ -36,4 +39,14 @@
|
|||||||
</config-file>
|
</config-file>
|
||||||
</platform>
|
</platform>
|
||||||
|
|
||||||
|
<platform name="blackberry10">
|
||||||
|
<dependency id="com.blackberry.app" />
|
||||||
|
<config-file target="www/config.xml" parent="/widget">
|
||||||
|
<feature name="net.yoik.cordova.plugins.screenorientation" value="net.yoik.cordova.plugins.screenorientation" />
|
||||||
|
</config-file>
|
||||||
|
<js-module src="www/screenorientation.bb10.js" name="screenorientation.bb10">
|
||||||
|
<merges target="cordova.plugins.screenorientation" />
|
||||||
|
</js-module>
|
||||||
|
</platform>
|
||||||
|
|
||||||
</plugin>
|
</plugin>
|
||||||
|
37
www/screenorientation.bb10.js
Normal file
37
www/screenorientation.bb10.js
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2014
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var screenOrientation = {};
|
||||||
|
|
||||||
|
screenOrientation.setOrientation = function(orientation) {
|
||||||
|
if (blackberry.app) {
|
||||||
|
if (orientation === 'unlocked') {
|
||||||
|
blackberry.app.unlockOrientation();
|
||||||
|
} else {
|
||||||
|
blackberry.app.lockOrientation(orientation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = screenOrientation;
|
52
www/screenorientation.ios.js
Normal file
52
www/screenorientation.ios.js
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2014
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// ios orientation callback/hook
|
||||||
|
window.shouldRotateToOrientation = function(orientation) {
|
||||||
|
var currOrientation = cordova.plugins.screenorientation.currOrientation;
|
||||||
|
switch (currOrientation) {
|
||||||
|
case 'portrait':
|
||||||
|
case 'portrait-primary':
|
||||||
|
if (orientation === 0) return true;
|
||||||
|
break;
|
||||||
|
case 'landscape':
|
||||||
|
case 'landscape-primary':
|
||||||
|
if (orientation === -90) return true;
|
||||||
|
break;
|
||||||
|
case 'landscape':
|
||||||
|
case 'landscape-secondary':
|
||||||
|
if (orientation === 90) return true;
|
||||||
|
break;
|
||||||
|
case 'portrait':
|
||||||
|
case 'portrait-secondary':
|
||||||
|
if (orientation === 180) return true;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if (orientation === -90 || orientation === 90 || orientation === 0) return true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {};
|
@ -23,6 +23,7 @@ SOFTWARE.
|
|||||||
*/
|
*/
|
||||||
var argscheck = require('cordova/argscheck'),
|
var argscheck = require('cordova/argscheck'),
|
||||||
exec = require('cordova/exec'),
|
exec = require('cordova/exec'),
|
||||||
|
screenOrientation = {},
|
||||||
Orientations = [
|
Orientations = [
|
||||||
'portrait-primary',
|
'portrait-primary',
|
||||||
// The orientation is in the primary portrait mode.
|
// The orientation is in the primary portrait mode.
|
||||||
@ -36,15 +37,14 @@ var argscheck = require('cordova/argscheck'),
|
|||||||
// The orientation is either portrait-primary or portrait-secondary.
|
// The orientation is either portrait-primary or portrait-secondary.
|
||||||
'landscape'
|
'landscape'
|
||||||
// The orientation is either landscape-primary or landscape-secondary.
|
// The orientation is either landscape-primary or landscape-secondary.
|
||||||
],
|
];
|
||||||
currOrientation = 'unlocked';
|
|
||||||
|
|
||||||
var orientationExports = {};
|
screenOrientation.Orientations = Orientations;
|
||||||
|
screenOrientation.currOrientation = 'unlocked';
|
||||||
|
|
||||||
function setOrientation(orientation) {
|
screenOrientation.setOrientation = function(orientation) {
|
||||||
currOrientation = orientation ? orientation : 'unlocked';
|
exec(null, null, "YoikScreenOrientation", "screenOrientation", ['set', orientation]);
|
||||||
exec(null, null, "YoikScreenOrientation", "screenOrientation", ['set', currOrientation]);
|
};
|
||||||
}
|
|
||||||
|
|
||||||
function addScreenOrientationApi(obj) {
|
function addScreenOrientationApi(obj) {
|
||||||
if (obj.unlockOrientation || obj.lockOrientation) {
|
if (obj.unlockOrientation || obj.lockOrientation) {
|
||||||
@ -56,40 +56,42 @@ function addScreenOrientationApi(obj) {
|
|||||||
console.log('INVALID ORIENTATION', orientation);
|
console.log('INVALID ORIENTATION', orientation);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setOrientation(orientation);
|
screenOrientation.currOrientation = orientation;
|
||||||
|
screenOrientation.setOrientation(orientation);
|
||||||
};
|
};
|
||||||
|
|
||||||
obj.unlockOrientation = function() {
|
obj.unlockOrientation = function() {
|
||||||
setOrientation('unlocked');
|
screenOrientation.currOrientation = 'unlocked';
|
||||||
|
screenOrientation.setOrientation('unlocked');
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
addScreenOrientationApi(screen);
|
addScreenOrientationApi(screen);
|
||||||
|
orientationChange();
|
||||||
|
|
||||||
// ios orientation callback/hook
|
function orientationChange() {
|
||||||
window.shouldRotateToOrientation = function(orientation) {
|
var orientation;
|
||||||
switch (currOrientation) {
|
|
||||||
case 'portrait':
|
switch (window.orientation) {
|
||||||
case 'portrait-primary':
|
case 0:
|
||||||
if (orientation === 0) return true;
|
orientation = 'portrait-primary';
|
||||||
break;
|
break;
|
||||||
case 'landscape':
|
case 90:
|
||||||
case 'landscape-primary':
|
orientation = 'landscape-secondary';
|
||||||
if (orientation === -90) return true;
|
break;
|
||||||
break;
|
case 180:
|
||||||
case 'landscape':
|
orientation = 'portrait-secondary';
|
||||||
case 'landscape-secondary':
|
break;
|
||||||
if (orientation === 90) return true;
|
case -90:
|
||||||
break;
|
orientation = 'landscape-primary';
|
||||||
case 'portrait':
|
break;
|
||||||
case 'portrait-secondary':
|
|
||||||
if (orientation === 180) return true;
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
if (orientation === -90 || orientation === 90 || orientation === 0) return true;
|
orientation = 'unknown';
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = {};
|
screen.orientation = orientation;
|
||||||
|
}
|
||||||
|
|
||||||
|
window.addEventListener("orientationchange", orientationChange, true);
|
||||||
|
|
||||||
|
module.exports = screenOrientation;
|
Loading…
x
Reference in New Issue
Block a user