-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgmaps-context-menu.js
180 lines (153 loc) · 5.66 KB
/
gmaps-context-menu.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
/**
* Meta context-menu object that all GmapsContextMenu's call on menu item click.
*/
const GmapsContext = (function () {
const settings = {
registerCloseForEverything: true
}
const contexts = {};
return {
register: function (ID, object) {
contexts[ID] = object;
},
onMenuRegister: function () {
/**
* When multiple context menus are open for multiple mapOrShapes,
* close them all when clicked outside all context menus.
*/
if (settings.registerCloseForEverything) {
console.log('register close for everything')
for (let id1 in contexts) {
const contextA = contexts[id1];
for (let id2 in contexts) {
if (id1 === id2) {
continue;
}
const contextB = contexts[id2];
const bindings = contextB.BINDED_TO;
for (let i=0; i < bindings.length; i++) {
contextA.registerCloseFor(bindings[i]);
}
}
}
}
},
runOption: function (ID, index) {
const context = contexts[ID];
const position = context.domReadyPosition;
context.settings.options[index].onclick({lat: position.lat(), lng: position.lng()});
context.close();
},
setSetting: function (setting, value) {
settings[setting] = value;
}
};
}());
/**
* Hacky way of creating a context menu by using and modifying an InfoWindow
*
* @author mattwright324
*/
class GmapsContextMenu {
// Random ID so that multiple custom context menus can be used at the same time
ID = '_' + Math.random().toString(36).substr(2, 9)
// Coords not visible in map. Initially set to this location before context-menu CSS
// has been added to the InfoWindow to prevent flicker of original style.
POS_HIDDEN = {lat: -200, lng: 200}
// InfoWindow being used as a context menu
INFO_WINDOW = new google.maps.InfoWindow({
disableAutoPan: true
})
DEFAULT_SETTINGS = {
registerOpenForMap: true,
registerCloseForMap: true
}
BINDED_TO = []
// Store mouse rightclick location. Add context-menu CSS first then set location.
domReadyPosition = {lat: 0, lng: 0}
/**
* @param map required, registers context menu for map
* @param settings optional
*/
constructor(map, settings = {}) {
GmapsContext.register(this.ID, this);
this.map = map;
this.settings = settings;
for (let key in this.DEFAULT_SETTINGS) {
if (!this.settings.hasOwnProperty(key)) {
this.settings[key] = this.DEFAULT_SETTINGS[key];
}
}
if (this.settings.registerOpenForMap) {
this.registerOpenFor(map);
}
if (this.settings.registerCloseForMap) {
this.registerCloseFor(map);
}
this.updateOptions();
// Listen
const menu = this;
google.maps.event.addListener(menu.INFO_WINDOW, "domready", function () {
menu.updateOptions();
const contextMenuEls = document.getElementsByClassName("context-menu-content");
for (let i = 0; i < contextMenuEls.length; i++) {
const iwT = contextMenuEls[i].parentElement.parentElement.parentElement.parentElement;
iwT.classList.add("context-menu");
iwT.setAttribute("oncontextmenu", "return false;")
}
// Set position once context-menu-css has been added
menu.INFO_WINDOW.setPosition(menu.domReadyPosition);
});
}
updateOptions() {
const optionsHtml = [];
if (!this.settings || !this.settings.options) {
optionsHtml.push("<li style='color:lightgray'>No options added</li>")
} else {
for (let i = 0; i < this.settings.options.length; i++) {
const option = this.settings.options[i];
if (!option.hasOwnProperty("showWhen") || option.showWhen()) {
optionsHtml.push("<li onclick='GmapsContext.runOption(\"" + this.ID + "\", " + i + ")'>" + option.text + "</li>")
}
}
}
const html =
"<div id='" + this.ID + "' class='context-menu-content'>" +
"<ul>" + optionsHtml.join("") + "</ul>" +
"</div>"
this.INFO_WINDOW.setContent(html);
}
registerFor(mapOrShape) {
this.registerOpenFor(mapOrShape);
this.registerCloseFor(mapOrShape);
}
registerOpenFor(mapOrShape) {
const menu = this;
mapOrShape.addListener('rightclick', function (event) {
menu.domReadyPosition = event.latLng;
if (menu.INFO_WINDOW.getMap()) {
menu.INFO_WINDOW.setPosition(menu.domReadyPosition);
} else {
menu.INFO_WINDOW.setPosition(menu.POS_HIDDEN)
menu.INFO_WINDOW.open(menu.map);
}
});
if (this.BINDED_TO.indexOf(mapOrShape) === -1) {
this.BINDED_TO.push(mapOrShape);
GmapsContext.onMenuRegister();
}
}
registerCloseFor(mapOrShape) {
const menu = this;
mapOrShape.addListener('click', function (event) {
menu.close();
});
if (this.BINDED_TO.indexOf(mapOrShape) === -1) {
this.BINDED_TO.push(mapOrShape);
GmapsContext.onMenuRegister();
}
}
close() {
this.INFO_WINDOW.close();
}
}