ArgumentForm.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 {
  101. domEl = document.createElement('input')
  102. if (arg.type.startsWith('regex:')) {
  103. domEl.setAttribute('pattern', arg.type.replace('regex:', ''))
  104. }
  105. domEl.onchange = () => {
  106. const validateArgumentTypeArgs = {
  107. value: domEl.value,
  108. type: arg.type
  109. }
  110. window.fetch(window.restBaseUrl + 'ValidateArgumentType', {
  111. method: 'POST',
  112. headers: {
  113. 'Content-Type': 'application/json'
  114. },
  115. body: JSON.stringify(validateArgumentTypeArgs)
  116. }).then((res) => {
  117. if (res.ok) {
  118. return res.json()
  119. } else {
  120. throw new Error(res.statusText)
  121. }
  122. }).then((json) => {
  123. if (json.valid) {
  124. domEl.setCustomValidity('')
  125. } else {
  126. domEl.setCustomValidity(json.description)
  127. }
  128. })
  129. }
  130. }
  131. domEl.name = arg.name
  132. domEl.value = arg.defaultValue
  133. if (typeof arg.suggestions === 'object' && Object.keys(arg.suggestions).length > 0) {
  134. domEl.setAttribute('list', arg.name + '-choices')
  135. }
  136. this.argInputs.push(domEl)
  137. return domEl
  138. }
  139. createDomDescription (arg) {
  140. const domArgumentDescription = document.createElement('span')
  141. domArgumentDescription.classList.add('argument-description')
  142. domArgumentDescription.innerText = arg.description
  143. return domArgumentDescription
  144. }
  145. createSelectOption (choice) {
  146. const domEl = document.createElement('option')
  147. domEl.setAttribute('value', choice.value)
  148. domEl.innerText = choice.title
  149. return domEl
  150. }
  151. }
  152. window.customElements.define('argument-form', ArgumentForm)