track_bar.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * DevExtreme (ui/track_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 $ = require("../core/renderer");
  11. var Editor = require("./editor/editor");
  12. var registerComponent = require("../core/component_registrator");
  13. var extend = require("../core/utils/extend").extend;
  14. var windowUtils = require("../core/utils/window");
  15. var fx = require("../animation/fx");
  16. var TRACKBAR_CLASS = "dx-trackbar";
  17. var TRACKBAR_CONTAINER_CLASS = "dx-trackbar-container";
  18. var TRACKBAR_RANGE_CLASS = "dx-trackbar-range";
  19. var TRACKBAR_WRAPPER_CLASS = "dx-trackbar-wrapper";
  20. var TrackBar = Editor.inherit({
  21. _getDefaultOptions: function() {
  22. return extend(this.callBase(), {
  23. min: 0,
  24. max: 100,
  25. value: 0
  26. })
  27. },
  28. _initMarkup: function() {
  29. this.$element().addClass(TRACKBAR_CLASS);
  30. this._renderWrapper();
  31. this._renderContainer();
  32. this._renderRange();
  33. this._renderValue();
  34. this._setRangeStyles();
  35. this.callBase()
  36. },
  37. _render: function() {
  38. this.callBase();
  39. this._setRangeStyles(this._rangeStylesConfig())
  40. },
  41. _renderWrapper: function() {
  42. this._$wrapper = $("<div>").addClass(TRACKBAR_WRAPPER_CLASS).appendTo(this.$element())
  43. },
  44. _renderContainer: function() {
  45. this._$bar = $("<div>").addClass(TRACKBAR_CONTAINER_CLASS).appendTo(this._$wrapper)
  46. },
  47. _renderRange: function() {
  48. this._$range = $("<div>").addClass(TRACKBAR_RANGE_CLASS).appendTo(this._$bar)
  49. },
  50. _renderValue: function() {
  51. var val = this.option("value");
  52. var min = this.option("min");
  53. var max = this.option("max");
  54. if (min > max) {
  55. return
  56. }
  57. if (val < min) {
  58. this.option("value", min);
  59. this._currentRatio = 0;
  60. return
  61. }
  62. if (val > max) {
  63. this.option("value", max);
  64. this._currentRatio = 1;
  65. return
  66. }
  67. var ratio = min === max ? 0 : (val - min) / (max - min);
  68. !this._needPreventAnimation && this._setRangeStyles({
  69. width: 100 * ratio + "%"
  70. });
  71. this.setAria({
  72. valuemin: this.option("min"),
  73. valuemax: max,
  74. valuenow: val
  75. });
  76. this._currentRatio = ratio
  77. },
  78. _rangeStylesConfig: function() {
  79. return {
  80. width: 100 * this._currentRatio + "%"
  81. }
  82. },
  83. _setRangeStyles: function(options) {
  84. fx.stop(this._$range);
  85. if (!options) {
  86. this._$range.css({
  87. width: 0
  88. });
  89. return
  90. }
  91. if (this._needPreventAnimation || !windowUtils.hasWindow()) {
  92. return
  93. }
  94. fx.animate(this._$range, {
  95. type: "custom",
  96. duration: 100,
  97. to: options
  98. })
  99. },
  100. _optionChanged: function(args) {
  101. switch (args.name) {
  102. case "value":
  103. this._renderValue();
  104. this.callBase(args);
  105. break;
  106. case "max":
  107. case "min":
  108. this._renderValue();
  109. break;
  110. default:
  111. this.callBase(args)
  112. }
  113. },
  114. _dispose: function() {
  115. fx.stop(this._$range);
  116. this.callBase()
  117. }
  118. });
  119. registerComponent("dxTrackBar", TrackBar);
  120. module.exports = TrackBar;