Skip to content

Commit

Permalink
Scroll to the response specified in the url
Browse files Browse the repository at this point in the history
- when the survey response is rendered and the panel is shown, look for the response element and scroll it into view
  • Loading branch information
atogle committed Apr 16, 2014
1 parent b9f3799 commit a246d28
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/sa_web/jstemplates/activity-list-item.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<li class="activity-item clearfix">
{{!-- data attributes are not ideal, see comment in activity view --}}
<a href="/place/{{ place.id }}{{#is target_type 'comments'}}/response/{{ id }}{{/is}}" data-action-type="{{ target_type }}" data-place-id="{{ place.id }}">{{#_}}<strong>
<a href="/place/{{ place.id }}{{#is target_type 'comments'}}/response/{{ target.id }}{{/is}}" data-action-type="{{ target_type }}" data-place-id="{{ place.id }}">{{#_}}<strong>

{{#if target.submitter}}
<img src="{{ target.submitter.avatar_url }}" class="avatar" /> {{ target.submitter.name }}
Expand Down
4 changes: 2 additions & 2 deletions src/sa_web/static/js/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ var Shareabouts = Shareabouts || {};
this.appView.newPlace();
},

viewPlace: function(id) {
this.appView.viewPlace(id, this.loading);
viewPlace: function(id, responseId) {
this.appView.viewPlace(id, responseId, this.loading);
},

editPlace: function(){},
Expand Down
28 changes: 20 additions & 8 deletions src/sa_web/static/js/views/app-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,13 +308,13 @@ var Shareabouts = Shareabouts || {};
// Called by the router
this.collection.add({});
},
viewPlace: function(model, zoom) {
viewPlace: function(model, responseId, zoom) {
var self = this,
onPlaceFound, onPlaceNotFound, modelId;

onPlaceFound = function(model) {
var map = self.mapView.map,
layer, center, placeDetailView;
layer, center, placeDetailView, $responseToScrollTo;

// If this model is a duplicate of one that already exists in the
// places collection, it may not correspond to a layerView. For this
Expand All @@ -328,7 +328,7 @@ var Shareabouts = Shareabouts || {};
center = layer.getLatLng ? layer.getLatLng() : layer.getBounds().getCenter();

self.$panel.removeClass().addClass('place-detail place-detail-' + model.id);
self.showPanel(placeDetailView.render().$el);
self.showPanel(placeDetailView.render().$el, true);
self.hideNewPin();
self.destroyNewModels();
self.hideCenterPoint();
Expand All @@ -345,6 +345,16 @@ var Shareabouts = Shareabouts || {};
map.panTo(center, {animate: true});
}

if (responseId) {
// get the element based on the id
$responseToScrollTo = placeDetailView.$el.find('[data-response-id="'+ responseId +'"]');

// call scrollIntoView()
if ($responseToScrollTo.length > 0) {
$responseToScrollTo.get(0).scrollIntoView();
}
}

// Focus the one we're looking
model.trigger('focus');
};
Expand Down Expand Up @@ -395,18 +405,20 @@ var Shareabouts = Shareabouts || {};
this.hideCenterPoint();
this.hideAddButton();
},
showPanel: function(markup) {
showPanel: function(markup, preventScrollToTop) {
var map = this.mapView.map;

this.unfocusAllPlaces();

this.$panelContent.html(markup);
this.$panel.show();

this.$panelContent.scrollTop(0);
// Scroll to the top of window when showing new content on mobile. Does
// nothing on desktop.
window.scrollTo(0, 0);
if (preventScrollToTop) {
this.$panelContent.scrollTop(0);
// Scroll to the top of window when showing new content on mobile. Does
// nothing on desktop.
window.scrollTo(0, 0);
}

$('body').addClass('content-visible');
map.invalidateSize({ pan:false });
Expand Down
21 changes: 20 additions & 1 deletion src/sa_web/static/js/views/survey-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ var Shareabouts = Shareabouts || {};

render: function() {
var self = this,
responses = [];
responses = [],
url = window.location.toString(),
urlParts = url.split('response/'),
responseIdToScrollTo,
$responseToScrollTo;

// get the response id from the url
if (urlParts.length === 2) {
responseIdToScrollTo = urlParts[1];
}

// I don't understand why we need to redelegate the event here, but they
// are definitely unbound after the first render.
Expand All @@ -43,6 +52,16 @@ var Shareabouts = Shareabouts || {};
survey_config: this.options.surveyConfig
}));

// get the element based on the id
$responseToScrollTo = this.$el.find('[data-response-id="'+ responseIdToScrollTo +'"]');

// call scrollIntoView()
if ($responseToScrollTo.length > 0) {
setTimeout(function() {
$responseToScrollTo.get(0).scrollIntoView();
}, 500);
}

return this;
},

Expand Down

0 comments on commit a246d28

Please sign in to comment.