scroll_bar.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * DevExtreme (viz/chart_components/scroll_bar.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var _events_engine = require("../../events/core/events_engine");
  11. var _events_engine2 = _interopRequireDefault(_events_engine);
  12. var _utils = require("../../events/utils");
  13. var _utils2 = _interopRequireDefault(_utils);
  14. var _extend = require("../../core/utils/extend");
  15. var _translator2d = require("../translators/translator2d");
  16. var _translator2d2 = _interopRequireDefault(_translator2d);
  17. var _type = require("../../core/utils/type");
  18. var _common = require("../../core/utils/common");
  19. var _drag = require("../../events/drag");
  20. var _drag2 = _interopRequireDefault(_drag);
  21. function _interopRequireDefault(obj) {
  22. return obj && obj.__esModule ? obj : {
  23. "default": obj
  24. }
  25. }
  26. var _min = Math.min;
  27. var _max = Math.max;
  28. var MIN_SCROLL_BAR_SIZE = 2;
  29. var ScrollBar = function(renderer, group) {
  30. this._translator = new _translator2d2.default.Translator2D({}, {}, {});
  31. this._scroll = renderer.rect().append(group);
  32. this._addEvents()
  33. };
  34. function _getXCoord(canvas, pos, offset, width) {
  35. var x = 0;
  36. if ("right" === pos) {
  37. x = canvas.width - canvas.right + offset
  38. } else {
  39. if ("left" === pos) {
  40. x = canvas.left - offset - width
  41. }
  42. }
  43. return x
  44. }
  45. function _getYCoord(canvas, pos, offset, width) {
  46. var y = 0;
  47. if ("top" === pos) {
  48. y = canvas.top - offset
  49. } else {
  50. if ("bottom" === pos) {
  51. y = canvas.height - canvas.bottom + width + offset
  52. }
  53. }
  54. return y
  55. }
  56. ScrollBar.prototype = {
  57. _addEvents: function() {
  58. var _this = this;
  59. var scrollElement = this._scroll.element;
  60. _events_engine2.default.on(scrollElement, _drag2.default.start, function(e) {
  61. _utils2.default.fireEvent({
  62. type: "dxc-scroll-start",
  63. originalEvent: e,
  64. target: scrollElement
  65. })
  66. });
  67. _events_engine2.default.on(scrollElement, _drag2.default.move, function(e) {
  68. var dX = -e.offset.x * _this._scale;
  69. var dY = -e.offset.y * _this._scale;
  70. var lx = _this._offset - (_this._layoutOptions.vertical ? dY : dX) / _this._scale;
  71. _this._applyPosition(lx, lx + _this._translator.canvasLength / _this._scale);
  72. _utils2.default.fireEvent({
  73. type: "dxc-scroll-move",
  74. originalEvent: e,
  75. target: scrollElement,
  76. offset: {
  77. x: dX,
  78. y: dY
  79. }
  80. })
  81. });
  82. _events_engine2.default.on(scrollElement, _drag2.default.end, function(e) {
  83. _utils2.default.fireEvent({
  84. type: "dxc-scroll-end",
  85. originalEvent: e,
  86. target: scrollElement,
  87. offset: {
  88. x: -e.offset.x * _this._scale,
  89. y: -e.offset.y * _this._scale
  90. }
  91. })
  92. })
  93. },
  94. update: function(options) {
  95. var that = this;
  96. var position = options.position;
  97. var isVertical = options.rotated;
  98. var defaultPosition = isVertical ? "right" : "top";
  99. var secondaryPosition = isVertical ? "left" : "bottom";
  100. if (position !== defaultPosition && position !== secondaryPosition) {
  101. position = defaultPosition
  102. }
  103. that._scroll.attr({
  104. rotate: !options.rotated ? -90 : 0,
  105. rotateX: 0,
  106. rotateY: 0,
  107. fill: options.color,
  108. width: options.width,
  109. opacity: options.opacity
  110. });
  111. that._layoutOptions = {
  112. width: options.width,
  113. offset: options.offset,
  114. vertical: isVertical,
  115. position: position
  116. };
  117. return that
  118. },
  119. init: function(range, stick) {
  120. var that = this;
  121. var isDiscrete = "discrete" === range.axisType;
  122. that._translateWithOffset = isDiscrete && !stick && 1 || 0;
  123. that._translator.update((0, _extend.extend)({}, range, {
  124. minVisible: null,
  125. maxVisible: null,
  126. visibleCategories: null
  127. }, isDiscrete && {
  128. min: null,
  129. max: null
  130. } || {}), that._canvas, {
  131. isHorizontal: !that._layoutOptions.vertical,
  132. stick: stick
  133. });
  134. return that
  135. },
  136. getOptions: function() {
  137. return this._layoutOptions
  138. },
  139. setPane: function(panes) {
  140. var position = this._layoutOptions.position;
  141. var pane;
  142. if ("left" === position || "top" === position) {
  143. pane = panes[0]
  144. } else {
  145. pane = panes[panes.length - 1]
  146. }
  147. this.pane = pane.name;
  148. return this
  149. },
  150. updateSize: function(canvas) {
  151. this._canvas = (0, _extend.extend)({}, canvas);
  152. var options = this._layoutOptions;
  153. var pos = options.position;
  154. var offset = options.offset;
  155. var width = options.width;
  156. this._scroll.attr({
  157. translateX: _getXCoord(canvas, pos, offset, width),
  158. translateY: _getYCoord(canvas, pos, offset, width)
  159. })
  160. },
  161. getMultipleAxesSpacing: function() {
  162. return 0
  163. },
  164. estimateMargins: function() {
  165. return this.getMargins()
  166. },
  167. getMargins: function() {
  168. var options = this._layoutOptions;
  169. var margins = {
  170. left: 0,
  171. top: 0,
  172. right: 0,
  173. bottom: 0
  174. };
  175. margins[options.position] = options.width + options.offset;
  176. return margins
  177. },
  178. draw: _common.noop,
  179. shift: _common.noop,
  180. hideTitle: _common.noop,
  181. hideOuterElements: _common.noop,
  182. prepareAnimation: _common.noop,
  183. setPosition: function(min, max) {
  184. var that = this;
  185. var translator = that._translator;
  186. var minPoint = (0, _type.isDefined)(min) ? translator.translate(min, -that._translateWithOffset) : translator.translate("canvas_position_start");
  187. var maxPoint = (0, _type.isDefined)(max) ? translator.translate(max, that._translateWithOffset) : translator.translate("canvas_position_end");
  188. that._offset = _min(minPoint, maxPoint);
  189. that._scale = translator.getScale(min, max);
  190. that._applyPosition(_min(minPoint, maxPoint), _max(minPoint, maxPoint))
  191. },
  192. dispose: function() {
  193. this._scroll.dispose();
  194. this._scroll = this._translator = null
  195. },
  196. _applyPosition: function(x1, x2) {
  197. var that = this;
  198. var visibleArea = that._translator.getCanvasVisibleArea();
  199. x1 = _max(x1, visibleArea.min);
  200. x1 = _min(x1, visibleArea.max);
  201. x2 = _min(x2, visibleArea.max);
  202. x2 = _max(x2, visibleArea.min);
  203. var height = Math.abs(x2 - x1);
  204. that._scroll.attr({
  205. y: x1,
  206. height: height < MIN_SCROLL_BAR_SIZE ? MIN_SCROLL_BAR_SIZE : height
  207. })
  208. }
  209. };
  210. exports.ScrollBar = ScrollBar;