Skip to content

Commit c3a8400

Browse files
committed
refactor: replace the deprecated js api
1 parent a2d0136 commit c3a8400

File tree

16 files changed

+48
-59
lines changed

16 files changed

+48
-59
lines changed

_javascript/commons/back-to-top.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Reference: https://bootsnipp.com/snippets/featured/link-to-top-page
33
*/
44
$(function() {
5-
$(window).scroll(() => {
5+
$(window).on('scroll',() => {
66
if ($(this).scrollTop() > 50 &&
77
$("#sidebar-trigger").css("display") === "none") {
88
$("#back-to-top").fadeIn();
@@ -11,7 +11,7 @@ $(function() {
1111
}
1212
});
1313

14-
$("#back-to-top").click(() => {
14+
$("#back-to-top").on('click',() => {
1515
$("body,html").animate({
1616
scrollTop: 0
1717
}, 800);

_javascript/commons/mode-toggle.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
* Listener for theme mode toggle
33
*/
44
$(function () {
5-
$(".mode-toggle").click((e) => {
5+
$(".mode-toggle").on('click',(e) => {
66
const $target = $(e.target);
77
let $btn = ($target.prop("tagName") === "button".toUpperCase() ?
88
$target : $target.parent());
99

10-
$btn.blur(); // remove the clicking outline
10+
$btn.trigger('blur'); // remove the clicking outline
1111
flipMode();
1212
});
1313
});

_javascript/commons/scroll-helper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const ScrollHelper = (function () {
1111
let orientationLocked = false;
1212

1313
return {
14-
hideTopbar: () => $body.attr(ATTR_TOPBAR_VISIBLE, false),
15-
showTopbar: () => $body.attr(ATTR_TOPBAR_VISIBLE, true),
14+
hideTopbar: () => $body.attr(ATTR_TOPBAR_VISIBLE, 'false'),
15+
showTopbar: () => $body.attr(ATTR_TOPBAR_VISIBLE, 'true'),
1616

1717
// scroll up
1818

_javascript/commons/search-display.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ $(function () {
7979
input.val("");
8080
visible = false;
8181
}
82-
},
83-
isVisible() {
84-
return visible;
8582
}
8683
};
8784

@@ -91,22 +88,22 @@ $(function () {
9188
return btnCancel.hasClass("loaded");
9289
}
9390

94-
btnSearchTrigger.click(function () {
91+
btnSearchTrigger.on('click',function () {
9592
mobileSearchBar.on();
9693
resultSwitch.on();
97-
input.focus();
94+
input.trigger('focus');
9895
});
9996

100-
btnCancel.click(function () {
97+
btnCancel.on('click',function () {
10198
mobileSearchBar.off();
10299
resultSwitch.off();
103100
});
104101

105-
input.focus(function () {
102+
input.on('focus',function () {
106103
searchWrapper.addClass("input-focus");
107104
});
108105

109-
input.focusout(function () {
106+
input.on('focusout', function () {
110107
searchWrapper.removeClass("input-focus");
111108
});
112109

_javascript/commons/sidebar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ $(function () {
2222

2323
}());
2424

25-
$("#sidebar-trigger").click(sidebarUtil.toggle);
25+
$("#sidebar-trigger").on('click', sidebarUtil.toggle);
2626

27-
$("#mask").click(sidebarUtil.toggle);
27+
$("#mask").on('click', sidebarUtil.toggle);
2828
});

_javascript/commons/topbar-switcher.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ $(function () {
2121
ScrollHelper.hideTopbar();
2222

2323
if ($searchInput.is(":focus")) {
24-
$searchInput.blur(); /* remove focus */
24+
$searchInput.trigger('blur'); /* remove focus */
2525
}
2626

2727
} else { // Scroll up
@@ -73,7 +73,7 @@ $(function () {
7373
});
7474
}
7575

76-
$(window).scroll(() => {
76+
$(window).on('scroll',() => {
7777
if (didScroll) {
7878
return;
7979
}

_javascript/commons/topbar-title.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ $(function () {
6060
observer.observe(document.querySelector(titleSelector));
6161

6262
/* Click title will scroll to top */
63-
$topbarTitle.click(function () {
63+
$topbarTitle.on('click', function () {
6464
$("body,html").animate({scrollTop: 0}, 800);
6565
});
6666

_javascript/utils/clipboard.js

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -97,37 +97,27 @@ $(function () {
9797

9898
/* --- Post link sharing --- */
9999

100-
$('#copy-link').click((e) => {
101-
100+
$('#copy-link').on('click',(e) => {
102101
let target = $(e.target);
103102

104103
if (isLocked(target)) {
105104
return;
106105
}
107106

108107
// Copy URL to clipboard
109-
110-
const url = window.location.href;
111-
const $temp = $("<input>");
112-
113-
$("body").append($temp);
114-
$temp.val(url).select();
115-
document.execCommand("copy");
116-
$temp.remove();
117-
118-
// Switch tooltip title
119-
120-
const defaultTitle = target.attr(ATTR_TITLE_ORIGIN);
121-
const succeedTitle = target.attr(ATTR_TITLE_SUCCEED);
122-
123-
target.attr(ATTR_TITLE_ORIGIN, succeedTitle).tooltip('show');
124-
lock(target);
125-
126-
setTimeout(() => {
127-
target.attr(ATTR_TITLE_ORIGIN, defaultTitle);
128-
unlock(target);
129-
}, TIMEOUT);
130-
108+
navigator.clipboard
109+
.writeText(window.location.href)
110+
.then(() => {
111+
const defaultTitle = target.attr(ATTR_TITLE_ORIGIN);
112+
const succeedTitle = target.attr(ATTR_TITLE_SUCCEED);
113+
// Switch tooltip title
114+
target.attr(ATTR_TITLE_ORIGIN, succeedTitle).tooltip('show');
115+
lock(target);
116+
117+
setTimeout(() => {
118+
target.attr(ATTR_TITLE_ORIGIN, defaultTitle);
119+
unlock(target);
120+
}, TIMEOUT);
121+
});
131122
});
132-
133123
});

_javascript/utils/locale-datetime.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
/* A tool for locale datetime */
88
const LocaleHelper = (function () {
9-
const locale = $('html').attr('lang').substr(0, 2);
9+
const locale = $('html').attr('lang').substring(0, 2);
1010
const attrTimestamp = 'data-ts';
1111
const attrDateFormat = 'data-df';
1212

_javascript/utils/smooth-scroll.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ $(function () {
1515
$("a[href*='#']")
1616
.not("[href='#']")
1717
.not("[href='#0']")
18-
.click(function (event) {
18+
.on('click', function (event) {
1919
if (this.pathname.replace(/^\//, "") !==
2020
location.pathname.replace(/^\//, "")) {
2121
return;
@@ -64,28 +64,30 @@ $(function () {
6464
$("html").animate({
6565
scrollTop: destOffset
6666
}, 500, () => {
67-
$target.focus();
67+
$target.trigger("focus");
6868

6969
/* clean up old scroll mark */
70-
if ($(`[${ATTR_SCROLL_FOCUS}=true]`).length) {
71-
$(`[${ATTR_SCROLL_FOCUS}=true]`).attr(ATTR_SCROLL_FOCUS, false);
70+
const $scroll_focus = $(`[${ATTR_SCROLL_FOCUS}=true]`);
71+
if ($scroll_focus.length) {
72+
$scroll_focus.attr(ATTR_SCROLL_FOCUS, "false");
7273
}
7374

7475
/* Clean :target links */
75-
if ($(":target").length) { /* element that visited by the URL with hash */
76-
$(":target").attr(ATTR_SCROLL_FOCUS, false);
76+
const $target_links = $(":target");
77+
if ($target_links.length) { /* element that visited by the URL with hash */
78+
$target_links.attr(ATTR_SCROLL_FOCUS, "false");
7779
}
7880

7981
/* set scroll mark to footnotes */
8082
if (toFootnote || toFootnoteRef) {
81-
$target.attr(ATTR_SCROLL_FOCUS, true);
83+
$target.attr(ATTR_SCROLL_FOCUS, "true");
8284
}
8385

8486
if ($target.is(":focus")) { /* Checking if the target was focused */
8587
return false;
8688
} else {
8789
$target.attr("tabindex", "-1"); /* Adding tabindex for elements not focusable */
88-
$target.focus(); /* Set focus again */
90+
$target.trigger("focus"); /* Set focus again */
8991
}
9092

9193
if (ScrollHelper.hasScrollUpTask()) {

0 commit comments

Comments
 (0)