forked from slutske22/leaflet-popup-modifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
popupMod.js
191 lines (147 loc) · 7.52 KB
/
popupMod.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
// Adding nametag labels to all popup-able leaflet layers
const sourceTypes = ['Layer','Circle','CircleMarker','Marker','Polyline','Polygon','ImageOverlay','VideoOverlay','SVGOverlay','Rectangle','LayerGroup','FeatureGroup','GeoJSON']
sourceTypes.forEach( type => {
L[type].include({
nametag: type.toLowerCase()
})
})
// Adding new options to the default options of a popup
L.Popup.mergeOptions({
removable: false,
editable: false,
})
// Modifying the popup mechanics
L.Popup.include({
// modifying the _initLayout method to include edit and remove buttons, if those options are enabled
// ---------------- Source code ---------------------------- //
// original from https://github.com/Leaflet/Leaflet/blob/master/src/layer/Popup.js
_initLayout: function () {
var prefix = 'leaflet-popup',
container = this._container = L.DomUtil.create('div',
prefix + ' ' + (this.options.className || '') +
' leaflet-zoom-animated');
var wrapper = this._wrapper = L.DomUtil.create('div', prefix + '-content-wrapper', container);
this._contentNode = L.DomUtil.create('div', prefix + '-content', wrapper);
L.DomEvent.disableClickPropagation(wrapper);
L.DomEvent.disableScrollPropagation(this._contentNode);
L.DomEvent.on(wrapper, 'contextmenu', L.DomEvent.stopPropagation);
this._tipContainer = L.DomUtil.create('div', prefix + '-tip-container', container);
this._tip = L.DomUtil.create('div', prefix + '-tip', this._tipContainer);
if (this.options.closeButton) {
var closeButton = this._closeButton = L.DomUtil.create('a', prefix + '-close-button', container);
closeButton.href = '#close';
closeButton.innerHTML = '×';
L.DomEvent.on(closeButton, 'click', this._onCloseButtonClick, this);
}
// ---------------- Source code ---------------------------- //
// --------------- My additions --------------------------- //
var nametag
if (this.options.nametag){
nametag = this.options.nametag
} else if (this._source) {
nametag = this._source.nametag
} else {
nametag = "popup"
}
if (this.options.removable && !this.options.editable){
var userActionButtons = this._userActionButtons = L.DomUtil.create('div', prefix + '-useraction-buttons', wrapper);
var removeButton = this._removeButton = L.DomUtil.create('a', prefix + '-remove-button', userActionButtons);
removeButton.href = '#close';
removeButton.innerHTML = `Remove this ${nametag}`;
this.options.minWidth = 110;
L.DomEvent.on(removeButton, 'click', this._onRemoveButtonClick, this);
}
if (this.options.editable && !this.options.removable){
var userActionButtons = this._userActionButtons = L.DomUtil.create('div', prefix + '-useraction-buttons', wrapper);
var editButton = this._editButton = L.DomUtil.create('a', prefix + '-edit-button', userActionButtons);
editButton.href = '#edit';
editButton.innerHTML = 'Edit';
L.DomEvent.on(editButton, 'click', this._onEditButtonClick, this);
}
if (this.options.editable && this.options.removable){
var userActionButtons = this._userActionButtons = L.DomUtil.create('div', prefix + '-useraction-buttons', wrapper);
var removeButton = this._removeButton = L.DomUtil.create('a', prefix + '-remove-button', userActionButtons);
removeButton.href = '#close';
removeButton.innerHTML = `Remove this ${nametag}`;
var editButton = this._editButton = L.DomUtil.create('a', prefix + '-edit-button', userActionButtons);
editButton.href = '#edit';
editButton.innerHTML = 'Edit';
this.options.minWidth = 160;
L.DomEvent.on(removeButton, 'click', this._onRemoveButtonClick, this);
L.DomEvent.on(editButton, 'click', this._onEditButtonClick, this);
}
},
_onRemoveButtonClick: function (e) {
this._source.remove();
L.DomEvent.stop(e);
var event = new CustomEvent("removeMarker", {
detail: {
marker: this._source
}
});
document.dispatchEvent(event);
},
_onEditButtonClick: function (e) {
//Needs to be defined first to capture width before changes are applied
var inputFieldWidth = this._inputFieldWidth = this._container.offsetWidth - 2*19;
this._contentNode.style.display = "none";
this._userActionButtons.style.display = "none";
var wrapper = this._wrapper;
var editScreen = this._editScreen = L.DomUtil.create('div', 'leaflet-popup-edit-screen', wrapper)
var inputField = this._inputField = L.DomUtil.create('div', 'leaflet-popup-input', editScreen);
inputField.setAttribute("contenteditable", "true");
inputField.innerHTML = this.getContent()
// ----------- Making the input field grow till max width ------- //
inputField.style.width = inputFieldWidth + 'px';
var inputFieldDiv = L.DomUtil.get(this._inputField);
// create invisible div to measure the text width in pixels
var ruler = L.DomUtil.create('div', 'leaflet-popup-input-ruler', editScreen);
let thisStandIn = this;
// Padd event listener to the textinput to trigger a check
this._inputField.addEventListener("keydown", function(){
// Check to see if the popup is already at its maxWidth
// and that text doesnt take up whole field
if (thisStandIn._container.offsetWidth < thisStandIn.options.maxWidth + 38
&& thisStandIn._inputFieldWidth + 5 < inputFieldDiv.clientWidth){
ruler.innerHTML = inputField.innerHTML;
if (ruler.offsetWidth + 20 > inputFieldDiv.clientWidth){
console.log('expand now');
inputField.style.width = thisStandIn._inputField.style.width = ruler.offsetWidth + 10 + 'px';
thisStandIn.update();
}
}
}, false)
var inputActions = this._inputActions = L.DomUtil.create('div', 'leaflet-popup-input-actions', editScreen);
var cancelButton = this._cancelButton = L.DomUtil.create('a', 'leaflet-popup-input-cancel', inputActions);
cancelButton.href = '#cancel';
cancelButton.innerHTML = 'Cancel';
var saveButton = this._saveButton = L.DomUtil.create('a', 'leaflet-popup-input-save', inputActions);
saveButton.href = "#save";
saveButton.innerHTML = 'Save';
L.DomEvent.on(cancelButton, 'click', this._onCancelButtonClick, this)
L.DomEvent.on(saveButton, 'click', this._onSaveButtonClick, this)
this.update();
L.DomEvent.stop(e);
},
_onCancelButtonClick: function (e) {
L.DomUtil.remove(this._editScreen);
this._contentNode.style.display = "block";
this._userActionButtons.style.display = "flex";
this.update();
L.DomEvent.stop(e);
},
_onSaveButtonClick: function (e) {
var inputField = this._inputField;
if (inputField.innerHTML.length > 0){
this.setContent(inputField.innerHTML)
} else {
alert('Enter something');
};
L.DomUtil.remove(this._editScreen);
this._contentNode.style.display = "block";
this._userActionButtons.style.display = "flex";
this.update();
L.DomEvent.stop(e);
// ---------------------End my additions --------------------------------------- //
}
})