-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathIconElement.js
More file actions
152 lines (127 loc) · 5.07 KB
/
IconElement.js
File metadata and controls
152 lines (127 loc) · 5.07 KB
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
import 'leaflet'
/**
* FormElement used for styling the icon
*/
export default function setupIconElement () {
L.StyleEditor.formElements.IconElement = L.StyleEditor.formElements.FormElement.extend({
// private classed used in the code
_selectOptionWrapperClasses: 'leaflet-styleeditor-select-option-wrapper leaflet-styleeditor-hidden',
_selectOptionClasses: 'leaflet-styleeditor-select-option',
/** create the icon selectBoxes */
createContent: function () {
let uiElement = this.options.uiElement
let selectBox = L.DomUtil.create('div', 'leaflet-styleeditor-select', uiElement)
this.options.selectBoxImage = this._createSelectInputImage(selectBox)
L.DomEvent.addListener(selectBox, 'click', this._toggleSelectInput, this)
},
/** show the correct icon in the correct color if the icon or color changed */
style: function () {
let iconOptions = this.options.styleEditorOptions.markerType.getIconOptions()
this._styleSelectInputImage(this.options.selectBoxImage,
iconOptions.icon, iconOptions.iconColor)
this._createColorSelect(this.options.styleEditorOptions.markerType.options.iconOptions.iconColor)
this._hideSelectOptions()
},
/** if lost focus hide potentially open SelectOption */
lostFocus: function () {
this._hideSelectOptions()
},
/** create image container that hides/shows the iconSelectBox */
_createSelectInputImage: function (parentUiElement) {
let wrapper = L.DomUtil.create('div', 'leaflet-styleeditor-select-image-wrapper', parentUiElement)
return L.DomUtil.create('div', 'leaflet-styleeditor-select-image', wrapper)
},
/** create appropriate image for color and icon */
_styleSelectInputImage: function (image, icon, color) {
if (!icon) {
icon = image.getAttribute('value')
if (!icon) {
return
}
}
let iconOptions = this.options.styleEditorOptions.markerType.getIconOptions()
if (color) {
iconOptions.iconColor = color
}
image.innerHTML = ''
this.options.styleEditorOptions.markerType.createSelectHTML(image, iconOptions, icon)
image.setAttribute('value', icon)
},
/** create the selectBox with the icons in the correct color */
_createColorSelect: function (color) {
if (!this.options.selectOptions) {
this.options.selectOptions = {}
}
if (color in this.options.selectOptions) {
return
}
let uiElement = this.options.uiElement
let selectOptionWrapper =
L.DomUtil.create('ul', this._selectOptionWrapperClasses, uiElement)
this.options.styleEditorOptions.util.getMarkersForColor(color).forEach(function (option) {
let selectOption = L.DomUtil.create('li', this._selectOptionClasses, selectOptionWrapper)
let selectImage = this._createSelectInputImage(selectOption)
this._styleSelectInputImage(selectImage, option, color)
}, this)
this.options.selectOptions[color] = selectOptionWrapper
L.DomEvent.addListener(selectOptionWrapper, 'click', function (e) {
e.stopPropagation()
let target = e.target
if (target.nodeName === 'UL') {
return
}
if (target.parentNode.className === 'leaflet-styleeditor-select-image') {
target = target.parentNode
} else {
while (target && target.className !== 'leaflet-styleeditor-select-image') {
target = target.childNodes[0]
}
}
this._selectMarker({
'target': target
}, this)
}, this)
},
/** show/hide iconSelectBox */
_toggleSelectInput: function (e) {
let currentColorElement = this._getCurrentColorElement(
this.options.styleEditorOptions.util.rgbToHex(
this.options.styleEditorOptions.markerType.options.iconOptions.iconColor
)
)
let show = false
if (currentColorElement) {
show = L.DomUtil.hasClass(currentColorElement, 'leaflet-styleeditor-hidden')
}
this._hideSelectOptions()
if (show) {
this.options.styleEditorOptions.util.showElement(currentColorElement)
}
},
/** called when user selects a marker */
_selectMarker: function (e) {
let value = this._getValue(e.target)
// update style
this.options.selectBoxImage.setAttribute('value', value)
this.setStyle(value)
this._hideSelectOptions()
},
/** helper function to return attribute value of target */
_getValue: function (target) {
return target.getAttribute('value')
},
/** return correct selectBox depending on which color is currently chosen */
_getCurrentColorElement: function (color) {
if (!this.options.selectOptions[color]) {
this._createColorSelect(color)
}
return this.options.selectOptions[color]
},
/** hide open SelectOption */
_hideSelectOptions: function () {
for (let selectOption in this.options.selectOptions) {
this.options.styleEditorOptions.util.hideElement(this.options.selectOptions[selectOption])
}
}
})
}