Permalink
openstreetmap-website/app/assets/javascripts/index.js
Newer
100644
413 lines (337 sloc)
10.6 KB
3
//= require leaflet.sidebar-pane
4
//= require leaflet.locatecontrol/src/L.Control.Locate
12
//= require leaflet.contextmenu
15
//= require index/browse
16
//= require index/export
17
//= require index/notes
30
var map = new L.OSM.Map("map", {
31
zoomControl: false,
32
layerControl: false,
33
contextmenu: true,
34
worldCopyJump: true
37
OSM.loadSidebarContent = function (path, callback) {
44
loaderTimeout = setTimeout(function () {
45
$("#sidebar_loader").show();
46
}, 200);
47
48
// IE<10 doesn't respect Vary: X-Requested-With header, so
49
// prevent caching the XHR response as a full-page URL.
50
if (content_path.indexOf("?") >= 0) {
51
content_path += "&xhr=1";
64
$("#flash").empty();
65
$("#sidebar_loader").hide();
66
67
var content = $(xhr.responseText);
68
69
if (xhr.getResponseHeader("X-Page-Title")) {
70
var title = xhr.getResponseHeader("X-Page-Title");
71
document.title = decodeURIComponent(title);
74
$("head")
75
.find("link[type=\"application/atom+xml\"]")
78
$("head")
79
.append(content.filter("link[type=\"application/atom+xml\"]"));
81
$("#sidebar_content").html(content.not("link[type=\"application/atom+xml\"]"));
92
map.attributionControl.setPrefix("");
96
map.on("baselayerchange", function (e) {
97
if (map.getZoom() > e.layer.options.maxZoom) {
98
map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
99
}
100
});
101
102
var sidebar = L.OSM.sidebar("#map-ui")
105
var position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
107
function addControlGroup(controls) {
108
controls.forEach(function (control) {
109
control.addTo(map);
110
});
112
var firstContainer = controls[0].getContainer();
113
$(firstContainer).find(".control-button").first()
114
.addClass("control-button-first");
116
var lastContainer = controls[controls.length - 1].getContainer();
117
$(lastContainer).find(".control-button").last()
118
.addClass("control-button-last");
121
addControlGroup([
122
L.OSM.zoom({ position: position }),
123
L.OSM.locate({ position: position })
124
]);
125
126
addControlGroup([
127
L.OSM.layers({
128
position: position,
129
layers: map.baseLayers,
130
sidebar: sidebar
131
}),
132
L.OSM.key({
133
position: position,
134
sidebar: sidebar
135
}),
136
L.OSM.share({
137
"position": position,
138
"sidebar": sidebar,
139
"short": true
140
})
141
]);
142
143
addControlGroup([
144
L.OSM.note({
145
position: position,
146
sidebar: sidebar
147
})
148
]);
149
150
addControlGroup([
151
L.OSM.query({
152
position: position,
153
sidebar: sidebar
154
})
155
]);
160
OSM.initializeContextMenu(map);
161
162
if (OSM.STATUS !== "api_offline" && OSM.STATUS !== "database_offline") {
163
OSM.initializeNotes(map);
164
if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
165
map.addLayer(map.noteLayer);
166
}
167
168
OSM.initializeBrowse(map);
169
if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
170
map.addLayer(map.dataLayer);
171
}
172
173
if (params.layers.indexOf(map.gpsLayer.options.code) >= 0) {
174
map.addLayer(map.gpsLayer);
175
}
178
var placement = $("html").attr("dir") === "rtl" ? "right" : "left";
179
$(".leaflet-control .control-button").tooltip({ placement: placement, container: "body" });
181
var expiry = new Date();
182
expiry.setYear(expiry.getFullYear() + 10);
183
184
map.on("moveend layeradd layerremove", function () {
186
map.getCenter().wrap(),
187
map.getZoom(),
188
map.getLayersCode(),
189
map._object);
190
191
Cookies.set("_osm_location", OSM.locationCookie(map), { secure: true, expires: expiry, path: "/", samesite: "lax" });
194
if (Cookies.get("_osm_welcome") !== "hide") {
195
$(".welcome").addClass("visible");
198
$(".welcome .btn-close").on("click", function () {
199
$(".welcome").removeClass("visible");
200
Cookies.set("_osm_welcome", "hide", { secure: true, expires: expiry, path: "/", samesite: "lax" });
204
bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
206
$("#banner .btn-close").on("click", function (e) {
207
var cookieId = e.target.id;
209
e.preventDefault();
210
if (cookieId) {
211
Cookies.set(cookieId, "hide", { secure: true, expires: bannerExpiry, path: "/", samesite: "lax" });
215
if (OSM.MATOMO) {
216
map.on("layeradd", function (e) {
217
if (e.layer.options) {
218
var goal = OSM.MATOMO.goals[e.layer.options.keyid];
219
220
if (goal) {
221
$("body").trigger("matomogoal", goal);
222
}
223
}
224
});
225
}
226
227
if (params.bounds) {
228
map.fitBounds(params.bounds);
229
} else {
230
map.setView([params.lat, params.lon], params.zoom);
231
}
232
233
if (params.marker) {
234
L.marker([params.mlat, params.mlon]).addTo(map);
237
$("#homeanchor").on("click", function (e) {
241
center = L.latLng(data.lat, data.lon);
244
L.marker(center, { icon: OSM.getUserIcon() }).addTo(map);
247
function remoteEditHandler(bbox, object) {
248
var remoteEditHost = "http://127.0.0.1:8111",
249
osmHost = location.protocol + "//" + location.host,
250
query = {
251
left: bbox.getWest() - 0.0001,
252
top: bbox.getNorth() + 0.0001,
253
right: bbox.getEast() + 0.0001,
254
bottom: bbox.getSouth() - 0.0001
255
};
256
257
if (object && object.type !== "note") query.select = object.type + object.id; // can't select notes
258
sendRemoteEditCommand(remoteEditHost + "/load_and_zoom?" + Qs.stringify(query), function () {
259
if (object && object.type === "note") {
260
var noteQuery = { url: osmHost + OSM.apiUrl(object) };
261
sendRemoteEditCommand(remoteEditHost + "/import?" + Qs.stringify(noteQuery));
262
}
263
});
265
function sendRemoteEditCommand(url, callback) {
266
var iframe = $("<iframe>");
267
var timeoutId = setTimeout(function () {
268
alert(I18n.t("site.index.remote_failed"));
270
}, 5000);
271
272
iframe
273
.hide()
274
.appendTo("body")
275
.attr("src", url)
276
.on("load", function () {
277
clearTimeout(timeoutId);
278
iframe.remove();
279
if (callback) callback();
280
});
281
}
282
283
return false;
284
}
285
286
$("a[data-editor=remote]").click(function (e) {
287
var params = OSM.mapParams(this.search);
288
remoteEditHandler(map.getBounds(), params.object);
289
e.preventDefault();
292
if (OSM.params().edit_help) {
293
$("#editanchor")
294
.removeAttr("title")
296
placement: "bottom",
297
title: I18n.t("javascripts.edit_help")
302
$("#editanchor").tooltip("hide");
309
page.pushstate = page.popstate = function () {
311
document.title = I18n.t("layouts.project_name.title");
315
var params = Qs.parse(location.search.substring(1));
316
if (params.query) {
317
$("#sidebar .search_form input[name=query]").value(params.query);
318
}
319
if (!("autofocus" in document.createElement("input"))) {
320
$("#sidebar .search_form input[name=query]").focus();
325
return page;
326
};
327
331
page.pushstate = page.popstate = function (path, id) {
332
OSM.loadSidebarContent(path, function () {
338
addObject(type, id, true);
341
function addObject(type, id, center) {
342
map.addObject({ type: type, id: parseInt(id, 10) }, function (bounds) {
343
if (!window.location.hash && bounds.isValid() &&
344
(center || !map.getBounds().contains(bounds))) {
345
OSM.router.withoutMoveListener(function () {
346
map.fitBounds(bounds);
347
});
352
$(this).css("background-color", $(this).data("colour"));
353
});
357
map.removeObject();
358
};
359
360
return page;
361
};
362
366
"/": OSM.Index(map),
367
"/search": OSM.Search(map),
368
"/directions": OSM.Directions(map),
369
"/export": OSM.Export(map),
370
"/note/new": OSM.NewNote(map),
371
"/history/friends": history,
372
"/history/nearby": history,
373
"/history": history,
374
"/user/:display_name/history": history,
375
"/note/:id": OSM.Note(map),
376
"/node/:id(/history)": OSM.Browse(map, "node"),
377
"/way/:id(/history)": OSM.Browse(map, "way"),
378
"/relation/:id(/history)": OSM.Browse(map, "relation"),
379
"/changeset/:id": OSM.Changeset(map),
380
"/query": OSM.Query(map)
383
if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
384
remoteEditHandler(map.getBounds(), params.object);
385
OSM.router.setCurrentPath("/");
386
}
387
390
$(document).on("click", "a", function (e) {
391
if (e.isDefaultPrevented() || e.isPropagationStopped()) {
394
395
// Open links in a new tab as normal.
396
if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
399
400
// Ignore cross-protocol and cross-origin links.
401
if (location.protocol !== this.protocol || location.host !== this.host) {
405
if (OSM.router.route(this.pathname + this.search + this.hash)) {
409
410
$(document).on("click", "#sidebar_content .btn-close", function () {
411
OSM.router.route("/" + OSM.formatHash(map));
412
});