Skip to content

Commit 34a679f

Browse files
committed
Restructured layout, icon is a separate element and text can be selected without collapsing the control
1 parent 93319b4 commit 34a679f

File tree

2 files changed

+63
-26
lines changed

2 files changed

+63
-26
lines changed

Control.Geocoder.css

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,49 @@
1+
.leaflet-control-geocoder {
2+
background: white;
3+
box-shadow: 0 1px 7px rgba(0,0,0,0.65);
4+
-webkit-border-radius: 4px;
5+
border-radius: 4px;
6+
}
7+
8+
.leaflet-touch .leaflet-bar {
9+
-webkit-border-radius: 10px;
10+
border-radius: 10px;
11+
}
12+
13+
.leaflet-control-geocoder-form {
14+
display: inline;
15+
}
16+
117
.leaflet-control-geocoder-form input, .leaflet-control-geocoder-form ul, .leaflet-control-geocoder-error {
2-
float: right;
18+
/* float: right;
319
clear: both;
420
box-shadow: 0 1px 7px rgba(0,0,0,0.4);
521
background: #f8f8f9;
622
-webkit-border-radius: 5px;
723
border-radius: 5px;
824
border: 0;
25+
color: transparent;*/
26+
border: 0;
927
color: transparent;
28+
background: white;
1029
}
1130

1231
.leaflet-control-geocoder-form input {
32+
font-size: 16px;
33+
width: 0;
34+
transition: width 0.125s ease-in;
35+
}
36+
37+
.leaflet-control-geocoder-icon {
38+
width: 26px;
39+
height: 26px;
1340
background-image: url(images/geocoder.png);
1441
background-repeat: no-repeat;
1542
background-position: right;
16-
font-size: 14px;
17-
width: 26px;
18-
height: 26px;
19-
transition: all 0.125s ease-in;
43+
float: right;
2044
}
2145

22-
.leaflet-control-geocoder-throbber input {
46+
.leaflet-control-geocoder-throbber .leaflet-control-geocoder-icon {
2347
background-image: url(images/throbber.gif);
2448
}
2549

@@ -49,12 +73,21 @@
4973
}
5074

5175
ul.leaflet-control-geocoder-alternatives {
52-
width: 234px;
76+
width: 260px;
5377
overflow: hidden;
5478
text-overflow: ellipsis;
5579
white-space: nowrap;
5680
list-style: none;
5781
padding: 0;
82+
transition: height 0.125s ease-in;
83+
}
84+
85+
.leaflet-control-geocoder-alternatives-minimized {
86+
width: 0 !important;
87+
height: 0;
88+
overflow: hidden;
89+
margin: 0;
90+
padding: 0;
5891
}
5992

6093
.leaflet-control-geocoder-alternatives li {
@@ -78,5 +111,5 @@ ul.leaflet-control-geocoder-alternatives {
78111
}
79112

80113
.leaflet-control-geocoder-alternatives a:hover, .leaflet-control-geocoder-selected {
81-
background-color: #eee;
114+
background-color: #ddd;
82115
}

Control.Geocoder.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,58 +36,63 @@
3636
},
3737

3838
onAdd: function (map) {
39-
this._map = map;
40-
var className = 'leaflet-control-geocoder';
41-
42-
var form = this._form = L.DomUtil.create('form', className + '-form');
39+
var className = 'leaflet-control-geocoder',
40+
container = L.DomUtil.create('div', className),
41+
form = this._form = L.DomUtil.create('form', className + '-form', container),
42+
input,
43+
icon;
4344

44-
var input = this._input = document.createElement('input');
45+
this._map = map;
46+
input = this._input = document.createElement('input');
4547
input.type = 'text';
4648

4749
L.DomEvent.addListener(input, 'keydown', this._keydown, this);
4850
//L.DomEvent.addListener(input, 'onpaste', this._clearResults, this);
4951
//L.DomEvent.addListener(input, 'oninput', this._clearResults, this);
5052

53+
icon = L.DomUtil.create('div', 'leaflet-control-geocoder-icon');
54+
container.appendChild(icon);
55+
5156
this._errorElement = document.createElement('div');
5257
this._errorElement.className = className + '-form-no-error';
5358
this._errorElement.innerHTML = this.options.errorMessage;
5459

55-
this._alts = L.DomUtil.create('ul', className + '-alternatives');
56-
this._alts.style.display = 'none';
60+
this._alts = L.DomUtil.create('ul', className + '-alternatives leaflet-control-geocoder-alternatives-minimized');
5761

5862
form.appendChild(input);
5963
form.appendChild(this._errorElement);
60-
form.appendChild(this._alts);
64+
container.appendChild(this._alts);
6165

6266
L.DomEvent.addListener(form, 'submit', this._geocode, this);
6367

6468
if (this.options.collapsed) {
6569
if (this.options.expand === 'click') {
66-
L.DomEvent.addListener(input, 'click', function(e) {
70+
L.DomEvent.addListener(icon, 'click', function(e) {
6771
// TODO: touch
6872
if (e.button === 0 && e.detail === 1) {
6973
this._toggle();
7074
}
7175
}, this);
7276
} else {
73-
L.DomEvent.addListener(input, 'mouseover', this._expand, this);
74-
L.DomEvent.addListener(input, 'mouseout', this._collapse, this);
77+
L.DomEvent.addListener(icon, 'mouseover', this._expand, this);
78+
L.DomEvent.addListener(icon, 'mouseout', this._collapse, this);
7579
this._map.on('movestart', this._collapse, this);
7680
}
7781
} else {
7882
this._expand();
7983
}
8084

81-
return form;
85+
return container;
8286
},
8387

8488
_geocodeResult: function (results) {
85-
this._form.className = this._form.className.replace(' leaflet-control-geocoder-throbber', '');
89+
L.DomUtil.removeClass(this._container, 'leaflet-control-geocoder-throbber');
8690
if (results.length === 1) {
8791
this._geocodeResultSelected(results[0]);
8892
} else if (results.length > 0) {
93+
this._alts.innerHTML = '';
8994
this._results = results;
90-
this._alts.style.display = 'block';
95+
L.DomUtil.removeClass(this._alts, 'leaflet-control-geocoder-alternatives-minimized');
9196
for (var i = 0; i < results.length; i++) {
9297
this._alts.appendChild(this._createAlt(results[i], i));
9398
}
@@ -114,7 +119,7 @@
114119
_geocode: function(event) {
115120
L.DomEvent.preventDefault(event);
116121

117-
this._form.className += ' leaflet-control-geocoder-throbber';
122+
L.DomUtil.addClass(this._container, 'leaflet-control-geocoder-throbber');
118123
this._clearResults();
119124
this.options.geocoder.geocode(this._input.value, this._geocodeResult, this);
120125

@@ -143,12 +148,11 @@
143148

144149
_collapse: function () {
145150
this._container.className = this._container.className.replace(' leaflet-control-geocoder-expanded', '');
146-
this._alts.style.display = 'none';
151+
L.DomUtil.addClass(this._alts, 'leaflet-control-geocoder-alternatives-minimized');
147152
},
148153

149154
_clearResults: function () {
150-
this._alts.style.display = 'none';
151-
this._alts.innerHTML = '';
155+
L.DomUtil.addClass(this._alts, 'leaflet-control-geocoder-alternatives-minimized');
152156
this._selection = null;
153157
L.DomUtil.removeClass(this._errorElement, 'leaflet-control-geocoder-error');
154158
},

0 commit comments

Comments
 (0)