Skip to content
This repository has been archived by the owner on Nov 27, 2022. It is now read-only.

Commit

Permalink
move scripts to post and page tempaltes
Browse files Browse the repository at this point in the history
  • Loading branch information
zutrinken committed Jan 26, 2020
1 parent 2031390 commit b183b48
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 94 deletions.
2 changes: 1 addition & 1 deletion assets/css/style.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/js/script.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions default.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,12 @@

</section>

{{!-- Load basic scripts --}}
<script type="text/javascript" src="{{asset "js/script.js"}}"></script>

{{!-- Load scripts from other template files --}}
{{{block "scripts"}}}

{{ghost_foot}}

</body>
Expand Down
33 changes: 33 additions & 0 deletions page.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,36 @@
</main>

{{/post}}

{{!-- The #contentFor helper here will send everything inside it up to the matching #block helper found in default.hbs --}}
{{#contentFor "scripts"}}
<script>
$(document).ready(function () {
var post = $('.post-content');
// Responsive videos with fitVids
post.fitVids();
// Format code blocks and add line numbers
function codestyling() {
$('pre code').each(function(i, e) {
// Code highlight
hljs.highlightBlock(e);
// No lines for plain text blocks
if (!$(this).hasClass('language-text')) {
var code = $(this);
// Calculate amount of lines
var lines = code.html().split(/\n(?!$)/g).length;
var numbers = [];
if (lines > 1) {
lines++;
}
for (i = 1; i < lines; i++) {
numbers += '<span class="line" aria-hidden="true">' + i + '</span>';
}
code.parent().append('<div class="lines">' + numbers + '</div>');
}
});
}
codestyling();
});
</script>
{{/contentFor}}
72 changes: 72 additions & 0 deletions post.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,75 @@
</main>

{{/post}}

{{!-- The #contentFor helper here will send everything inside it up to the matching #block helper found in default.hbs --}}
{{#contentFor "scripts"}}
<script>
$(document).ready(function () {
var viewport = $(window);
var post = $('.post-content');
// Responsive videos with fitVids
post.fitVids();
// Format code blocks and add line numbers
function codestyling() {
$('pre code').each(function(i, e) {
// Code highlight
hljs.highlightBlock(e);
// No lines for plain text blocks
if (!$(this).hasClass('language-text')) {
var code = $(this);
// Calculate amount of lines
var lines = code.html().split(/\n(?!$)/g).length;
var numbers = [];
if (lines > 1) {
lines++;
}
for (i = 1; i < lines; i++) {
numbers += '<span class="line" aria-hidden="true">' + i + '</span>';
}
code.parent().append('<div class="lines">' + numbers + '</div>');
}
});
}
codestyling();
// Reading progress bar on window top
function readingProgress() {
var postBottom = post.offset().top + post.height();
var viewportHeight = viewport.height();
var progress = 100 - (((postBottom - (viewport.scrollTop() + viewportHeight) + viewportHeight / 3) / (postBottom - viewportHeight + viewportHeight / 3)) * 100);
$('.progress-bar').css('width', progress + '%');
(progress > 100) ? $('.progress-container').addClass('complete'): $('.progress-container').removeClass('complete');
}
readingProgress();
// Trigger reading progress
viewport.on({
'scroll': function() {
readingProgress();
},
'resize': function() {
readingProgress();
},
'orientationchange': function() {
readingProgress();
}
});
// Check if disqus is defined by code injection
if (typeof disqus === 'undefined') {
// hide comment section
$('.post-comments').css({
'display': 'none'
});
} else {
$('#show-disqus').on('click', function() {
$.ajax({
type: "GET",
url: "//" + disqus + ".disqus.com/embed.js",
dataType: "script",
cache: true
});
$(this).parent().addClass('activated');
});
}
});
</script>
{{/contentFor}}
90 changes: 2 additions & 88 deletions src/js/script.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
jQuery(function($) {

var body = $('body');
var html = $('html');
var viewport = $(window);

Expand Down Expand Up @@ -43,10 +42,8 @@ jQuery(function($) {
ghostHunter
========================================================================== */

if (typeof ghosthunter_key === 'undefined') {

} else {
var searchField = $("#search-field")
if (typeof ghosthunter_key !== 'undefined') {
var searchField = $("#search-field");
$(".nav-search").css({
'display': 'block'
});
Expand Down Expand Up @@ -118,36 +115,6 @@ jQuery(function($) {
}
});

/* ==========================================================================
Reading Progress
========================================================================== */

var post = $('.post-content');

function readingProgress() {
if (post.length >= 1) {
var postBottom = post.offset().top + post.height();
var windowBottom = viewport.scrollTop() + viewport.height();
var progress = 100 - (((postBottom - windowBottom + viewport.height() / 3) / (postBottom - viewport.height() + viewport.height() / 3)) * 100);
$('.progress-bar').css('width', progress + '%');
(progress > 100) ? $('.progress-container').addClass('ready'): $('.progress-container').removeClass('ready');
}
}

readingProgress();

viewport.on({
'scroll': function() {
readingProgress();
},
'resize': function() {
readingProgress();
},
'orientationchange': function() {
readingProgress();
}
});

/* ==========================================================================
Gallery
========================================================================== */
Expand All @@ -163,57 +130,4 @@ jQuery(function($) {
});
}
gallery();

/* ==========================================================================
Style code blocks with highlight and numbered lines
========================================================================== */

function codestyling() {
$('pre code').each(function(i, e) {
hljs.highlightBlock(e);
/* No lines for plain text blocks */
if (!$(this).hasClass('language-text')) {
var code = $(this);
var lines = code.html().split(/\n(?!$)/g).length;
var numbers = [];
if (lines > 1) {
lines++;
}
for (i = 1; i < lines; i++) {
numbers += '<span class="line" aria-hidden="true">' + i + '</span>';
}
code.parent().append('<div class="lines">' + numbers + '</div>');
}
});
}
codestyling();

/* ==========================================================================
Responsive Videos with Fitvids
========================================================================== */

function video() {
$('#wrapper').fitVids();
}
video();

/* ==========================================================================
Initialize and load Disqus
========================================================================== */

if (typeof disqus === 'undefined') {
$('.post-comments').css({
'display': 'none'
});
} else {
$('#show-disqus').on('click', function() {
$.ajax({
type: "GET",
url: "//" + disqus + ".disqus.com/embed.js",
dataType: "script",
cache: true
});
$(this).parent().addClass('activated');
});
}
});
7 changes: 3 additions & 4 deletions src/sass/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1412,18 +1412,17 @@ textarea {
z-index: 1000;
background: transparent;
transition: all ease-out 0.2s;
}

.progress-container.ready {
transform: translateY(-3px);
&.complete {
transform: translateY(-3px);
}
}

.progress-bar {
display: block;
width: 0;
height: 3px;
background: $color-primary;
//box-shadow: 0 0 1px $color-primary, 0 0 3px $color-primary;
}
/* ==========================================================================
Footer
Expand Down

0 comments on commit b183b48

Please sign in to comment.