ArgumentForm.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. class ArgumentForm extends window.HTMLElement {
  2. getQueryParams () {
  3. return new URLSearchParams(window.location.search.substring(1))
  4. }
  5. setup (json, callback) {
  6. this.setAttribute('class', 'action-arguments')
  7. this.constructTemplate()
  8. this.domTitle.innerText = json.title
  9. this.domIcon.innerHTML = json.icon
  10. this.createDomFormArguments(json.arguments)
  11. this.domBtnStart.onclick = () => {
  12. for (const arg of this.argInputs) {
  13. if (!arg.validity.valid) {
  14. return
  15. }
  16. }
  17. const argvs = this.getArgumentValues()
  18. callback(argvs)
  19. this.remove()
  20. }
  21. this.domBtnCancel.onclick = () => {
  22. this.clearBookmark()
  23. this.remove()
  24. }
  25. }
  26. getArgumentValues () {
  27. const ret = []
  28. for (const arg of this.argInputs) {
  29. ret.push({
  30. name: arg.name,
  31. value: arg.value
  32. })
  33. }
  34. return ret
  35. }
  36. constructTemplate () {
  37. const tpl = document.getElementById('tplArgumentForm')
  38. const content = tpl.content.cloneNode(true)
  39. this.appendChild(content)
  40. this.domTitle = this.querySelector('h2')
  41. this.domIcon = this.querySelector('span.icon')
  42. this.domWrapper = this.querySelector('.wrapper')
  43. this.domArgs = this.querySelector('.arguments')
  44. this.domBtnStart = this.querySelector('[name=start]')
  45. this.domBtnCancel = this.querySelector('[name=cancel]')
  46. }
  47. createDomFormArguments (args) {
  48. this.argInputs = []
  49. for (const arg of args) {
  50. this.domArgs.appendChild(this.createDomLabel(arg))
  51. this.domArgs.appendChild(this.createDomSuggestions(arg))
  52. this.domArgs.appendChild(this.createDomInput(arg))
  53. this.domArgs.appendChild(this.createDomDescription(arg))
  54. }
  55. }
  56. createDomLabel (arg) {
  57. const domLbl = document.createElement('label')
  58. const lastChar = arg.title.charAt(arg.title.length - 1)
  59. if (lastChar === '?' || lastChar === '.' || lastChar === ':') {
  60. domLbl.innerHTML = arg.title
  61. } else {
  62. domLbl.innerHTML = arg.title + ':'
  63. }
  64. domLbl.setAttribute('for', arg.name)
  65. return domLbl
  66. }
  67. createDomSuggestions (arg) {
  68. if (typeof arg.suggestions !== 'object' || arg.suggestions.length === 0) {
  69. return document.createElement('span')
  70. }
  71. const ret = document.createElement('datalist')
  72. ret.setAttribute('id', arg.name + '-choices')
  73. for (const suggestion of Object.keys(arg.suggestions)) {
  74. const opt = document.createElement('option')
  75. opt.setAttribute('value', suggestion)
  76. if (typeof arg.suggestions[suggestion] !== 'undefined' && arg.suggestions[suggestion].length > 0) {
  77. opt.innerText = arg.suggestions[suggestion]
  78. }
  79. ret.appendChild(opt)
  80. }
  81. return ret
  82. }
  83. createDomInput (arg) {
  84. let domEl = null
  85. if (arg.choices.length > 0) {
  86. domEl = document.createElement('select')
  87. // select/choice elements don't get an onchange/validation because theoretically
  88. // the user should only select from a dropdown of valid options. The choices are
  89. // riggeriously checked on StartAction anyway. ValidateArgumentType is only
  90. // meant for showing simple warnings in the UI before running.
  91. for (const choice of arg.choices) {
  92. domEl.appendChild(this.createSelectOption(choice))
  93. }
  94. } else {
  95. switch (arg.type) {
  96. case 'html':
  97. domEl = document.createElement('div')
  98. domEl.innerHTML = arg.defaultValue
  99. return domEl
  100. case 'confirmation':
  101. this.domBtnStart.disabled = true
  102. domEl = document.createElement('input')
  103. domEl.setAttribute('type', 'checkbox')
  104. domEl.onchange = () => {
  105. this.domBtnStart.disabled = false
  106. domEl.disabled = true
  107. }
  108. break
  109. case 'raw_string_multiline':
  110. domEl = document.createElement('textarea')
  111. domEl.setAttribute('rows', '5')
  112. domEl.style.resize = 'vertical'
  113. break
  114. case 'datetime':
  115. domEl = document.createElement('input')
  116. domEl.setAttribute('type', 'datetime-local')
  117. domEl.setAttribute('step', '1')
  118. break
  119. case 'password':
  120. case 'email':
  121. domEl = document.createElement('input')
  122. domEl.setAttribute('type', arg.type)
  123. break
  124. default:
  125. domEl = document.createElement('input')
  126. if (arg.type.startsWith('regex:')) {
  127. domEl.setAttribute('pattern', arg.type.replace('regex:', ''))
  128. }
  129. domEl.onchange = () => {
  130. const validateArgumentTypeArgs = {
  131. value: domEl.value,
  132. type: arg.type
  133. }
  134. window.fetch(window.restBaseUrl + 'ValidateArgumentType', {
  135. method: 'POST',
  136. headers: {
  137. 'Content-Type': 'application/json'
  138. },
  139. body: JSON.stringify(validateArgumentTypeArgs)
  140. }).then((res) => {
  141. if (res.ok) {
  142. return res.json()
  143. } else {
  144. throw new Error(res.statusText)
  145. }
  146. }).then((json) => {
  147. if (json.valid) {
  148. domEl.setCustomValidity('')
  149. } else {
  150. domEl.setCustomValidity(json.description)
  151. }
  152. })
  153. }
  154. }
  155. }
  156. domEl.name = arg.name
  157. // Use query parameter value if available
  158. const params = this.getQueryParams()
  159. const paramValue = params.get(arg.name)
  160. if (paramValue !== null) {
  161. domEl.value = paramValue
  162. } else {
  163. domEl.value = arg.defaultValue
  164. }
  165. // update the URL when a parameter is changed
  166. domEl.addEventListener('change', this.updateUrlWithArg)
  167. if (typeof arg.suggestions === 'object' && Object.keys(arg.suggestions).length > 0) {
  168. domEl.setAttribute('list', arg.name + '-choices')
  169. }
  170. this.argInputs.push(domEl)
  171. return domEl
  172. }
  173. updateUrlWithArg (ev) {
  174. if (!ev.target.name) {
  175. return
  176. }
  177. const url = new URL(window.location.href)
  178. if (ev.target.type === 'password') {
  179. return;
  180. }
  181. // copy the parameter value
  182. url.searchParams.set(ev.target.name, ev.target.value)
  183. // Update the URL without reloading the page
  184. window.history.replaceState({}, '', url.toString())
  185. }
  186. createDomDescription (arg) {
  187. const domArgumentDescription = document.createElement('span')
  188. domArgumentDescription.classList.add('argument-description')
  189. domArgumentDescription.innerHTML = arg.description
  190. return domArgumentDescription
  191. }
  192. createSelectOption (choice) {
  193. const domEl = document.createElement('option')
  194. domEl.setAttribute('value', choice.value)
  195. domEl.innerText = choice.title
  196. return domEl
  197. }
  198. clearBookmark () {
  199. // remove the action from the URL
  200. window.history.replaceState({
  201. path: window.location.pathname
  202. }, '', window.location.pathname)
  203. }
  204. }
  205. window.customElements.define('argument-form', ArgumentForm)