ExecutionButton.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { ExecutionDialog } from './ExecutionDialog.js'
  2. class ExecutionButton extends window.HTMLElement {
  3. constructFromJson (json) {
  4. this.executionUuid = json
  5. this.ellapsed = 0
  6. this.appendChild(document.createElement('button'))
  7. this.isWaiting = true
  8. this.setAttribute('id', 'execution-' + json)
  9. this.btn = this.querySelector('button')
  10. this.btn.innerText = 'Executing...'
  11. this.btn.onclick = () => {
  12. this.show()
  13. }
  14. }
  15. show () {
  16. if (typeof (window.executionDialog) === 'undefined') {
  17. window.executionDialog = new ExecutionDialog()
  18. }
  19. window.executionDialog.show(this.executionUuid)
  20. }
  21. onFinished (LogEntry) {
  22. if (LogEntry.timedOut) {
  23. this.onActionResult('action-timeout', 'Timed out')
  24. } else if (LogEntry.blocked) {
  25. this.onActionResult('action-blocked', 'Blocked!')
  26. } else if (LogEntry.exitCode !== 0) {
  27. this.onActionResult('action-nonzero-exit', 'Exit code ' + LogEntry.exitCode)
  28. } else {
  29. console.log(LogEntry)
  30. this.ellapsed = Math.ceil(new Date(LogEntry.datetimeFinished) - new Date(LogEntry.datetimeStarted)) / 1000
  31. this.onActionResult('action-success', 'Success!')
  32. }
  33. }
  34. onActionResult (cssClass, temporaryStatusMessage) {
  35. this.temporaryStatusMessage = '[' + temporaryStatusMessage + ']'
  36. this.updateDom()
  37. this.btn.classList.add(cssClass)
  38. }
  39. onActionError (err) {
  40. console.error('callback error', err)
  41. this.isWaiting = false
  42. this.updateDom()
  43. this.btn.classList.add('action-failed')
  44. }
  45. updateDom () {
  46. if (this.temporaryStatusMessage != null) {
  47. this.btn.innerText = this.temporaryStatusMessage
  48. this.btn.classList.add('temporary-status-message')
  49. this.isWaiting = false
  50. setTimeout(() => {
  51. this.temporaryStatusMessage = null
  52. this.btn.classList.remove('temporary-status-message')
  53. this.updateDom()
  54. }, 2000)
  55. } else if (this.isWaiting) {
  56. this.btn.innerText = 'Waiting...'
  57. } else {
  58. this.btn.innerText = this.ellapsed + 's'
  59. this.btn.title = this.ellapsed + ' seconds'
  60. }
  61. }
  62. }
  63. window.customElements.define('execution-button', ExecutionButton)