Skip to content
Permalink
Browse files
CB-10636 Add JSHint for plugins
  • Loading branch information
daserge committed Feb 25, 2016
1 parent 104fb81 commit 367aefb72dae9eff053856f61dad100ec3932f72
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 42 deletions.
@@ -12,7 +12,7 @@ Thumbs.db
*.swp
*.user


node_modules



@@ -0,0 +1,17 @@
{
"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
}
}
@@ -0,0 +1,4 @@
language: node_js
sudo: false
node_js:
- "4.2"
@@ -17,6 +17,8 @@
# under the License.
-->

[![Build Status](https://travis-ci.org/apache/cordova-plugin-battery-status.svg?branch=master)](https://travis-ci.org/apache/cordova-plugin-battery-status)

# cordova-plugin-battery-status


@@ -34,6 +34,13 @@
"cordova-tizen",
"cordova-firefoxos"
],
"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"
},
"author": "Apache Software Foundation",
"license": "Apache-2.0"
"license": "Apache-2.0",
"devDependencies": {
"jshint": "^2.6.0"
}
}
@@ -19,8 +19,10 @@
*
*/

var _clientListeners = {},
_webkitBattery = navigator.webkitBattery || navigator.battery;
/* global PluginResult */

var _clientListeners = {},
_webkitBattery = navigator.webkitBattery || navigator.battery;

module.exports = {
start: function (success, fail, args, env) {
@@ -35,8 +37,8 @@ module.exports = {

//put data from webkitBattery into a format cordova expects
//webkitBattery seems to return level as a decimal pre 10.2
resultInfo.level = info.level <= 1 ? info.level * 100 : info.level,
resultInfo.isPlugged = info.charging
resultInfo.level = info.level <= 1 ? info.level * 100 : info.level;
resultInfo.isPlugged = info.charging;
}

result.callbackOk(resultInfo, true);
@@ -42,7 +42,7 @@ var Battery = {
w3cBattery.onlevelchange = success;
w3cBattery.onchargingchange = success;
}
}
};

if (typeof navigator.getBattery === 'function') {
navigator.getBattery().then(function(battery) {
@@ -59,7 +59,7 @@ var Battery = {
}
} catch(e) {
fail(e);
};
}
},

stop: function() {
@@ -73,7 +73,7 @@ var Battery = {
}
} catch(e) {
console.warn('Error occured while trying to stop battery: ' + JSON.stringify(e));
};
}
}
};

@@ -19,7 +19,7 @@
*
*/

var cordova = require('cordova');
/* global tizen */

var batteryListenerId = null;

@@ -19,6 +19,8 @@
*
*/

/* global WinJS, BatteryStatus */

var stopped;

function handleResponse(successCb, errorCb, jsonResponse) {
@@ -34,6 +36,24 @@ function handleResponse(successCb, errorCb, jsonResponse) {

var Battery = {
start: function (win, fail, args, env) {
function getBatteryStatus(success, error) {
handleResponse(success, error, BatteryStatus.BatteryStatus.start());
}

function getBatteryStatusLevelChangeEvent(success, error) {
return BatteryStatus.BatteryStatus.getBatteryStatusChangeEvent().done(function (result) {
if (stopped) {
return;
}

handleResponse(success, error, result);

setTimeout(function() { getBatteryStatusLevelChangeEvent(success, error); }, 0);
}, function(err) {
fail(err);
});
}

// Battery API supported on Phone devices only so in case of
// desktop/tablet the only one choice is to fail with appropriate message.
if (!WinJS.Utilities.isPhone) {
@@ -43,30 +63,11 @@ var Battery = {

stopped = false;
try {
function getBatteryStatus(success, error) {
handleResponse(success, error, BatteryStatus.BatteryStatus.start());
}

function getBatteryStatusLevelChangeEvent(success, error) {
return BatteryStatus.BatteryStatus.getBatteryStatusChangeEvent().done(function (result) {
if (stopped) {
return;
}

handleResponse(success, error, result);

setTimeout(function() { getBatteryStatusLevelChangeEvent(success, error); }, 0);
}, function(err) {
fail(err);
});
}

getBatteryStatus(win, fail);

getBatteryStatusLevelChangeEvent(win, fail);
} catch(e) {
fail(e);
}
}
},

stop: function () {
@@ -19,6 +19,9 @@
*
*/

/* jshint jasmine: true */
/* global WinJS */

exports.defineAutoTests = function () {
var isWindowsStore = (cordova.platformId == "windows8") || (cordova.platformId == "windows" && !WinJS.Utilities.isPhone),
onEvent;
@@ -44,7 +47,7 @@ exports.defineAutoTests = function () {
window.removeEventListener("batterystatus", onEvent, false);
}
catch (e) {
console.err('Error removing batterystatus event listener: ' + e)
console.err('Error removing batterystatus event listener: ' + e);
}
}
});
@@ -80,7 +83,7 @@ exports.defineAutoTests = function () {
window.removeEventListener("batterylow", onEvent, false);
}
catch (e) {
console.err('Error removing batterylow event listener: ' + e)
console.err('Error removing batterylow event listener: ' + e);
}
}
});
@@ -199,7 +202,7 @@ exports.defineAutoTests = function () {
window.removeEventListener("batterycritical", onEvent, false);
}
catch (e) {
console.err('Error removing batterycritical event listener: ' + e)
console.err('Error removing batterycritical event listener: ' + e);
}
}
});
@@ -26,12 +26,6 @@
var cordova = require('cordova'),
exec = require('cordova/exec');

function handlers() {
return battery.channels.batterystatus.numHandlers +
battery.channels.batterylow.numHandlers +
battery.channels.batterycritical.numHandlers;
}

var STATUS_CRITICAL = 5;
var STATUS_LOW = 20;

@@ -48,6 +42,13 @@ var Battery = function() {
this.channels[key].onHasSubscribersChange = Battery.onHasSubscribersChange;
}
};

function handlers() {
return battery.channels.batterystatus.numHandlers +
battery.channels.batterylow.numHandlers +
battery.channels.batterycritical.numHandlers;
}

/**
* Event handlers for when callbacks get registered for the battery.
* Keep track of how many handlers we have so we can start and stop the native battery listener
@@ -72,7 +73,7 @@ Battery.prototype._status = function (info) {
if (info) {
if (battery._level !== info.level || battery._isPlugged !== info.isPlugged) {

if(info.level == null && battery._level != null) {
if(info.level === null && battery._level !== null) {
return; // special case where callback is called because we stopped listening to the native side.
}

@@ -104,7 +105,6 @@ Battery.prototype._error = function(e) {
console.log("Error initializing Battery: " + e);
};

var battery = new Battery();
var battery = new Battery(); // jshint ignore:line

module.exports = battery;

0 comments on commit 367aefb

Please sign in to comment.