提交 6aae2f0a 编写于 作者: S Samy Pessé

Merge branch 'fix/redirection'

......@@ -66,72 +66,91 @@ function handleNavigation(relativeUrl, push) {
prevUri = uri;
return loading.show($.get(uri)
.then(function (html) {
// Replace html content
html = html.replace( /<(\/?)(html|head|body)([^>]*)>/ig, function(a,b,c,d){
return '<' + b + 'div' + ( b ? '' : ' data-element="' + c + '"' ) + d + '>';
var promise = $.Deferred(function(deferred) {
$.ajax({
type: 'GET',
url: uri,
cache: true,
headers:{
'Access-Control-Expose-Headers': 'X-Current-Location'
},
success: function(html, status, xhr) {
// For GitBook.com, we handle redirection signaled by the server
var responseURL = xhr.getResponseHeader('X-Current-Location') || uri;
// Replace html content
html = html.replace( /<(\/?)(html|head|body)([^>]*)>/ig, function(a,b,c,d){
return '<' + b + 'div' + ( b ? '' : ' data-element="' + c + '"' ) + d + '>';
});
var $page = $(html);
var $pageHead = $page.find('[data-element=head]');
var $pageBody = $page.find('.book');
// We only use history.pushState for pages generated with GitBook
if ($pageBody.length === 0) {
var err = new Error('Invalid gitbook page, redirecting...');
return deferred.reject(err);
}
// Push url to history
if (push) {
history.pushState({
path: responseURL
}, null, responseURL);
}
// Merge heads
// !! Warning !!: we only update necessary portions to avoid strange behavior (page flickering etc ...)
// Update title
document.title = $pageHead.find('title').text();
// Reference to $('head');
var $head = $('head');
// Update next & prev <link> tags
// Remove old
$head.find('link[rel=prev]').remove();
$head.find('link[rel=next]').remove();
// Add new next * prev <link> tags
$head.append($pageHead.find('link[rel=prev]'));
$head.append($pageHead.find('link[rel=next]'));
// Merge body
var bodyClass = $('.book').attr('class');
var scrollPosition = $('.book-summary').scrollTop();
$pageBody.toggleClass('with-summary', $('.book').hasClass('with-summary'));
$('.book').replaceWith($pageBody);
$('.book').attr('class', bodyClass);
$('.book-summary').scrollTop(scrollPosition);
// Scroll to hashtag position
if (hash) {
scrollToHash(hash);
}
// Update state
gitbook.state.$book = $('.book');
preparePage(!hash);
deferred.resolve();
}
});
}).promise();
var $page = $(html);
var $pageHead = $page.find('[data-element=head]');
var $pageBody = $page.find('.book');
// We only use history.pushState for pages generated with GitBook
if ($pageBody.length === 0) {
return $.Deferred(function (deferred) {
var err = new Error('Invalid gitbook page, redirecting...');
deferred.reject(err);
}).promise();
}
// Push url to history
if (push) {
history.pushState({
path: uri
}, null, uri);
}
// Merge heads
// !! Warning !!: we only update necessary portions to avoid strange behavior (page flickering etc ...)
// Update title
document.title = $pageHead.find('title').text();
// Reference to $('head');
var $head = $('head');
// Update next & prev <link> tags
// Remove old
$head.find('link[rel=prev]').remove();
$head.find('link[rel=next]').remove();
// Add new next * prev <link> tags
$head.append($pageHead.find('link[rel=prev]'));
$head.append($pageHead.find('link[rel=next]'));
// Merge body
var bodyClass = $('.book').attr('class');
var scrollPosition = $('.book-summary').scrollTop();
$pageBody.toggleClass('with-summary', $('.book').hasClass('with-summary'));
$('.book').replaceWith($pageBody);
$('.book').attr('class', bodyClass);
$('.book-summary').scrollTop(scrollPosition);
// Scroll to hashtag position
if (hash) {
scrollToHash(hash);
}
// Update state
gitbook.state.$book = $('.book');
preparePage(!hash);
})
.fail(function (e) {
location.href = relativeUrl;
}));
return loading.show(
promise
.fail(function (e) {
console.log(e);
//location.href = relativeUrl;
})
);
}
function updateNavigationPosition() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册