ActionButton.js 4.1 KB

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