Skip to content

Commit

Permalink
Address latest round of comments
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Feb 10, 2019
1 parent 84bad1c commit 4f49e41
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 55 deletions.
99 changes: 50 additions & 49 deletions src/core/core.helpers.js
Expand Up @@ -6,6 +6,56 @@ var color = require('chartjs-color');
var defaults = require('./core.defaults');
var helpers = require('../helpers/index');

// Private helper function to convert max-width/max-height values that may be percentages into a number
function parseMaxStyle(styleValue, node, parentProperty) {
var valueInPixels;
if (typeof styleValue === 'string') {
valueInPixels = parseInt(styleValue, 10);

if (styleValue.indexOf('%') !== -1) {
// percentage * size in dimension
valueInPixels = valueInPixels / 100 * node.parentNode[parentProperty];
}
} else {
valueInPixels = styleValue;
}

return valueInPixels;
}

/**
* Returns if the given value contains an effective constraint.
* @private
*/
function isConstrainedValue(value) {
return value !== undefined && value !== null && value !== 'none';
}

/**
* Returns the max width or height of the given DOM node in a cross-browser compatible fashion
* @param {HTMLElement} domNode - the node to check the constraint on
* @param {string} maxStyle - the style that defines the maximum for the direction we are using ('max-width' / 'max-height')
* @param {string} percentageProperty - property of parent to use when calculating width as a percentage
* @see {@link https://www.nathanaeljones.com/blog/2013/reading-max-width-cross-browser}
*/
function getConstraintDimension(domNode, maxStyle, percentageProperty) {
var view = document.defaultView;
var parentNode = helpers._getParentNode(domNode);
var constrainedNode = view.getComputedStyle(domNode)[maxStyle];
var constrainedContainer = view.getComputedStyle(parentNode)[maxStyle];
var hasCNode = isConstrainedValue(constrainedNode);
var hasCContainer = isConstrainedValue(constrainedContainer);
var infinity = Number.POSITIVE_INFINITY;

if (hasCNode || hasCContainer) {
return Math.min(
hasCNode ? parseMaxStyle(constrainedNode, domNode, percentageProperty) : infinity,
hasCContainer ? parseMaxStyle(constrainedContainer, parentNode, percentageProperty) : infinity);
}

return 'none';
}

module.exports = function() {

// -- Basic js utility methods
Expand Down Expand Up @@ -405,55 +455,6 @@ module.exports = function() {

};

// Private helper function to convert max-width/max-height values that may be percentages into a number
function parseMaxStyle(styleValue, node, parentProperty) {
var valueInPixels;
if (typeof styleValue === 'string') {
valueInPixels = parseInt(styleValue, 10);

if (styleValue.indexOf('%') !== -1) {
// percentage * size in dimension
valueInPixels = valueInPixels / 100 * node.parentNode[parentProperty];
}
} else {
valueInPixels = styleValue;
}

return valueInPixels;
}

/**
* Returns if the given value contains an effective constraint.
* @private
*/
function isConstrainedValue(value) {
return value !== undefined && value !== null && value !== 'none';
}

/**
* Private helper to get a constraint dimension
* @param domNode - the node to check the constraint on
* @param maxStyle - the style that defines the maximum for the direction we are using (maxWidth / maxHeight)
* @param percentageProperty - property of parent to use when calculating width as a percentage
* @see https://www.nathanaeljones.com/blog/2013/reading-max-width-cross-browser
*/
function getConstraintDimension(domNode, maxStyle, percentageProperty) {
var view = document.defaultView;
var parentNode = helpers._getParentNode(domNode);
var constrainedNode = view.getComputedStyle(domNode)[maxStyle];
var constrainedContainer = view.getComputedStyle(parentNode)[maxStyle];
var hasCNode = isConstrainedValue(constrainedNode);
var hasCContainer = isConstrainedValue(constrainedContainer);
var infinity = Number.POSITIVE_INFINITY;

if (hasCNode || hasCContainer) {
return Math.min(
hasCNode ? parseMaxStyle(constrainedNode, domNode, percentageProperty) : infinity,
hasCContainer ? parseMaxStyle(constrainedContainer, parentNode, percentageProperty) : infinity);
}

return 'none';
}
// returns Number or undefined if no constraint
helpers.getConstraintWidth = function(domNode) {
return getConstraintDimension(domNode, 'max-width', 'clientWidth');
Expand Down
6 changes: 3 additions & 3 deletions src/core/core.interaction.js
Expand Up @@ -6,7 +6,7 @@ var helpers = require('../helpers/index');
* Helper function to get relative position for an event
* @param {Event|IEvent} event - The event to get the position for
* @param {Chart} chart - The chart
* @returns {Point} the event position
* @returns {object} the event position
*/
function getRelativePosition(e, chart) {
if (e.native) {
Expand Down Expand Up @@ -46,7 +46,7 @@ function parseVisibleItems(chart, handler) {
/**
* Helper function to get the items that intersect the event position
* @param {ChartElement[]} items - elements to filter
* @param {Point} position - the point to be nearest to
* @param {object} position - the point to be nearest to
* @return {ChartElement[]} the nearest items
*/
function getIntersectItems(chart, position) {
Expand All @@ -64,7 +64,7 @@ function getIntersectItems(chart, position) {
/**
* Helper function to get the items nearest to the event position considering all visible items in teh chart
* @param {Chart} chart - the chart to look at elements from
* @param {Point} position - the point to be nearest to
* @param {object} position - the point to be nearest to
* @param {boolean} intersect - if true, only consider items that intersect the position
* @param {function} distanceMetric - function to provide the distance between points
* @return {ChartElement[]} the nearest items
Expand Down
6 changes: 3 additions & 3 deletions src/core/core.tooltip.js
Expand Up @@ -107,7 +107,7 @@ var positioners = {
* Average mode places the tooltip at the average position of the elements shown
* @function Chart.Tooltip.positioners.average
* @param elements {ChartElement[]} the elements being displayed in the tooltip
* @returns {Point} tooltip position
* @returns {object} tooltip position
*/
average: function(elements) {
if (!elements.length) {
Expand Down Expand Up @@ -139,8 +139,8 @@ var positioners = {
* Gets the tooltip position nearest of the item nearest to the event position
* @function Chart.Tooltip.positioners.nearest
* @param elements {Chart.Element[]} the tooltip elements
* @param eventPosition {Point} the position of the event in canvas coordinates
* @returns {Point} the tooltip position
* @param eventPosition {object} the position of the event in canvas coordinates
* @returns {object} the tooltip position
*/
nearest: function(elements, eventPosition) {
var x = eventPosition.x;
Expand Down

0 comments on commit 4f49e41

Please sign in to comment.