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
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",
"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": {

View File

@@ -19,6 +19,8 @@
*
*/
/* global Windows, WinJS */
var DisplayInfo = Windows.Graphics.Display.DisplayInformation;
var Orientations = Windows.Graphics.Display.DisplayOrientations;
@@ -34,29 +36,31 @@ module.exports = {
var prefOrients = args[0];
var winPrefs = 0;
if (prefOrients & 1) { // UIInterfaceOrientationPortrait
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);

View File

@@ -18,18 +18,15 @@
* under the License.
*
*/
/* jshint jasmine: true */
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 () {
expect(window.screen.orientation).toBeDefined();
});
@@ -75,18 +72,15 @@ exports.defineAutoTests = function() {
expect(window.screen.orientation.removeEventListener).toBeDefined();
expect(typeof window.screen.orientation.removeEventListener).toBe('function');
});
});
describe('OrientationType', function () {
it("should be defined", 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();
@@ -95,28 +89,25 @@ exports.defineAutoTests = function() {
});
describe('OrientationLockType', function () {
it("should be defined", 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 () {
expect(window.screen.orientation.type).toMatch(/^portrait-/);
@@ -126,9 +117,8 @@ exports.defineAutoTests = function() {
});
};
exports.defineManualTests = function (contentEl, createActionButton) {
createActionButton('Listen to orientationchange events', function () {
window.addEventListener("orientationchange", function(){
window.addEventListener('orientationchange', function () {
contentEl.innerHTML += '<p>Orientation changed! ' + screen.orientation.type + '</p>';
});
});

View File

@@ -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) {
orientationMask = window.OrientationLockType[orientation];
cordova.exec(null, null, "CDVOrientation", "screenOrientation", [orientationMask, orientation]);
cordova.exec(null, null, 'CDVOrientation', 'screenOrientation', [orientationMask, orientation]);
};
if (!screen.orientation) {
@@ -51,7 +54,6 @@ if (!screen.orientation) {
setOrientationProperties();
function addScreenOrientationApi (screenObject) {
if (screenObject.unlock || screenObject.lock) {
screenObject.nativeLock = screenObject.lock;
}
@@ -61,12 +63,15 @@ function addScreenOrientationApi(screenObject) {
var p = new Promise(function (resolve, reject) {
if (screenObject.nativeLock) {
promiseLock = screenObject.nativeLock(orientation);
promiseLock.then(function success(res) {
promiseLock.then(
function success (_) {
resolve();
}, function error(err) {
},
function error (_) {
screenObject.nativeLock = null;
resolveOrientation(orientation, resolve, reject);
});
}
);
} else {
resolveOrientation(orientation, resolve, reject);
}
@@ -79,15 +84,14 @@ function addScreenOrientationApi(screenObject) {
}
function resolveOrientation (orientation, resolve, reject) {
if (!OrientationLockType.hasOwnProperty(orientation)) {
if (!Object.prototype.hasOwnProperty.call(OrientationLockType, 'orientation')) {
var err = new Error();
err.name = "NotSupportedError";
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);
@@ -96,7 +100,6 @@ var onChangeListener = null;
Object.defineProperty(screen.orientation, 'onchange', {
set: function (listener) {
if (onChangeListener) {
screen.orientation.removeEventListener('change', onChangeListener);
}
@@ -106,17 +109,16 @@ Object.defineProperty(screen.orientation, 'onchange', {
}
},
get: function () {
return (onChangeListener ? onChangeListener : null);
return onChangeListener || null;
},
enumerable: true,
enumerable: true
});
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);
};
@@ -147,8 +149,7 @@ function setOrientationProperties() {
break;
}
screen.orientation.angle = window.orientation || 0;
}
window.addEventListener("orientationchange", orientationchange, true);
window.addEventListener('orientationchange', orientationchange, true);
module.exports = screenOrientation;