refactor(eslint): use cordova-eslint /w fix (#66)

This commit is contained in:
Tim Brust 2020-07-02 10:12:53 +00:00 committed by GitHub
parent 4b719848a6
commit 57df0189e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 126 additions and 108 deletions

23
.eslintrc.yml Normal file
View File

@ -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'

View File

@ -5,8 +5,8 @@
"repository": "github:apache/cordova-plugin-screen-orientation", "repository": "github:apache/cordova-plugin-screen-orientation",
"bugs": "https://github.com/apache/cordova-plugin-screen-orientation/issues", "bugs": "https://github.com/apache/cordova-plugin-screen-orientation/issues",
"scripts": { "scripts": {
"test": "npm run jshint", "test": "npm run lint",
"jshint": "node node_modules/jshint/bin/jshint www && node node_modules/jshint/bin/jshint src" "lint": "eslint ."
}, },
"cordova": { "cordova": {
"id": "cordova-plugin-screen-orientation", "id": "cordova-plugin-screen-orientation",
@ -31,7 +31,7 @@
"author": "Apache Software Foundation", "author": "Apache Software Foundation",
"license": "Apache-2.0", "license": "Apache-2.0",
"devDependencies": { "devDependencies": {
"jshint": "^2.9.4" "@cordova/eslint-config": "^3.0.0"
}, },
"engines": { "engines": {
"cordovaDependencies": { "cordovaDependencies": {

View File

@ -17,7 +17,9 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
* *
*/ */
/* global Windows, WinJS */
var DisplayInfo = Windows.Graphics.Display.DisplayInformation; var DisplayInfo = Windows.Graphics.Display.DisplayInformation;
var Orientations = Windows.Graphics.Display.DisplayOrientations; var Orientations = Windows.Graphics.Display.DisplayOrientations;
@ -28,35 +30,37 @@ if (!window.Promise) {
module.exports = { module.exports = {
screenOrientation: function (win, fail, args) { screenOrientation: function (win, fail, args) {
//console.log("screenOrientation proxy called with " + args); // console.log("screenOrientation proxy called with " + args);
try { try {
var prefOrients = args[0]; var prefOrients = args[0];
var winPrefs = 0; var winPrefs = 0;
if (prefOrients & 1) { // UIInterfaceOrientationPortrait if (prefOrients & 1) {
winPrefs = winPrefs | Orientations.portrait; // UIInterfaceOrientationPortrait
winPrefs = winPrefs | Orientations.portrait;
} }
if (prefOrients & 2) { // UIInterfaceOrientationPortraitUpsideDown if (prefOrients & 2) {
// UIInterfaceOrientationPortraitUpsideDown
winPrefs = winPrefs | Orientations.portraitFlipped; winPrefs = winPrefs | Orientations.portraitFlipped;
} }
if(prefOrients & 4) { // UIInterfaceOrientationLandscapeLeft if (prefOrients & 4) {
// UIInterfaceOrientationLandscapeLeft
winPrefs = winPrefs | Orientations.landscape; winPrefs = winPrefs | Orientations.landscape;
} }
if (prefOrients & 8) { // UIInterfaceOrientationLandscapeRight if (prefOrients & 8) {
// UIInterfaceOrientationLandscapeRight
winPrefs = winPrefs | Orientations.landscapeFlipped; winPrefs = winPrefs | Orientations.landscapeFlipped;
} }
setTimeout(function () { setTimeout(function () {
DisplayInfo.autoRotationPreferences = winPrefs; DisplayInfo.autoRotationPreferences = winPrefs;
win(); win();
}, 0); }, 0);
} } catch (err) {
catch (err) { console.log('error :: ' + err);
console.log("error :: " + err);
fail(); fail();
} }
} }
}; };
require("cordova/exec/proxy").add("CDVOrientation", module.exports); require('cordova/exec/proxy').add('CDVOrientation', module.exports);

View File

@ -18,47 +18,44 @@
* under the License. * under the License.
* *
*/ */
/* jshint jasmine: true */
exports.defineAutoTests = function() {
describe('window.screen', function() { exports.defineAutoTests = function () {
describe('window.screen', function () {
it('should be defined', function() { it('should be defined', function () {
expect(window.screen).toBeDefined(); expect(window.screen).toBeDefined();
}); });
}); });
describe('window.screen.orientation', function() { describe('window.screen.orientation', function () {
it('should be defined', function () {
it('should be defined', function() {
expect(window.screen.orientation).toBeDefined(); 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(window.screen.orientation.lock).toBeDefined();
expect(typeof window.screen.orientation.lock).toBe('function'); 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(window.screen.orientation.unlock).toBeDefined();
expect(typeof window.screen.orientation.unlock).toBe('function'); 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(window.screen.orientation.type).toBeDefined();
expect(typeof window.screen.orientation.type).toBe('string'); 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(window.screen.orientation.angle).toBeDefined();
expect(typeof window.screen.orientation.angle).toBe('number'); 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 // it should be null to start
expect(window.screen.orientation.onchange).toBe(null); expect(window.screen.orientation.onchange).toBe(null);
// then we set it // then we set it
var funk = function(){}; var funk = function () {};
window.screen.orientation.onchange = funk; window.screen.orientation.onchange = funk;
// now it should exist // now it should exist
expect(window.screen.orientation.onchange).toBeDefined(); expect(window.screen.orientation.onchange).toBeDefined();
@ -69,24 +66,21 @@ exports.defineAutoTests = function() {
expect(window.screen.orientation.onchange).toBe(null); 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(window.screen.orientation.addEventListener).toBeDefined();
expect(typeof window.screen.orientation.addEventListener).toBe('function'); expect(typeof window.screen.orientation.addEventListener).toBe('function');
expect(window.screen.orientation.removeEventListener).toBeDefined(); expect(window.screen.orientation.removeEventListener).toBeDefined();
expect(typeof window.screen.orientation.removeEventListener).toBe('function'); expect(typeof window.screen.orientation.removeEventListener).toBe('function');
}); });
}); });
describe('OrientationType', function() { describe('OrientationType', function () {
it('should be defined', function () {
it("should be defined", function() {
expect(window.OrientationType).toBeDefined(); 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-primary']).toBeDefined();
expect(window.OrientationType['portrait-secondary']).toBeDefined(); expect(window.OrientationType['portrait-secondary']).toBeDefined();
expect(window.OrientationType['landscape-primary']).toBeDefined(); expect(window.OrientationType['landscape-primary']).toBeDefined();
@ -94,53 +88,49 @@ exports.defineAutoTests = function() {
}); });
}); });
describe('OrientationLockType', function() { describe('OrientationLockType', function () {
it('should be defined', function () {
it("should be defined", function() {
expect(window.OrientationLockType).toBeDefined(); 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-primary']).toBeDefined();
expect(window.OrientationLockType['portrait-secondary']).toBeDefined(); expect(window.OrientationLockType['portrait-secondary']).toBeDefined();
expect(window.OrientationLockType['landscape-primary']).toBeDefined(); expect(window.OrientationLockType['landscape-primary']).toBeDefined();
expect(window.OrientationLockType['landscape-secondary']).toBeDefined(); expect(window.OrientationLockType['landscape-secondary']).toBeDefined();
expect(window.OrientationLockType['portrait']).toBeDefined(); expect(window.OrientationLockType.portrait).toBeDefined();
expect(window.OrientationLockType['landscape']).toBeDefined(); expect(window.OrientationLockType.landscape).toBeDefined();
expect(window.OrientationLockType['any']).toBeDefined(); expect(window.OrientationLockType.any).toBeDefined();
}); });
}); });
// TODO: // TODO:
// test addEventListener('change') works // test addEventListener('change') works
// test onchange works // test onchange works
describe('window.screen.orientation', function() { describe('window.screen.orientation', function () {
it('should successfully lock and unlock screen orientation', function () {
it('should successfully lock and unlock screen orientation', function() { return window.screen.orientation.lock('portrait').then(function () {
return window.screen.orientation.lock('portrait').then(function() {
expect(window.screen.orientation.type).toMatch(/^portrait-/); expect(window.screen.orientation.type).toMatch(/^portrait-/);
expect(window.screen.orientation.unlock).not.toThrow(); expect(window.screen.orientation.unlock).not.toThrow();
}); });
}); });
}); });
}; };
exports.defineManualTests = function(contentEl, createActionButton) { exports.defineManualTests = function (contentEl, createActionButton) {
createActionButton('Listen to orientationchange events', function () {
createActionButton('Listen to orientationchange events', function() { window.addEventListener('orientationchange', function () {
window.addEventListener("orientationchange", function(){
contentEl.innerHTML += '<p>Orientation changed! ' + screen.orientation.type + '</p>'; contentEl.innerHTML += '<p>Orientation changed! ' + screen.orientation.type + '</p>';
}); });
}); });
createActionButton('Unlock orientation', function() { createActionButton('Unlock orientation', function () {
screen.orientation.unlock(); screen.orientation.unlock();
contentEl.innerHTML += '<p>Orientation unlocked.</p>'; contentEl.innerHTML += '<p>Orientation unlocked.</p>';
}); });
createActionButton('Lock to portrait', function() { createActionButton('Lock to portrait', function () {
screen.orientation.lock('portrait'); screen.orientation.lock('portrait');
contentEl.innerHTML += '<p>Orientation locked to portrait.</p>'; contentEl.innerHTML += '<p>Orientation locked to portrait.</p>';
}); });
createActionButton('Lock to landscape', function() { createActionButton('Lock to landscape', function () {
screen.orientation.lock('landscape'); screen.orientation.lock('landscape');
contentEl.innerHTML += '<p>Orientation locked to landscape.</p>'; contentEl.innerHTML += '<p>Orientation locked to landscape.</p>';
}); });

View File

@ -18,6 +18,9 @@
* under the License. * under the License.
* *
*/ */
/* global cordova, OrientationLockType */
var screenOrientation = {}; var screenOrientation = {};
if (!window.OrientationType) { if (!window.OrientationType) {
window.OrientationType = { window.OrientationType = {
@ -33,15 +36,15 @@ if (!window.OrientationLockType) {
'portrait-secondary': 2, 'portrait-secondary': 2,
'landscape-primary': 4, 'landscape-primary': 4,
'landscape-secondary': 8, 'landscape-secondary': 8,
'portrait': 3, // either portrait-primary or portrait-secondary. portrait: 3, // either portrait-primary or portrait-secondary.
'landscape': 12, // either landscape-primary or landscape-secondary. landscape: 12, // either landscape-primary or landscape-secondary.
'any': 15 // All orientations are supported (unlocked orientation) any: 15 // All orientations are supported (unlocked orientation)
}; };
} }
var orientationMask = 1; var orientationMask = 1;
screenOrientation.setOrientation = function(orientation) { screenOrientation.setOrientation = function (orientation) {
orientationMask = window.OrientationLockType[orientation]; orientationMask = window.OrientationLockType[orientation];
cordova.exec(null, null, "CDVOrientation", "screenOrientation", [orientationMask, orientation]); cordova.exec(null, null, 'CDVOrientation', 'screenOrientation', [orientationMask, orientation]);
}; };
if (!screen.orientation) { if (!screen.orientation) {
@ -50,44 +53,45 @@ if (!screen.orientation) {
setOrientationProperties(); setOrientationProperties();
function addScreenOrientationApi(screenObject) { function addScreenOrientationApi (screenObject) {
if (screenObject.unlock || screenObject.lock) { if (screenObject.unlock || screenObject.lock) {
screenObject.nativeLock = screenObject.lock; screenObject.nativeLock = screenObject.lock;
} }
screenObject.lock = function(orientation) { screenObject.lock = function (orientation) {
var promiseLock; var promiseLock;
var p = new Promise(function(resolve, reject) { var p = new Promise(function (resolve, reject) {
if (screenObject.nativeLock) { if (screenObject.nativeLock) {
promiseLock = screenObject.nativeLock(orientation); promiseLock = screenObject.nativeLock(orientation);
promiseLock.then(function success(res) { promiseLock.then(
resolve(); function success (_) {
}, function error(err) { resolve();
screenObject.nativeLock = null; },
resolveOrientation(orientation, resolve, reject); function error (_) {
}); screenObject.nativeLock = null;
resolveOrientation(orientation, resolve, reject);
}
);
} else { } else {
resolveOrientation(orientation, resolve, reject); resolveOrientation(orientation, resolve, reject);
} }
}); });
return p; return p;
}; };
screenObject.unlock = function() { screenObject.unlock = function () {
screenOrientation.setOrientation('any'); screenOrientation.setOrientation('any');
}; };
} }
function resolveOrientation(orientation, resolve, reject) { function resolveOrientation (orientation, resolve, reject) {
if (!OrientationLockType.hasOwnProperty(orientation)) { if (!Object.prototype.hasOwnProperty.call(OrientationLockType, 'orientation')) {
var err = new Error(); var err = new Error();
err.name = "NotSupportedError"; err.name = 'NotSupportedError';
reject(err); //"cannot change orientation"); reject(err); // "cannot change orientation");
} else { } else {
screenOrientation.setOrientation(orientation); screenOrientation.setOrientation(orientation);
resolve("Orientation set"); // orientation change successful resolve('Orientation set'); // orientation change successful
} }
} }
addScreenOrientationApi(screen.orientation); addScreenOrientationApi(screen.orientation);
@ -95,8 +99,7 @@ addScreenOrientationApi(screen.orientation);
var onChangeListener = null; var onChangeListener = null;
Object.defineProperty(screen.orientation, 'onchange', { Object.defineProperty(screen.orientation, 'onchange', {
set: function(listener) { set: function (listener) {
if (onChangeListener) { if (onChangeListener) {
screen.orientation.removeEventListener('change', onChangeListener); screen.orientation.removeEventListener('change', onChangeListener);
} }
@ -105,50 +108,48 @@ Object.defineProperty(screen.orientation, 'onchange', {
screen.orientation.addEventListener('change', onChangeListener); screen.orientation.addEventListener('change', onChangeListener);
} }
}, },
get: function() { get: function () {
return (onChangeListener ? onChangeListener : null); return onChangeListener || null;
}, },
enumerable: true, enumerable: true
}); });
var evtTarget = new XMLHttpRequest(); // document.createElement('div');
var evtTarget = new XMLHttpRequest(); //document.createElement('div'); var orientationchange = function () {
var orientationchange = function() {
setOrientationProperties(); setOrientationProperties();
var event = document.createEvent('Events'); var event = document.createEvent('Events');
event.initEvent("change", false, false); event.initEvent('change', false, false);
evtTarget.dispatchEvent(event); evtTarget.dispatchEvent(event);
}; };
screen.orientation.addEventListener = function(a,b,c) { screen.orientation.addEventListener = function (a, b, c) {
return evtTarget.addEventListener(a,b,c); return evtTarget.addEventListener(a, b, c);
}; };
screen.orientation.removeEventListener = function(a,b,c) { screen.orientation.removeEventListener = function (a, b, c) {
return evtTarget.removeEventListener(a,b,c); return evtTarget.removeEventListener(a, b, c);
}; };
function setOrientationProperties() { function setOrientationProperties () {
switch (window.orientation) { switch (window.orientation) {
case 0: case 0:
screen.orientation.type = 'portrait-primary'; screen.orientation.type = 'portrait-primary';
break; break;
case 90: case 90:
screen.orientation.type = 'landscape-primary'; screen.orientation.type = 'landscape-primary';
break; break;
case 180: case 180:
screen.orientation.type = 'portrait-secondary'; screen.orientation.type = 'portrait-secondary';
break; break;
case -90: case -90:
screen.orientation.type = 'landscape-secondary'; screen.orientation.type = 'landscape-secondary';
break; break;
default: default:
screen.orientation.type = 'portrait-primary'; screen.orientation.type = 'portrait-primary';
break; break;
} }
screen.orientation.angle = window.orientation || 0; screen.orientation.angle = window.orientation || 0;
} }
window.addEventListener("orientationchange", orientationchange, true); window.addEventListener('orientationchange', orientationchange, true);
module.exports = screenOrientation; module.exports = screenOrientation;