Github user purplecabbage commented on a diff in the pull request: https://github.com/apache/cordova-plugin-screen-orientation/pull/12#discussion_r105305926 --- Diff: www/screenorientation.js --- @@ -18,83 +18,127 @@ * under the License. * */ +var screenOrientation = {}; +if (!window.OrientationType) { + window.OrientationType = { + 'portrait-primary': 0, + 'portrait-secondary': 180, + 'landscape-primary': 90, + 'landscape-secondary': -90 + }; +} +if (!window.OrientationLockType) { + window.OrientationLockType = { + 'portrait-primary': 1, + 'portrait-secondary': 2, + 'landscape-primary': 4, + 'landscape-secondary': 8, + '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; +screenOrientation.setOrientation = function(orientation) { + orientationMask = window.OrientationLockType[orientation]; + cordova.exec(null, null, "CDVOrientation", "screenOrientation", [orientationMask, orientation]); +}; +if (!screen.orientation) { + screen.orientation = {}; +} - var screenOrientation = {}; - if (!window.OrientationType) { - window.OrientationType = { - '0': 'portrait-primary', - '180': 'portrait-secondary', - '90': 'landscape-primary', - '-90': 'landscape-secondary' - }; - } - if (!window.OrientationLockType) { - window.OrientationLockType = { - 'portrait-primary': 1, - 'portrait-secondary': 2, - 'landscape-primary': 4, - 'landscape-secondary': 8, - '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; - screenOrientation.setOrientation = function(orientation) { - orientationMask = window.OrientationLockType[orientation]; - cordova.exec(null, null, "CDVOrientation", "screenOrientation", [orientationMask, orientation]); - }; +setOrientationProperties(); - function addScreenOrientationApi(screenObject) { - if (screenObject.unlock || screenObject.lock) { - screenObject.nativeLock = screenObject.lock; - } +function addScreenOrientationApi(screenObject) { - screenObject.lock = function(orientation) { - var promiseLock; - var p = new Promise(function(resolve, reject) { - if (screenObject.nativeLock != null) { - promiseLock = screenObject.nativeLock(orientation); - promiseLock.then(function success(res) { + if (screenObject.unlock || screenObject.lock) { + screenObject.nativeLock = screenObject.lock; + } + + screenObject.lock = function(orientation) { + var promiseLock; + var p = new Promise(function(resolve, reject) { + if (screenObject.nativeLock != null) { + promiseLock = screenObject.nativeLock(orientation); + promiseLock.then(function success(res) { resolve(); }, function error(err) { screenObject.nativeLock = null; resolveOrientation(orientation, resolve, reject); }); - } else { - resolveOrientation(orientation, resolve, reject); - } - }) - return p; - } + } else { + resolveOrientation(orientation, resolve, reject); + } + }) + return p; + } + + screenObject.unlock = function() { + screenOrientation.setOrientation('any'); + }; - screenObject.unlock = function() { - screenOrientation.setOrientation('any'); - }; +} +function resolveOrientation(orientation, resolve, reject) { + if (!OrientationLockType.hasOwnProperty(orientation)) { + var err = new Error(); + err.name = "NotSupportedError"; + reject(err); //"cannot change orientation"); + } else { + screenOrientation.setOrientation(orientation); + resolve("Orientation set"); // orientation change successful } - 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 + +} + +addScreenOrientationApi(screen.orientation); + +var onChangeListener = null; +Object.defineProperty(screen.orientation, 'onchange', { + + set: function(listener) { + if (onChangeListener != null) { + screen.orienation.removeEventListener('change', onChangeListener); --- End diff -- Spelling error: orienation <= orientation
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@cordova.apache.org For additional commands, e-mail: dev-h...@cordova.apache.org