ExecutionButton.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 (window.executionDialog === undefined) {
  17. window.executionDialog = new ExecutionDialog()
  18. }
  19. const executionStatusArgs = {
  20. executionUuid: this.executionUuid
  21. }
  22. window.executionDialog.constructFromJson(this.executionUuid)
  23. window.executionDialog.show()
  24. window.fetch(window.restBaseUrl + 'ExecutionStatus', {
  25. method: 'POST',
  26. headers: {
  27. 'Content-Type': 'application/json'
  28. },
  29. body: JSON.stringify(executionStatusArgs)
  30. }).then((res) => {
  31. if (res.ok) {
  32. return res.json()
  33. } else {
  34. throw new Error(res.statusText)
  35. }
  36. }
  37. ).then((json) => {
  38. window.executionDialog.renderResult(json)
  39. }).catch(err => {
  40. window.executionDialog.renderError(err)
  41. })
  42. }
  43. onFinished (LogEntry) {
  44. if (LogEntry.timedOut) {
  45. this.onActionResult('action-timeout', 'Timed out')
  46. } else if (LogEntry.blocked) {
  47. this.onActionResult('action-blocked', 'Blocked!')
  48. } else if (LogEntry.exitCode !== 0) {
  49. this.onActionResult('action-nonzero-exit', 'Exit code ' + LogEntry.exitCode)
  50. } else {
  51. console.log(LogEntry)
  52. this.ellapsed = Math.ceil(new Date(LogEntry.datetimeFinished) - new Date(LogEntry.datetimeStarted)) / 1000
  53. this.onActionResult('action-success', 'Success!')
  54. }
  55. }
  56. onActionResult (cssClass, temporaryStatusMessage) {
  57. this.temporaryStatusMessage = '[' + temporaryStatusMessage + ']'
  58. this.updateDom()
  59. this.btn.classList.add(cssClass)
  60. }
  61. onActionError (err) {
  62. console.error('callback error', err)
  63. this.isWaiting = false
  64. this.updateDom()
  65. this.btn.classList.add('action-failed')
  66. }
  67. updateDom () {
  68. if (this.temporaryStatusMessage != null) {
  69. this.btn.innerText = this.temporaryStatusMessage
  70. this.btn.classList.add('temporary-status-message')
  71. this.isWaiting = false
  72. setTimeout(() => {
  73. this.temporaryStatusMessage = null
  74. this.btn.classList.remove('temporary-status-message')
  75. this.updateDom()
  76. }, 2000)
  77. } else if (this.isWaiting) {
  78. this.btn.innerText = 'Waiting...'
  79. } else {
  80. this.btn.innerText = this.ellapsed + 's'
  81. this.btn.title = this.ellapsed + ' seconds'
  82. }
  83. }
  84. }
  85. window.customElements.define('execution-button', ExecutionButton)