diff options
author | Joris | 2022-07-05 21:55:41 +0200 |
---|---|---|
committer | Joris | 2023-01-28 09:35:55 +0100 |
commit | 063d8ef9eaf874a941f4459e831057dd0a1b7ddd (patch) | |
tree | c4a8b27cb8fdb5d1dc26c560c7483c9593f40dac /src/markerForm.ts | |
parent | 2936f06576997bffe7903ea840df563a408efc21 (diff) |
Rewrite in TSmain
Diffstat (limited to 'src/markerForm.ts')
-rw-r--r-- | src/markerForm.ts | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/src/markerForm.ts b/src/markerForm.ts new file mode 100644 index 0000000..54670ae --- /dev/null +++ b/src/markerForm.ts @@ -0,0 +1,116 @@ +import { h } from 'lib/h' +import * as AutoComplete from 'lib/autoComplete' +import * as Button from 'lib/button' +import * as Dom from 'lib/dom' +import * as Form from 'lib/form' +import * as Icons from 'lib/icons' +import * as Layout from 'lib/layout' +import * as State from 'state' + +interface FormParams { + onValidate: (color: string, icon: string, name: string, radius: number) => void, + onCancel: () => void, + color: string, + icon: string, + name: string, + radius: number, +} + +export function view({ onValidate, onCancel, color, icon, name, radius }: FormParams): Element { + var radiusStr = radius.toString() + const onSubmit = () => onValidate(color, icon, name, parseInt(radiusStr) || 0) + const domIcon = h('div', + { className: 'g-MarkerForm__Icon' }, + Icons.get(icon, { fill: 'black', stroke: 'black' }) + ) + return h('div', + {}, + Layout.section( + {}, + h('form', + { className: 'g-MarkerForm', + onsubmit: (e: Event) => { + e.preventDefault() + onSubmit() + } + }, + Layout.section( + {}, + Form.input({ + label: 'Name', + attrs: { + oninput: (e: Event) => { + if (e.target !== null) { + name = (e.target as HTMLInputElement).value + } + }, + onblur: (e: Event) => { + if (e.target !== null) { + name = (e.target as HTMLInputElement).value.trim() + } + }, + value: name + } + }), + Form.colorInput({ + colors: State.colors(), + label: 'Color', + initValue: color, + onInput: newColor => color = newColor + }), + h('div', + { className: 'g-Form__Field' }, + h('div', + { className: 'g-Form__Label' }, + h('label', { for: 'g-MarkerForm__IconInput' }, 'Icon') + ), + Layout.line( + { className: 'g-MarkerForm__AutoCompleteAndIcon' }, + AutoComplete.create( + { value: icon, + className: 'g-MarkerForm__AutoComplete' + }, + 'g-MarkerForm__IconInput', + Icons.keys().sort(), + iconKey => h('div', + { className: 'g-MarkerForm__IconEntry' }, + h('div', { className: 'g-MarkerForm__IconElem' }, Icons.get(iconKey, { fill: 'black', stroke: 'black' }) ), + iconKey + ), + newIcon => { + icon = newIcon + Dom.replaceChildren(domIcon, Icons.get(icon, { fill: 'black', stroke: 'black' })) + }), + domIcon + ) + ), + Form.input({ + label: 'Radius (m)', + attrs: { oninput: (e: Event) => { + if (e.target !== null) { + radiusStr = (e.target as HTMLInputElement).value + } + }, + value: radiusStr, + type: 'number', + } + }) + ), + ), + Layout.line( + {}, + Button.action({ onclick: () => onSubmit() }, 'Save'), + Button.cancel( + { onclick: () => onCancel(), + type: 'button' + }, + 'Cancel' + ) + ) + ) + ) +} + +function restrictCharacters(str: string, chars: string): string { + return str.split('').filter(c => chars.indexOf(c) != -1).join('') +} |