ArgumentForm.js 7.3 KB

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