|
| 1 | +import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js"; |
| 2 | +import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js"; |
| 3 | +import AnimationMode from "@ui5/webcomponents-base/dist/types/AnimationMode.js"; |
| 4 | +import ValueState from "@ui5/webcomponents-base/dist/types/ValueState.js"; |
| 5 | +import Integer from "@ui5/webcomponents-base/dist/types/Integer.js"; |
| 6 | +import { getAnimationMode } from "@ui5/webcomponents-base/dist/config/AnimationMode.js"; |
| 7 | +import { fetchI18nBundle, getI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js"; |
| 8 | +import ProgressIndicatorTemplate from "./generated/templates/ProgressIndicatorTemplate.lit.js"; |
| 9 | +import { |
| 10 | + VALUE_STATE_ERROR, |
| 11 | + VALUE_STATE_WARNING, |
| 12 | + VALUE_STATE_SUCCESS, |
| 13 | + VALUE_STATE_INFORMATION, |
| 14 | +} from "./generated/i18n/i18n-defaults.js"; |
| 15 | + |
| 16 | +// Styles |
| 17 | +import ProgressIndicatorCss from "./generated/themes/ProgressIndicator.css.js"; |
| 18 | + |
| 19 | +/** |
| 20 | + * @public |
| 21 | + */ |
| 22 | +const metadata = { |
| 23 | + tag: "ui5-progress-indicator", |
| 24 | + properties: /** @lends sap.ui.webcomponents.main.ProgressIndicator.prototype */ { |
| 25 | + /** |
| 26 | + * Defines whether <code>ui5-progress-indicator</code> is in disabled state. |
| 27 | + * |
| 28 | + * @type {boolean} |
| 29 | + * @defaultvalue false |
| 30 | + * @public |
| 31 | + */ |
| 32 | + disabled: { |
| 33 | + type: Boolean, |
| 34 | + }, |
| 35 | + /** |
| 36 | + * Defines whether <code>ui5-progress-indicator</code> value is shown. |
| 37 | + * |
| 38 | + * @type {boolean} |
| 39 | + * @defaultvalue false |
| 40 | + * @public |
| 41 | + */ |
| 42 | + hideValue: { |
| 43 | + type: Boolean, |
| 44 | + }, |
| 45 | + /** |
| 46 | + * Specifies the numerical value in percent for the length of the <code>ui5-progress-indicator</code>. |
| 47 | + * |
| 48 | + * <b>Note:</b> |
| 49 | + * If a value greater than 100 is provided, the percentValue is set to 100. In other cases of invalid value, percentValue is set to its default of 0. |
| 50 | + * @type {Integer} |
| 51 | + * @defaultvalue 0 |
| 52 | + * @public |
| 53 | + */ |
| 54 | + value: { |
| 55 | + type: Integer, |
| 56 | + defaultValue: 0, |
| 57 | + }, |
| 58 | + /** |
| 59 | + * Defines the value state of the <code>ui5-progress-indicator</code>. |
| 60 | + * <br><br> |
| 61 | + * Available options are: |
| 62 | + * <ul> |
| 63 | + * <li><code>None</code></li> |
| 64 | + * <li><code>Error</code></li> |
| 65 | + * <li><code>Warning</code></li> |
| 66 | + * <li><code>Success</code></li> |
| 67 | + * <li><code>Information</code></li> |
| 68 | + * </ul> |
| 69 | + * |
| 70 | + * @type {ValueState} |
| 71 | + * @defaultvalue "None" |
| 72 | + * @public |
| 73 | + */ |
| 74 | + valueState: { |
| 75 | + type: ValueState, |
| 76 | + defaultValue: ValueState.None, |
| 77 | + }, |
| 78 | + }, |
| 79 | + slots: /** @lends sap.ui.webcomponents.main.ProgressIndicator.prototype */ { |
| 80 | + // |
| 81 | + }, |
| 82 | + events: /** @lends sap.ui.webcomponents.main.ProgressIndicator.prototype */ { |
| 83 | + // |
| 84 | + }, |
| 85 | +}; |
| 86 | + |
| 87 | +/** |
| 88 | + * @class |
| 89 | + * |
| 90 | + * <h3 class="comment-api-title">Overview</h3> |
| 91 | + * Shows the progress of a process in a graphical way. To indicate the progress, |
| 92 | + * the inside of the <code>ui5-progress-indicator</code> is filled with a color. |
| 93 | + * |
| 94 | + * <h3>Responsive Behavior</h3> |
| 95 | + * You can change the size of the Rating Indicator by changing its <code>width</code> or <code>height</code> CSS properties. |
| 96 | + * <br> |
| 97 | + * Example: <code><ui5-progress-indicator style="height: 2rem; width: 6rem;"></ui5-progress-indicator></code> |
| 98 | + * |
| 99 | + * For the <code>ui5-progress-indicator</code> |
| 100 | + * <h3>ES6 Module Import</h3> |
| 101 | + * |
| 102 | + * <code>import @ui5/webcomponents/dist/ProgressIndicator.js";</code> |
| 103 | + * |
| 104 | + * @constructor |
| 105 | + * @author SAP SE |
| 106 | + * @alias sap.ui.webcomponents.main.ProgressIndicator |
| 107 | + * @extends UI5Element |
| 108 | + * @tagname ui5-progress-indicator |
| 109 | + * @public |
| 110 | + * @since 1.0.0-rc.8 |
| 111 | + */ |
| 112 | +class ProgressIndicator extends UI5Element { |
| 113 | + static get metadata() { |
| 114 | + return metadata; |
| 115 | + } |
| 116 | + |
| 117 | + static get render() { |
| 118 | + return litRender; |
| 119 | + } |
| 120 | + |
| 121 | + static get styles() { |
| 122 | + return ProgressIndicatorCss; |
| 123 | + } |
| 124 | + |
| 125 | + static get template() { |
| 126 | + return ProgressIndicatorTemplate; |
| 127 | + } |
| 128 | + |
| 129 | + constructor() { |
| 130 | + super(); |
| 131 | + |
| 132 | + this._previousValue = 0; |
| 133 | + this._transitionDuration = 0; |
| 134 | + |
| 135 | + this.i18nBundle = getI18nBundle("@ui5/webcomponents"); |
| 136 | + } |
| 137 | + |
| 138 | + onBeforeRendering() { |
| 139 | + this._transitionDuration = Math.abs(this._previousValue - this.validatedValue) * 20; |
| 140 | + this._previousValue = this.validatedValue; |
| 141 | + } |
| 142 | + |
| 143 | + valueStateTextMappings() { |
| 144 | + const i18nBundle = this.i18nBundle; |
| 145 | + |
| 146 | + return { |
| 147 | + "Error": i18nBundle.getText(VALUE_STATE_ERROR), |
| 148 | + "Warning": i18nBundle.getText(VALUE_STATE_WARNING), |
| 149 | + "Success": i18nBundle.getText(VALUE_STATE_SUCCESS), |
| 150 | + "Information": i18nBundle.getText(VALUE_STATE_INFORMATION), |
| 151 | + }; |
| 152 | + } |
| 153 | + |
| 154 | + valueStateIconMappings() { |
| 155 | + return { |
| 156 | + "Error": "status-negative", |
| 157 | + "Warning": "status-critical", |
| 158 | + "Success": "status-positive", |
| 159 | + "Information": "hint", |
| 160 | + }; |
| 161 | + } |
| 162 | + |
| 163 | + get styles() { |
| 164 | + return { |
| 165 | + bar: { |
| 166 | + "width": `${this.validatedValue}%`, |
| 167 | + "transition-duration": this.shouldAnimate ? `${this._transitionDuration}ms` : "none", |
| 168 | + }, |
| 169 | + }; |
| 170 | + } |
| 171 | + |
| 172 | + get classes() { |
| 173 | + return { |
| 174 | + root: { |
| 175 | + "ui5-progress-indicator-max-value": this.validatedValue === 100, |
| 176 | + "ui5-progress-indicator-min-value": this.validatedValue === 0, |
| 177 | + }, |
| 178 | + }; |
| 179 | + } |
| 180 | + |
| 181 | + get validatedValue() { |
| 182 | + if (this.value < 0) { |
| 183 | + return 0; |
| 184 | + } |
| 185 | + |
| 186 | + if (this.value > 100) { |
| 187 | + return 100; |
| 188 | + } |
| 189 | + |
| 190 | + return this.value; |
| 191 | + } |
| 192 | + |
| 193 | + get showValueInRemainingBar() { |
| 194 | + return this.value <= 50; |
| 195 | + } |
| 196 | + |
| 197 | + get shouldAnimate() { |
| 198 | + return getAnimationMode() !== AnimationMode.None; |
| 199 | + } |
| 200 | + |
| 201 | + get valueStateText() { |
| 202 | + const percentValue = `${this.validatedValue}%`; |
| 203 | + const valueText = this.valueStateTextMappings()[this.valueState]; |
| 204 | + |
| 205 | + return valueText ? `${percentValue} ${valueText}` : percentValue; |
| 206 | + } |
| 207 | + |
| 208 | + get showIcon() { |
| 209 | + return this.valueState !== ValueState.None; |
| 210 | + } |
| 211 | + |
| 212 | + get valueStateIcon() { |
| 213 | + return this.valueStateIconMappings()[this.valueState]; |
| 214 | + } |
| 215 | + |
| 216 | + static async onDefine() { |
| 217 | + await fetchI18nBundle("@ui5/webcomponents"); |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +ProgressIndicator.define(); |
| 222 | + |
| 223 | +export default ProgressIndicator; |
0 commit comments