ExecutionButton.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { ExecutionDialog } from './ExecutionDialog.js'
  2. class ExecutionButton extends window.HTMLElement {
  3. constructFromJson (json) {
  4. this.executionUuid = json
  5. this.appendChild(document.createElement('button'))
  6. this.isWaiting = true
  7. this.setAttribute('id', 'execution-' + json)
  8. this.btn = this.querySelector('button')
  9. this.btn.innerText = 'Executing...'
  10. this.btn.onclick = () => {
  11. this.show()
  12. }
  13. }
  14. show () {
  15. if (window.executionDialog === undefined) {
  16. window.executionDialog = new ExecutionDialog()
  17. }
  18. const executionStatusArgs = {
  19. executionUuid: this.executionUuid
  20. }
  21. window.executionDialog.constructFromJson(this.executionUuid)
  22. window.executionDialog.show()
  23. window.fetch(window.restBaseUrl + 'ExecutionStatus', {
  24. method: 'POST',
  25. headers: {
  26. 'Content-Type': 'application/json'
  27. },
  28. body: JSON.stringify(executionStatusArgs)
  29. }).then((res) => {
  30. if (res.ok) {
  31. return res.json()
  32. } else {
  33. throw new Error(res.statusText)
  34. }
  35. }
  36. ).then((json) => {
  37. window.executionDialog.renderResult(json)
  38. }).catch(err => {
  39. window.executionDialog.renderError(err)
  40. })
  41. }
  42. onFinished (LogEntry) {
  43. if (LogEntry.timedOut) {
  44. this.onActionResult('action-timeout', 'Timed out')
  45. } else if (LogEntry.blocked) {
  46. this.onActionResult('action-blocked', 'Blocked!')
  47. } else if (LogEntry.exitCode !== 0) {
  48. this.onActionResult('action-nonzero-exit', 'Exit code ' + LogEntry.exitCode)
  49. } else {
  50. this.onActionResult('action-success', 'Success!')
  51. }
  52. }
  53. onActionResult (cssClass, temporaryStatusMessage) {
  54. this.temporaryStatusMessage = '[ ' + temporaryStatusMessage + ' ]'
  55. this.updateDom()
  56. this.btn.classList.add(cssClass)
  57. setTimeout(() => {
  58. this.btn.classList.remove(cssClass)
  59. }, 1000)
  60. }
  61. onActionError (err) {
  62. console.error('callback error', err)
  63. this.btn.disabled = false
  64. this.isWaiting = false
  65. this.updateDom()
  66. this.btn.classList.add('action-failed')
  67. setTimeout(() => {
  68. this.btn.classList.remove('action-failed')
  69. }, 1000)
  70. }
  71. updateDom () {
  72. if (this.temporaryStatusMessage != null) {
  73. this.btn.innerText = this.temporaryStatusMessage
  74. this.btn.classList.add('temporary-status-message')
  75. this.isWaiting = false
  76. this.disabled = false
  77. setTimeout(() => {
  78. this.temporaryStatusMessage = null
  79. this.btn.classList.remove('temporary-status-message')
  80. this.updateDom()
  81. }, 2000)
  82. } else if (this.isWaiting) {
  83. this.btn.innerText = 'Waiting...'
  84. } else {
  85. this.btn.innerText = 'Finished'
  86. }
  87. }
  88. }
  89. window.customElements.define('execution-button', ExecutionButton)