flash-progress.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { css, html, LitElement } from "lit";
  2. import { customElement, state } from "lit/decorators.js";
  3. import { FlashState, State } from "./const";
  4. import "@material/mwc-linear-progress";
  5. import { classMap } from "lit/directives/class-map.js";
  6. @customElement("esp-web-flash-progress")
  7. export class FlashProgress extends LitElement {
  8. @state() private _state?: FlashState;
  9. @state() private _indeterminate = true;
  10. @state() private _progress = 0;
  11. public processState(state: FlashState) {
  12. this._state = state;
  13. if (this._state.state === State.WRITING) {
  14. this._indeterminate = false;
  15. this._progress = this._state.details.percentage / 100;
  16. }
  17. if (this._state.state === State.ERROR) {
  18. this._indeterminate = false;
  19. }
  20. }
  21. public clear() {
  22. this._state = undefined;
  23. this._progress = 0;
  24. this._indeterminate = true;
  25. }
  26. protected render() {
  27. if (!this._state) {
  28. return;
  29. }
  30. return html`<h2
  31. class=${classMap({
  32. error: this._state.state === State.ERROR,
  33. done: this._state.state === State.FINISHED,
  34. })}
  35. >
  36. ${this._state.message}
  37. </h2>
  38. <p>
  39. ${this._state.manifest
  40. ? html`${this._state.manifest.name}: ${this._state.chipFamily}`
  41. : html`&nbsp;`}
  42. </p>
  43. <mwc-linear-progress
  44. class=${classMap({
  45. error: this._state.state === State.ERROR,
  46. done: this._state.state === State.FINISHED,
  47. })}
  48. .indeterminate=${this._indeterminate}
  49. .progress=${this._progress}
  50. ></mwc-linear-progress>`;
  51. }
  52. static styles = css`
  53. :host {
  54. display: block;
  55. --mdc-theme-primary: var(--esp-tools-progress-color, #03a9f4);
  56. }
  57. .error {
  58. color: var(--esp-tools-error-color, #dc3545);
  59. --mdc-theme-primary: var(--esp-tools-error-color, #dc3545);
  60. }
  61. .done {
  62. color: var(--esp-tools-success-color, #28a745);
  63. --mdc-theme-primary: var(--esp-tools-success-color, #28a745);
  64. }
  65. h2 {
  66. margin: 16px 0 0;
  67. }
  68. p {
  69. margin: 4px 0;
  70. }
  71. `;
  72. }
  73. declare global {
  74. interface HTMLElementTagNameMap {
  75. "esp-web-flash-progress": FlashProgress;
  76. }
  77. }