diff options
Diffstat (limited to '_www/functions.ts')
| -rw-r--r-- | _www/functions.ts | 141 |
1 files changed, 135 insertions, 6 deletions
diff --git a/_www/functions.ts b/_www/functions.ts index 383fbac..086b3a7 100644 --- a/_www/functions.ts +++ b/_www/functions.ts @@ -1,15 +1,28 @@ import { WuiInputNumber, WuiInputNumberOpts } from "./wui/input/number.js" import { WuiInputString, WuiInputStringOpts } from "./wui/input/string.js" -import { CLASS_INPUT, CLASS_INPUT_LABEL, FormInput, FormInputKindNumber, FormInputKindString } from "./interface.js" +import { + CLASS_INPUT, + CLASS_INPUT_LABEL, + FormInput, + FormInputKindNumber, + FormInputKindString, + HttpTargetInterface, + TargetInterface, + WebSocketTargetInterface, +} from "./interface.js" -export function GenerateFormInput(parent: HTMLElement, fi: FormInput) { +export function GenerateFormInput( + parent: HTMLElement, + fi: FormInput, + value: string, +) { switch (fi.kind) { case FormInputKindNumber: let wui_input_number_opts: WuiInputNumberOpts = { label: fi.label, hint: fi.hint, - value: +fi.value, + value: +value, class_input: CLASS_INPUT, class_label: CLASS_INPUT_LABEL, is_hint_toggled: true, @@ -23,7 +36,9 @@ export function GenerateFormInput(parent: HTMLElement, fi: FormInput) { if (fi.min) { wui_input_number_opts.min = fi.min } - let wui_input_number = new WuiInputNumber(wui_input_number_opts) + let wui_input_number = new WuiInputNumber( + wui_input_number_opts, + ) parent.appendChild(wui_input_number.el) break @@ -31,7 +46,7 @@ export function GenerateFormInput(parent: HTMLElement, fi: FormInput) { let wui_input_string_opts: WuiInputStringOpts = { label: fi.label, hint: fi.hint, - value: fi.value, + value: value, class_input: CLASS_INPUT, class_label: CLASS_INPUT_LABEL, is_hint_toggled: true, @@ -39,7 +54,9 @@ export function GenerateFormInput(parent: HTMLElement, fi: FormInput) { fi.value = new_value }, } - let wui_input_string = new WuiInputString(wui_input_string_opts) + let wui_input_string = new WuiInputString( + wui_input_string_opts, + ) parent.appendChild(wui_input_string.el) break } @@ -68,3 +85,115 @@ export function HttpMethodToString(m: number): string { } return "???" } + +// +// LoadHttpTargetHeader get HttpTarget header from local storage by key. +// If no header exist in storage return the one from HttpTarget itself. +// +export function LoadHttpTargetHeader( + target: TargetInterface, + httpTarget: HttpTargetInterface, + key: string, +): string { + let storageKey = `${target.ID}.http.${httpTarget.ID}.header.${key}` + return ( + window.localStorage.getItem(storageKey) || + httpTarget.Headers[key].value + ) +} + +// +// LoadHttpTargetParam get HttpTarget parameter from local storage by key. +// If no parameter exist in storage return the one from HttpTarget itself. +// +export function LoadHttpTargetParam( + target: TargetInterface, + httpTarget: HttpTargetInterface, + key: string, +): string { + let storageKey = `${target.ID}.http.${httpTarget.ID}.var.${key}` + return ( + window.localStorage.getItem(storageKey) || + httpTarget.Params[key].value + ) +} + +// +// LoadTargetVar get target variable from local storage or return the original +// value. +// +export function LoadTargetVar(target: TargetInterface, key: string): string { + let storageKey = `${target.ID}.var.${key}` + return window.localStorage.getItem(storageKey) || target.Vars[key].value +} + +// +// LoadWsTargetHeader get the WebSocketTarget from local storage by key. +// +export function LoadWsTargetHeader( + target: TargetInterface, + wsTarget: WebSocketTargetInterface, + key: string, +): string { + let storageKey = `${target.ID}.ws.${wsTarget.ID}.header.${key}` + return ( + window.localStorage.getItem(storageKey) || + wsTarget.Headers[key].value + ) +} + +// +// LoadWsTargetParam get the WebSocketTarget parameter from local storage or +// return the one from wsTarget if its not exist. +// +export function LoadWsTargetParam( + target: TargetInterface, + wsTarget: WebSocketTargetInterface, + key: string, +): string { + let storageKey = `${target.ID}.ws.${wsTarget.ID}.var.${key}` + return ( + window.localStorage.getItem(storageKey) || + wsTarget.Params[key].value + ) +} + +// +// Save the variables on the Target, Params and Headers on HttpTarget or +// WebSocket to local storage. +// +export function Save( + target: TargetInterface, + httpTarget: HttpTargetInterface | null, + wsTarget: WebSocketTargetInterface | null, +) { + for (const k in target.Vars) { + let fi = target.Vars[k] + let key = `${target.ID}.var.${k}` + window.localStorage.setItem(key, fi.value) + } + if (httpTarget) { + for (const k in httpTarget.Headers) { + let fi = httpTarget.Headers[k] + let key = `${target.ID}.http.${httpTarget.ID}.header.${k}` + window.localStorage.setItem(key, fi.value) + } + for (const k in httpTarget.Params) { + let fi = httpTarget.Params[k] + let key = `${target.ID}.http.${httpTarget.ID}.param.${k}` + window.localStorage.setItem(key, fi.value) + } + } + if (wsTarget) { + for (const k in wsTarget.Headers) { + let fi = wsTarget.Headers[k] + let key = `${target.ID}.http.${wsTarget.ID}.header.${k}` + window.localStorage.setItem(key, fi.value) + } + for (const k in wsTarget.Params) { + let fi = wsTarget.Params[k] + let key = `${target.ID}.ws.${wsTarget.ID}.param.${k}` + window.localStorage.setItem(key, fi.value) + } + } +} |
