ActionButton.js 3.8 KB

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