mirror of
https://github.com/jerryc127/hexo-theme-butterfly.git
synced 2026-04-16 17:50:54 +08:00
first commit
This commit is contained in:
136
source/js/search/algolia.js
Normal file
136
source/js/search/algolia.js
Normal file
@@ -0,0 +1,136 @@
|
||||
$(function () {
|
||||
$('a.social-icon.search').on('click', function () {
|
||||
$('body').css('width', '100%')
|
||||
$('body').css('overflow', 'hidden')
|
||||
|
||||
$('.search-dialog').animate({}, function () {
|
||||
$('.search-dialog').css({
|
||||
'display': 'block',
|
||||
'animation': 'titlescale 0.5s'
|
||||
})
|
||||
})
|
||||
|
||||
$('.ais-search-box--input').focus()
|
||||
$('.search-mask').fadeIn();
|
||||
// shortcut: ESC
|
||||
document.addEventListener('keydown', function f(event) {
|
||||
if (event.code == "Escape") {
|
||||
closeSearch();
|
||||
document.removeEventListener('keydown', f);
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
var closeSearch = function () {
|
||||
$('body').css('overflow', 'auto')
|
||||
|
||||
$('.search-dialog').animate({}, function () {
|
||||
$('.search-dialog').css({
|
||||
'display': 'none'
|
||||
})
|
||||
})
|
||||
|
||||
$('.search-mask').fadeOut();
|
||||
}
|
||||
$('.search-mask, .search-close-button').on('click', closeSearch)
|
||||
|
||||
|
||||
var algolia = GLOBAL_CONFIG.algolia
|
||||
var isAlgoliaValid = algolia.appId && algolia.apiKey && algolia.indexName
|
||||
if (!isAlgoliaValid) {
|
||||
return console.error('Algolia setting is invalid!')
|
||||
}
|
||||
|
||||
var search = instantsearch({
|
||||
appId: algolia.appId,
|
||||
apiKey: algolia.apiKey,
|
||||
indexName: algolia.indexName,
|
||||
searchParameters: {
|
||||
hitsPerPage: algolia.hits.per_page || 10
|
||||
},
|
||||
searchFunction: function (helper) {
|
||||
var searchInput = $('#algolia-search-input').find('input')
|
||||
|
||||
if (searchInput.val()) {
|
||||
helper.search()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
search.addWidget(
|
||||
instantsearch.widgets.searchBox({
|
||||
container: '#algolia-search-input',
|
||||
reset: false,
|
||||
magnifier: false,
|
||||
placeholder: GLOBAL_CONFIG.algolia.languages.input_placeholder
|
||||
})
|
||||
)
|
||||
search.addWidget(
|
||||
instantsearch.widgets.hits({
|
||||
container: '#algolia-hits',
|
||||
templates: {
|
||||
item: function (data) {
|
||||
var link = data.permalink ? data.permalink : (GLOBAL_CONFIG.root + data.path)
|
||||
return (
|
||||
'<a href="' + link + '" class="algolia-hit-item-link">' +
|
||||
data._highlightResult.title.value +
|
||||
'</a>'
|
||||
)
|
||||
},
|
||||
empty: function (data) {
|
||||
return (
|
||||
'<div id="algolia-hits-empty">' +
|
||||
GLOBAL_CONFIG.algolia.languages.hits_empty.replace(/\$\{query}/, data.query) +
|
||||
'</div>'
|
||||
)
|
||||
}
|
||||
},
|
||||
cssClasses: {
|
||||
item: 'algolia-hit-item'
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
search.addWidget(
|
||||
instantsearch.widgets.stats({
|
||||
container: '#algolia-stats',
|
||||
templates: {
|
||||
body: function (data) {
|
||||
var stats = GLOBAL_CONFIG.algolia.languages.hits_stats
|
||||
.replace(/\$\{hits}/, data.nbHits)
|
||||
.replace(/\$\{time}/, data.processingTimeMS)
|
||||
return (
|
||||
'<hr>' +
|
||||
stats +
|
||||
'<span class="algolia-logo pull-right">' +
|
||||
' <img src="' + GLOBAL_CONFIG.root + 'img/algolia.svg" alt="Algolia" />' +
|
||||
'</span>'
|
||||
)
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
search.addWidget(
|
||||
instantsearch.widgets.pagination({
|
||||
container: '#algolia-pagination',
|
||||
scrollTo: false,
|
||||
showFirstLast: false,
|
||||
labels: {
|
||||
first: '<i class="fa fa-angle-double-left"></i>',
|
||||
last: '<i class="fa fa-angle-double-right"></i>',
|
||||
previous: '<i class="fa fa-angle-left"></i>',
|
||||
next: '<i class="fa fa-angle-right"></i>'
|
||||
},
|
||||
cssClasses: {
|
||||
root: 'pagination',
|
||||
item: 'pagination-item',
|
||||
link: 'page-number',
|
||||
active: 'current',
|
||||
disabled: 'disabled-item'
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
search.start()
|
||||
})
|
||||
102
source/js/search/local-search.js
Normal file
102
source/js/search/local-search.js
Normal file
@@ -0,0 +1,102 @@
|
||||
$(function () {
|
||||
var loadFlag = false
|
||||
$('a.social-icon.search').on('click', function () {
|
||||
$('body').css('width', '100%')
|
||||
$('body').css('overflow', 'hidden')
|
||||
$('.search-dialog').animate({}, function () {
|
||||
$('.search-dialog').css({
|
||||
'display': 'block',
|
||||
'animation': 'titlescale 0.5s'
|
||||
})
|
||||
})
|
||||
$('#local-search-input input').focus()
|
||||
|
||||
$('.search-mask').fadeIn();
|
||||
if (!loadFlag) {
|
||||
search(GLOBAL_CONFIG.localSearch.path)
|
||||
loadFlag = true
|
||||
}
|
||||
|
||||
// shortcut: ESC
|
||||
document.addEventListener('keydown', function f(event) {
|
||||
if (event.code === 'Escape') {
|
||||
closeSearch()
|
||||
document.removeEventListener('keydown', f)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
var closeSearch = function () {
|
||||
$('body').css('overflow', 'auto')
|
||||
$('.search-dialog').animate({}, function () {
|
||||
$('.search-dialog').css({
|
||||
'display': 'none'
|
||||
})
|
||||
})
|
||||
$('.search-mask').fadeOut();
|
||||
}
|
||||
$('.search-mask, .search-close-button').on('click', closeSearch)
|
||||
|
||||
function search(path) {
|
||||
$.ajax({
|
||||
url: GLOBAL_CONFIG.root + path,
|
||||
dataType: 'xml',
|
||||
success: function (xmlResponse) {
|
||||
// get the contents from search data
|
||||
var datas = $('entry', xmlResponse).map(function () {
|
||||
return {
|
||||
title: $('title', this).text(),
|
||||
content: $('content', this).text(),
|
||||
url: $('url', this).text()
|
||||
}
|
||||
}).get()
|
||||
var $input = $('#local-search-input input')[0]
|
||||
var $resultContent = $('#local-hits')[0]
|
||||
$input.addEventListener('input', function () {
|
||||
var str = '<div class="search-result-list">'
|
||||
var keywords = this.value.trim().toLowerCase().split(/[\s]+/)
|
||||
$resultContent.innerHTML = ''
|
||||
if (this.value.trim().length <= 0) {
|
||||
$('.local-search-stats__hr').hide()
|
||||
return
|
||||
}
|
||||
var count = 0
|
||||
// perform local searching
|
||||
datas.forEach(function (data) {
|
||||
var isMatch = true
|
||||
var dataTitle = data.title.trim().toLowerCase()
|
||||
var dataContent = data.content.trim().replace(/<[^>]+>/g, '').toLowerCase()
|
||||
var dataUrl = data.url
|
||||
var indexTitle = -1
|
||||
var indexContent = -1
|
||||
// only match artiles with not empty titles and contents
|
||||
if (dataTitle !== '' && dataContent !== '') {
|
||||
keywords.forEach(function (keyword, i) {
|
||||
indexTitle = dataTitle.indexOf(keyword)
|
||||
indexContent = dataContent.indexOf(keyword)
|
||||
if (indexTitle < 0 && indexContent < 0) {
|
||||
isMatch = false
|
||||
} else {
|
||||
if (indexContent < 0) {
|
||||
indexContent = 0
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
// show search results
|
||||
if (isMatch) {
|
||||
str += '<div class="local-search__hit-item"><a href="' + dataUrl + '" class="search-result-title">' + dataTitle + '</a>' + '</div>'
|
||||
count += 1
|
||||
$('.local-search-stats__hr').show()
|
||||
}
|
||||
})
|
||||
if (count === 0) {
|
||||
str += '<div id="local-search__hits-empty">' + GLOBAL_CONFIG.localSearch.languages.hits_empty.replace(/\$\{query}/, this.value.trim()) +
|
||||
'</div>'
|
||||
}
|
||||
$resultContent.innerHTML = str
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user