Skip to content

Commit

Permalink
ESLint fixes (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored and simonbrunel committed Jan 30, 2018
1 parent 77f047d commit 1643f3c
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 126 deletions.
6 changes: 4 additions & 2 deletions src/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ module.exports = function(Chart) {

function setAfterDataLimitsHook(axisOptions) {
helpers.decorate(axisOptions, 'afterDataLimits', function(previous, scale) {
if (previous) previous(scale);
if (previous) {
previous(scale);
}
helpers.adjustScaleRange(scale);
});
}
Expand Down Expand Up @@ -66,7 +68,7 @@ module.exports = function(Chart) {
// Add new elements, or update existing ones
ns.options.annotations.forEach(function(annotation) {
var id = annotation.id || helpers.objectId();

// No element with that ID exists, and it's a valid annotation type
if (!ns.elements[id] && annotationTypes[annotation.type]) {
var cls = annotationTypes[annotation.type];
Expand Down
2 changes: 1 addition & 1 deletion src/element.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = function(Chart) {
var chartHelpers = Chart.helpers;

var AnnotationElement = Chart.Element.extend({
initialize: function() {
this.hidden = false;
Expand Down
40 changes: 20 additions & 20 deletions src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ module.exports = function(Chart) {
var hover = false;
var filteredEvents = events.filter(function(eventName) {
switch (eventName) {
case 'mouseenter':
case 'mouseover':
case 'mouseout':
case 'mouseleave':
hover = true;
return false;
case 'mouseenter':
case 'mouseover':
case 'mouseout':
case 'mouseleave':
hover = true;
return false;

default:
return true;
default:
return true;
}
});
if (hover && filteredEvents.indexOf('mousemove') === -1) {
Expand All @@ -39,24 +39,24 @@ module.exports = function(Chart) {
if (element && !element.hovering) {
// hover started
['mouseenter', 'mouseover'].forEach(function(eventName) {
var eventHandlerName = helpers.getEventHandlerName(eventName);
var handlerName = helpers.getEventHandlerName(eventName);
var hoverEvent = helpers.createMouseEvent(eventName, e); // recreate the event to match the handler
element.hovering = true;
if (typeof options[eventHandlerName] === 'function') {
eventHandlers.push([ options[eventHandlerName], hoverEvent, element ]);
if (typeof options[handlerName] === 'function') {
eventHandlers.push([options[handlerName], hoverEvent, element]);
}
});
} else if (!element) {
// hover ended
elements.forEach(function(element) {
if (element.hovering) {
element.hovering = false;
var options = element.options;
elements.forEach(function(el) {
if (el.hovering) {
el.hovering = false;
var opt = el.options;
['mouseout', 'mouseleave'].forEach(function(eventName) {
var eventHandlerName = helpers.getEventHandlerName(eventName);
var handlerName = helpers.getEventHandlerName(eventName);
var hoverEvent = helpers.createMouseEvent(eventName, e); // recreate the event to match the handler
if (typeof options[eventHandlerName] === 'function') {
eventHandlers.push([ options[eventHandlerName], hoverEvent, element ]);
if (typeof opt[handlerName] === 'function') {
eventHandlers.push([opt[handlerName], hoverEvent, el]);
}
});
}
Expand All @@ -69,7 +69,7 @@ module.exports = function(Chart) {
//
// 1: wait dblClickSpeed ms, then fire click
// 2: cancel (1) if it is waiting then wait dblClickSpeed ms then fire click, else fire click immediately
// 3: cancel (1) or (2) if waiting, then fire dblclick
// 3: cancel (1) or (2) if waiting, then fire dblclick
if (element && events.indexOf('dblclick') > -1 && typeof options.onDblclick === 'function') {
if (e.type === 'click' && typeof options.onClick === 'function') {
clearTimeout(element.clickTimeout);
Expand All @@ -88,7 +88,7 @@ module.exports = function(Chart) {

// Dispatch the event to the usual handler, but only if we haven't substituted it
if (element && typeof options[eventHandlerName] === 'function' && eventHandlers.length === 0) {
eventHandlers.push([ options[eventHandlerName], e, element ]);
eventHandlers.push([options[eventHandlerName], e, element]);
}

if (eventHandlers.length > 0) {
Expand Down
16 changes: 8 additions & 8 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ function noop() {}

function elements(chartInstance) {
// Turn the elements object into an array of elements
var elements = chartInstance.annotation.elements;
return Object.keys(elements).map(function(id) {
return elements[id];
var els = chartInstance.annotation.elements;
return Object.keys(els).map(function(id) {
return els[id];
});
}

Expand All @@ -17,9 +17,8 @@ function isValid(rawValue) {
return false;
} else if (typeof rawValue === 'number') {
return isFinite(rawValue);
} else {
return !!rawValue;
}
return !!rawValue;
}

function decorate(obj, prop, func) {
Expand All @@ -28,12 +27,12 @@ function decorate(obj, prop, func) {
if (obj[prop]) {
obj[prefix + prop] = obj[prop].bind(obj);
obj[prop] = function() {
var args = [ obj[prefix + prop] ].concat(Array.prototype.slice.call(arguments));
var args = [obj[prefix + prop]].concat(Array.prototype.slice.call(arguments));
return func.apply(obj, args);
};
} else {
obj[prop] = function() {
var args = [ undefined ].concat(Array.prototype.slice.call(arguments));
var args = [undefined].concat(Array.prototype.slice.call(arguments));
return func.apply(obj, args);
};
}
Expand Down Expand Up @@ -162,7 +161,8 @@ module.exports = function(Chart) {
.sort(function(a, b) {
// If there are multiple elements equally close,
// sort them by size, then by index
var sizeA = a.getArea(), sizeB = b.getArea();
var sizeA = a.getArea();
var sizeB = b.getArea();
return (sizeA > sizeB || sizeA < sizeB) ? sizeA - sizeB : a._index - b._index;
})
.slice(0, 1)[0]; // return only the top item
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Get the chart variable
var Chart = require('chart.js');
Chart = typeof(Chart) === 'function' ? Chart : window.Chart;
Chart = typeof Chart === 'function' ? Chart : window.Chart;

// Configure plugin namespace
Chart.Annotation = Chart.Annotation || {};
Expand Down
26 changes: 13 additions & 13 deletions src/types/box.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Box Annotation implementation
module.exports = function(Chart) {
var helpers = require('../helpers.js')(Chart);

var BoxAnnotation = Chart.Annotation.Element.extend({
setDataLimits: function() {
var model = this._model;
Expand All @@ -14,14 +14,14 @@ module.exports = function(Chart) {

// Set the data range for this annotation
model.ranges = {};

if (!chartArea) {
return;
}

var min = 0;
var max = 0;

if (xScale) {
min = helpers.isValid(options.xMin) ? options.xMin : xScale.getPixelForValue(chartArea.left);
max = helpers.isValid(options.xMax) ? options.xMax : xScale.getPixelForValue(chartArea.right);
Expand Down Expand Up @@ -59,10 +59,10 @@ module.exports = function(Chart) {
y2: chartArea.bottom
};

var left = chartArea.left,
top = chartArea.top,
right = chartArea.right,
bottom = chartArea.bottom;
var left = chartArea.left;
var top = chartArea.top;
var right = chartArea.right;
var bottom = chartArea.bottom;

var min, max;

Expand Down Expand Up @@ -94,9 +94,9 @@ module.exports = function(Chart) {
inRange: function(mouseX, mouseY) {
var model = this._model;
return model &&
mouseX >= model.left &&
mouseX <= model.right &&
mouseY >= model.top &&
mouseX >= model.left &&
mouseX <= model.right &&
mouseY >= model.top &&
mouseY <= model.bottom;
},
getCenterPoint: function() {
Expand Down Expand Up @@ -133,8 +133,8 @@ module.exports = function(Chart) {
ctx.fillStyle = view.backgroundColor;

// Draw
var width = view.right - view.left,
height = view.bottom - view.top;
var width = view.right - view.left;
var height = view.bottom - view.top;
ctx.fillRect(view.left, view.top, width, height);
ctx.strokeRect(view.left, view.top, width, height);

Expand Down
Loading

0 comments on commit 1643f3c

Please sign in to comment.