ActionButton.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import './ExecutionButton.js'
  2. import './ArgumentForm.js'
  3. import { ExecutionFeedbackButton } from './ExecutionFeedbackButton.js'
  4. class ActionButton extends ExecutionFeedbackButton {
  5. constructDomFromTemplate () {
  6. const tpl = document.getElementById('tplActionButton')
  7. const content = tpl.content.cloneNode(true)
  8. /*
  9. * FIXME: Should probably be using a shadowdom here, but seem to
  10. * get an error when combined with custom elements.
  11. */
  12. this.appendChild(content)
  13. this.btn = this.querySelector('button')
  14. this.domTitle = this.btn.querySelector('.title')
  15. this.domIcon = this.btn.querySelector('.icon')
  16. }
  17. constructFromJson (json) {
  18. this.updateIterationTimestamp = 0
  19. this.constructDomFromTemplate()
  20. // Class attributes
  21. this.temporaryStatusMessage = null
  22. this.isWaiting = false
  23. this.actionCallUrl = window.restBaseUrl + 'StartAction'
  24. this.updateFromJson(json)
  25. // DOM Attributes
  26. this.setAttribute('role', 'none')
  27. this.btn.title = json.title
  28. this.btn.onclick = () => {
  29. if (json.arguments.length > 0) {
  30. for (const oldArgumentForm of document.querySelectorAll('argument-form')) {
  31. oldArgumentForm.remove()
  32. }
  33. const frm = document.createElement('argument-form')
  34. frm.setup(json, (args) => {
  35. this.startAction(args)
  36. })
  37. document.body.appendChild(frm)
  38. frm.querySelector('dialog').showModal()
  39. } else {
  40. this.startAction()
  41. }
  42. }
  43. this.popupOnStart = json.popupOnStart
  44. this.updateFromJson(json)
  45. this.domTitle.innerText = this.btn.title
  46. this.domIcon.innerHTML = this.unicodeIcon
  47. this.setAttribute('id', 'actionButton-' + json.id)
  48. }
  49. updateFromJson (json) {
  50. // Fields that should not be updated
  51. //
  52. // title - as the callback URL relies on it
  53. // actionCallbackUrl - as it's based on the title
  54. // temporaryStatusMessage - as the button might be "waiting" on execution to finish while it's being updated.
  55. if (json.icon === '') {
  56. this.unicodeIcon = '&#x1f4a9'
  57. } else {
  58. this.unicodeIcon = unescape(json.icon)
  59. }
  60. }
  61. onExecStatusChanged () {
  62. this.btn.disabled = false
  63. setTimeout(() => {
  64. this.updateDom(null, this.btn.title)
  65. }, 2000)
  66. }
  67. getUniqueId () {
  68. if (window.isSecureContext) {
  69. return window.crypto.randomUUID()
  70. } else {
  71. return Date.now().toString()
  72. }
  73. }
  74. startAction (actionArgs) {
  75. // this.btn.disabled = true
  76. // this.isWaiting = true
  77. this.btn.classList = [] // Removes old animation classes
  78. if (actionArgs === undefined) {
  79. actionArgs = []
  80. }
  81. // UUIDs are create client side, so that we can setup a "execution-button"
  82. // to track the execution before we send the request to the server.
  83. const startActionArgs = {
  84. actionName: this.btn.title,
  85. arguments: actionArgs,
  86. uuid: this.getUniqueId()
  87. }
  88. this.onActionStarted(startActionArgs.uuid)
  89. window.fetch(this.actionCallUrl, {
  90. method: 'POST',
  91. headers: {
  92. 'Content-Type': 'application/json'
  93. },
  94. body: JSON.stringify(startActionArgs)
  95. }).then((res) => {
  96. if (res.ok) {
  97. return res.json()
  98. } else {
  99. throw new Error(res.statusText)
  100. }
  101. }
  102. ).then((json) => {
  103. // The button used to wait for the action to finish, but now it is fire & forget
  104. }).catch(err => {
  105. throw err // We used to flash buttons red, but now hand to the global error handler
  106. })
  107. }
  108. onActionStarted (executionUuid) {
  109. if (this.popupOnStart === 'execution-button') {
  110. const btnExecution = document.createElement('execution-button')
  111. btnExecution.constructFromJson(executionUuid)
  112. this.querySelector('.action-button-footer').prepend(btnExecution)
  113. return
  114. }
  115. if (this.popupOnStart.includes('execution-dialog')) {
  116. window.executionDialog.reset()
  117. if (this.popupOnStart === 'execution-dialog-stdout-only') {
  118. window.executionDialog.hideEverythingApartFromOutput()
  119. }
  120. window.executionDialog.show(this)
  121. }
  122. this.btn.disabled = true
  123. }
  124. }
  125. window.customElements.define('action-button', ActionButton)