Skip to content

Commit

Permalink
Improve JS code style.
Browse files Browse the repository at this point in the history
  • Loading branch information
cotes2020 committed Aug 19, 2020
1 parent 71ac3e1 commit 142a6dc
Show file tree
Hide file tree
Showing 11 changed files with 256 additions and 250 deletions.
10 changes: 5 additions & 5 deletions assets/js/_commons/back-to-top.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ Reference: https://bootsnipp.com/snippets/featured/link-to-top-page
*/
$(window).scroll(function() {
if ($(this).scrollTop() > 50
&& $('#sidebar-trigger').css('display') == 'none') {
$('#back-to-top').fadeIn();
&& $("#sidebar-trigger").css("display") === "none") {
$("#back-to-top").fadeIn();
} else {
$('#back-to-top').fadeOut();
$("#back-to-top").fadeOut();
}
});

$(function() {
$('#back-to-top').click(function() {
$('body,html').animate({scrollTop: 0}, 800);
$("#back-to-top").click(function() {
$("body,html").animate({scrollTop: 0}, 800);
return false;
});
});
18 changes: 11 additions & 7 deletions assets/js/_commons/copy-link.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/*
* Copy current page url to clipboard.
* v2.1
* https://github.com/cotes2020/jekyll-theme-chirpy
* © 2020 Cotes Chung
* MIT License
*/
* Copy current page url to clipboard.
* v2.1
* https://github.com/cotes2020/jekyll-theme-chirpy
* © 2020 Cotes Chung
* MIT License
*/

function copyLink(url) {
if (!url || 0 === url.length)
if (!url || 0 === url.length) {
return;
}

url = window.location.href;
var $temp = $("<input>");

Expand All @@ -17,4 +20,5 @@ function copyLink(url) {
$temp.remove();

alert("Link copied successfully!");

}
119 changes: 60 additions & 59 deletions assets/js/_commons/search-display.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,94 +8,95 @@

$(function() {

var btnSbTrigger = $('#sidebar-trigger');
var btnSearchTrigger = $('#search-trigger');
var btnCancel = $('#search-cancel');
var btnClear = $('#search-cleaner');
var btnSbTrigger = $("#sidebar-trigger");
var btnSearchTrigger = $("#search-trigger");
var btnCancel = $("#search-cancel");
var btnClear = $("#search-cleaner");

var main = $('#main');
var topbarTitle = $('#topbar-title');
var searchWrapper = $('#search-wrapper');
var resultWrapper = $('#search-result-wrapper');
var results = $('#search-results');
var input = $('#search-input');
var hints = $('#search-hints');
var main = $("#main");
var topbarTitle = $("#topbar-title");
var searchWrapper = $("#search-wrapper");
var resultWrapper = $("#search-result-wrapper");
var results = $("#search-results");
var input = $("#search-input");
var hints = $("#search-hints");


/*--- Actions in small screens (Sidebar unloaded) ---*/

var scrollBlocker = (function() {
var offset = 0;
return {
block: function() {
block() {
offset = $(window).scrollTop();
},
release: function() {
$('html,body').scrollTop(offset);
release() {
$("html,body").scrollTop(offset);
},
getOffset: function() {
getOffset() {
return offset;
}
}
})();
};
}());

var mobileSearchBar = (function() {
return {
on: function() {
btnSbTrigger.addClass('unloaded');
topbarTitle.addClass('unloaded');
btnSearchTrigger.addClass('unloaded');
searchWrapper.addClass('d-flex');
btnCancel.addClass('loaded');
on() {
btnSbTrigger.addClass("unloaded");
topbarTitle.addClass("unloaded");
btnSearchTrigger.addClass("unloaded");
searchWrapper.addClass("d-flex");
btnCancel.addClass("loaded");
},
off: function() {
btnCancel.removeClass('loaded');
searchWrapper.removeClass('d-flex');
btnSbTrigger.removeClass('unloaded');
topbarTitle.removeClass('unloaded');
btnSearchTrigger.removeClass('unloaded');
off() {
btnCancel.removeClass("loaded");
searchWrapper.removeClass("d-flex");
btnSbTrigger.removeClass("unloaded");
topbarTitle.removeClass("unloaded");
btnSearchTrigger.removeClass("unloaded");
}
}
})();
};
}());

var resultSwitch = (function() {
var visable = false;

return {
on: function() {
on() {
if (!visable) {
resultWrapper.removeClass('unloaded');
main.addClass('hidden');
resultWrapper.removeClass("unloaded");
main.addClass("hidden");

visable = true;
scrollBlocker.block();
}
},
off: function() {
off() {
if (visable) {
results.empty();
if (hints.hasClass('unloaded')) {
hints.removeClass('unloaded');
if (hints.hasClass("unloaded")) {
hints.removeClass("unloaded");
}
resultWrapper.addClass('unloaded');
btnClear.removeClass('visable');
main.removeClass('hidden');
resultWrapper.addClass("unloaded");
btnClear.removeClass("visable");
main.removeClass("hidden");

input.val('');
input.val("");
visable = false;

scrollBlocker.release();
}
},
isVisable: function() {
isVisable() {
return visable;
}
}
})();
};

}());


function isMobileView() {
return btnCancel.hasClass('loaded');
return btnCancel.hasClass("loaded");
}

btnSearchTrigger.click(function() {
Expand All @@ -110,45 +111,45 @@ $(function() {
});

input.focus(function() {
searchWrapper.addClass('input-focus');
searchWrapper.addClass("input-focus");
});

input.focusout(function() {
searchWrapper.removeClass('input-focus');
searchWrapper.removeClass("input-focus");
});

input.on('keyup', function(e) {
if (e.keyCode == 8 && input.val() == '') {
input.on("keyup", function(e) {
if (e.keyCode === 8 && input.val() === "") {
if (!isMobileView()) {
resultSwitch.off();
} else {
hints.removeClass('unloaded');
hints.removeClass("unloaded");
}
} else {
if (input.val() != '') {
if (input.val() !== "") {
resultSwitch.on();

if (!btnClear.hasClass('visible')) {
btnClear.addClass('visable');
if (!btnClear.hasClass("visible")) {
btnClear.addClass("visable");
}

if (isMobileView()) {
hints.addClass('unloaded');
hints.addClass("unloaded");
}
}
}
});

btnClear.on('click', function() {
input.val('');
btnClear.on("click", function() {
input.val("");
if (isMobileView()) {
hints.removeClass('unloaded');
hints.removeClass("unloaded");
results.empty();
} else {
resultSwitch.off();
}
input.focus();
btnClear.removeClass('visable');
btnClear.removeClass("visable");
});

});
});
14 changes: 7 additions & 7 deletions assets/js/_commons/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ $(function() {
var sidebarUtil = (function() {
const ATTR_DISPLAY = "sidebar-display";
var isExpanded = false;
var body = $('body');
var body = $("body");

return {
toggle: function() {
if (isExpanded == false) {
body.attr(ATTR_DISPLAY, '');
toggle() {
if (isExpanded === false) {
body.attr(ATTR_DISPLAY, "");
} else {
body.removeAttr(ATTR_DISPLAY);
}

isExpanded = !isExpanded;
}
}
};

})();
}());

$("#sidebar-trigger").click(sidebarUtil.toggle);

$('#mask').click(sidebarUtil.toggle);
$("#mask").click(sidebarUtil.toggle);

});
56 changes: 29 additions & 27 deletions assets/js/_commons/topbar-switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,59 @@ $(function() {
var didScroll;
var lastScrollTop = 0;
var delta = 5;
var topbarHeight = $('#topbar-wrapper').outerHeight();

$(window).scroll(function(event) {
if ($("#topbar-title").is(":hidden")) { /* Not in small screens */
didScroll = true;
}
});

setInterval(function() {
if (didScroll) {
hasScrolled();
didScroll = false;
}
}, 250);
var topbarHeight = $("#topbar-wrapper").outerHeight();

function hasScrolled() {
var st = $(this).scrollTop();

/* Make sure they scroll more than delta */
if (Math.abs(lastScrollTop - st) <= delta)
if (Math.abs(lastScrollTop - st) <= delta) {
return;
}

if (st > lastScrollTop && st > topbarHeight) {
/* Scroll Down */
$('#topbar-wrapper').removeClass('topbar-down').addClass('topbar-up');
$("#topbar-wrapper").removeClass("topbar-down").addClass("topbar-up");

if ( $('#toc-wrapper').length > 0) {
$('#toc-wrapper').removeClass('topbar-down');
if ($("#toc-wrapper").length > 0) {
$("#toc-wrapper").removeClass("topbar-down");
}

if ( $('.access').length > 0) {
$('.access').removeClass('topbar-down');
if ($(".access").length > 0) {
$(".access").removeClass("topbar-down");
}

if ($('#search-input').is(':focus')) {
$('#search-input').blur(); /* remove focus */
if ($("#search-input").is(":focus")) {
$("#search-input").blur(); /* remove focus */
}

} else {
/* Scroll Up */
if (st + $(window).height() < $(document).height()) {
$('#topbar-wrapper').removeClass('topbar-up').addClass('topbar-down');
if ( $('#toc-wrapper').length > 0) {
$('#toc-wrapper').addClass('topbar-down');
$("#topbar-wrapper").removeClass("topbar-up").addClass("topbar-down");
if ($("#toc-wrapper").length > 0) {
$("#toc-wrapper").addClass("topbar-down");
}
if ( $('.access').length > 0) {
$('.access').addClass('topbar-down');
if ($(".access").length > 0) {
$(".access").addClass("topbar-down");
}
}
}

lastScrollTop = st;
}

$(window).scroll(function(event) {
if ($("#topbar-title").is(":hidden")) { /* Not in small screens */
didScroll = true;
}
});

setInterval(function() {
if (didScroll) {
hasScrolled();
didScroll = false;
}
}, 250);

});
Loading

0 comments on commit 142a6dc

Please sign in to comment.