FIX: Direct links to hash URLS were broken. Regression?

This commit is contained in:
Robin Ward 2014-07-24 12:52:43 -04:00
parent 5aa411b58c
commit fa25700400
3 changed files with 31 additions and 7 deletions

View File

@ -71,6 +71,20 @@ Discourse.URL = Em.Object.createWithMixins({
} }
}, },
// Scroll to the same page, different anchor
scrollToId: function(id) {
if (Em.isEmpty(id)) { return; }
jumpScheduled = true;
Em.run.schedule('afterRender', function() {
var $elem = $(id);
if ($elem.length > 0) {
$('html,body').scrollTop($elem.offset().top - $('header').height() - 15);
jumpScheduled = false;
}
});
},
/** /**
Our custom routeTo method is used to intelligently overwrite default routing Our custom routeTo method is used to intelligently overwrite default routing
behavior. behavior.
@ -98,12 +112,7 @@ Discourse.URL = Em.Object.createWithMixins({
// Scroll to the same page, different anchor // Scroll to the same page, different anchor
if (path.indexOf('#') === 0) { if (path.indexOf('#') === 0) {
var $elem = $(path); this.scrollToId(path);
if ($elem.length > 0) {
Em.run.schedule('afterRender', function() {
$('html,body').scrollTop($elem.offset().top - $('header').height() - 15);
});
}
return; return;
} }

View File

@ -32,7 +32,15 @@ Ember.DiscourseLocation = Ember.Object.extend({
*/ */
initState: function() { initState: function() {
set(this, 'history', get(this, 'history') || window.history); set(this, 'history', get(this, 'history') || window.history);
this.replaceState(this.formatURL(this.getURL()));
var url = this.formatURL(this.getURL()),
loc = get(this, 'location');
if (loc && loc.hash) {
url += loc.hash;
}
this.replaceState(url);
}, },
/** /**

View File

@ -21,6 +21,13 @@ Discourse.StaticController.PAGES.forEach(function(page) {
} }
}, },
activate: function() {
this._super();
// Scroll to an element if exists
Discourse.URL.scrollToId(document.location.hash);
},
model: function() { model: function() {
return Discourse.StaticPage.find(page); return Discourse.StaticPage.find(page);
}, },