diff --git a/.eslintrc.yml b/.eslintrc.yml new file mode 100644 index 0000000..17277f7 --- /dev/null +++ b/.eslintrc.yml @@ -0,0 +1,23 @@ +# 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. + +root: true +extends: '@cordova/eslint-config/browser' + +overrides: + - files: [tests/**/*.js] + extends: '@cordova/eslint-config/node-tests' diff --git a/package.json b/package.json index ea6db91..4500339 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,8 @@ "repository": "github:apache/cordova-plugin-screen-orientation", "bugs": "https://github.com/apache/cordova-plugin-screen-orientation/issues", "scripts": { - "test": "npm run jshint", - "jshint": "node node_modules/jshint/bin/jshint www && node node_modules/jshint/bin/jshint src" + "test": "npm run lint", + "lint": "eslint ." }, "cordova": { "id": "cordova-plugin-screen-orientation", @@ -31,7 +31,7 @@ "author": "Apache Software Foundation", "license": "Apache-2.0", "devDependencies": { - "jshint": "^2.9.4" + "@cordova/eslint-config": "^3.0.0" }, "engines": { "cordovaDependencies": { diff --git a/src/windows/CDVOrientationProxy.js b/src/windows/CDVOrientationProxy.js index 7f36b49..0bb7259 100644 --- a/src/windows/CDVOrientationProxy.js +++ b/src/windows/CDVOrientationProxy.js @@ -17,7 +17,9 @@ * specific language governing permissions and limitations * under the License. * -*/ + */ + +/* global Windows, WinJS */ var DisplayInfo = Windows.Graphics.Display.DisplayInformation; var Orientations = Windows.Graphics.Display.DisplayOrientations; @@ -28,35 +30,37 @@ if (!window.Promise) { module.exports = { screenOrientation: function (win, fail, args) { - //console.log("screenOrientation proxy called with " + args); + // console.log("screenOrientation proxy called with " + args); try { var prefOrients = args[0]; var winPrefs = 0; - if (prefOrients & 1) { // UIInterfaceOrientationPortrait - winPrefs = winPrefs | Orientations.portrait; + if (prefOrients & 1) { + // UIInterfaceOrientationPortrait + winPrefs = winPrefs | Orientations.portrait; } - if (prefOrients & 2) { // UIInterfaceOrientationPortraitUpsideDown + if (prefOrients & 2) { + // UIInterfaceOrientationPortraitUpsideDown winPrefs = winPrefs | Orientations.portraitFlipped; } - if(prefOrients & 4) { // UIInterfaceOrientationLandscapeLeft + if (prefOrients & 4) { + // UIInterfaceOrientationLandscapeLeft winPrefs = winPrefs | Orientations.landscape; } - if (prefOrients & 8) { // UIInterfaceOrientationLandscapeRight + if (prefOrients & 8) { + // UIInterfaceOrientationLandscapeRight winPrefs = winPrefs | Orientations.landscapeFlipped; } setTimeout(function () { DisplayInfo.autoRotationPreferences = winPrefs; win(); }, 0); - } - catch (err) { - console.log("error :: " + err); + } catch (err) { + console.log('error :: ' + err); fail(); } - } }; -require("cordova/exec/proxy").add("CDVOrientation", module.exports); +require('cordova/exec/proxy').add('CDVOrientation', module.exports); diff --git a/tests/tests.js b/tests/tests.js index f722964..60c1d98 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -18,47 +18,44 @@ * under the License. * */ -/* jshint jasmine: true */ -exports.defineAutoTests = function() { - describe('window.screen', function() { - - it('should be defined', function() { +exports.defineAutoTests = function () { + describe('window.screen', function () { + it('should be defined', function () { expect(window.screen).toBeDefined(); }); }); - describe('window.screen.orientation', function() { - - it('should be defined', function() { + describe('window.screen.orientation', function () { + it('should be defined', function () { expect(window.screen.orientation).toBeDefined(); }); - it('should have a `lock` function', function() { + it('should have a `lock` function', function () { expect(window.screen.orientation.lock).toBeDefined(); expect(typeof window.screen.orientation.lock).toBe('function'); }); - it('should have an `unlock` function', function() { + it('should have an `unlock` function', function () { expect(window.screen.orientation.unlock).toBeDefined(); expect(typeof window.screen.orientation.unlock).toBe('function'); }); - it('should have a `type` property (string)', function() { + it('should have a `type` property (string)', function () { expect(window.screen.orientation.type).toBeDefined(); expect(typeof window.screen.orientation.type).toBe('string'); }); - it('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'); }); - it('should have an `onchange` settable 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(){}; + var funk = function () {}; window.screen.orientation.onchange = funk; // now it should exist expect(window.screen.orientation.onchange).toBeDefined(); @@ -69,24 +66,21 @@ exports.defineAutoTests = function() { expect(window.screen.orientation.onchange).toBe(null); }); - it('should have an eventListener interface',function() { + 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'); - - }); }); - describe('OrientationType', function() { - - it("should be defined", function() { + describe('OrientationType', function () { + it('should be defined', function () { expect(window.OrientationType).toBeDefined(); }); - it("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(); @@ -94,53 +88,49 @@ exports.defineAutoTests = function() { }); }); - describe('OrientationLockType', function() { - - it("should be defined", function() { + describe('OrientationLockType', function () { + it('should be defined', 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(); expect(window.OrientationLockType['landscape-secondary']).toBeDefined(); - expect(window.OrientationLockType['portrait']).toBeDefined(); - expect(window.OrientationLockType['landscape']).toBeDefined(); - expect(window.OrientationLockType['any']).toBeDefined(); + expect(window.OrientationLockType.portrait).toBeDefined(); + expect(window.OrientationLockType.landscape).toBeDefined(); + expect(window.OrientationLockType.any).toBeDefined(); }); }); - // TODO: // test addEventListener('change') works // test onchange works - describe('window.screen.orientation', function() { - - it('should successfully lock and unlock screen orientation', function() { - return window.screen.orientation.lock('portrait').then(function() { + describe('window.screen.orientation', function () { + it('should successfully lock and unlock screen orientation', function () { + return window.screen.orientation.lock('portrait').then(function () { expect(window.screen.orientation.type).toMatch(/^portrait-/); expect(window.screen.orientation.unlock).not.toThrow(); }); }); }); }; -exports.defineManualTests = function(contentEl, createActionButton) { - - createActionButton('Listen to orientationchange events', function() { - window.addEventListener("orientationchange", function(){ +exports.defineManualTests = function (contentEl, createActionButton) { + createActionButton('Listen to orientationchange events', function () { + window.addEventListener('orientationchange', function () { contentEl.innerHTML += '

