ActionButton.js 4.2 KB

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