Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
Fix remaining bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Schalk Neethling committed Oct 10, 2018
1 parent eadb7f4 commit ea6bbe0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
31 changes: 19 additions & 12 deletions kuma/static/js/components/local-anchor.js
Expand Up @@ -46,19 +46,26 @@

mainDocument.addEventListener('click', function(event) {
var target = event.target;
/* getting the attribute value as apposed to the target property
to ensure we get the actual text value of the attribute */
var hrefAttrValue = target.getAttribute('href');
var isInDocumentLink = hrefAttrValue.startsWith('#');

var headingId = isInDocumentLink
? hrefAttrValue.substr(1)
: target.dataset.headingId;
/* only handle clicks on in document links or, that originated from
a `local-anchor` */
if (target.classList.contains('local-anchor') || isInDocumentLink) {
event.preventDefault();
mdn.utils.scrollToHeading(headingId);
// only handle clicks on anchor elements
if (target.tagName === 'A') {
/* getting the attribute value as apposed to the target property
to ensure we get the actual text value of the attribute */
var hrefAttrValue = target.getAttribute('href');
var isInDocumentLink = hrefAttrValue.startsWith('#');

var headingId = isInDocumentLink
? hrefAttrValue.substr(1)
: target.dataset.headingId;
/* only handle clicks on in document links or, that originated from
a `local-anchor` */
if (
target.classList.contains('local-anchor') ||
isInDocumentLink
) {
event.preventDefault();
mdn.utils.scrollToHeading(headingId);
}
}
});
}
Expand Down
7 changes: 6 additions & 1 deletion kuma/static/js/components/page-load-actions.js
Expand Up @@ -11,7 +11,12 @@ window.addEventListener('load', function() {
var id = hash.substr(1);
var elem = document.getElementById(id);
if (elem) {
mdn.utils.scrollToHeading(id);
/* Firefox ignores whatever value is passed to
`scroll` if called immediately on `load`, so,
wait for 60ms before calling the function */
setTimeout(function() {
mdn.utils.scrollToHeading(id);
}, 60);
}
}
});
29 changes: 24 additions & 5 deletions kuma/static/js/utils/utils.js
@@ -1,4 +1,6 @@
window.mdn.utils = {
isTocSticky: false,
tocHeight: 0,
/**
* From the jQuery source:
* Get document-relative position by adding viewport scroll
Expand All @@ -12,7 +14,9 @@ window.mdn.utils = {
/* Get the window object associated with the
top-level document object for this node */
var elemDocumentWindow = elem.ownerDocument.defaultView;
return boundingClientRect.top + elemDocumentWindow.pageYOffset;
var top = boundingClientRect.top;
var yOffset = parseInt(elemDocumentWindow.pageYOffset, 10);
return top + yOffset;
},
/**
* Generate and returns a random string thanks to:
Expand Down Expand Up @@ -43,15 +47,30 @@ window.mdn.utils = {
*/
scrollToHeading: function(id) {
'use strict';
var toc = document.getElementById('toc');

// if page has a TOC,
if (toc !== undefined) {
// get and store the `offsetHeight`
this.tocHeight = toc.offsetHeight;
// and its sticky status
this.isTocSticky = getComputedStyle(toc).position === 'sticky';
}

var heading = document.getElementById(id);
var headingTop = mdn.utils.getOffsetTop(heading);
var toc = document.getElementById('toc');
var tocHeight = toc.offsetHeight;
var scrollY = headingTop - tocHeight;
var scrollY = 0;

// if the toc is sticky
if (toc && this.isTocSticky) {
scrollY = headingTop - this.tocHeight;
} else {
scrollY = headingTop;
}

// update hash
window.location.hash = id;
// scroll to heading
window.scroll(0, scrollY);
window.scroll(0, parseInt(scrollY, 10));
}
};
6 changes: 0 additions & 6 deletions kuma/static/js/wiki-toc.js
Expand Up @@ -188,12 +188,6 @@
// underline current section
countDownToUnderline();
});

if(window.location.hash) {
var thisHash = window.location.hash;
var headingId = thisHash.replace(/^#/, '');
mdn.utils.scrollToHeading(headingId);
}
}

if(win.matchMedia(breakpoint).matches === true) {
Expand Down

0 comments on commit ea6bbe0

Please sign in to comment.