Orientation changed! ' + screen.orientation.type + '

'; }); }); - createActionButton('Unlock orientation', function() { + createActionButton('Unlock orientation', function () { screen.orientation.unlock(); contentEl.innerHTML += '

Orientation unlocked.

'; }); - createActionButton('Lock to portrait', function() { + createActionButton('Lock to portrait', function () { screen.orientation.lock('portrait'); contentEl.innerHTML += '

Orientation locked to portrait.

'; }); - createActionButton('Lock to landscape', function() { + createActionButton('Lock to landscape', function () { screen.orientation.lock('landscape'); contentEl.innerHTML += '

Orientation locked to landscape.

'; }); diff --git a/www/screenorientation.js b/www/screenorientation.js index ef2eb49..d8fe529 100644 --- a/www/screenorientation.js +++ b/www/screenorientation.js @@ -18,6 +18,9 @@ * under the License. * */ + +/* global cordova, OrientationLockType */ + var screenOrientation = {}; if (!window.OrientationType) { window.OrientationType = { @@ -33,15 +36,15 @@ if (!window.OrientationLockType) { '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) + 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) { +screenOrientation.setOrientation = function (orientation) { orientationMask = window.OrientationLockType[orientation]; - cordova.exec(null, null, "CDVOrientation", "screenOrientation", [orientationMask, orientation]); + cordova.exec(null, null, 'CDVOrientation', 'screenOrientation', [orientationMask, orientation]); }; if (!screen.orientation) { @@ -50,44 +53,45 @@ if (!screen.orientation) { setOrientationProperties(); -function addScreenOrientationApi(screenObject) { - +function addScreenOrientationApi (screenObject) { if (screenObject.unlock || screenObject.lock) { screenObject.nativeLock = screenObject.lock; } - screenObject.lock = function(orientation) { + screenObject.lock = function (orientation) { var promiseLock; - var p = new Promise(function(resolve, reject) { + var p = new Promise(function (resolve, reject) { if (screenObject.nativeLock) { promiseLock = screenObject.nativeLock(orientation); - promiseLock.then(function success(res) { - resolve(); - }, function error(err) { - screenObject.nativeLock = null; - resolveOrientation(orientation, resolve, reject); - }); + promiseLock.then( + function success (_) { + resolve(); + }, + function error (_) { + screenObject.nativeLock = null; + resolveOrientation(orientation, resolve, reject); + } + ); } else { resolveOrientation(orientation, resolve, reject); } }); return p; }; - screenObject.unlock = function() { + screenObject.unlock = function () { screenOrientation.setOrientation('any'); }; } -function resolveOrientation(orientation, resolve, reject) { - if (!OrientationLockType.hasOwnProperty(orientation)) { +function resolveOrientation (orientation, resolve, reject) { + if (!Object.prototype.hasOwnProperty.call(OrientationLockType, 'orientation')) { var err = new Error(); - err.name = "NotSupportedError"; - reject(err); //"cannot change orientation"); + err.name = 'NotSupportedError'; + reject(err); // "cannot change orientation"); } else { screenOrientation.setOrientation(orientation); - resolve("Orientation set"); // orientation change successful + resolve('Orientation set'); // orientation change successful } - } addScreenOrientationApi(screen.orientation); @@ -95,8 +99,7 @@ addScreenOrientationApi(screen.orientation); var onChangeListener = null; Object.defineProperty(screen.orientation, 'onchange', { - set: function(listener) { - + set: function (listener) { if (onChangeListener) { screen.orientation.removeEventListener('change', onChangeListener); } @@ -105,50 +108,48 @@ Object.defineProperty(screen.orientation, 'onchange', { screen.orientation.addEventListener('change', onChangeListener); } }, - get: function() { - return (onChangeListener ? onChangeListener : null); + get: function () { + return onChangeListener || null; }, - enumerable: true, + enumerable: true }); - -var evtTarget = new XMLHttpRequest(); //document.createElement('div'); -var orientationchange = function() { +var evtTarget = new XMLHttpRequest(); // document.createElement('div'); +var orientationchange = function () { setOrientationProperties(); var event = document.createEvent('Events'); - event.initEvent("change", false, false); + event.initEvent('change', false, false); evtTarget.dispatchEvent(event); }; -screen.orientation.addEventListener = function(a,b,c) { - return evtTarget.addEventListener(a,b,c); +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); +screen.orientation.removeEventListener = function (a, b, c) { + return evtTarget.removeEventListener(a, b, c); }; -function setOrientationProperties() { +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; - default: - screen.orientation.type = 'portrait-primary'; - break; + 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; + default: + screen.orientation.type = 'portrait-primary'; + break; } screen.orientation.angle = window.orientation || 0; - } -window.addEventListener("orientationchange", orientationchange, true); +window.addEventListener('orientationchange', orientationchange, true); module.exports = screenOrientation;