2 //= require leaflet.sidebar
3 //= require leaflet.locate
4 //= require leaflet.layers
5 //= require leaflet.key
6 //= require leaflet.note
7 //= require leaflet.share
8 //= require index/search
9 //= require index/browse
10 //= require index/export
11 //= require index/notes
12 //= require index/history
13 //= require index/note
16 $(document).ready(function () {
17 var params = OSM.mapParams();
19 var map = new L.OSM.Map("map", {
24 map.attributionControl.setPrefix('');
26 map.updateLayers(params.layers);
28 map.on("baselayerchange", function (e) {
29 if (map.getZoom() > e.layer.options.maxZoom) {
30 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
34 var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
36 L.OSM.zoom({position: position})
42 title: I18n.t('javascripts.map.locate.title'),
43 popup: I18n.t('javascripts.map.locate.popup')
47 var sidebar = L.OSM.sidebar('#map-ui')
52 layers: map.baseLayers,
75 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
77 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
78 map.addLayer(map.noteLayer);
81 initializeBrowse(map);
82 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
83 map.addLayer(map.dataLayer);
87 $('.leaflet-control .control-button').tooltip({placement: 'left', container: 'body'});
89 map.on('moveend layeradd layerremove', function() {
91 map.getCenter().wrap(),
96 var expiry = new Date();
97 expiry.setYear(expiry.getFullYear() + 10);
98 $.cookie("_osm_location", cookieContent(map), { expires: expiry });
102 map.on('layeradd', function (e) {
103 if (e.layer.options) {
104 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
107 $('body').trigger('piwikgoal', goal);
114 map.fitBounds(params.bounds);
116 map.setView([params.lat, params.lon], params.zoom);
119 var marker = L.marker([0, 0], {icon: getUserIcon()});
122 marker.setLatLng([params.mlat, params.mlon]).addTo(map);
125 $("#homeanchor").on("click", function(e) {
128 var data = $(this).data(),
129 center = L.latLng(data.lat, data.lon);
131 map.setView(center, data.zoom);
132 marker.setLatLng(center).addTo(map);
135 $("a[data-editor=remote]").click(function(e) {
136 remoteEditHandler(map.getBounds());
140 if (OSM.preferred_editor == "remote" && $('body').hasClass("site-edit")) {
141 remoteEditHandler(map.getBounds());
144 if (OSM.params().edit_help) {
149 title: I18n.t('javascripts.edit_help')
153 $('body').one('click', function() {
154 $('#editanchor').tooltip('hide');
158 OSM.Index = function(map) {
161 function loadContent(path) {
162 $('#sidebar_content').load(path + "?xhr=1", function(a, b, xhr) {
163 if (xhr.getResponseHeader('X-Page-Title')) {
164 document.title = xhr.getResponseHeader('X-Page-Title');
169 page.pushstate = function(path) {
170 $("#content").addClass("overlay-sidebar");
171 map.invalidateSize({pan: false})
172 .panBy([-300, 0], {animate: false});
176 page.popstate = function(path) {
177 $("#content").addClass("overlay-sidebar");
178 map.invalidateSize({pan: false});
182 page.unload = function() {
183 map.panBy([300, 0], {animate: false});
184 $("#content").removeClass("overlay-sidebar");
185 map.invalidateSize({pan: false});
191 OSM.Browse = function(map) {
194 page.pushstate = page.popstate = function(path, type, id) {
195 $('#sidebar_content').load(path + "?xhr=1", function(a, b, xhr) {
196 if (xhr.getResponseHeader('X-Page-Title')) {
197 document.title = xhr.getResponseHeader('X-Page-Title');
199 page.load(path, type, id);
203 page.load = function(path, type, id) {
204 if (OSM.STATUS === 'api_offline' || OSM.STATUS === 'database_offline') return;
206 map.addObject({type: type, id: parseInt(id)}, {zoom: true});
209 page.unload = function() {
216 var history = OSM.History(map),
217 note = OSM.Note(map);
219 OSM.route = OSM.Router(map, {
221 "/search": OSM.Search(map),
222 "/export": OSM.Export(map),
224 "/user/:display_name/edits": history,
225 "/browse/friends": history,
226 "/browse/nearby": history,
227 "/browse/note/:id": note,
228 "/browse/:type/:id(/history)": OSM.Browse(map)
231 $(document).on("click", "a", function(e) {
232 if (e.isDefaultPrevented() || e.isPropagationStopped()) return;
233 if (this.host === window.location.host && OSM.route(this.pathname + this.search + this.hash)) e.preventDefault();
236 $(".search_form").on("submit", function(e) {
238 $("header").addClass("closed");
239 OSM.route("/search?query=" + encodeURIComponent($(this).find("input[name=query]").val()) + OSM.formatHash(map));
242 $(".describe_location").on("click", function(e) {
244 var precision = zoomPrecision(map.getZoom());
245 OSM.route("/search?query=" + encodeURIComponent(
246 map.getCenter().lat.toFixed(precision) + "," +
247 map.getCenter().lng.toFixed(precision)));