-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
setPageStatus.js
61 lines (50 loc) · 1.73 KB
/
setPageStatus.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { isTouch, isTouchDevice } from "../common/constants.js";
import { getOptions } from "../common/options.js";
import { setState } from "../common/state.js";
import { setBodyClass } from "../stateClasses.js";
/**
* Sets the state of the website depending on the active section/slide.
* It changes the URL hash when needed and updates the body class.
*/
export function setPageStatus(slideIndex, slideAnchor, anchorLink){
var sectionHash = '';
if(getOptions().anchors.length && !getOptions().lockAnchors){
//isn't it the first slide?
if(slideIndex){
if(anchorLink != null){
sectionHash = anchorLink;
}
//slide without anchor link? We take the index instead.
if(slideAnchor == null){
slideAnchor = slideIndex;
}
setState({lastScrolledSlide: slideAnchor});
setUrlHash(sectionHash + '/' + slideAnchor);
//first slide won't have slide anchor, just the section one
}else if(slideIndex != null){
setState({lastScrolledSlide: slideAnchor});
setUrlHash(anchorLink);
}
//section without slides
else{
setUrlHash(anchorLink);
}
}
setBodyClass();
}
/**
* Sets the URL hash.
*/
function setUrlHash(url){
if(getOptions().recordHistory){
location.hash = url;
}else{
//Mobile Chrome doesn't work the normal way, so... lets use HTML5 for phones :)
if(isTouchDevice || isTouch){
window.history.replaceState(undefined, undefined, '#' + url);
}else{
var baseUrl = window.location.href.split('#')[0];
window.location.replace( baseUrl + '#' + url );
}
}
}