Skip to content

Commit

Permalink
feat: scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
kitian616 committed Feb 19, 2018
1 parent fb2a060 commit 45e26f4
Show file tree
Hide file tree
Showing 21 changed files with 550 additions and 382 deletions.
13 changes: 12 additions & 1 deletion _data/variables.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,15 @@ default:
rss: /feed.xml
lang: zh
toc:
selectors: 'h1,h2,h3'
selectors: 'h1,h2,h3'

sources:
jquery: 'https://cdn.bootcss.com/jquery/3.1.1/jquery.min.js'
leancloud_js_sdk: '//cdn1.lncld.net/static/js/3.4.1/av-min.js'
toc: 'https://cdn.bootcss.com/toc/0.3.2/toc.min.js'
chart: 'https://cdn.bootcss.com/Chart.js/2.7.1/Chart.bundle.min.js'
gitalk:
js: '//unpkg.com/gitalk/dist/gitalk.min.js'
css: '//unpkg.com/gitalk/dist/gitalk.css'
mathjax: 'https://cdn.bootcss.com/mathjax/2.7.2/MathJax.js?config=TeX-MML-AM_CHTML'
mermaid: 'https://cdn.bootcss.com/mermaid/7.1.2/mermaid.min.js'
6 changes: 3 additions & 3 deletions _includes/components/tags.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
{%- if gap_size < 1 -%}
{%- assign gap_size = 1 -%}
{%- endif -%}
<div class="m-tags">
<div class="m-tags js-tags">
<ul>
<li>
<button type="button" class="js-article-tag js-tag-show-all all pill-button" onclick="tagSelect('')">Show All
<button type="button" class="js-article-tag js-tag-show-all all pill-button">Show All
<div class="tag-count">{{ site.posts | size }}</div>
</button>
</li>
Expand All @@ -44,7 +44,7 @@
{%- assign c_index = 4 -%}
{%- endif -%}
<li><button type="button" class="js-article-tag tag-{{ c_index }} pill-button"
data-tag="{{ tag[0] | strip | url_encode }}" onclick="tagSelect('{{ tag[0] | strip | url_encode }}')">
data-tag="{{ tag[0] | strip | url_encode }}">
<span>{{ tag[0] | strip }}</span><div class="tag-count">{{ tag[1].size }}</div>
</button>
</li>
Expand Down
101 changes: 101 additions & 0 deletions _includes/scripts/all.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{%- include snippets/page-title.html -%}
<script>
(function() {
function queryString() {
// This function is anonymous, is executed immediately and
// the return value is assigned to QueryString!
var i = 0,
queryObj = {},
pair;
var queryStr = window.location.search.substring(1);
var queryArr = queryStr.split('&');
for (i = 0; i < queryArr.length; i++) {
pair = queryArr[i].split('=');
// If first entry with this name
if (typeof queryObj[pair[0]] === 'undefined') {
queryObj[pair[0]] = pair[1];
// If second entry with this name
} else if (typeof queryObj[pair[0]] === 'string') {
queryObj[pair[0]] = [queryObj[pair[0]], pair[1]];
// If third or later entry with this name
} else {
queryObj[pair[0]].push(pair[1]);
}
}
return queryObj;
}

window.Lazyload.js('{{ site.data.variables.sources.jquery }}', function() {
var $pageHeading = $('.js-page-heading');
var $tags = $('.js-tags');
var $articleTag = $('.js-article-tag');
var $tagShowAll = $('.js-tag-show-all');
var $postBlock = $('.js-post-block');
var $yearSection = $('.js-year-wrapper section');

$tags.on('click', 'button', function() {
tagSelect($(this).data('tag'));
});

function tagSelect(tag) {
var i = 0,
flag = false,
cur, tagStr = '',
tags = [],
_tag = '',
lis = [];
if (tag === undefined || tag === '') {
$pageHeading.text('{{ __return }}');
$tagShowAll.focus();
$postBlock.show();
} else {
tag = tag.trim();
// $articleTag.filter('.tag-' + tag).focus();
for (i = 0; i < $articleTag.length; i++) {
cur = $articleTag.eq(i);
_tag = cur.data('tag');
if (typeof _tag === 'string' && _tag.trim() === tag) {
cur.focus();
tagStr = cur.children('span').text();
(typeof tagStr === 'string') && (tagStr = tagStr.trim());
$pageHeading.text('Tag: ' + tagStr);
break;
}
}
$postBlock.each(function() {
flag = false;
cur = $(this);
tags = cur.data('tag').split(',');
for (i = 0; i < tags.length; i++) {
_tag = tags[i].trim();
if (_tag === tag) {
flag = true;
cur.show();
break;
}
}
flag || (cur.hide());
});
}
$yearSection.each(function() {
flag = false;
cur = $(this);
lis = cur.find('li');
for (i = 0; i < lis.length; i++) {
if (lis.eq(i).css('display') !== 'none') {
flag = true;
break;
}
}
flag ? cur.show() : cur.hide();
});
tag === undefined || window.history.replaceState(null, '', window.location.href.split('?')[0] + '?tag=' + tag);
}

var query = queryString(),
tag = undefined;
query.tag === undefined || (tag = query.tag);
tagSelect(tag);
});
})();
</script>
24 changes: 24 additions & 0 deletions _includes/scripts/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{%- if page.key and site.leancloud.app_id and site.leancloud.app_key and site.leancloud.app_class and jekyll.environment != "development" -%}
<script>
window.Lazyload.js('{{ site.data.variables.sources.leancloud_js_sdk }}', function() {
AV.init({
appId: '{{ site.leancloud.app_id }}',
appKey: '{{ site.leancloud.app_key }}'
});
$(".article-view").each(function() {
var curId = this.id;
var query = new AV.Query('{{ site.leancloud.app_class }}');
query.equalTo('key', curId.substr(9));
query.first().then(function(result) {
if (result) {
$('#' + curId).text(result.attributes.views);
}
}, function(error) {
if (error) {
throw error;
}
});
});
});
</script>
{%- endif -%}
24 changes: 24 additions & 0 deletions _includes/scripts/page-post.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<script>
window.Lazyload.js('{{ site.data.variables.sources.jquery }}', function() {
var $articleContent = $('.m-post, .m-page').find('.m-article-content'), $this;
$articleContent.children('.highlight').each(function() {
$this = $(this);
$this.attr('data-lang', $this.find('code').attr('data-lang'));
});
function scrollAnimateTo(destination, duration, callback) {
var $body = $('html, body'), bodyScrollTop = $body.scrollTop();
if(bodyScrollTop === destination) { return; }
$body.animate({ scrollTop: destination }, duration, callback);
}
$articleContent.children('h1, h2, h3, h4, h5, h6').each(function() {
$this = $(this);
$this.append($('<a class="anchor" aria-hidden="true"></a>').html('{%- include icon/link.svg -%}'));
});
$articleContent.on('click', '.anchor', function(e) {
var targetId = $(this).parent().attr('id');
scrollAnimateTo($('#' + targetId).offset().top, 400, function() {
window.history.replaceState(null, '', window.location.href.split('#')[0] + '#' + targetId);
});
});
});
</script>
128 changes: 128 additions & 0 deletions _includes/scripts/post.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
{%- assign _toc_selectors = site.toc.selectors | default: site.data.variables.default.toc.selectors -%}
<script>
window.Lazyload.js('{{ site.data.variables.sources.jquery }}', function() {
window.Lazyload.js('{{ site.data.variables.sources.toc }}', function() {
var $window = $(window);
var $pageStage = $('.js-page-stage');
var $pageMain = $('.js-main');
var $pageFooter = $('.js-page-footer');
var $articleContent = $('.js-article-content');
var $articleAside = $('.js-article-aside');
var $toc = $('.js-toc');
var $col2 = $('.js-col-2');
var hasTitle = $articleContent.find('{{ _toc_selectors }}').length > 0;
function asideSticky() {
return $col2.css('display') !== 'none' && $pageStage.hasClass('has-toc');
}

function setTocClass() {
if (hasTitle) {
!$pageStage.hasClass('has-toc') && $pageStage.addClass('has-toc');
}
}

setTocClass();

function setAsideTOC() {
var asideTop, asideLeft, scrollBottom, asideBottomTop, lastScrollTop;

function init() {
var asideOffset = $articleAside.offset();
var footerOffset = $pageFooter.offset();
var mainOffset = $pageMain.offset();
asideTop = mainOffset.top;
asideHeight = $toc.outerHeight() + parseInt($articleAside.css('padding-top'), 10) + parseInt($articleAside.css('padding-bottom'), 10);
asideLeft = mainOffset.left + $pageMain.outerWidth() - $articleAside.outerWidth() - parseInt($pageMain.css('padding-right'), 10);
scrollBottom = footerOffset.top - asideHeight;
asideBottomTop = scrollBottom - mainOffset.top;
}

function setAside(force) {
force !== true && (force = false);
var scrollTop = $window.scrollTop();
if (scrollTop >= asideTop && scrollTop <= scrollBottom) {
(!force && lastScrollTop >= asideTop && lastScrollTop <= scrollBottom) ||
$articleAside.addClass('fixed').css({
left: asideLeft + 'px',
top: 0
});
} else if (scrollTop < asideTop) {
(!force && lastScrollTop < asideTop) ||
$articleAside.removeClass('fixed').css({
left: 0,
top: 0
});
} else {
(!force && lastScrollTop > scrollBottom) ||
$articleAside.removeClass('fixed').css({
left: 0,
top: asideBottomTop + 'px'
});
}
lastScrollTop = scrollTop;
}
asideSticky() && (init(), setAside());
$window.on('scroll', function() {
asideSticky() && setAside();
});
$window.on('resize', window.throttle(function() {
setTocClass();
asideSticky() && (init(), setAside(true));
}, 100));
setTimeout(init, 4000);
}
setTimeout(setAsideTOC, 1000);
$toc.toc({
selectors: '{{ _toc_selectors }}',
container: '.js-article-content',
highlightOffset: 0
});
});
});

