refactor(eslint): use cordova-eslint /w fix (#76)
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/.jshintrc b/.jshintrc
deleted file mode 100644
index df32482..0000000
--- a/.jshintrc
+++ /dev/null
@@ -1,17 +0,0 @@
-{
- "browser": true
- , "devel": true
- , "bitwise": true
- , "undef": true
- , "trailing": true
- , "quotmark": false
- , "indent": 4
- , "unused": "vars"
- , "latedef": "nofunc"
- , "globals": {
- "module": false,
- "exports": false,
- "require": false,
- "cordova": true
- }
-}
diff --git a/package.json b/package.json
index 0394d41..e475fd4 100644
--- a/package.json
+++ b/package.json
@@ -25,8 +25,8 @@
"cordova-browser"
],
"scripts": {
- "test": "npm run jshint",
- "jshint": "node node_modules/jshint/bin/jshint www && node node_modules/jshint/bin/jshint src && node node_modules/jshint/bin/jshint tests"
+ "test": "npm run lint",
+ "lint": "eslint ."
},
"author": "Apache Software Foundation",
"license": "Apache-2.0",
@@ -38,6 +38,6 @@
}
},
"devDependencies": {
- "jshint": "^2.6.0"
+ "@cordova/eslint-config": "^3.0.0"
}
}
diff --git a/src/browser/AccelerometerProxy.js b/src/browser/AccelerometerProxy.js
index 838ae54..9260852 100644
--- a/src/browser/AccelerometerProxy.js
+++ b/src/browser/AccelerometerProxy.js
@@ -17,15 +17,14 @@
* specific language governing permissions and limitations
* under the License.
*
-*/
+ */
-
-function listener(success) {
+function listener (success) {
var accel = {};
- accel.x = (Math.round(((Math.random() * 2) - 1) * 100) / 100);
- accel.y = (Math.round(((Math.random() * 2) - 1) * 100) / 100);
- accel.z = (Math.round(((Math.random() * 2) - 1) * 100) / 100);
+ accel.x = Math.round((Math.random() * 2 - 1) * 100) / 100;
+ accel.y = Math.round((Math.random() * 2 - 1) * 100) / 100;
+ accel.z = Math.round((Math.random() * 2 - 1) * 100) / 100;
accel.timestamp = new Date().getTime();
success(accel);
@@ -34,10 +33,14 @@
}
var Accelerometer = {
- start: function start(success, error) {
- return window.addEventListener('devicemotion', function(){
- listener(success);
- }, false);
+ start: function start (success, error) {
+ return window.addEventListener(
+ 'devicemotion',
+ function () {
+ listener(success);
+ },
+ false
+ );
}
};
diff --git a/src/windows/AccelerometerProxy.js b/src/windows/AccelerometerProxy.js
index aa07780..c98d64d 100644
--- a/src/windows/AccelerometerProxy.js
+++ b/src/windows/AccelerometerProxy.js
@@ -17,52 +17,53 @@
* specific language governing permissions and limitations
* under the License.
*
-*/
+ */
-/*global Windows:true */
+/* global Windows */
var Acceleration = require('cordova-plugin-device-motion.Acceleration');
/* This is the actual implementation part that returns the result on Windows 8
-*/
+ */
var gConstant = -9.81;
module.exports = {
- onDataChanged:null,
- start:function(win,lose){
-
+ onDataChanged: null,
+ start: function (win, lose) {
var accel = Windows.Devices.Sensors.Accelerometer.getDefault();
- if(!accel) {
+ if (!accel) {
if (lose) {
- lose("No accelerometer found");
+ lose('No accelerometer found');
}
- }
- else {
- accel.reportInterval = Math.max(16,accel.minimumReportInterval);
+ } else {
+ accel.reportInterval = Math.max(16, accel.minimumReportInterval);
// store our bound function
- this.onDataChanged = function(e) {
+ this.onDataChanged = function (e) {
var a = e.reading;
- win(new Acceleration(a.accelerationX * gConstant, a.accelerationY * gConstant, a.accelerationZ * gConstant), {keepCallback: true});
+ win(new Acceleration(a.accelerationX * gConstant, a.accelerationY * gConstant, a.accelerationZ * gConstant), {
+ keepCallback: true
+ });
};
- accel.addEventListener("readingchanged",this.onDataChanged);
+ accel.addEventListener('readingchanged', this.onDataChanged);
- setTimeout(function(){
+ setTimeout(function () {
var a = accel.getCurrentReading();
- win(new Acceleration(a.accelerationX * gConstant, a.accelerationY * gConstant, a.accelerationZ * gConstant), {keepCallback: true});
- },0); // async do later
+ win(new Acceleration(a.accelerationX * gConstant, a.accelerationY * gConstant, a.accelerationZ * gConstant), {
+ keepCallback: true
+ });
+ }, 0); // async do later
}
},
- stop:function(win,lose){
- win = win || function(){};
+ stop: function (win, lose) {
+ win = win || function () {};
var accel = Windows.Devices.Sensors.Accelerometer.getDefault();
- if(!accel) {
+ if (!accel) {
if (lose) {
- lose("No accelerometer found");
+ lose('No accelerometer found');
}
- }
- else {
- accel.removeEventListener("readingchanged",this.onDataChanged);
+ } else {
+ accel.removeEventListener('readingchanged', this.onDataChanged);
this.onDataChanged = null;
accel.reportInterval = 0; // back to the default
win();
@@ -70,4 +71,4 @@
}
};
-require("cordova/exec/proxy").add("Accelerometer",module.exports);
+require('cordova/exec/proxy').add('Accelerometer', module.exports);
diff --git a/tests/tests.js b/tests/tests.js
index aa2baa2..9a4745a 100644
--- a/tests/tests.js
+++ b/tests/tests.js
@@ -1,4 +1,4 @@
-/*
+/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -17,236 +17,241 @@
* specific language governing permissions and limitations
* under the License.
*
-*/
+ */
-/* jshint jasmine: true */
-/* global Windows */
+/* global cordova, Windows */
exports.defineAutoTests = function () {
- var isWindows = (cordova.platformId === "windows"),
- // Checking existence of accelerometer for windows platform
- // Assumed that accelerometer always exists on other platforms. Extend
- // condition to support accelerometer check on other platforms
- isAccelExist = isWindows ? Windows.Devices.Sensors.Accelerometer.getDefault() !== null : true;
+ var isWindows = cordova.platformId === 'windows';
+ // Checking existence of accelerometer for windows platform
+ // Assumed that accelerometer always exists on other platforms. Extend
+ // condition to support accelerometer check on other platforms
+ var isAccelExist = isWindows ? Windows.Devices.Sensors.Accelerometer.getDefault() !== null : true;
- describe('Accelerometer (navigator.accelerometer)', function () {
- var fail = function(done) {
- expect(true).toBe(false);
- done();
- };
-
- // This timeout is here to lessen the load on native accelerometer
- // intensive use of which can lead to occasional test failures
- afterEach(function(done) {
- setTimeout(function() {
- done();
- }, 100);
- });
-
- it("accelerometer.spec.1 should exist", function () {
- expect(navigator.accelerometer).toBeDefined();
- });
-
- describe("getCurrentAcceleration", function() {
- it("accelerometer.spec.2 should exist", function() {
- expect(typeof navigator.accelerometer.getCurrentAcceleration).toBeDefined();
- expect(typeof navigator.accelerometer.getCurrentAcceleration == 'function').toBe(true);
- });
-
- it("accelerometer.spec.3 success callback should be called with an Acceleration object", function(done) {
- // skip the test if Accelerometer doesn't exist on this device
- if (!isAccelExist) {
- pending();
- }
- var win = function(a) {
- expect(a).toBeDefined();
- expect(a.x).toBeDefined();
- expect(typeof a.x == 'number').toBe(true);
- expect(a.y).toBeDefined();
- expect(typeof a.y == 'number').toBe(true);
- expect(a.z).toBeDefined();
- expect(typeof a.z == 'number').toBe(true);
- expect(a.timestamp).toBeDefined();
- expect(typeof a.timestamp).toBe('number');
- done();
- };
-
- var onError = function(err){
- console.log(err);
- console.log("Skipping gyroscope tests, marking all as pending.");
- isAccelExist = false;
- expect(true).toBe(true);
+ describe('Accelerometer (navigator.accelerometer)', function () {
+ var fail = function (done) {
+ expect(true).toBe(false);
done();
- };
-
- navigator.accelerometer.getCurrentAcceleration(win, onError);
- });
-
- it("accelerometer.spec.4 success callback Acceleration object should have (reasonable) values for x, y and z expressed in m/s^2", function(done) {
- // skip the test if Accelerometer doesn't exist on this device
- if (!isAccelExist) {
- pending();
- }
- var reasonableThreshold = 15;
- var win = function(a) {
- expect(a.x).toBeLessThan(reasonableThreshold);
- expect(a.x).toBeGreaterThan(reasonableThreshold * -1);
- expect(a.y).toBeLessThan(reasonableThreshold);
- expect(a.y).toBeGreaterThan(reasonableThreshold * -1);
- expect(a.z).toBeLessThan(reasonableThreshold);
- expect(a.z).toBeGreaterThan(reasonableThreshold * -1);
- done();
};
- navigator.accelerometer.getCurrentAcceleration(win, fail.bind(null,done));
- });
-
- it("accelerometer.spec.5 success callback Acceleration object should return a recent timestamp", function(done) {
- // skip the test if Accelerometer doesn't exist on this device
- if (!isAccelExist) {
- pending();
- }
- var veryRecently = (new Date()).getTime();
- // Need to check that dates returned are not vastly greater than a recent time stamp.
- // In case the timestamps returned are ridiculously high
- var reasonableTimeLimit = veryRecently + 5000; // 5 seconds from now
- var win = function(a) {
- expect(a.timestamp).toBeGreaterThan(veryRecently - 200); // this is flakey, relaxing a bit
- expect(a.timestamp).toBeLessThan(reasonableTimeLimit);
- done();
- };
-
- navigator.accelerometer.getCurrentAcceleration(win, fail.bind(null,done));
- });
- });
-
- describe("watchAcceleration", function() {
- var id;
-
- afterEach(function(done) {
- if (id) {
- navigator.accelerometer.clearWatch(id);
- }
- // clearWatch implementation is async but doesn't accept a cllback
- // so let's give it some time before starting next spec
- setTimeout(done, 100);
- });
-
- it("accelerometer.spec.6 should exist", function() {
- expect(navigator.accelerometer.watchAcceleration).toBeDefined();
- expect(typeof navigator.accelerometer.watchAcceleration == 'function').toBe(true);
- });
-
- it("accelerometer.spec.7 success callback should be called with an Acceleration object", function(done) {
- // skip the test if Accelerometer doesn't exist on this device
- if (!isAccelExist) {
- pending();
- }
- var win = function(a) {
- expect(a).toBeDefined();
- expect(a.x).toBeDefined();
- expect(typeof a.x == 'number').toBe(true);
- expect(a.y).toBeDefined();
- expect(typeof a.y == 'number').toBe(true);
- expect(a.z).toBeDefined();
- expect(typeof a.z == 'number').toBe(true);
- expect(a.timestamp).toBeDefined();
- expect(typeof a.timestamp).toBe('number');
- done();
- };
-
- id = navigator.accelerometer.watchAcceleration(win, fail.bind(null,done), {frequency:100});
- });
-
- it("accelerometer.spec.8 success callback Acceleration object should have (reasonable) values for x, y and z expressed in m/s^2", function(done) {
- // skip the test if Accelerometer doesn't exist on this device
- if (!isAccelExist) {
- pending();
- }
- var reasonableThreshold = 15;
- var win = function(a) {
- expect(a.x).toBeLessThan(reasonableThreshold);
- expect(a.x).toBeGreaterThan(reasonableThreshold * -1);
- expect(a.y).toBeLessThan(reasonableThreshold);
- expect(a.y).toBeGreaterThan(reasonableThreshold * -1);
- expect(a.z).toBeLessThan(reasonableThreshold);
- expect(a.z).toBeGreaterThan(reasonableThreshold * -1);
- done();
- };
-
- id = navigator.accelerometer.watchAcceleration(win, fail.bind(null,done), {frequency:100});
- });
-
- it("accelerometer.spec.9 success callback Acceleration object should return a recent timestamp", function(done) {
- // skip the test if Accelerometer doesn't exist on this device
- if (!isAccelExist) {
- pending();
- }
- var veryRecently = (new Date()).getTime();
- // Need to check that dates returned are not vastly greater than a recent time stamp.
- // In case the timestamps returned are ridiculously high
- var reasonableTimeLimit = veryRecently + 5000; // 5 seconds from now
- var win = function(a) {
- expect(a.timestamp).toBeGreaterThan(veryRecently - 200); // this is flakey, relaxing a bit
- expect(a.timestamp).toBeLessThan(reasonableTimeLimit);
- done();
- };
-
- id = navigator.accelerometer.watchAcceleration(win, fail.bind(null,done), {frequency:100});
- });
-
- it("accelerometer.spec.12 success callback should be preserved and called several times", function (done) {
- // skip the test if Accelerometer doesn't exist on this device
- if (!isAccelExist) {
- pending();
- }
- var callbacksCallCount = 0,
- callbacksCallTestCount = 3;
-
- var win = function (a) {
- if (callbacksCallCount++ < callbacksCallTestCount) return;
- expect(typeof a).toBe('object');
+ // This timeout is here to lessen the load on native accelerometer
+ // intensive use of which can lead to occasional test failures
+ afterEach(function (done) {
+ setTimeout(function () {
done();
- };
+ }, 100);
+ });
- id = navigator.accelerometer.watchAcceleration(win, fail.bind(null, done), { frequency: 100 });
+ it('accelerometer.spec.1 should exist', function () {
+ expect(navigator.accelerometer).toBeDefined();
+ });
+
+ describe('getCurrentAcceleration', function () {
+ it('accelerometer.spec.2 should exist', function () {
+ expect(typeof navigator.accelerometer.getCurrentAcceleration).toBeDefined();
+ expect(typeof navigator.accelerometer.getCurrentAcceleration === 'function').toBe(true);
+ });
+
+ it('accelerometer.spec.3 success callback should be called with an Acceleration object', function (done) {
+ // skip the test if Accelerometer doesn't exist on this device
+ if (!isAccelExist) {
+ pending();
+ }
+ var win = function (a) {
+ expect(a).toBeDefined();
+ expect(a.x).toBeDefined();
+ expect(typeof a.x === 'number').toBe(true);
+ expect(a.y).toBeDefined();
+ expect(typeof a.y === 'number').toBe(true);
+ expect(a.z).toBeDefined();
+ expect(typeof a.z === 'number').toBe(true);
+ expect(a.timestamp).toBeDefined();
+ expect(typeof a.timestamp).toBe('number');
+ done();
+ };
+
+ var onError = function (err) {
+ console.log(err);
+ console.log('Skipping gyroscope tests, marking all as pending.');
+ isAccelExist = false;
+ expect(true).toBe(true);
+ done();
+ };
+
+ navigator.accelerometer.getCurrentAcceleration(win, onError);
+ });
+
+ it('accelerometer.spec.4 success callback Acceleration object should have (reasonable) values for x, y and z expressed in m/s^2', function (done) {
+ // skip the test if Accelerometer doesn't exist on this device
+ if (!isAccelExist) {
+ pending();
+ }
+ var reasonableThreshold = 15;
+ var win = function (a) {
+ expect(a.x).toBeLessThan(reasonableThreshold);
+ expect(a.x).toBeGreaterThan(reasonableThreshold * -1);
+ expect(a.y).toBeLessThan(reasonableThreshold);
+ expect(a.y).toBeGreaterThan(reasonableThreshold * -1);
+ expect(a.z).toBeLessThan(reasonableThreshold);
+ expect(a.z).toBeGreaterThan(reasonableThreshold * -1);
+ done();
+ };
+
+ navigator.accelerometer.getCurrentAcceleration(win, fail.bind(null, done));
+ });
+
+ it('accelerometer.spec.5 success callback Acceleration object should return a recent timestamp', function (done) {
+ // skip the test if Accelerometer doesn't exist on this device
+ if (!isAccelExist) {
+ pending();
+ }
+ var veryRecently = new Date().getTime();
+ // Need to check that dates returned are not vastly greater than a recent time stamp.
+ // In case the timestamps returned are ridiculously high
+ var reasonableTimeLimit = veryRecently + 5000; // 5 seconds from now
+ var win = function (a) {
+ expect(a.timestamp).toBeGreaterThan(veryRecently - 200); // this is flakey, relaxing a bit
+ expect(a.timestamp).toBeLessThan(reasonableTimeLimit);
+ done();
+ };
+
+ navigator.accelerometer.getCurrentAcceleration(win, fail.bind(null, done));
+ });
+ });
+
+ describe('watchAcceleration', function () {
+ var id;
+
+ afterEach(function (done) {
+ if (id) {
+ navigator.accelerometer.clearWatch(id);
+ }
+ // clearWatch implementation is async but doesn't accept a cllback
+ // so let's give it some time before starting next spec
+ setTimeout(done, 100);
+ });
+
+ it('accelerometer.spec.6 should exist', function () {
+ expect(navigator.accelerometer.watchAcceleration).toBeDefined();
+ expect(typeof navigator.accelerometer.watchAcceleration === 'function').toBe(true);
+ });
+
+ it('accelerometer.spec.7 success callback should be called with an Acceleration object', function (done) {
+ // skip the test if Accelerometer doesn't exist on this device
+ if (!isAccelExist) {
+ pending();
+ }
+ var win = function (a) {
+ expect(a).toBeDefined();
+ expect(a.x).toBeDefined();
+ expect(typeof a.x === 'number').toBe(true);
+ expect(a.y).toBeDefined();
+ expect(typeof a.y === 'number').toBe(true);
+ expect(a.z).toBeDefined();
+ expect(typeof a.z === 'number').toBe(true);
+ expect(a.timestamp).toBeDefined();
+ expect(typeof a.timestamp).toBe('number');
+ done();
+ };
+
+ id = navigator.accelerometer.watchAcceleration(win, fail.bind(null, done), { frequency: 100 });
+ });
+
+ it('accelerometer.spec.8 success callback Acceleration object should have (reasonable) values for x, y and z expressed in m/s^2', function (done) {
+ // skip the test if Accelerometer doesn't exist on this device
+ if (!isAccelExist) {
+ pending();
+ }
+ var reasonableThreshold = 15;
+ var win = function (a) {
+ expect(a.x).toBeLessThan(reasonableThreshold);
+ expect(a.x).toBeGreaterThan(reasonableThreshold * -1);
+ expect(a.y).toBeLessThan(reasonableThreshold);
+ expect(a.y).toBeGreaterThan(reasonableThreshold * -1);
+ expect(a.z).toBeLessThan(reasonableThreshold);
+ expect(a.z).toBeGreaterThan(reasonableThreshold * -1);
+ done();
+ };
+
+ id = navigator.accelerometer.watchAcceleration(win, fail.bind(null, done), { frequency: 100 });
+ });
+
+ it('accelerometer.spec.9 success callback Acceleration object should return a recent timestamp', function (done) {
+ // skip the test if Accelerometer doesn't exist on this device
+ if (!isAccelExist) {
+ pending();
+ }
+ var veryRecently = new Date().getTime();
+ // Need to check that dates returned are not vastly greater than a recent time stamp.
+ // In case the timestamps returned are ridiculously high
+ var reasonableTimeLimit = veryRecently + 5000; // 5 seconds from now
+ var win = function (a) {
+ expect(a.timestamp).toBeGreaterThan(veryRecently - 200); // this is flakey, relaxing a bit
+ expect(a.timestamp).toBeLessThan(reasonableTimeLimit);
+ done();
+ };
+
+ id = navigator.accelerometer.watchAcceleration(win, fail.bind(null, done), { frequency: 100 });
+ });
+
+ it('accelerometer.spec.12 success callback should be preserved and called several times', function (done) {
+ // skip the test if Accelerometer doesn't exist on this device
+ if (!isAccelExist) {
+ pending();
+ }
+ var callbacksCallCount = 0;
+ var callbacksCallTestCount = 3;
+
+ var win = function (a) {
+ if (callbacksCallCount++ < callbacksCallTestCount) return;
+ expect(typeof a).toBe('object');
+ done();
+ };
+
+ id = navigator.accelerometer.watchAcceleration(win, fail.bind(null, done), { frequency: 100 });
+ });
+ });
+
+ describe('clearWatch', function () {
+ it('accelerometer.spec.10 should exist', function () {
+ expect(navigator.accelerometer.clearWatch).toBeDefined();
+ expect(typeof navigator.accelerometer.clearWatch === 'function').toBe(true);
+ });
+
+ it('accelerometer.spec.11 should clear an existing watch', function (done) {
+ // skip the test if Accelerometer doesn't exist on this device
+ if (!isAccelExist) {
+ pending();
+ }
+ var id;
+
+ // expect win to get called exactly once
+ var win = function (a) {
+ // clear watch on first call
+ navigator.accelerometer.clearWatch(id);
+ // if win isn't called again in 201 ms we assume success
+ var tid = setTimeout(function () {
+ expect(true).toBe(true);
+ done();
+ }, 101);
+ // if win is called again, clear the timeout and fail the test
+ win = function () {
+ clearTimeout(tid);
+ fail(done);
+ };
+ };
+
+ // wrap the success call in a closure since the value of win changes between calls
+ id = navigator.accelerometer.watchAcceleration(
+ function () {
+ win();
+ },
+ fail.bind(null, done),
+ { frequency: 100 }
+ );
+ });
});
});
-
- describe("clearWatch", function() {
- it("accelerometer.spec.10 should exist", function() {
- expect(navigator.accelerometer.clearWatch).toBeDefined();
- expect(typeof navigator.accelerometer.clearWatch == 'function').toBe(true);
- });
-
- it("accelerometer.spec.11 should clear an existing watch", function(done) {
- // skip the test if Accelerometer doesn't exist on this device
- if (!isAccelExist) {
- pending();
- }
- var id;
-
- // expect win to get called exactly once
- var win = function(a) {
- // clear watch on first call
- navigator.accelerometer.clearWatch(id);
- // if win isn't called again in 201 ms we assume success
- var tid = setTimeout(function() {
- expect(true).toBe(true);
- done();
- }, 101);
- // if win is called again, clear the timeout and fail the test
- win = function() {
- clearTimeout(tid);
- fail(done);
- };
- };
-
- // wrap the success call in a closure since the value of win changes between calls
- id = navigator.accelerometer.watchAcceleration(function() { win(); }, fail.bind(null, done), {frequency:100});
- });
- });
- });
};
/******************************************************************************/
@@ -254,7 +259,7 @@
/******************************************************************************/
exports.defineManualTests = function (contentEl, createActionButton) {
- function roundNumber(num) {
+ function roundNumber (num) {
var dec = 3;
var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
return result;
@@ -265,16 +270,16 @@
/**
* Set accelerometer status
*/
- function setAccelStatus(status) {
+ function setAccelStatus (status) {
document.getElementById('accel_status').innerHTML = status;
}
/**
* Stop watching the acceleration
*/
- function stopAccel() {
- console.log("stopAccel()");
- setAccelStatus("Stopped");
+ function stopAccel () {
+ console.log('stopAccel()');
+ setAccelStatus('Stopped');
if (watchAccelId) {
navigator.accelerometer.clearWatch(watchAccelId);
watchAccelId = null;
@@ -285,7 +290,7 @@
* Start watching acceleration
*/
var watchAccel = function () {
- console.log("watchAccel()");
+ console.log('watchAccel()');
// Success callback
var success = function (a) {
@@ -297,7 +302,7 @@
// Fail callback
var fail = function (e) {
- console.log("watchAccel fail callback with error code " + e);
+ console.log('watchAccel fail callback with error code ' + e);
stopAccel();
setAccelStatus(e);
};
@@ -307,14 +312,14 @@
opt.frequency = 1000;
watchAccelId = navigator.accelerometer.watchAcceleration(success, fail, opt);
- setAccelStatus("Running");
+ setAccelStatus('Running');
};
/**
* Get current acceleration
*/
var getAccel = function () {
- console.log("getAccel()");
+ console.log('getAccel()');
// Stop accel if running
stopAccel();
@@ -325,12 +330,12 @@
document.getElementById('y').innerHTML = roundNumber(a.y);
document.getElementById('z').innerHTML = roundNumber(a.z);
document.getElementById('t').innerHTML = a.timestamp;
- console.log("getAccel success callback");
+ console.log('getAccel success callback');
};
// Fail callback
var fail = function (e) {
- console.log("getAccel fail callback with error code " + e);
+ console.log('getAccel fail callback with error code ' + e);
setAccelStatus(e);
};
@@ -341,14 +346,16 @@
/******************************************************************************/
- var accelerometer_tests = '<div id="getAcceleration"></div>' +
+ var accelerometer_tests =
+ '<div id="getAcceleration"></div>' +
'Expected result: Will update the status box with X, Y, and Z values when pressed. Status will read "Stopped"' +
'<p/> <div id="watchAcceleration"></div>' +
'Expected result: When pressed, will start a watch on the accelerometer and update X,Y,Z values when movement is sensed. Status will read "Running"' +
'<p/> <div id="clearAcceleration"></div>' +
'Expected result: Will clear the accelerometer watch, so X,Y,Z values will no longer be updated. Status will read "Stopped"';
- contentEl.innerHTML = '<div id="info">' +
+ contentEl.innerHTML =
+ '<div id="info">' +
'Status: <span id="accel_status">Stopped</span>' +
'<table width="100%">' +
'<tr><td width="30%">X:</td><td id="x"> </td></tr>' +
@@ -358,15 +365,27 @@
'</table></div>' +
accelerometer_tests;
- createActionButton('Get Acceleration', function () {
- getAccel();
- }, 'getAcceleration');
+ createActionButton(
+ 'Get Acceleration',
+ function () {
+ getAccel();
+ },
+ 'getAcceleration'
+ );
- createActionButton('Start Watch', function () {
- watchAccel();
- }, 'watchAcceleration');
+ createActionButton(
+ 'Start Watch',
+ function () {
+ watchAccel();
+ },
+ 'watchAcceleration'
+ );
- createActionButton('Clear Watch', function () {
- stopAccel();
- }, 'clearAcceleration');
+ createActionButton(
+ 'Clear Watch',
+ function () {
+ stopAccel();
+ },
+ 'clearAcceleration'
+ );
};
diff --git a/www/Acceleration.js b/www/Acceleration.js
index d1669b5..a7b5e2b 100644
--- a/www/Acceleration.js
+++ b/www/Acceleration.js
@@ -17,13 +17,13 @@
* specific language governing permissions and limitations
* under the License.
*
-*/
+ */
-var Acceleration = function(x, y, z, timestamp) {
+var Acceleration = function (x, y, z, timestamp) {
this.x = x;
this.y = y;
this.z = z;
- this.timestamp = timestamp || (new Date()).getTime();
+ this.timestamp = timestamp || new Date().getTime();
};
module.exports = Acceleration;
diff --git a/www/accelerometer.js b/www/accelerometer.js
index 40d320a..eba2220 100644
--- a/www/accelerometer.js
+++ b/www/accelerometer.js
@@ -17,16 +17,18 @@
* specific language governing permissions and limitations
* under the License.
*
-*/
+ */
+
+/* global cordova */
/**
* This class provides access to device accelerometer data.
* @constructor
*/
-var argscheck = require('cordova/argscheck'),
- utils = require("cordova/utils"),
- exec = require("cordova/exec"),
- Acceleration = require('./Acceleration');
+var argscheck = require('cordova/argscheck');
+var utils = require('cordova/utils');
+var exec = require('cordova/exec');
+var Acceleration = require('./Acceleration');
// Is the accel sensor running?
var running = false;
@@ -44,36 +46,42 @@
var eventTimerId = null;
// Tells native to start.
-function start() {
- exec(function (a) {
- var tempListeners = listeners.slice(0);
- accel = new Acceleration(a.x, a.y, a.z, a.timestamp);
- for (var i = 0, l = tempListeners.length; i < l; i++) {
- tempListeners[i].win(accel);
- }
- }, function (e) {
- var tempListeners = listeners.slice(0);
- for (var i = 0, l = tempListeners.length; i < l; i++) {
- tempListeners[i].fail(e);
- }
- }, "Accelerometer", "start", []);
+function start () {
+ exec(
+ function (a) {
+ var tempListeners = listeners.slice(0);
+ accel = new Acceleration(a.x, a.y, a.z, a.timestamp);
+ for (var i = 0, l = tempListeners.length; i < l; i++) {
+ tempListeners[i].win(accel);
+ }
+ },
+ function (e) {
+ var tempListeners = listeners.slice(0);
+ for (var i = 0, l = tempListeners.length; i < l; i++) {
+ tempListeners[i].fail(e);
+ }
+ },
+ 'Accelerometer',
+ 'start',
+ []
+ );
running = true;
}
// Tells native to stop.
-function stop() {
- exec(null, null, "Accelerometer", "stop", []);
+function stop () {
+ exec(null, null, 'Accelerometer', 'stop', []);
accel = null;
running = false;
}
// Adds a callback pair to the listeners array
-function createCallbackPair(win, fail) {
+function createCallbackPair (win, fail) {
return { win: win, fail: fail };
}
// Removes a win/fail listener pair from the listeners array
-function removeListeners(l) {
+function removeListeners (l) {
var idx = listeners.indexOf(l);
if (idx > -1) {
listeners.splice(idx, 1);
@@ -94,21 +102,27 @@
getCurrentAcceleration: function (successCallback, errorCallback, options) {
argscheck.checkArgs('fFO', 'accelerometer.getCurrentAcceleration', arguments);
- if (cordova.platformId === "windowsphone") {
- exec(function (a) {
- accel = new Acceleration(a.x, a.y, a.z, a.timestamp);
- successCallback(accel);
- }, function (e) {
- errorCallback(e);
- }, "Accelerometer", "getCurrentAcceleration", []);
+ if (cordova.platformId === 'windowsphone') {
+ exec(
+ function (a) {
+ accel = new Acceleration(a.x, a.y, a.z, a.timestamp);
+ successCallback(accel);
+ },
+ function (e) {
+ errorCallback(e);
+ },
+ 'Accelerometer',
+ 'getCurrentAcceleration',
+ []
+ );
return;
}
- if (cordova.platformId === "browser" && !eventTimerId) {
+ if (cordova.platformId === 'browser' && !eventTimerId) {
// fire devicemotion event once
var devicemotionEvent = new Event('devicemotion');
- window.setTimeout(function() {
+ window.setTimeout(function () {
window.dispatchEvent(devicemotionEvent);
}, 200);
}
@@ -144,17 +158,20 @@
watchAcceleration: function (successCallback, errorCallback, options) {
argscheck.checkArgs('fFO', 'accelerometer.watchAcceleration', arguments);
// Default interval (10 sec)
- var frequency = (options && options.frequency && typeof options.frequency == 'number') ? options.frequency : 10000;
+ var frequency = options && options.frequency && typeof options.frequency === 'number' ? options.frequency : 10000;
// Keep reference to watch id, and report accel readings as often as defined in frequency
var id = utils.createUUID();
- var p = createCallbackPair(function () { }, function (e) {
- removeListeners(p);
- if (errorCallback) {
- errorCallback(e);
+ var p = createCallbackPair(
+ function () {},
+ function (e) {
+ removeListeners(p);
+ if (errorCallback) {
+ errorCallback(e);
+ }
}
- });
+ );
listeners.push(p);
timers[id] = {
@@ -176,10 +193,10 @@
start();
}
- if (cordova.platformId === "browser" && !eventTimerId) {
+ if (cordova.platformId === 'browser' && !eventTimerId) {
// Start firing devicemotion events if we haven't already
var devicemotionEvent = new Event('devicemotion');
- eventTimerId = window.setInterval(function() {
+ eventTimerId = window.setInterval(function () {
window.dispatchEvent(devicemotionEvent);
}, 200);
}