From bb8b2c30c22dc37b4d4323d452ea623f646a70fd Mon Sep 17 00:00:00 2001 From: Grant Benvenuti Date: Sun, 27 Jul 2014 13:51:20 +1000 Subject: [PATCH] Added bb10 support and orientation property to screen object --- README.md | 44 ++++++++++++++--------- plugin.xml | 19 ++++++++-- www/screenorientation.bb10.js | 37 +++++++++++++++++++ www/screenorientation.ios.js | 52 +++++++++++++++++++++++++++ www/screenorientation.js | 68 ++++++++++++++++++----------------- 5 files changed, 168 insertions(+), 52 deletions(-) create mode 100644 www/screenorientation.bb10.js create mode 100644 www/screenorientation.ios.js diff --git a/README.md b/README.md index 3f423d4..a3f0af3 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,17 @@ #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 -interface is based on the [Screen Orientation API](http://www.w3.org/TR/screen-orientation/). +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/). + +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 @@ -11,7 +21,7 @@ cordova plugin add net.yoik.cordova.plugins.screenorientation https://github.com/yoik/cordova-yoik-screenorientation -##Orientations +##Supported Orientations __portrait-primary__ The orientation is in the primary portrait mode. @@ -33,31 +43,29 @@ The orientation is either landscape-primary or landscape-secondary (sensor). ##Usage + // set to either landscape screen.lockOrientation('landscape'); + // allow user rotate screen.unlockOrientation(); + // access current orientation + console.log('Orientation is ' + screen.orientation); + ##Events 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. -i.e. - function init() { - window.addEventListener("orientationchange", orientationChange, true); - } - - function orientationChange(e) { - var orientation="portrait"; - if(window.orientation == -90 || window.orientation == 90) orientation = "landscape"; - document.getElementById("status").innerHTML+=orientation+"
"; - } - -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 +For this plugin to follow the full API events should be fired on the screen object. +iOS and BB10 do not currently support events on the _screen_ object so custom event 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 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. +##BB10 Notes + +Wraps the com.blackberry.app plugin functions, auto installed as a dependancy. + Pull requests welcome. diff --git a/plugin.xml b/plugin.xml index 0b8ec68..34e6cf1 100644 --- a/plugin.xml +++ b/plugin.xml @@ -2,10 +2,10 @@ + version="1.1.0"> - YoikScreenOrientation - Yoik Screen Orientation Plugin + Yoik Screen Orientation + The Yoik Screen Orientation Plugin adds Screen Orientation API lock and unlock functions to the global screen object in android, iOS and BB10. MIT @@ -22,6 +22,9 @@ + + + @@ -36,4 +39,14 @@ + + + + + + + + + + diff --git a/www/screenorientation.bb10.js b/www/screenorientation.bb10.js new file mode 100644 index 0000000..c3b56c6 --- /dev/null +++ b/www/screenorientation.bb10.js @@ -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; \ No newline at end of file diff --git a/www/screenorientation.ios.js b/www/screenorientation.ios.js new file mode 100644 index 0000000..980c69b --- /dev/null +++ b/www/screenorientation.ios.js @@ -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 = {}; \ No newline at end of file diff --git a/www/screenorientation.js b/www/screenorientation.js index 98cc1f5..1d431f5 100644 --- a/www/screenorientation.js +++ b/www/screenorientation.js @@ -23,6 +23,7 @@ SOFTWARE. */ var argscheck = require('cordova/argscheck'), exec = require('cordova/exec'), + screenOrientation = {}, Orientations = [ 'portrait-primary', // 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. 'landscape' // The orientation is either landscape-primary or landscape-secondary. - ], - currOrientation = 'unlocked'; + ]; -var orientationExports = {}; +screenOrientation.Orientations = Orientations; +screenOrientation.currOrientation = 'unlocked'; -function setOrientation(orientation) { - currOrientation = orientation ? orientation : 'unlocked'; - exec(null, null, "YoikScreenOrientation", "screenOrientation", ['set', currOrientation]); -} +screenOrientation.setOrientation = function(orientation) { + exec(null, null, "YoikScreenOrientation", "screenOrientation", ['set', orientation]); +}; function addScreenOrientationApi(obj) { if (obj.unlockOrientation || obj.lockOrientation) { @@ -56,40 +56,42 @@ function addScreenOrientationApi(obj) { console.log('INVALID ORIENTATION', orientation); return; } - setOrientation(orientation); + screenOrientation.currOrientation = orientation; + screenOrientation.setOrientation(orientation); }; obj.unlockOrientation = function() { - setOrientation('unlocked'); + screenOrientation.currOrientation = 'unlocked'; + screenOrientation.setOrientation('unlocked'); }; } addScreenOrientationApi(screen); +orientationChange(); -// ios orientation callback/hook -window.shouldRotateToOrientation = function(orientation) { - 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; +function orientationChange() { + var orientation; + + switch (window.orientation) { + case 0: + orientation = 'portrait-primary'; + break; + case 90: + orientation = 'landscape-secondary'; + break; + case 180: + orientation = 'portrait-secondary'; + break; + case -90: + orientation = 'landscape-primary'; + break; default: - if (orientation === -90 || orientation === 90 || orientation === 0) return true; - break; + orientation = 'unknown'; } - return false; -}; -module.exports = {}; \ No newline at end of file + screen.orientation = orientation; +} + +window.addEventListener("orientationchange", orientationChange, true); + +module.exports = screenOrientation; \ No newline at end of file