{%- if page.key and site.leancloud.app_id and site.leancloud.app_key and site.leancloud.app_class and jekyll.environment != "development" -%}
window.Lazyload.js(['{{ site.data.variables.sources.jquery }}', '{{ site.data.variables.sources.leancloud_js_sdk }}'], function() {
AV.init({
appId: '{{ site.leancloud.app_id }}',
appKey: '{{ site.leancloud.app_key }}'
});
var query = new AV.Query('{{ site.leancloud.app_class }}');
query.equalTo('key', '{{ page.key }}');
query.first().then(function(result) {
if (result) {
addOne(result)
} else {
var Blog = AV.Object.extend('{{ site.leancloud.app_class }}');
var blog = new Blog();
blog.set('title', '{{ page.title }}');
blog.set('key', '{{ page.key }}');
blog.set('views', 0);
blog.save().then(function(page) {
addOne(page)
}, function(error) {
if (error) {
throw error;
}
});
}

function addOne(page) {
page.increment('views', 1);
page.save(null, {
fetchWhenSave: true
}).then(function(page) {
$("#post-key-{{ page.key }}").text(page.attributes.views);
}, function(error) {
if (error) {
throw error;
}
});
}
}, function(error) {
if (error) {
throw error;
}
});
});
{%- endif -%}
</script>
Loading

0 comments on commit 45e26f4

Please sign in to comment.