FIX: Don't scroll to top if there's an anchor present.

This commit is contained in:
Robin Ward 2014-07-16 18:14:07 -04:00
parent 4c867c5796
commit 62ef81d895
4 changed files with 32 additions and 24 deletions

View File

@ -6,8 +6,15 @@
@namespace Discourse
@module Discourse
**/
Discourse.StaticController = Em.ObjectController.extend({
showLoginButton: Em.computed.equal('path', 'login')
showLoginButton: Em.computed.equal('path', 'login'),
actions: {
markFaqRead: function() {
Discourse.ajax("/users/read-faq", { method: "POST" });
}
}
});
Discourse.StaticController.reopenClass({

View File

@ -6,11 +6,18 @@
@namespace Discourse
@module Discourse
**/
var jumpScheduled = false;
Discourse.URL = Em.Object.createWithMixins({
// Used for matching a topic
TOPIC_REGEXP: /\/t\/([^\/]+)\/(\d+)\/?(\d+)?/,
isJumpScheduled: function() {
return jumpScheduled;
},
/**
Jumps to a particular post in the stream
**/
@ -89,7 +96,7 @@ Discourse.URL = Em.Object.createWithMixins({
return;
}
// Scroll to the same page, differnt anchor
// Scroll to the same page, different anchor
if (path.indexOf('#') === 0) {
var $elem = $(path);
if ($elem.length > 0) {
@ -301,10 +308,13 @@ Discourse.URL = Em.Object.createWithMixins({
var transition = router.handleURL(path);
transition.promise.then(function() {
if (elementId) {
jumpScheduled = true;
Em.run.next('afterRender', function() {
var offset = $('#' + elementId).offset();
if (offset && offset.top) {
$('html, body').scrollTop(offset.top - $('header').height() - 10);
jumpScheduled = false;
}
});
}

View File

@ -1,16 +1,8 @@
/**
This mixin will cause a view to scroll the viewport to the top once it has been inserted
@class Discourse.ScrollTop
@extends Ember.Mixin
@namespace Discourse
@module Discourse
**/
Discourse.ScrollTop = Em.Mixin.create({
_scrollTop: function() {
if (Discourse.URL.isJumpScheduled()) { return; }
Em.run.schedule('afterRender', function() {
$(document).scrollTop(0);
});
}.on('didInsertElement')
});

View File

@ -1,22 +1,21 @@
var readFaq = false;
export default Discourse.View.extend({
didInsertElement: function() {
export default Ember.View.extend(Discourse.ScrollTop, {
_checkRead: function() {
var path = this.get('controller.model.path');
if(path === "faq" || path === "guidelines"){
var $window = $(window);
var $window = $(window),
controller = this.get('controller');
$window.on('scroll.faq', function(){
if($window.scrollTop() + $window.height() > $(document).height() - 10) {
if(!this._notifiedBottom){
this._notifiedBottom = true;
Discourse.ajax("/users/read-faq", {
method: "POST"
});
}
if(!readFaq && ($window.scrollTop() + $window.height() > $(document).height() - 10)) {
readFaq = true;
controller.send('markFaqRead');
}
});
}
},
willDestroyElement: function(){
}.on('didInsertElement'),
_stopChecking: function(){
$(window).off('scroll.faq');
}
}.on('willDestroyElement')
});