-
Notifications
You must be signed in to change notification settings - Fork 47
/
Form.js
202 lines (178 loc) · 6.57 KB
/
Form.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import 'leaflet'
/**
* Forms consist of FormElements and are shown in the StyleForm
* There exists a MarkerForm to modify Markers and a GeometryForm to modify Geometries (i.e. rectangles...)
* Style options based on:
* - path: https://leafletjs.com/reference.html#path-options
* - icon: https://leafletjs.com/reference.html#icon
*/
export default function setupForm () {
L.StyleEditor.forms.Form = L.Class.extend({
initialize: function (options) {
if (options) {
L.setOptions(this, options)
}
this.options.initializedElements = []
},
/** create every FormElement in the parentUiElement */
create: function (parentUiElement) {
this.options.parentUiElement = parentUiElement
let formElements = this.getFormElements()
let styleFormKeys = Object.keys(formElements)
for (let i = 0; i < styleFormKeys.length; i++) {
let formElement = this.getFormElementClass(styleFormKeys[i], formElements)
if (formElement !== undefined) {
formElement.create(parentUiElement)
this.options.initializedElements.push(formElement)
}
}
},
/** hide the Form including its FormElements */
hide: function () {
this.hideFormElements()
this.hideForm()
},
/** hide the FormElements */
hideFormElements: function () {
for (let i = 0; i < this.options.initializedElements.length; i++) {
this.options.initializedElements[i].hide()
}
},
/** hide the Form */
hideForm: function () {
this.options.styleEditorOptions.util.hideElement(this.options.parentUiElement)
},
/** make FormElements and Form visible */
show: function () {
this.preShow()
this.showFormElements()
this.showForm()
this.style()
},
/** hook which is called at the beginning of the show function */
preShow: function () {
},
/** make every FormElement visible */
showFormElements: function () {
for (let i = 0; i < this.options.initializedElements.length; i++) {
this.showFormElement(this.options.initializedElements[i])
}
},
/** make the Form visible */
showForm: function () {
this.options.styleEditorOptions.util.showElement(this.options.parentUiElement)
},
/** inform FormElements the selected style has changed, so they can adapt */
style: function () {
for (let i = 0; i < this.options.initializedElements.length; i++) {
this.options.initializedElements[i].style()
}
},
/** inform Form it lost it's focus */
lostFocus: function () {
for (let i = 0; i < this.options.initializedElements.length; i++) {
this.options.initializedElements[i].lostFocus()
}
},
/**
* @returns a Boolean indicating if the @param formElement should be shown
*/
showFormElement: function (formElement) {
// check wether element should be shown or not
if (this.showFormElementForStyleOption(formElement.options.styleOption)) {
formElement.show()
} else {
formElement.hide()
}
},
/**
* get the currently used formElements
* either standard or the ones provided while instanciation
*/
getFormElements: function () {
let formElements
if (this.options.formOptionKey in this.options.styleEditorOptions.forms) {
formElements = this.options.styleEditorOptions.forms[this.options.formOptionKey]
} else {
formElements = this.options.formElements
}
return formElements
},
/**
* get the Class of the Formelement to instanciate
* @param {*} styleOption, the styleOption to get the FormElement for
*/
getFormElementClass: function (styleOption) {
let formElements = this.getFormElements()
let formElementKeys = Object.keys(formElements)
if (formElementKeys.indexOf(styleOption) >= 0) {
let FormElement = formElements[styleOption]
if (FormElement) {
// may be a dictionary
if (typeof FormElement === 'boolean') {
return this.getFormElementStandardClass(styleOption)
}
if ('formElement' in FormElement && 'boolean' in FormElement) {
FormElement = FormElement['formElement']
}
// try to instantiate FormElementOption and return StandardClass if it does not work
try {
let formElementInstance = new FormElement({
styleOption: styleOption,
parentForm: this,
styleEditorOptions: this.options.styleEditorOptions
})
if (formElementInstance instanceof L.StyleEditor.formElements.FormElement) {
return formElementInstance
}
} catch (e) {
// creating instance failed fallback to StandardClass
}
}
// if nothing works return it
return this.getFormElementStandardClass(styleOption)
}
},
/**
* check whether a FormElement should be shown
* @param {*} styleOption, the styleOption to check
*/
showFormElementForStyleOption (styleOption) {
let formElements = this.getFormElements()
if (styleOption in formElements) {
let styleFormElement = formElements[styleOption]
// maybe a function is given to declare when to show the FormElement
if (typeof styleFormElement === 'function') {
try {
return styleFormElement(this.options.styleEditorOptions.util.getCurrentElement())
} catch (err) {
// the given function presumably is a constructor -> always show it
return true
}
}
// maybe a boolean is given to indicate whether to show it
if (typeof styleFormElement === 'boolean') {
return styleFormElement
}
// check for dictionary
if ('boolean' in styleFormElement) {
// in a dictionary boolean may be a function or boolean
if (typeof styleFormElement['boolean'] === 'function') {
return styleFormElement['boolean'](this.options.styleEditorOptions.util.getCurrentElement())
}
return styleFormElement['boolean']
}
return true
}
return false
},
/**
* get Leaflet.StyleEditor standard FormElement class for given styleOption
* @param {*} styleOption, the styleOption to get the standard class for
*/
getFormElementStandardClass (styleOption) {
return new this.options.formElements[styleOption](
{styleOption: styleOption, parentForm: this, styleEditorOptions: this.options.styleEditorOptions})
}
})
}