From 6157280d19ca65b3ff00fd8c1f4dfc94fcbdb64c Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 4 Oct 2013 14:37:22 -0700 Subject: [PATCH] Refactor browsed object loading --- app/assets/javascripts/index.js | 34 +++++++++++-------- app/assets/javascripts/index/notes.js.erb | 26 +++++++------- app/assets/javascripts/osm.js.erb | 30 ++++------------ .../javascripts/templates/notes/show.jst.ejs | 2 +- 4 files changed, 39 insertions(+), 53 deletions(-) diff --git a/app/assets/javascripts/index.js b/app/assets/javascripts/index.js index 7c65f35c3..3dd9e78d8 100644 --- a/app/assets/javascripts/index.js +++ b/app/assets/javascripts/index.js @@ -173,16 +173,14 @@ $(document).ready(function () { }); } - var marker = L.marker([0, 0], {icon: getUserIcon()}); - - if (!params.object_zoom) { - if (params.bounds) { - map.fitBounds(params.bounds); - } else { - map.setView([params.lat, params.lon], params.zoom); - } + if (params.bounds) { + map.fitBounds(params.bounds); + } else { + map.setView([params.lat, params.lon], params.zoom); } + var marker = L.marker([0, 0], {icon: getUserIcon()}); + if (params.marker) { marker.setLatLng([params.mlat, params.mlon]).addTo(map); } @@ -220,8 +218,8 @@ $(document).ready(function () { }); } - initializeBrowse(map, params); - initializeNotes(map, params); + initializeBrowse(map); + initializeNotes(map); OSM.Index = function(map) { var page = {}; @@ -241,12 +239,20 @@ $(document).ready(function () { OSM.Browse = function(map) { var page = {}; - page.pushstate = page.popstate = function(path) { - $('#sidebar_content').load(path, page.load); + page.pushstate = page.popstate = function(path, type, id) { + $('#sidebar_content').load(path, function() { + page.load(path, type, id); + }); }; - page.load = function() { - map.addObject(OSM.mapParams().object, {zoom: true}); + page.load = function(path, type, id) { + if (OSM.STATUS === 'api_offline' || OSM.STATUS === 'database_offline') return; + + if (type === 'note') { + map.noteLayer.showNote(parseInt(id)); + } else { + map.addObject({type: type, id: parseInt(id)}, {zoom: true}); + } }; page.unload = function() { diff --git a/app/assets/javascripts/index/notes.js.erb b/app/assets/javascripts/index/notes.js.erb index a30a4919b..4538e9968 100644 --- a/app/assets/javascripts/index/notes.js.erb +++ b/app/assets/javascripts/index/notes.js.erb @@ -1,7 +1,7 @@ //= require templates/notes/show //= require templates/notes/new -function initializeNotes(map, params) { +function initializeNotes(map) { var noteLayer = map.noteLayer, notes = {}, newNote; @@ -48,19 +48,17 @@ function initializeNotes(map, params) { } }); - if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') { - if (params.note) { - $.ajax({ - url: "/api/" + OSM.API_VERSION + "/notes/" + params.note + ".json", - success: function (feature) { - var marker = updateMarker(notes[feature.properties.id], feature); - notes[feature.properties.id] = marker; - map.addLayer(noteLayer); - marker.openPopup(); - } - }); - } - } + noteLayer.showNote = function(id) { + $.ajax({ + url: "/api/" + OSM.API_VERSION + "/notes/" + id + ".json", + success: function (feature) { + var marker = updateMarker(notes[feature.properties.id], feature); + notes[feature.properties.id] = marker; + map.addLayer(noteLayer); + marker.openPopup(); + } + }); + }; function updateMarker(marker, feature) { if (marker) { diff --git a/app/assets/javascripts/osm.js.erb b/app/assets/javascripts/osm.js.erb index 215d5b7ca..59d880efc 100644 --- a/app/assets/javascripts/osm.js.erb +++ b/app/assets/javascripts/osm.js.erb @@ -46,27 +46,12 @@ OSM = { mapParams.mlat = parseFloat(params.mlat); } - match = window.location.pathname.match(/\/browse\/(node|way|relation|changeset)\/(\d+)/); - if (match) { - mapParams.object_zoom = true; - mapParams.object = {type: match[1], id: parseInt(match[2])}; - } else if (params.node || params.way || params.relation || params.note) { - mapParams.object_zoom = true; - - if (params.node) { - mapParams.object = {type: 'node', id: parseInt(params.node)}; - } else if (params.way) { - mapParams.object = {type: 'way', id: parseInt(params.way)}; - } else if (params.relation) { - mapParams.object = {type: 'relation', id: parseInt(params.relation)}; - } - } - - match = window.location.pathname.match(/\/browse\/note\/(\d+)/); - if (match) { - mapParams.note = parseInt(match[1]); - } else if (params.note) { - mapParams.note = parseInt(params.note); + if (params.node) { + mapParams.object = {type: 'node', id: parseInt(params.node)}; + } else if (params.way) { + mapParams.object = {type: 'way', id: parseInt(params.way)}; + } else if (params.relation) { + mapParams.object = {type: 'relation', id: parseInt(params.relation)}; } var hash = OSM.parseHash(location.hash); @@ -76,17 +61,14 @@ OSM = { mapParams.lon = hash.center.lng; mapParams.lat = hash.center.lat; mapParams.zoom = hash.zoom; - mapParams.object_zoom = false; } else if (params.lon && params.lat) { mapParams.lon = parseFloat(params.lon); mapParams.lat = parseFloat(params.lat); mapParams.zoom = parseInt(params.zoom || 5); - mapParams.object_zoom = false; } else if (params.mlon && params.mlat) { mapParams.lon = parseFloat(params.mlon); mapParams.lat = parseFloat(params.mlat); mapParams.zoom = parseInt(params.zoom || 12); - mapParams.object_zoom = false; } else if (loc = $.cookie('_osm_location')) { loc = loc.split("|"); mapParams.lon = parseFloat(loc[0]); diff --git a/app/assets/javascripts/templates/notes/show.jst.ejs b/app/assets/javascripts/templates/notes/show.jst.ejs index bea60ed4a..240259140 100644 --- a/app/assets/javascripts/templates/notes/show.jst.ejs +++ b/app/assets/javascripts/templates/notes/show.jst.ejs @@ -1,5 +1,5 @@
- + <% if (note.comments.some(function (comment) { return !comment.user })) { %> <%- I18n.t('javascripts.notes.show.anonymous_warning') %> <% } %> -- 2.39.5