aboutsummaryrefslogtreecommitdiff
path: root/src/markerForm.ts
blob: 54670aef3add96ee94c1a8886b814ac904f705c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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('')
}