mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-18 00:25:07 +00:00
Fix #153 and Improve pushState navigation
This commit is contained in:
@@ -1,32 +1,34 @@
|
||||
define([
|
||||
"jQuery",
|
||||
"utils/path",
|
||||
"core/events",
|
||||
"core/state",
|
||||
"core/search",
|
||||
"core/progress",
|
||||
"core/exercise",
|
||||
"core/quiz"
|
||||
], function($, events, state, search, progress, exercises, quiz) {
|
||||
], function($, path, events, state, search, progress, exercises, quiz) {
|
||||
var prev, next;
|
||||
var githubCountStars, githubCountWatch;
|
||||
|
||||
var usePushState = !navigator.userAgent.match('CriOS') && (typeof history.pushState !== "undefined");
|
||||
var usePushState = (typeof history.pushState !== "undefined");
|
||||
|
||||
var updateHistory = function(url, title) {
|
||||
history.pushState({ path: url }, title, url);
|
||||
};
|
||||
var handleNavigation = function(relativeUrl, push) {
|
||||
var url = path.isAbsolute(relativeUrl) ? relativeUrl : path.join(path.dirname(window.location.pathname), relativeUrl);
|
||||
console.log("navigate to ", url, "baseurl="+relativeUrl);
|
||||
|
||||
var handleNavigation = function(url, push) {
|
||||
if (!usePushState) {
|
||||
// Refresh the page to the new URL if pushState not supported
|
||||
location.href = url;
|
||||
location.href = relativeUrl;
|
||||
return
|
||||
}
|
||||
|
||||
return $.get(url)
|
||||
.done(function (html) {
|
||||
if (push) updateHistory(url, null);
|
||||
// Push url to history
|
||||
if (push) history.pushState({ path: url }, null, url);
|
||||
|
||||
// Replace html content
|
||||
html = html.replace( /<(\/?)(html|head|body)([^>]*)>/ig, function(a,b,c,d){
|
||||
return '<' + b + 'div' + ( b ? '' : ' data-element="' + c + '"' ) + d + '>';
|
||||
});
|
||||
@@ -55,8 +57,8 @@ define([
|
||||
state.update($("html"));
|
||||
preparePage();
|
||||
})
|
||||
.fail(function () {
|
||||
location.href = url;
|
||||
.fail(function (e) {
|
||||
location.href = relativeUrl;
|
||||
});
|
||||
};
|
||||
|
||||
@@ -148,7 +150,6 @@ define([
|
||||
if (event.state === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
return handleNavigation(event.state.path, false);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user