4
0

ArgumentForm.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. class ArgumentForm extends window.HTMLElement {
  2. setup (json, callback) {
  3. this.setAttribute('class', 'action-arguments')
  4. this.constructTemplate()
  5. this.domTitle.innerText = json.title
  6. this.domIcon.innerHTML = json.icon
  7. this.createDomFormArguments(json.arguments)
  8. this.domBtnStart.onclick = () => {
  9. for (const arg of this.argInputs) {
  10. if (!arg.validity.valid) {
  11. return
  12. }
  13. }
  14. const argvs = this.getArgumentValues()
  15. callback(argvs)
  16. this.remove()
  17. }
  18. this.domBtnCancel.onclick = () => {
  19. this.remove()
  20. }
  21. }
  22. getArgumentValues () {
  23. const ret = []
  24. for (const arg of this.argInputs) {
  25. ret.push({
  26. name: arg.name,
  27. value: arg.value
  28. })
  29. }
  30. return ret
  31. }
  32. constructTemplate () {
  33. const tpl = document.getElementById('tplArgumentForm')
  34. const content = tpl.content.cloneNode(true)
  35. this.appendChild(content)
  36. this.domTitle = this.querySelector('h2')
  37. this.domIcon = this.querySelector('span.icon')
  38. this.domWrapper = this.querySelector('.wrapper')
  39. this.domArgs = this.querySelector('.arguments')
  40. this.domBtnStart = this.querySelector('[name=start]')
  41. this.domBtnCancel = this.querySelector('[name=cancel]')
  42. }
  43. createDomFormArguments (args) {
  44. this.argInputs = []
  45. for (const arg of args) {
  46. const domArgumentWrapper = document.createElement('p')
  47. domArgumentWrapper.classList.add('argument-wrapper')
  48. domArgumentWrapper.appendChild(this.createDomLabel(arg))
  49. domArgumentWrapper.appendChild(this.createDomInput(arg))
  50. domArgumentWrapper.appendChild(this.createDomDescription(arg))
  51. this.domArgs.appendChild(domArgumentWrapper)
  52. }
  53. }
  54. createDomLabel (arg) {
  55. const domLbl = document.createElement('label')
  56. domLbl.innerText = arg.title + ':'
  57. domLbl.setAttribute('for', arg.name)
  58. return domLbl
  59. }
  60. createDomInput (arg) {
  61. let domEl = null
  62. if (arg.choices.length > 0) {
  63. domEl = document.createElement('select')
  64. // select/choice elements don't get an onchange/validation because theoretically
  65. // the user should only select from a dropdown of valid options. The choices are
  66. // riggeriously checked on StartAction anyway. ValidateArgumentType is only
  67. // meant for showing simple warnings in the UI before running.
  68. for (const choice of arg.choices) {
  69. domEl.appendChild(this.createSelectOption(choice))
  70. }
  71. } else if (arg.type === 'confirmation') {
  72. this.domBtnStart.disabled = true
  73. domEl = document.createElement('input')
  74. domEl.setAttribute('type', 'checkbox')
  75. domEl.onchange = () => {
  76. this.domBtnStart.disabled = false
  77. domEl.disabled = true
  78. }
  79. } else {
  80. domEl = document.createElement('input')
  81. domEl.onchange = () => {
  82. const validateArgumentTypeArgs = {
  83. value: domEl.value,
  84. type: arg.type
  85. }
  86. window.fetch(window.restBaseUrl + 'ValidateArgumentType', {
  87. method: 'POST',
  88. headers: {
  89. 'Content-Type': 'application/json'
  90. },
  91. body: JSON.stringify(validateArgumentTypeArgs)
  92. }).then((res) => {
  93. if (res.ok) {
  94. return res.json()
  95. } else {
  96. throw new Error(res.statusText)
  97. }
  98. }).then((json) => {
  99. if (json.valid) {
  100. domEl.setCustomValidity('')
  101. } else {
  102. domEl.setCustomValidity(json.description)
  103. }
  104. })
  105. }
  106. }
  107. domEl.name = arg.name
  108. domEl.value = arg.defaultValue
  109. this.argInputs.push(domEl)
  110. return domEl
  111. }
  112. createDomDescription (arg) {
  113. const domArgumentDescription = document.createElement('span')
  114. domArgumentDescription.classList.add('argument-description')
  115. domArgumentDescription.innerText = arg.description
  116. return domArgumentDescription
  117. }
  118. createSelectOption (choice) {
  119. const domEl = document.createElement('option')
  120. domEl.setAttribute('value', choice.value)
  121. domEl.innerText = choice.title
  122. return domEl
  123. }
  124. }
  125. window.customElements.define('argument-form', ArgumentForm)