progress_bar.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * DevExtreme (ui/progress_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 TrackBar = require("./track_bar");
  12. var extend = require("../core/utils/extend").extend;
  13. var isFunction = require("../core/utils/type").isFunction;
  14. var registerComponent = require("../core/component_registrator");
  15. var PROGRESSBAR_CLASS = "dx-progressbar";
  16. var PROGRESSBAR_CONTAINER_CLASS = "dx-progressbar-container";
  17. var PROGRESSBAR_RANGE_CONTAINER_CLASS = "dx-progressbar-range-container";
  18. var PROGRESSBAR_RANGE_CLASS = "dx-progressbar-range";
  19. var PROGRESSBAR_WRAPPER_CLASS = "dx-progressbar-wrapper";
  20. var PROGRESSBAR_STATUS_CLASS = "dx-progressbar-status";
  21. var PROGRESSBAR_INDETERMINATE_SEGMENT_CONTAINER = "dx-progressbar-animating-container";
  22. var PROGRESSBAR_INDETERMINATE_SEGMENT = "dx-progressbar-animating-segment";
  23. var ProgressBar = TrackBar.inherit({
  24. _getDefaultOptions: function() {
  25. return extend(this.callBase(), {
  26. value: 0,
  27. statusFormat: function(ratio) {
  28. return "Progress: " + Math.round(100 * ratio) + "%"
  29. },
  30. showStatus: true,
  31. onComplete: null,
  32. activeStateEnabled: false,
  33. statusPosition: "bottom left",
  34. _animatingSegmentCount: 0
  35. })
  36. },
  37. _defaultOptionsRules: function() {
  38. return this.callBase().concat([{
  39. device: {
  40. platform: "win"
  41. },
  42. options: {
  43. _animatingSegmentCount: 5
  44. }
  45. }, {
  46. device: function(_device) {
  47. return "android" === _device.platform
  48. },
  49. options: {
  50. _animatingSegmentCount: 2
  51. }
  52. }])
  53. },
  54. _initMarkup: function() {
  55. this._renderStatus();
  56. this._createCompleteAction();
  57. this.callBase();
  58. this.$element().addClass(PROGRESSBAR_CLASS);
  59. this._$wrapper.addClass(PROGRESSBAR_WRAPPER_CLASS);
  60. this._$bar.addClass(PROGRESSBAR_CONTAINER_CLASS);
  61. this.setAria("role", "progressbar");
  62. $("<div>").addClass(PROGRESSBAR_RANGE_CONTAINER_CLASS).appendTo(this._$wrapper).append(this._$bar);
  63. this._$range.addClass(PROGRESSBAR_RANGE_CLASS);
  64. this._toggleStatus(this.option("showStatus"))
  65. },
  66. _createCompleteAction: function() {
  67. this._completeAction = this._createActionByOption("onComplete")
  68. },
  69. _renderStatus: function() {
  70. this._$status = $("<div>").addClass(PROGRESSBAR_STATUS_CLASS)
  71. },
  72. _renderIndeterminateState: function() {
  73. this._$segmentContainer = $("<div>").addClass(PROGRESSBAR_INDETERMINATE_SEGMENT_CONTAINER);
  74. var segments = this.option("_animatingSegmentCount");
  75. for (var i = 0; i < segments; i++) {
  76. $("<div>").addClass(PROGRESSBAR_INDETERMINATE_SEGMENT).addClass(PROGRESSBAR_INDETERMINATE_SEGMENT + "-" + (i + 1)).appendTo(this._$segmentContainer)
  77. }
  78. this._$segmentContainer.appendTo(this._$wrapper)
  79. },
  80. _toggleStatus: function(value) {
  81. var splitPosition = this.option("statusPosition").split(" ");
  82. if (value) {
  83. if ("top" === splitPosition[0] || "left" === splitPosition[0]) {
  84. this._$status.prependTo(this._$wrapper)
  85. } else {
  86. this._$status.appendTo(this._$wrapper)
  87. }
  88. } else {
  89. this._$status.detach()
  90. }
  91. this._togglePositionClass()
  92. },
  93. _togglePositionClass: function() {
  94. var position = this.option("statusPosition");
  95. var splitPosition = position.split(" ");
  96. this._$wrapper.removeClass("dx-position-top-left dx-position-top-right dx-position-bottom-left dx-position-bottom-right dx-position-left dx-position-right");
  97. var positionClass = "dx-position-" + splitPosition[0];
  98. if (splitPosition[1]) {
  99. positionClass += "-" + splitPosition[1]
  100. }
  101. this._$wrapper.addClass(positionClass)
  102. },
  103. _toggleIndeterminateState: function(value) {
  104. if (value) {
  105. this._renderIndeterminateState();
  106. this._$bar.toggle(false)
  107. } else {
  108. this._$bar.toggle(true);
  109. this._$segmentContainer.remove();
  110. delete this._$segmentContainer
  111. }
  112. },
  113. _renderValue: function() {
  114. var val = this.option("value");
  115. var max = this.option("max");
  116. if (!val && 0 !== val) {
  117. this._toggleIndeterminateState(true);
  118. return
  119. }
  120. if (this._$segmentContainer) {
  121. this._toggleIndeterminateState(false)
  122. }
  123. if (val === max) {
  124. this._completeAction()
  125. }
  126. this.callBase();
  127. this._setStatus()
  128. },
  129. _setStatus: function() {
  130. var format = this.option("statusFormat");
  131. if (isFunction(format)) {
  132. format = format.bind(this)
  133. } else {
  134. format = function(value) {
  135. return value
  136. }
  137. }
  138. var statusText = format(this._currentRatio, this.option("value"));
  139. this._$status.text(statusText)
  140. },
  141. _dispose: function() {
  142. this._$status.remove();
  143. this.callBase()
  144. },
  145. _optionChanged: function(args) {
  146. switch (args.name) {
  147. case "statusFormat":
  148. this._setStatus();
  149. break;
  150. case "showStatus":
  151. this._toggleStatus(args.value);
  152. break;
  153. case "statusPosition":
  154. this._toggleStatus(this.option("showStatus"));
  155. break;
  156. case "onComplete":
  157. this._createCompleteAction();
  158. break;
  159. case "_animatingSegmentCount":
  160. break;
  161. default:
  162. this.callBase(args)
  163. }
  164. }
  165. });
  166. registerComponent("dxProgressBar", ProgressBar);
  167. module.exports = ProgressBar;
  168. module.exports.default = module.exports;