diff --git a/src/core/core.helpers.js b/src/core/core.helpers.js index a79aeaadfab..44a722285c4 100644 --- a/src/core/core.helpers.js +++ b/src/core/core.helpers.js @@ -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 @@ -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'); diff --git a/src/core/core.interaction.js b/src/core/core.interaction.js index 47f9c4aa3aa..e163f1182a3 100644 --- a/src/core/core.interaction.js +++ b/src/core/core.interaction.js @@ -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) { @@ -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) { @@ -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 diff --git a/src/core/core.tooltip.js b/src/core/core.tooltip.js index 6366cdb230a..f85055b2e6c 100644 --- a/src/core/core.tooltip.js +++ b/src/core/core.tooltip.js @@ -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) { @@ -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;