ArgumentForm.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.createDomSuggestions(arg))
  50. domArgumentWrapper.appendChild(this.createDomInput(arg))
  51. domArgumentWrapper.appendChild(this.createDomDescription(arg))
  52. this.domArgs.appendChild(domArgumentWrapper)
  53. }
  54. }
  55. createDomLabel (arg) {
  56. const domLbl = document.createElement('label')
  57. domLbl.innerText = arg.title + ':'
  58. domLbl.setAttribute('for', arg.name)
  59. return domLbl
  60. }
  61. createDomSuggestions (arg) {
  62. if (typeof arg.suggestions !== 'object' || arg.suggestions.length === 0) {
  63. return document.createElement('span')
  64. }
  65. const ret = document.createElement('datalist')
  66. ret.setAttribute('id', arg.name + '-choices')
  67. for (const suggestion of Object.keys(arg.suggestions)) {
  68. const opt = document.createElement('option')
  69. opt.setAttribute('value', suggestion)
  70. if (typeof arg.suggestions[suggestion] !== 'undefined' && arg.suggestions[suggestion].length > 0) {
  71. opt.innerText = arg.suggestions[suggestion]
  72. }
  73. ret.appendChild(opt)
  74. }
  75. return ret
  76. }
  77. createDomInput (arg) {
  78. let domEl = null
  79. if (arg.choices.length > 0) {
  80. domEl = document.createElement('select')
  81. // select/choice elements don't get an onchange/validation because theoretically
  82. // the user should only select from a dropdown of valid options. The choices are
  83. // riggeriously checked on StartAction anyway. ValidateArgumentType is only
  84. // meant for showing simple warnings in the UI before running.
  85. for (const choice of arg.choices) {
  86. domEl.appendChild(this.createSelectOption(choice))
  87. }
  88. } else if (arg.type === 'confirmation') {
  89. this.domBtnStart.disabled = true
  90. domEl = document.createElement('input')
  91. domEl.setAttribute('type', 'checkbox')
  92. domEl.onchange = () => {
  93. this.domBtnStart.disabled = false
  94. domEl.disabled = true
  95. }
  96. } else if (arg.type === 'datetime') {
  97. domEl = document.createElement('input')
  98. domEl.setAttribute('type', 'datetime-local')
  99. domEl.setAttribute('step', '1')
  100. } else if (arg.type === 'password') {
  101. domEl = document.createElement('input')
  102. domEl.setAttribute('type', 'password')
  103. } else {
  104. domEl = document.createElement('input')
  105. if (arg.type.startsWith('regex:')) {
  106. domEl.setAttribute('pattern', arg.type.replace('regex:', ''))
  107. }
  108. domEl.onchange = () => {
  109. const validateArgumentTypeArgs = {
  110. value: domEl.value,
  111. type: arg.type
  112. }
  113. window.fetch(window.restBaseUrl + 'ValidateArgumentType', {
  114. method: 'POST',
  115. headers: {
  116. 'Content-Type': 'application/json'
  117. },
  118. body: JSON.stringify(validateArgumentTypeArgs)
  119. }).then((res) => {
  120. if (res.ok) {
  121. return res.json()
  122. } else {
  123. throw new Error(res.statusText)
  124. }
  125. }).then((json) => {
  126. if (json.valid) {
  127. domEl.setCustomValidity('')
  128. } else {
  129. domEl.setCustomValidity(json.description)
  130. }
  131. })
  132. }
  133. }
  134. domEl.name = arg.name
  135. domEl.value = arg.defaultValue
  136. if (typeof arg.suggestions === 'object' && Object.keys(arg.suggestions).length > 0) {
  137. domEl.setAttribute('list', arg.name + '-choices')
  138. }
  139. this.argInputs.push(domEl)
  140. return domEl
  141. }
  142. createDomDescription (arg) {
  143. const domArgumentDescription = document.createElement('span')
  144. domArgumentDescription.classList.add('argument-description')
  145. domArgumentDescription.innerText = arg.description
  146. return domArgumentDescription
  147. }
  148. createSelectOption (choice) {
  149. const domEl = document.createElement('option')
  150. domEl.setAttribute('value', choice.value)
  151. domEl.innerText = choice.title
  152. return domEl
  153. }
  154. }
  155. window.customElements.define('argument-form', ArgumentForm)