提交 efbe2c03 编写于 作者: EvanOne(文一)'s avatar EvanOne(文一)

feat: Add pjax support

上级 1eef9320
...@@ -627,6 +627,26 @@ quicklink: ...@@ -627,6 +627,26 @@ quicklink:
# - (uri, el) => el.hasAttribute('nofollow') # - (uri, el) => el.hasAttribute('nofollow')
ignores: ignores:
# Pjax
# See: https://github.com/MoOx/pjax/
pjax:
enable: true
# ! -----------------------------------------------------
# ! If you don't understand, please ignore the following.
# ! -----------------------------------------------------
# Please see: https://github.com/MoOx/pjax/#options
elements:
selectors:
switches:
switchesOptions:
history: true
scrollTo: false
scrollRestoration: false
cacheBust: false
debug: false
currentUrlFullReload: false
timeout: 0
# Google AdSense # Google AdSense
google_adsense: google_adsense:
enable: false enable: false
...@@ -787,3 +807,9 @@ cdn: ...@@ -787,3 +807,9 @@ cdn:
# Example: # Example:
# quicklink: //cdn.jsdelivr.net/npm/quicklink@latest/dist/quicklink.umd.js # quicklink: //cdn.jsdelivr.net/npm/quicklink@latest/dist/quicklink.umd.js
quicklink: quicklink:
# Using version: latest
# See: https://github.com/MoOx/pjax/
# Example:
# pjax: //cdn.jsdelivr.net/npm/pjax@latest/pjax.min.js
pjax:
...@@ -20,3 +20,6 @@ html(lang=config.language) ...@@ -20,3 +20,6 @@ html(lang=config.language)
include ../_third-party/comments/index.pug include ../_third-party/comments/index.pug
include ../_third-party/math/index.pug include ../_third-party/math/index.pug
include ../_third-party/search/index.pug include ../_third-party/search/index.pug
if theme.pjax && theme.pjax.enable
include ../_third-party/pjax.pug
script(src=`${url_for(theme.js)}/utils.js?v=${stun_env("version")}`) script(src=`${url_for(theme.js)}/utils.js?v=${stun_env("version")}`)
script(src=`${url_for(theme.js)}/stun-boot.js?v=${stun_env("version")}`) script(src=`${url_for(theme.js)}/stun-boot.js?v=${stun_env("version")}`)
script(src=`${url_for(theme.js)}/copy.js?v=${stun_env("version")}`)
script(src=`${url_for(theme.js)}/scroll.js?v=${stun_env("version")}`) script(src=`${url_for(theme.js)}/scroll.js?v=${stun_env("version")}`)
script(src=`${url_for(theme.js)}/header.js?v=${stun_env("version")}`) script(src=`${url_for(theme.js)}/header.js?v=${stun_env("version")}`)
......
-
var pjaxArgs = {
'elements': [
'a:not([target=_blank])'
],
'selectors': [
'title',
'meta[name=description]',
'#main'
],
'history': theme.pjax.history,
'scrollTo': theme.pjax.scrollTo,
'scrollRestoration': theme.pjax.scrollRestoration,
'cacheBust': theme.pjax.cacheBust,
'debug': theme.pjax.debug,
'currentUrlFullReload': theme.pjax.currentUrlFullReload,
'timeout': theme.pjax.timeout
};
if (theme.pjax.elements) {
pjaxArgs.elements.push(theme.pjax.elements);
}
if (theme.pjax.selectors) {
pjaxArgs.selectors.push(theme.pjax.selectors);
}
if (theme.pjax.selectors) {
pjaxArgs.switches = theme.pjax.selectors;
}
if (theme.pjax.selectors) {
pjaxArgs.switchesOptions = theme.pjax.selectors;
}
pjaxArgs = JSON.stringify(pjaxArgs)
if theme.pjax && theme.pjax.enable
- var pjax_js = "https://cdn.jsdelivr.net/npm/pjax@latest/pjax.min.js"
- if (theme.cdn.pjax) pjax_js = theme.cdn.pjax
script(src=pjax_js)
script.
var header = document.querySelector('#header');
var headerHeight = header.offsetHeight;
var pjax = new Pjax(!{ pjaxArgs });
// 加载进度条的计时器
var loadingTimer = null;
// 重置页面 Y 方向上的滚动偏移量
document.addEventListener('pjax:send', function () {
$('html').velocity('scroll', {
duration: 500,
offset: $('#header').height(),
easing: 'easeInOutCubic'
});
var loadingBarWidth = 0;
var MAX_LOADING_WIDTH = 95;
$('.loading-bar .progress').css('transform', 'translateX(-100%)');
$('.loading-bar').addClass('loading');
clearInterval(loadingTimer);
loadingTimer = setInterval(function () {
loadingBarWidth += 2;
if (loadingBarWidth > MAX_LOADING_WIDTH) {
loadingBarWidth = MAX_LOADING_WIDTH;
}
$('.loading-bar .progress').css(
'transform', 'translateX(' + (loadingBarWidth - 100) + '%)'
);
}, 100);
}, false);
document.addEventListener('pjax:complete', function () {
clearInterval(loadingTimer);
$('.loading-bar .progress').css('transform', 'translateX(0%)');
setTimeout(function () {
$('.loading-bar').removeClass('loading');
$('.loading-bar .progress').css('transform', 'translateX(-100%)');
}, 400);
// 重载失效的函数,使其再执行一次
Stun.utils.pjaxReloadBoot();
Stun.utils.pjaxReloadScroll();
Stun.utils.pjaxReloadSidebar();
}, false);
...@@ -85,16 +85,21 @@ $(document).ready(function () { ...@@ -85,16 +85,21 @@ $(document).ready(function () {
headerNavScroll(); headerNavScroll();
}, 100)); }, 100));
// Click the heading. Stun.utils.pjaxReloadScroll = function () {
$('.content') // Click the heading.
.find('h1,h2,h3,h4,h5,h6') $('.content')
.on('click', function () { .find('h1,h2,h3,h4,h5,h6')
scrollHeadingToTop('#' + $(this).attr('id')); .on('click', function () {
scrollHeadingToTop('#' + $(this).attr('id'));
});
// Click the post toc.
$('.toc-link').on('click', function (e) {
e.preventDefault();
scrollHeadingToTop($(this).attr('href'));
}); });
};
// Click the post toc. // Initializaiton
$('.toc-link').on('click', function (e) { Stun.utils.pjaxReloadScroll();
e.preventDefault();
scrollHeadingToTop($(this).attr('href'));
});
}); });
...@@ -142,28 +142,33 @@ $(document).ready(function () { ...@@ -142,28 +142,33 @@ $(document).ready(function () {
readProgress(); readProgress();
}, 150)); }, 150));
var $tocWrapper = $('.sidebar-toc'); Stun.utils.pjaxReloadSidebar = function () {
var $view = $('.sidebar-overview'); var $tocWrapper = $('.sidebar-toc');
var $view = $('.sidebar-overview');
$('.sidebar-nav-toc').on('click', function () {
$('.sidebar-nav-toc').toggleClass('current'); $('.sidebar-nav-toc').on('click', function () {
$('.sidebar-nav-overview').toggleClass('current'); $('.sidebar-nav-toc').toggleClass('current');
$('.sidebar-nav-overview').toggleClass('current');
$tocWrapper.css('display', 'block');
$tocWrapper.velocity('fadeIn'); $tocWrapper.css('display', 'block');
$tocWrapper.velocity('fadeIn');
$view.css('display', 'none');
$view.velocity('fadeOut'); $view.css('display', 'none');
}); $view.velocity('fadeOut');
});
$('.sidebar-nav-overview').on('click', function () {
$('.sidebar-nav-toc').toggleClass('current'); $('.sidebar-nav-overview').on('click', function () {
$('.sidebar-nav-overview').toggleClass('current'); $('.sidebar-nav-toc').toggleClass('current');
$('.sidebar-nav-overview').toggleClass('current');
$tocWrapper.css('display', 'none');
$tocWrapper.velocity('fadeOut'); $tocWrapper.css('display', 'none');
$tocWrapper.velocity('fadeOut');
$view.css('display', 'block');
$view.velocity('fadeIn');
});
};
$view.css('display', 'block'); // Initialization
$view.velocity('fadeIn'); Stun.utils.pjaxReloadSidebar();
});
}); });
...@@ -6,21 +6,26 @@ $(document).ready(function () { ...@@ -6,21 +6,26 @@ $(document).ready(function () {
Stun.utils.addIconToExternalLink('#footer'); Stun.utils.addIconToExternalLink('#footer');
} }
Stun.utils.addCopyButtonToCopyright(); Stun.utils.pjaxReloadBoot = function () {
Stun.utils.registerCopyEvent(); this.addCopyButtonToCopyright();
CONFIG.reward && Stun.utils.registerShowReward(); this.registerCopyEvent();
CONFIG.gallery_waterfall && Stun.utils.galleryWaterFall(); CONFIG.reward && this.registerShowReward();
CONFIG.lazyload && Stun.utils.lazyLoadImages(); CONFIG.gallery_waterfall && this.galleryWaterFall();
CONFIG.lazyload && this.lazyLoadImages();
if (CONFIG.external_link) { if (CONFIG.external_link) {
var WRAPPER = '.archive-inner, .post-title'; var WRAPPER = '.archive-inner, .post-title';
Stun.utils.addIconToExternalLink(WRAPPER); this.addIconToExternalLink(WRAPPER);
} }
if (CONFIG.fancybox) { if (CONFIG.fancybox) {
Stun.utils.wrapImageWithFancyBox(); this.wrapImageWithFancyBox();
} else if (CONFIG.zoom_image) { } else if (CONFIG.zoom_image) {
Stun.utils.registerClickToZoomImage(); this.registerClickToZoomImage();
} }
};
// Initializaiton
Stun.utils.pjaxReloadBoot();
}); });
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册