Skip to content

Commit

Permalink
Fix initial jump with hashed url (apidoc#871)
Browse files Browse the repository at this point in the history
* Move all code except font loading to a separate init function

* Fix initial jump
  • Loading branch information
Rafael Gomes authored May 1, 2020
1 parent f4029b3 commit fdf34ec
Showing 1 changed file with 24 additions and 31 deletions.
55 changes: 24 additions & 31 deletions template/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,19 @@ require([
'list'
], function($, _, locale, Handlebars, apiProject, apiData, prettyPrint, sampleRequest, semver, WebFont) {

// load google web fonts
loadGoogleFontCss();
// Load google web fonts.
WebFont.load({
active: function() {
// Only init after fonts are loaded.
init($, _, locale, Handlebars, apiProject, apiData, prettyPrint, sampleRequest, semver);
},
google: {
families: ['Source Code Pro', 'Source Sans Pro:n4,n6,n7']
}
});
});

function init($, _, locale, Handlebars, apiProject, apiData, prettyPrint, sampleRequest, semver) {
var api = apiData.api;

//
Expand Down Expand Up @@ -387,7 +397,7 @@ require([
$('#sections').append( content );

// Bootstrap Scrollspy
$(this).scrollspy({ target: '#scrollingNav', offset: 18 });
$(this).scrollspy({ target: '#scrollingNav' });

// Content-Scroll on Navigation click.
$('.sidenav').find('a').on('click', function(e) {
Expand All @@ -398,13 +408,6 @@ require([
window.location.hash = $(this).attr('href');
});

// Quickjump on Pageload to hash position.
if(window.location.hash) {
var id = window.location.hash;
if ($(id).length > 0)
$('html,body').animate({ scrollTop: parseInt($(id).offset().top) }, 0);
}

/**
* Check if Parameter (sub) List has a type Field.
* Example: @apiSuccess varname1 No type.
Expand Down Expand Up @@ -603,11 +606,17 @@ require([
if ($.urlParam('compare')) {
// URL Paramter ?compare=1 is set
$('#compareAllWithPredecessor').trigger('click');
}

if (window.location.hash) {
var id = window.location.hash;
$('html,body').animate({ scrollTop: parseInt($(id).offset().top) - 18 }, 0);
}
// Quick jump on page load to hash position.
// Should happen after setting the main version
// and after triggering the click on the compare button,
// as these actions modify the content
// and would make it jump to the wrong position or not jump at all.
if (window.location.hash) {
var id = window.location.hash;
if ($(id).length > 0)
$('html,body').animate({ scrollTop: parseInt($(id).offset().top) }, 0);
}

/**
Expand Down Expand Up @@ -865,21 +874,6 @@ require([
return;
}

/**
* Load google fonts.
*/
function loadGoogleFontCss() {
WebFont.load({
active: function() {
// Update scrollspy
$(window).scrollspy('refresh')
},
google: {
families: ['Source Code Pro', 'Source Sans Pro:n4,n6,n7']
}
});
}

/**
* Return ordered entries by custom order and append not defined entries to the end.
* @param {String[]} elements
Expand Down Expand Up @@ -910,5 +904,4 @@ require([
});
return results;
}

});
}

0 comments on commit fdf34ec

Please sign in to comment.