代码格式化 & 去除异步

i18n
枋柚 5 years ago
parent b6754389b5
commit 36354ce71c
  1. 2
      layout/_partial/scripts.ejs
  2. 192
      source/js/app.js

@ -23,7 +23,7 @@
<script src="//instant.page/1.2.2" type="module" integrity="sha384-2xV8M5griQmzyiY3CDqh1dn4z3llDVqZDqzjzcY+jCBCk/a5fXJmuZ/40JJAPeoU"></script> <script src="//instant.page/1.2.2" type="module" integrity="sha384-2xV8M5griQmzyiY3CDqh1dn4z3llDVqZDqzjzcY+jCBCk/a5fXJmuZ/40JJAPeoU"></script>
<% if (theme.scrollreveal == true) { %> <% if (theme.scrollreveal == true) { %>
<script async src="https://cdn.jsdelivr.net/npm/scrollreveal@4.0.5/dist/scrollreveal.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/scrollreveal@4.0.5/dist/scrollreveal.min.js"></script>
<script type="text/javascript"> <script type="text/javascript">
$(function() { $(function() {
const $reveal = $('.reveal'); const $reveal = $('.reveal');

@ -12,10 +12,12 @@ var customSearch;
function scrolltoElement(elem, correction) { function scrolltoElement(elem, correction) {
correction = correction || scrollCorrection; correction = correction || scrollCorrection;
const $elem = elem.href ? $(elem.getAttribute('href')) : $(elem); const $elem = elem.href ? $(elem.getAttribute('href')) : $(elem);
$('html, body').animate({ 'scrollTop': $elem.offset().top - correction }, 400); $('html, body').animate({
}; 'scrollTop': $elem.offset().top - correction
}, 400);
}
function setScrollAnchor(){ function setScrollAnchor() {
// button // button
const $postsBtn = $('.menu .active'); const $postsBtn = $('.menu .active');
const $topBtn = $('.s-top'); const $topBtn = $('.s-top');
@ -24,13 +26,25 @@ var customSearch;
const $bodyAnchor = $('.l_body'); const $bodyAnchor = $('.l_body');
// action // action
if ($postsBtn.length && $bodyAnchor) { if ($postsBtn.length && $bodyAnchor) {
$postsBtn.click(e => { e.preventDefault(); e.stopPropagation(); scrolltoElement($bodyAnchor); }); $postsBtn.click(e => {
e.preventDefault();
e.stopPropagation();
scrolltoElement($bodyAnchor);
});
} }
if ($titleBtn.length && $bodyAnchor) { if ($titleBtn.length && $bodyAnchor) {
$titleBtn.click(e => { e.preventDefault(); e.stopPropagation(); scrolltoElement($bodyAnchor); }); $titleBtn.click(e => {
e.preventDefault();
e.stopPropagation();
scrolltoElement($bodyAnchor);
});
} }
if ($topBtn.length && $bodyAnchor) { if ($topBtn.length && $bodyAnchor) {
$topBtn.click(e => { e.preventDefault(); e.stopPropagation(); scrolltoElement($bodyAnchor); }); $topBtn.click(e => {
e.preventDefault();
e.stopPropagation();
scrolltoElement($bodyAnchor);
});
} }
const $coverAnchor = $('.cover-wrapper'); const $coverAnchor = $('.cover-wrapper');
@ -45,11 +59,11 @@ var customSearch;
pos = scrollTop; pos = scrollTop;
if (scrollTop > 150) { if (scrollTop > 150) {
$topBtn.addClass('show'); $topBtn.addClass('show');
if (del > 0) { if (del > 0) {
$topBtn.removeClass('hl'); $topBtn.removeClass('hl');
} else { } else {
$topBtn.addClass('hl'); $topBtn.addClass('hl');
} }
} else { } else {
$topBtn.removeClass('show').removeClass('hl'); $topBtn.removeClass('show').removeClass('hl');
} }
@ -59,7 +73,7 @@ var customSearch;
$headerAnchor.removeClass('show'); $headerAnchor.removeClass('show');
} }
}); });
} }
function setHeader() { function setHeader() {
if (!window.subData) return; if (!window.subData) return;
@ -84,12 +98,19 @@ var customSearch;
// bind events to every btn // bind events to every btn
const $commentTarget = $('#comments'); const $commentTarget = $('#comments');
if ($commentTarget.length) { if ($commentTarget.length) {
$comment.click(e => { e.preventDefault(); e.stopPropagation(); scrolltoElement($commentTarget); }); $comment.click(e => {
e.preventDefault();
e.stopPropagation();
scrolltoElement($commentTarget);
});
} else $comment.remove(); } else $comment.remove();
const $tocTarget = $('.toc-wrapper'); const $tocTarget = $('.toc-wrapper');
if ($tocTarget.length && $tocTarget.children().length) { if ($tocTarget.length && $tocTarget.children().length) {
$toc.click((e) => { e.stopPropagation(); $tocTarget.toggleClass('active'); }); $toc.click((e) => {
e.stopPropagation();
$tocTarget.toggleClass('active');
});
} else $toc.remove(); } else $toc.remove();
@ -97,9 +118,9 @@ var customSearch;
} }
function setHeaderMenuSelection() { function setHeaderMenuSelection() {
var $headerMenu = $('body .navgation'); var $headerMenu = $('body .navgation');
// 先把已经激活的取消激活 // 先把已经激活的取消激活
$headerMenu.find('li a.active').removeClass('active'); $headerMenu.find('li a.active').removeClass('active');
// var $underline = $headerMenu.find('.underline'); // var $underline = $headerMenu.find('.underline');
function setUnderline($item) { function setUnderline($item) {
// if (!transition) $underline.addClass('disable-trans'); // if (!transition) $underline.addClass('disable-trans');
@ -109,20 +130,20 @@ var customSearch;
} }
//set current active nav //set current active nav
var $active_link = null; var $active_link = null;
var idname = location.pathname.replace(/\/|%/g, ""); var idname = location.pathname.replace(/\/|%/g, "");
if (idname.length == 0) { if (idname.length == 0) {
idname = "home"; idname = "home";
} }
var page = idname.match(/page\d{0,}$/g); var page = idname.match(/page\d{0,}$/g);
if (page) { if (page) {
page = page[0]; page = page[0];
idname = idname.split(page)[0]; idname = idname.split(page)[0];
} }
var index = idname.match(/index.html/); var index = idname.match(/index.html/);
if (index) { if (index) {
index = index[0]; index = index[0];
idname = idname.split(index)[0]; idname = idname.split(index)[0];
} }
if (idname && $headerMenu) { if (idname && $headerMenu) {
$active_link = $('#' + idname, $headerMenu); $active_link = $('#' + idname, $headerMenu);
setUnderline($active_link); setUnderline($active_link);
@ -158,53 +179,53 @@ var customSearch;
$search.click(function (e) { $search.click(function (e) {
e.stopPropagation(); e.stopPropagation();
}); });
$header.ready(function () { $header.ready(function () {
$header.bind('keydown', function (event) { $header.bind('keydown', function (event) {
if (event.keyCode == 9) { if (event.keyCode == 9) {
return false; return false;
} else { } else {
var isie = (document.all) ? true: false; var isie = (document.all) ? true : false;
var key; var key;
var ev; var ev;
if (isie) { //IE浏览器 if (isie) { //IE浏览器
key = window.event.keyCode; key = window.event.keyCode;
ev = window.event; ev = window.event;
} else { //火狐浏览器 } else { //火狐浏览器
key = event.which; key = event.which;
ev = event; ev = event;
} }
if (key == 9) { //IE浏览器 if (key == 9) { //IE浏览器
if (isie) { if (isie) {
ev.keyCode = 0; ev.keyCode = 0;
ev.returnValue = false; ev.returnValue = false;
} else { //火狐浏览器 } else { //火狐浏览器
ev.which = 0; ev.which = 0;
ev.preventDefault(); ev.preventDefault();
} }
} }
} }
}); });
}); });
} }
function setTocToggle() { function setTocToggle() {
const $toc = $('.toc-wrapper'); const $toc = $('.toc-wrapper');
if ($toc.length === 0) return; if ($toc.length === 0) return;
// $toc.click((e) => { // $toc.click((e) => {
// e.stopPropagation(); // e.stopPropagation();
// $toc.addClass('active'); // $toc.addClass('active');
// }); // });
$(document).click(() => $toc.removeClass('active')); $(document).click(() => $toc.removeClass('active'));
$toc.on('click', 'a', (e) => { $toc.on('click', 'a', (e) => {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
if (e.target.tagName === 'A') { if (e.target.tagName === 'A') {
scrolltoElement(e.target); scrolltoElement(e.target);
} else if (e.target.tagName === 'SPAN') { } else if (e.target.tagName === 'SPAN') {
scrolltoElement(e.target.parentElement); scrolltoElement(e.target.parentElement);
} }
$toc.removeClass('active'); $toc.removeClass('active');
}); });
const liElements = Array.from($toc.find('li a')); const liElements = Array.from($toc.find('li a'));
@ -216,7 +237,9 @@ var customSearch;
const scrollTop = $('html').scrollTop() || $('body').scrollTop(); const scrollTop = $('html').scrollTop() || $('body').scrollTop();
if (!anchor) return; if (!anchor) return;
//binary search. //binary search.
let l = 0, r = anchor.length - 1, mid; let l = 0,
r = anchor.length - 1,
mid;
while (l < r) { while (l < r) {
mid = (l + r + 1) >> 1; mid = (l + r + 1) >> 1;
if (anchor[mid] === scrollTop) l = r = mid; if (anchor[mid] === scrollTop) l = r = mid;
@ -236,57 +259,52 @@ var customSearch;
scrollListener(); scrollListener();
} }
function setSearchService() {
$(function () {
//set header
setHeader();
setHeaderMenuSelection();
setHeaderMenuPhone();
setHeaderSearch();
setTocToggle();
setScrollAnchor();
// $(".article .video-container").fitVids();
setTimeout(function () {
$('#loading-bar-wrapper').fadeOut(500);
}, 300);
if (SEARCH_SERVICE === 'google') { if (SEARCH_SERVICE === 'google') {
customSearch = new GoogleCustomSearch({ customSearch = new GoogleCustomSearch({
apiKey: GOOGLE_CUSTOM_SEARCH_API_KEY, apiKey: GOOGLE_CUSTOM_SEARCH_API_KEY,
engineId: GOOGLE_CUSTOM_SEARCH_ENGINE_ID, engineId: GOOGLE_CUSTOM_SEARCH_ENGINE_ID,
imagePath: "/img/" imagePath: "/img/"
}); });
} } else if (SEARCH_SERVICE === 'algolia') {
else if (SEARCH_SERVICE === 'algolia') {
customSearch = new AlgoliaSearch({ customSearch = new AlgoliaSearch({
apiKey: ALGOLIA_API_KEY, apiKey: ALGOLIA_API_KEY,
appId: ALGOLIA_APP_ID, appId: ALGOLIA_APP_ID,
indexName: ALGOLIA_INDEX_NAME, indexName: ALGOLIA_INDEX_NAME,
imagePath: "/img/" imagePath: "/img/"
}); });
} } else if (SEARCH_SERVICE === 'hexo') {
else if (SEARCH_SERVICE === 'hexo') {
customSearch = new HexoSearch({ customSearch = new HexoSearch({
imagePath: "/img/" imagePath: "/img/"
}); });
} } else if (SEARCH_SERVICE === 'azure') {
else if (SEARCH_SERVICE === 'azure') {
customSearch = new AzureSearch({ customSearch = new AzureSearch({
serviceName: AZURE_SERVICE_NAME, serviceName: AZURE_SERVICE_NAME,
indexName: AZURE_INDEX_NAME, indexName: AZURE_INDEX_NAME,
queryKey: AZURE_QUERY_KEY, queryKey: AZURE_QUERY_KEY,
imagePath: "/img/" imagePath: "/img/"
}); });
} } else if (SEARCH_SERVICE === 'baidu') {
else if (SEARCH_SERVICE === 'baidu') {
customSearch = new BaiduSearch({ customSearch = new BaiduSearch({
apiId: BAIDU_API_ID, apiId: BAIDU_API_ID,
imagePath: "/img/" imagePath: "/img/"
}); });
} }
}
$(function () {
setHeader();
setHeaderMenuSelection();
setHeaderMenuPhone();
setHeaderSearch();
setTocToggle();
setScrollAnchor();
setSearchService();
// $(".article .video-container").fitVids();
setTimeout(function () {
$('#loading-bar-wrapper').fadeOut(500);
}, 300);
}); });
})(jQuery); })(jQuery);
Loading…
Cancel
Save