]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix some CC issues (#7796)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Mon, 14 Sep 2020 18:40:19 +0000 (21:40 +0300)
committerGitHub <noreply@github.com>
Mon, 14 Sep 2020 18:40:19 +0000 (14:40 -0400)
* Fix some CC issues
* Merge _lookup, _lookupByKey, _rlookupByKey
* Remove TODO from types
* Merge TRBL logics
* Merge getMaximumWidth and getMaximumHeight

src/elements/element.rectangle.js
src/helpers/helpers.collection.js
src/helpers/helpers.dom.js
src/helpers/helpers.options.js
types/elements/index.d.ts

index 2363a87ffdeda811d439e8a0081173cfe070b1ad..990a28c20b63ea5380764809b73170f0c7edcafe 100644 (file)
@@ -1,5 +1,5 @@
 import Element from '../core/core.element';
-import {isObject} from '../helpers/helpers.core';
+import {toTRBL} from '../helpers/helpers.options';
 
 /**
  * Helper function to get the bounds of the bar regardless of the orientation
@@ -71,22 +71,13 @@ function skipOrLimit(skip, value, min, max) {
 function parseBorderWidth(bar, maxW, maxH) {
        const value = bar.options.borderWidth;
        const skip = parseBorderSkipped(bar);
-       let t, r, b, l;
-
-       if (isObject(value)) {
-               t = +value.top || 0;
-               r = +value.right || 0;
-               b = +value.bottom || 0;
-               l = +value.left || 0;
-       } else {
-               t = r = b = l = +value || 0;
-       }
+       const o = toTRBL(value);
 
        return {
-               t: skipOrLimit(skip.top, t, 0, maxH),
-               r: skipOrLimit(skip.right, r, 0, maxW),
-               b: skipOrLimit(skip.bottom, b, 0, maxH),
-               l: skipOrLimit(skip.left, l, 0, maxW)
+               t: skipOrLimit(skip.top, o.top, 0, maxH),
+               r: skipOrLimit(skip.right, o.right, 0, maxW),
+               b: skipOrLimit(skip.bottom, o.bottom, 0, maxH),
+               l: skipOrLimit(skip.left, o.left, 0, maxW)
        };
 }
 
@@ -115,7 +106,8 @@ function boundingRects(bar) {
 function inRange(bar, x, y, useFinalPosition) {
        const skipX = x === null;
        const skipY = y === null;
-       const bounds = !bar || (skipX && skipY) ? false : getBarBounds(bar, useFinalPosition);
+       const skipBoth = skipX && skipY;
+       const bounds = bar && !skipBoth && getBarBounds(bar, useFinalPosition);
 
        return bounds
                && (skipX || x >= bounds.left && x <= bounds.right)
index 4cea116ce5f232f6e7441a29f00e1bd4c33efcf5..d973464d2dd9a43168274eb99bafd871db803588 100644 (file)
@@ -4,16 +4,18 @@ import {_capitalize} from './helpers.core';
  * Binary search
  * @param {array} table - the table search. must be sorted!
  * @param {number} value - value to find
+ * @param {function} [cmp]
  * @private
  */
-export function _lookup(table, value) {
+export function _lookup(table, value, cmp) {
+       cmp = cmp || ((index) => table[index] < value);
        let hi = table.length - 1;
        let lo = 0;
        let mid;
 
        while (hi - lo > 1) {
                mid = (lo + hi) >> 1;
-               if (table[mid] < value) {
+               if (cmp(mid)) {
                        lo = mid;
                } else {
                        hi = mid;
@@ -30,22 +32,8 @@ export function _lookup(table, value) {
  * @param {number} value - value to find
  * @private
  */
-export function _lookupByKey(table, key, value) {
-       let hi = table.length - 1;
-       let lo = 0;
-       let mid;
-
-       while (hi - lo > 1) {
-               mid = (lo + hi) >> 1;
-               if (table[mid][key] < value) {
-                       lo = mid;
-               } else {
-                       hi = mid;
-               }
-       }
-
-       return {lo, hi};
-}
+export const _lookupByKey = (table, key, value) =>
+       _lookup(table, value, index => table[index][key] < value);
 
 /**
  * Reverse binary search
@@ -54,22 +42,8 @@ export function _lookupByKey(table, key, value) {
  * @param {number} value - value to find
  * @private
  */
-export function _rlookupByKey(table, key, value) {
-       let hi = table.length - 1;
-       let lo = 0;
-       let mid;
-
-       while (hi - lo > 1) {
-               mid = (lo + hi) >> 1;
-               if (table[mid][key] < value) {
-                       hi = mid;
-               } else {
-                       lo = mid;
-               }
-       }
-
-       return {lo, hi};
-}
+export const _rlookupByKey = (table, key, value) =>
+       _lookup(table, value, index => table[index][key] >= value);
 
 /**
  * Return subset of `values` between `min` and `max` inclusive.
index 6fc70c62782968a3076eb5712b0501a106f60e37..1539af9fbe22a16e17e3538eb1fe6c2a677f3386 100644 (file)
@@ -64,16 +64,6 @@ export function getStyle(el, property) {
                document.defaultView.getComputedStyle(el, null).getPropertyValue(property);
 }
 
-/** @return {number=} number or undefined if no constraint */
-function getConstraintWidth(domNode) {
-       return getConstraintDimension(domNode, 'max-width', 'clientWidth');
-}
-
-/** @return {number=} number or undefined if no constraint */
-function getConstraintHeight(domNode) {
-       return getConstraintDimension(domNode, 'max-height', 'clientHeight');
-}
-
 /**
  * @private
  */
@@ -133,35 +123,22 @@ function fallbackIfNotValid(measure, fallback) {
        return typeof measure === 'number' ? measure : fallback;
 }
 
-export function getMaximumWidth(domNode) {
+function getMax(domNode, prop, fallback, paddings) {
        const container = _getParentNode(domNode);
        if (!container) {
-               return fallbackIfNotValid(domNode.clientWidth, domNode.width);
+               return fallbackIfNotValid(domNode[prop], domNode[fallback]);
        }
 
-       const clientWidth = container.clientWidth;
-       const paddingLeft = _calculatePadding(container, 'padding-left', clientWidth);
-       const paddingRight = _calculatePadding(container, 'padding-right', clientWidth);
+       const value = container[prop];
+       const padding = paddings.reduce((acc, cur) => acc + _calculatePadding(container, 'padding-' + cur, value), 0);
 
-       const w = clientWidth - paddingLeft - paddingRight;
-       const cw = getConstraintWidth(domNode);
-       return isNaN(cw) ? w : Math.min(w, cw);
+       const v = value - padding;
+       const cv = getConstraintDimension(domNode, 'max-' + fallback, prop);
+       return isNaN(cv) ? v : Math.min(v, cv);
 }
 
-export function getMaximumHeight(domNode) {
-       const container = _getParentNode(domNode);
-       if (!container) {
-               return fallbackIfNotValid(domNode.clientHeight, domNode.height);
-       }
-
-       const clientHeight = container.clientHeight;
-       const paddingTop = _calculatePadding(container, 'padding-top', clientHeight);
-       const paddingBottom = _calculatePadding(container, 'padding-bottom', clientHeight);
-
-       const h = clientHeight - paddingTop - paddingBottom;
-       const ch = getConstraintHeight(domNode);
-       return isNaN(ch) ? h : Math.min(h, ch);
-}
+export const getMaximumWidth = (domNode) => getMax(domNode, 'clientWidth', 'width', ['left', 'right']);
+export const getMaximumHeight = (domNode) => getMax(domNode, 'clientHeight', 'height', ['top', 'bottom']);
 
 export function retinaScale(chart, forceRatio) {
        const pixelRatio = chart.currentDevicePixelRatio = forceRatio || (typeof window !== 'undefined' && window.devicePixelRatio) || 1;
index 540f7c250431679406f05ad402053d25fe111cde..ba61f5e5249f831ae97087f1f73fecd1788b22bd 100644 (file)
@@ -35,35 +35,51 @@ export function toLineHeight(value, size) {
        return size * value;
 }
 
+const numberOrZero = v => +v || 0;
+
 /**
- * Converts the given value into a padding object with pre-computed width/height.
+ * Converts the given value into a TRBL object.
  * @param {number|object} value - If a number, set the value to all TRBL component,
  *  else, if an object, use defined properties and sets undefined ones to 0.
- * @returns {object} The padding values (top, right, bottom, left, width, height)
- * @since 2.7.0
+ * @returns {object} The padding values (top, right, bottom, left)
+ * @since 3.0.0
  */
-export function toPadding(value) {
+export function toTRBL(value) {
        let t, r, b, l;
 
        if (isObject(value)) {
-               t = +value.top || 0;
-               r = +value.right || 0;
-               b = +value.bottom || 0;
-               l = +value.left || 0;
+               t = numberOrZero(value.top);
+               r = numberOrZero(value.right);
+               b = numberOrZero(value.bottom);
+               l = numberOrZero(value.left);
        } else {
-               t = r = b = l = +value || 0;
+               t = r = b = l = numberOrZero(value);
        }
 
        return {
                top: t,
                right: r,
                bottom: b,
-               left: l,
-               height: t + b,
-               width: l + r
+               left: l
        };
 }
 
+/**
+ * Converts the given value into a padding object with pre-computed width/height.
+ * @param {number|object} value - If a number, set the value to all TRBL component,
+ *  else, if an object, use defined properties and sets undefined ones to 0.
+ * @returns {object} The padding values (top, right, bottom, left, width, height)
+ * @since 2.7.0
+ */
+export function toPadding(value) {
+       const obj = toTRBL(value);
+
+       obj.width = obj.left + obj.right;
+       obj.height = obj.top + obj.bottom;
+
+       return obj;
+}
+
 /**
  * Parses font options and returns the font object.
  * @param {object} options - A object that contains font options to be parsed.
index 680ff7f83bc49695c2659b6121b314c1e3f03732..087de648d5798e612b0684ad4d207915bd401098 100644 (file)
@@ -91,7 +91,7 @@ export interface ILineOptions extends ICommonOptions {
   cubicInterpolationMode: 'default' | 'monotone';
   /**
    * Bézier curve tension (0 for no Bézier curves).
-   * @default 0.4 or 0 // TODO
+   * @default 0
    */
   tension: number;
   /**