ArgumentForm.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <template>
  2. <section id = "argument-popup">
  3. <div class="section-header">
  4. <h2>Start action: {{ title }}</h2>
  5. </div>
  6. <div class="section-content padding">
  7. <form @submit="handleSubmit">
  8. <template v-if="actionArguments.length > 0">
  9. <template v-for="arg in actionArguments" :key="arg.name">
  10. <label :for="arg.name">
  11. {{ formatLabel(arg.title) }}
  12. </label>
  13. <datalist v-if="(arg.suggestions && Object.keys(arg.suggestions).length > 0) || getBrowserSuggestions(arg).length > 0" :id="`${arg.name}-choices`">
  14. <option v-for="(suggestion, key) in arg.suggestions" :key="key" :value="key">
  15. {{ suggestion }}
  16. </option>
  17. <option v-for="(suggestion, index) in getBrowserSuggestions(arg)" :key="`browser-${index}`" :value="suggestion">
  18. {{ suggestion }}
  19. </option>
  20. </datalist>
  21. <select v-if="getInputComponent(arg) === 'select'" :id="arg.name" :name="arg.name" :value="getArgumentValue(arg)"
  22. :required="arg.required" @input="handleInput(arg, $event)" @change="handleChange(arg, $event)">
  23. <option v-for="choice in arg.choices" :key="choice.value" :value="choice.value">
  24. {{ choice.title || choice.value }}
  25. </option>
  26. </select>
  27. <component v-else :is="getInputComponent(arg)" :id="arg.name" :name="arg.name"
  28. :value="(arg.type === 'checkbox' || arg.type === 'confirmation') ? undefined : getArgumentValue(arg)"
  29. :checked="(arg.type === 'checkbox' || arg.type === 'confirmation') ? getArgumentValue(arg) : undefined"
  30. :list="(arg.suggestions || getBrowserSuggestions(arg).length > 0) ? `${arg.name}-choices` : undefined"
  31. :type="getInputComponent(arg) !== 'select' ? getInputType(arg) : undefined"
  32. :rows="arg.type === 'raw_string_multiline' ? 5 : undefined"
  33. :step="arg.type === 'datetime' ? 1 : undefined" :pattern="getPattern(arg)"
  34. @input="handleInput(arg, $event)" @change="handleChange(arg, $event)" />
  35. <span class="argument-description" v-html="arg.description"></span>
  36. </template>
  37. </template>
  38. <div v-else>
  39. <p>No arguments required</p>
  40. </div>
  41. <div class="buttons">
  42. <button name="start" type="submit" :disabled="hasConfirmation && !confirmationChecked">
  43. Start
  44. </button>
  45. <button name="cancel" type="button" @click="handleCancel">
  46. Cancel
  47. </button>
  48. </div>
  49. </form>
  50. </div>
  51. </section>
  52. </template>
  53. <script setup>
  54. import { ref, onMounted, nextTick } from 'vue'
  55. import { useRouter } from 'vue-router'
  56. const router = useRouter()
  57. // Reactive data
  58. const dialog = ref(null)
  59. const title = ref('')
  60. const icon = ref('')
  61. //const arguments = ref([])
  62. const argValues = ref({})
  63. const confirmationChecked = ref(false)
  64. const hasConfirmation = ref(false)
  65. const formErrors = ref({})
  66. const actionArguments = ref([])
  67. const popupOnStart = ref('')
  68. // Computed properties
  69. const props = defineProps({
  70. bindingId: {
  71. type: String,
  72. required: true
  73. }
  74. })
  75. // Methods
  76. async function setup() {
  77. const ret = await window.client.getActionBinding({
  78. bindingId: props.bindingId
  79. })
  80. const action = ret.action
  81. title.value = action.title
  82. icon.value = action.icon
  83. popupOnStart.value = action.popupOnStart || ''
  84. actionArguments.value = action.arguments || []
  85. argValues.value = {}
  86. formErrors.value = {}
  87. confirmationChecked.value = false
  88. hasConfirmation.value = false
  89. // Initialize values from query params or defaults
  90. actionArguments.value.forEach(arg => {
  91. if (arg.type === 'confirmation') {
  92. hasConfirmation.value = true
  93. const paramValue = getQueryParamValue(arg.name)
  94. let checkedValue = false
  95. if (paramValue !== null) {
  96. checkedValue = paramValue === '1' || paramValue === 'true' || paramValue === true
  97. } else if (arg.defaultValue !== undefined && arg.defaultValue !== '') {
  98. checkedValue = arg.defaultValue === '1' || arg.defaultValue === 'true' || arg.defaultValue === true
  99. }
  100. argValues.value[arg.name] = checkedValue
  101. confirmationChecked.value = checkedValue
  102. } else {
  103. const paramValue = getQueryParamValue(arg.name)
  104. if (arg.type === 'checkbox') {
  105. // For checkboxes, handle boolean default values properly
  106. if (paramValue !== null) {
  107. argValues.value[arg.name] = paramValue === '1' || paramValue === 'true' || paramValue === true
  108. } else if (arg.defaultValue !== undefined && arg.defaultValue !== '') {
  109. argValues.value[arg.name] = arg.defaultValue === '1' || arg.defaultValue === 'true' || arg.defaultValue === true
  110. } else {
  111. argValues.value[arg.name] = false
  112. }
  113. } else {
  114. argValues.value[arg.name] = paramValue !== null ? paramValue : arg.defaultValue || ''
  115. }
  116. }
  117. })
  118. // Run initial validation on all fields after DOM is updated
  119. await nextTick()
  120. for (const arg of actionArguments.value) {
  121. if (arg.type && !arg.type.startsWith('regex:') && arg.type !== 'select' && arg.type !== '' && arg.type !== 'confirmation' && arg.type !== 'checkbox') {
  122. await validateArgument(arg, argValues.value[arg.name] || '')
  123. }
  124. }
  125. }
  126. function getQueryParamValue(paramName) {
  127. const params = new URLSearchParams(window.location.search.substring(1))
  128. return params.get(paramName)
  129. }
  130. function formatLabel(title) {
  131. const lastChar = title.charAt(title.length - 1)
  132. if (lastChar === '?' || lastChar === '.' || lastChar === ':') {
  133. return title
  134. }
  135. return title + ':'
  136. }
  137. function getInputComponent(arg) {
  138. if (arg.type === 'html') {
  139. return 'div'
  140. } else if (arg.type === 'raw_string_multiline') {
  141. return 'textarea'
  142. } else if (arg.choices && arg.choices.length > 0 && (arg.type === 'select' || arg.type === '')) {
  143. return 'select'
  144. } else {
  145. return 'input'
  146. }
  147. }
  148. function getInputType(arg) {
  149. if (arg.type === 'html' || arg.type === 'raw_string_multiline' || arg.type === 'select') {
  150. return undefined
  151. }
  152. if (arg.type === 'confirmation') {
  153. return 'checkbox'
  154. }
  155. if (arg.type === 'ascii_identifier' || arg.type === 'ascii') {
  156. return 'text'
  157. }
  158. if (arg.type === 'datetime') {
  159. return 'datetime-local'
  160. }
  161. return arg.type
  162. }
  163. function getPattern(arg) {
  164. if (arg.type && arg.type.startsWith('regex:')) {
  165. return arg.type.replace('regex:', '')
  166. }
  167. return undefined
  168. }
  169. function getArgumentValue(arg) {
  170. if (arg.type === 'checkbox' || arg.type === 'confirmation') {
  171. return argValues.value[arg.name] === '1' || argValues.value[arg.name] === true || argValues.value[arg.name] === 'true'
  172. }
  173. return argValues.value[arg.name] || ''
  174. }
  175. function handleInput(arg, event) {
  176. const value = event.target.type === 'checkbox' ? event.target.checked : event.target.value
  177. argValues.value[arg.name] = value
  178. updateUrlWithArg(arg.name, value)
  179. }
  180. function handleChange(arg, event) {
  181. if (arg.type === 'confirmation') {
  182. confirmationChecked.value = event.target.checked
  183. return
  184. }
  185. // Validate the input
  186. validateArgument(arg, event.target.value)
  187. }
  188. async function validateArgument(arg, value) {
  189. if (!arg.type || arg.type.startsWith('regex:')) {
  190. return
  191. }
  192. // Skip validation for datetime - backend will handle mangling values without seconds
  193. if (arg.type === 'datetime') {
  194. const inputElement = document.getElementById(arg.name)
  195. if (inputElement) {
  196. inputElement.setCustomValidity('')
  197. }
  198. delete formErrors.value[arg.name]
  199. return
  200. }
  201. // Skip validation for checkbox and confirmation - they're always valid
  202. if (arg.type === 'checkbox' || arg.type === 'confirmation') {
  203. const inputElement = document.getElementById(arg.name)
  204. if (inputElement) {
  205. inputElement.setCustomValidity('')
  206. }
  207. delete formErrors.value[arg.name]
  208. return
  209. }
  210. try {
  211. const validateArgumentTypeArgs = {
  212. value: value,
  213. type: arg.type,
  214. bindingId: props.bindingId,
  215. argumentName: arg.name
  216. }
  217. const validation = await window.client.validateArgumentType(validateArgumentTypeArgs)
  218. // Get the input element to set custom validity
  219. const inputElement = document.getElementById(arg.name)
  220. if (validation.valid) {
  221. delete formErrors.value[arg.name]
  222. // Clear custom validity message
  223. if (inputElement) {
  224. inputElement.setCustomValidity('')
  225. }
  226. } else {
  227. formErrors.value[arg.name] = validation.description
  228. // Set custom validity message
  229. if (inputElement) {
  230. inputElement.setCustomValidity(validation.description)
  231. }
  232. }
  233. } catch (err) {
  234. console.warn('Validation failed:', err)
  235. // On error, clear any custom validity
  236. const inputElement = document.getElementById(arg.name)
  237. if (inputElement) {
  238. inputElement.setCustomValidity('')
  239. }
  240. }
  241. }
  242. function updateUrlWithArg(name, value) {
  243. if (name && value !== undefined) {
  244. const url = new URL(window.location.href)
  245. // Don't add passwords to URL
  246. const arg = actionArguments.value.find(a => a.name === name)
  247. if (arg && arg.type === 'password') {
  248. return
  249. }
  250. url.searchParams.set(name, value)
  251. window.history.replaceState({}, '', url.toString())
  252. }
  253. }
  254. function getArgumentValues() {
  255. const ret = []
  256. for (const arg of actionArguments.value) {
  257. let value = argValues.value[arg.name] || ''
  258. if (arg.type === 'checkbox' || arg.type === 'confirmation') {
  259. value = value ? '1' : '0'
  260. }
  261. ret.push({
  262. name: arg.name,
  263. value: value
  264. })
  265. }
  266. return ret
  267. }
  268. function getUniqueId() {
  269. if (window.isSecureContext) {
  270. return window.crypto.randomUUID()
  271. } else {
  272. return Date.now().toString()
  273. }
  274. }
  275. function getBrowserSuggestions(arg) {
  276. if (!arg.suggestionsBrowserKey) {
  277. return []
  278. }
  279. try {
  280. const stored = localStorage.getItem(`olivetin-suggestions-${arg.suggestionsBrowserKey}`)
  281. if (stored) {
  282. const suggestions = JSON.parse(stored)
  283. return Array.isArray(suggestions) ? suggestions : []
  284. }
  285. } catch (err) {
  286. console.warn('Failed to load browser suggestions:', err)
  287. }
  288. return []
  289. }
  290. function saveBrowserSuggestions() {
  291. for (const arg of actionArguments.value) {
  292. if (arg.suggestionsBrowserKey) {
  293. const value = argValues.value[arg.name]
  294. // Only save non-empty values for non-checkbox/confirmation/password types
  295. if (value && value !== '' && arg.type !== 'checkbox' && arg.type !== 'confirmation' && arg.type !== 'password') {
  296. try {
  297. const key = `olivetin-suggestions-${arg.suggestionsBrowserKey}`
  298. const stored = localStorage.getItem(key)
  299. let suggestions = []
  300. if (stored) {
  301. suggestions = JSON.parse(stored)
  302. if (!Array.isArray(suggestions)) {
  303. suggestions = []
  304. }
  305. }
  306. // Add value if not already present
  307. if (!suggestions.includes(value)) {
  308. suggestions.unshift(value) // Add to beginning
  309. // Keep only the most recent 50 suggestions
  310. if (suggestions.length > 50) {
  311. suggestions = suggestions.slice(0, 50)
  312. }
  313. localStorage.setItem(key, JSON.stringify(suggestions))
  314. }
  315. } catch (err) {
  316. console.warn('Failed to save browser suggestions:', err)
  317. }
  318. }
  319. }
  320. }
  321. }
  322. async function startAction(actionArgs) {
  323. const startActionArgs = {
  324. bindingId: props.bindingId,
  325. arguments: actionArgs,
  326. uniqueTrackingId: getUniqueId()
  327. }
  328. try {
  329. const response = await window.client.startAction(startActionArgs)
  330. console.log('Action started successfully with tracking ID:', response.executionTrackingId)
  331. return response
  332. } catch (err) {
  333. console.error('Failed to start action:', err)
  334. throw err
  335. }
  336. }
  337. async function handleSubmit(event) {
  338. // Set custom validity for required fields
  339. for (const arg of actionArguments.value) {
  340. const value = argValues.value[arg.name]
  341. const inputElement = document.getElementById(arg.name)
  342. if (arg.required && (!value || value === '')) {
  343. formErrors.value[arg.name] = 'This field is required'
  344. // Set custom validity for required field validation
  345. if (inputElement) {
  346. inputElement.setCustomValidity('This field is required')
  347. }
  348. }
  349. }
  350. const form = event.target
  351. if (!form.checkValidity()) {
  352. console.log('argument form has elements that failed validation')
  353. return
  354. }
  355. event.preventDefault()
  356. const argvs = getArgumentValues()
  357. console.log('argument form has elements that passed validation')
  358. // Save values to localStorage for arguments with suggestionsBrowserKey
  359. saveBrowserSuggestions()
  360. try {
  361. const response = await startAction(argvs)
  362. if (popupOnStart.value && popupOnStart.value.includes('execution-dialog')) {
  363. router.push(`/logs/${response.executionTrackingId}`)
  364. } else {
  365. router.back()
  366. }
  367. } catch (err) {
  368. console.error('Failed to start action:', err)
  369. }
  370. }
  371. function handleCancel() {
  372. router.back()
  373. clearBookmark()
  374. }
  375. function clearBookmark() {
  376. window.history.replaceState({
  377. path: window.location.pathname
  378. }, '', window.location.pathname)
  379. }
  380. function show() {
  381. if (dialog.value) {
  382. dialog.value.showModal()
  383. }
  384. }
  385. function close() {
  386. if (dialog.value) {
  387. dialog.value.close()
  388. }
  389. }
  390. // Expose methods for parent components
  391. defineExpose({
  392. show,
  393. close
  394. })
  395. // Lifecycle
  396. onMounted(() => {
  397. setup()
  398. })
  399. </script>
  400. <style scoped>
  401. form {
  402. grid-template-columns: max-content auto auto;
  403. }
  404. .argument-description {
  405. font-size: 0.875rem;
  406. color: #666;
  407. margin-top: 0.25rem;
  408. }
  409. .buttons {
  410. display: flex;
  411. gap: 0.5rem;
  412. justify-content: flex-end;
  413. padding-top: 1rem;
  414. border-top: 1px solid #eee;
  415. }
  416. /* Checkbox specific styling */
  417. .argument-group input[type="checkbox"] {
  418. width: auto;
  419. margin-right: 0.5rem;
  420. }
  421. .argument-group input[type="checkbox"]+label {
  422. display: inline;
  423. font-weight: normal;
  424. }
  425. </style>