ActionButton.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.updateFromJson(json)
  22. this.actionId = json.id
  23. // DOM Attributes
  24. this.setAttribute('role', 'none')
  25. this.setAttribute('id', 'actionButton-' + this.actionId)
  26. this.btn.setAttribute('id', 'actionButtonInner-' + this.actionId)
  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. }
  48. updateFromJson (json) {
  49. // Fields that should not be updated
  50. //
  51. // title - as the callback URL relies on it
  52. if (json.icon === '') {
  53. this.unicodeIcon = '&#x1f4a9'
  54. } else {
  55. this.unicodeIcon = unescape(json.icon)
  56. }
  57. }
  58. onExecStatusChanged () {
  59. this.btn.disabled = false
  60. setTimeout(() => {
  61. this.updateDom(null, this.btn.title)
  62. }, 2000)
  63. }
  64. getUniqueId () {
  65. if (window.isSecureContext) {
  66. return window.crypto.randomUUID()
  67. } else {
  68. return Date.now().toString()
  69. }
  70. }
  71. startAction (actionArgs) {
  72. this.btn.classList = [] // Removes old animation classes
  73. if (actionArgs === undefined) {
  74. actionArgs = []
  75. }
  76. // UUIDs are create client side, so that we can setup a "execution-button"
  77. // to track the execution before we send the request to the server.
  78. const startActionArgs = {
  79. actionId: this.actionId,
  80. arguments: actionArgs,
  81. uniqueTrackingId: this.getUniqueId()
  82. }
  83. this.onActionStarted(startActionArgs.uniqueTrackingId)
  84. window.fetch(window.restBaseUrl + 'StartAction', {
  85. method: 'POST',
  86. headers: {
  87. 'Content-Type': 'application/json'
  88. },
  89. body: JSON.stringify(startActionArgs)
  90. }).then((res) => {
  91. if (res.ok) {
  92. return res.json()
  93. } else {
  94. throw new Error(res.statusText)
  95. }
  96. }
  97. ).then((json) => {
  98. // The button used to wait for the action to finish, but now it is fire & forget
  99. }).catch(err => {
  100. throw err // We used to flash buttons red, but now hand to the global error handler
  101. })
  102. }
  103. onActionStarted (executionTrackingId) {
  104. if (this.popupOnStart === 'execution-button') {
  105. const btnExecution = document.createElement('execution-button')
  106. btnExecution.constructFromJson(executionTrackingId)
  107. this.querySelector('.action-button-footer').hidden = false
  108. this.querySelector('.action-button-footer').style.display = 'flex'
  109. this.querySelector('.action-button-footer').prepend(btnExecution)
  110. return
  111. }
  112. if (this.popupOnStart.includes('execution-dialog')) {
  113. window.executionDialog.reset()
  114. if (this.popupOnStart === 'execution-dialog-stdout-only') {
  115. window.executionDialog.hideEverythingApartFromOutput()
  116. }
  117. window.executionDialog.show(this)
  118. }
  119. this.btn.disabled = true
  120. }
  121. }
  122. window.customElements.define('action-button', ActionButton)