UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

274 lines (271 loc) • 9.04 kB
/** * DevExtreme (esm/__internal/viz/chart_components/scroll_bar.js) * Version: 26.1.6 * Build date: Fri Oct 16 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import { noop } from "../../core/utils/m_common"; import { extend } from "../../core/utils/m_extend"; import { isDefined } from "../../core/utils/m_type"; import eventsEngine from "../../events/core/events_engine"; import { end as dragEventEnd, move as dragEventMove, start as dragEventStart } from "../../events/drag"; import { fireEvent } from "../../events/utils/index"; import { Translator2D } from "../translators/translator2d"; const _min = Math.min; const _max = Math.max; const MIN_SCROLL_BAR_SIZE = 10; function _getXCoord(canvas, pos, offset, width) { let x = 0; if ("right" === pos) { x = canvas.width - canvas.right + offset } else if ("left" === pos) { x = canvas.left - offset - width } return x } function _getYCoord(canvas, pos, offset, width) { let y = 0; if ("top" === pos) { y = canvas.top - offset } else if ("bottom" === pos) { y = canvas.height - canvas.bottom + width + offset } return y } export let ScrollBar = class { constructor(renderer, group) { this._translator = new Translator2D({}, {}, {}); this._scroll = renderer.rect().append(group); this._addEvents() } _addEvents() { const scrollElement = this._scroll.element; eventsEngine.on(scrollElement, dragEventStart, e => { this._dragStartOffset = this._offset; fireEvent({ type: "dxc-scroll-start", originalEvent: e, target: scrollElement }) }); eventsEngine.on(scrollElement, dragEventMove, e => { const position = this._getDragPosition(e); this._applyPosition(position, position + this._thumbLength); fireEvent(this._getDragEvent("dxc-scroll-move", e, scrollElement, position)) }); eventsEngine.on(scrollElement, dragEventEnd, e => { fireEvent(this._getDragEvent("dxc-scroll-end", e, scrollElement, this._getDragPosition(e))) }) } _getDragPosition(e) { const offset = this._layoutOptions.vertical ? e.offset.y : e.offset.x; return (this._dragStartOffset ?? this._offset) + offset } _getDragEvent(type, e, target, position) { return { type: type, originalEvent: e, target: target, offset: { x: -e.offset.x * this._scale, y: -e.offset.y * this._scale }, scrollRange: this._getRangeAtPosition(position) } } _getBoundaryDirection() { return this._translateWithOffset || (this._hasBreaks ? 1 : 0) } _getRangeAtPosition(position) { const translator = this._translator; const length = this._thumbLength; if (!isFinite(position) || !isFinite(length)) { return } const visibleArea = translator.getCanvasVisibleArea(); const lastPosition = _max(visibleArea.max - length, visibleArea.min); const start = _min(_max(position, visibleArea.min), lastPosition); const direction = this._getBoundaryDirection(); const from = translator.from(start, -direction); const to = translator.from(start + length, direction); return translator.isInverted() ? { startValue: to, endValue: from } : { startValue: from, endValue: to } } update(options) { let position = options.position; const isVertical = options.rotated; const defaultPosition = isVertical ? "right" : "top"; const secondaryPosition = isVertical ? "left" : "bottom"; if (position !== defaultPosition && position !== secondaryPosition) { position = defaultPosition } this._scroll.attr({ rotate: !options.rotated ? -90 : 0, rotateX: 0, rotateY: 0, fill: options.color, width: options.width, opacity: options.opacity }); this._layoutOptions = { width: options.width, offset: options.offset, vertical: isVertical, position: position }; return this } init(range, stick, wholeRangeBreaks) { const isDiscrete = "discrete" === range.axisType; this._translateWithOffset = isDiscrete && !stick ? 1 : 0; this._hasBreaks = !!(null !== wholeRangeBreaks && void 0 !== wholeRangeBreaks && wholeRangeBreaks.length); this._translator.update(extend({}, range, { minVisible: null, maxVisible: null, visibleCategories: null, breaks: null !== wholeRangeBreaks && void 0 !== wholeRangeBreaks && wholeRangeBreaks.length ? wholeRangeBreaks : null, userBreaks: null }, isDiscrete && { min: null, max: null } || {}), this._canvas, { isHorizontal: !this._layoutOptions.vertical, stick: stick, breaksSize: 0 }); return this } getOptions() { return this._layoutOptions } setPane(panes) { const position = this._layoutOptions.position; let pane; if ("left" === position || "top" === position) { pane = panes[0] } else { pane = panes[panes.length - 1] } this.pane = pane.name; return this } updateSize(canvas) { this._canvas = extend({}, canvas); const options = this._layoutOptions; const pos = options.position; const offset = options.offset; const width = options.width; this._scroll.attr({ translateX: _getXCoord(canvas, pos, offset, width), translateY: _getYCoord(canvas, pos, offset, width) }) } getMultipleAxesSpacing() { return 0 } estimateMargins() { return this.getMargins() } getMargins() { const options = this._layoutOptions; const margins = { left: 0, top: 0, right: 0, bottom: 0 }; margins[options.position] = options.width + options.offset; return margins } shift(margins) { const options = this._layoutOptions; const side = options.position; const isVertical = options.vertical; const attr = { translateX: this._scroll.attr("translateX") ?? 0, translateY: this._scroll.attr("translateY") ?? 0 }; const shift = margins[side]; attr[isVertical ? "translateX" : "translateY"] += ("left" === side || "top" === side ? -1 : 1) * shift; this._scroll.attr(attr) } setPosition(min, max) { const translator = this._translator; const direction = this._getBoundaryDirection(); const minPoint = isDefined(min) ? translator.translate(min, -direction) : translator.translate("canvas_position_start"); const maxPoint = isDefined(max) ? translator.translate(max, direction) : translator.translate("canvas_position_end"); this._offset = _min(minPoint, maxPoint); this._thumbLength = Math.abs(maxPoint - minPoint); this._scale = this._thumbLength ? translator.canvasLength / this._thumbLength : translator.getScale(min, max); this._applyPosition(_min(minPoint, maxPoint), _max(minPoint, maxPoint)) } customPositionIsAvailable() { return false } dispose() { this._scroll.dispose(); this._scroll = this._translator = null } _applyPosition(x1, x2) { const visibleArea = this._translator.getCanvasVisibleArea(); const min = visibleArea.min; const max = visibleArea.max; if (max <= min) { return } if (x1 > x2) { [x1, x2] = [x2, x1] } x1 = Math.max(x1, min); x2 = Math.min(x2, max); if (x2 - x1 < 10) { if (max - min < 10) { x1 = min; x2 = max } else { const center = (x1 + x2) / 2; x1 = center - 5; x2 = center + 5; if (x1 < min) { x1 = min; x2 = min + 10 } else if (x2 > max) { x2 = max; x1 = max - 10 } } } x1 = Math.max(x1, min); x2 = Math.min(x2, max); const height = Math.max(x2 - x1, 0); this._scroll.attr({ y: x1, height: height }) } }; Object.assign(ScrollBar.prototype, { hideTitle: noop, hideOuterElements: noop });