From fa257004002a9aeddd4b075ea3496c06a03bad89 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 24 Jul 2014 12:52:43 -0400 Subject: [PATCH] FIX: Direct links to hash URLS were broken. Regression? --- app/assets/javascripts/discourse/lib/url.js | 21 +++++++++++++------ .../discourse/routes/discourse_location.js | 10 ++++++++- .../discourse/routes/static_route.js | 7 +++++++ 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/discourse/lib/url.js b/app/assets/javascripts/discourse/lib/url.js index bc371e46472..6805ddb259c 100644 --- a/app/assets/javascripts/discourse/lib/url.js +++ b/app/assets/javascripts/discourse/lib/url.js @@ -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 behavior. @@ -98,12 +112,7 @@ Discourse.URL = Em.Object.createWithMixins({ // Scroll to the same page, different anchor if (path.indexOf('#') === 0) { - var $elem = $(path); - if ($elem.length > 0) { - Em.run.schedule('afterRender', function() { - $('html,body').scrollTop($elem.offset().top - $('header').height() - 15); - }); - } + this.scrollToId(path); return; } diff --git a/app/assets/javascripts/discourse/routes/discourse_location.js b/app/assets/javascripts/discourse/routes/discourse_location.js index f5a6ed2d837..b7125ae1da8 100644 --- a/app/assets/javascripts/discourse/routes/discourse_location.js +++ b/app/assets/javascripts/discourse/routes/discourse_location.js @@ -32,7 +32,15 @@ Ember.DiscourseLocation = Ember.Object.extend({ */ initState: function() { 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); }, /** diff --git a/app/assets/javascripts/discourse/routes/static_route.js b/app/assets/javascripts/discourse/routes/static_route.js index 4c7891a2c07..a6117316cfd 100644 --- a/app/assets/javascripts/discourse/routes/static_route.js +++ b/app/assets/javascripts/discourse/routes/static_route.js @@ -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() { return Discourse.StaticPage.find(page); },