-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add insight & algolia search & export assets
- Loading branch information
ppoffice
committed
Mar 13, 2020
1 parent
e91113e
commit 5edcc36
Showing
18 changed files
with
866 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "../../.eslintrc.json", | ||
"env": { | ||
"browser": true, | ||
"jquery": true, | ||
"node": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* global instantsearch, algoliasearch */ | ||
function loadAlgolia(config, translation) { // eslint-disable-line no-unused-vars | ||
const search = instantsearch({ | ||
indexName: config.indexName, | ||
searchClient: algoliasearch(config.applicationId, config.apiKey) | ||
}); | ||
|
||
search.addWidgets([ | ||
instantsearch.widgets.configure({ | ||
attributesToSnippet: ['excerpt'] | ||
}) | ||
]); | ||
|
||
search.addWidget(instantsearch.widgets.searchBox({ | ||
container: '#algolia-input', | ||
placeholder: translation.hint, | ||
showReset: false, | ||
showSubmit: false, | ||
showLoadingIndicator: false, | ||
cssClasses: { | ||
root: 'searchbox-input-container', | ||
form: 'searchbox-input-container', | ||
input: 'searchbox-input' | ||
} | ||
})); | ||
|
||
search.addWidget(instantsearch.widgets.poweredBy({ | ||
container: '#algolia-poweredby' | ||
})); | ||
|
||
search.addWidget(instantsearch.widgets.hits({ | ||
container: '.searchbox-body', | ||
escapeHTML: false, | ||
cssClasses: { | ||
root: 'searchbox-result-container', | ||
emptyRoot: ['searchbox-result-item', 'disabled'] | ||
}, | ||
templates: { | ||
empty: function(results) { | ||
return translation.no_result + ': ' + results.query; | ||
}, | ||
item: function(hit) { | ||
const title = instantsearch.highlight({ attribute: 'title', hit }); | ||
let excerpt = instantsearch.highlight({ attribute: 'excerpt', hit }); | ||
excerpt = excerpt.replace(new RegExp('<em>', 'ig'), '[algolia-highlight]') | ||
.replace(new RegExp('</em>', 'ig'), '[/algolia-highlight]') | ||
.replace(/(<([^>]+)>)/ig, '') | ||
.replace(/(\[algolia-highlight\])/ig, '<em>') | ||
.replace(/(\[\/algolia-highlight\])/ig, '</em>'); | ||
return `<section class="searchbox-result-section"> | ||
<a class="searchbox-result-item" href="${hit.permalink}"> | ||
<span class="searchbox-result-content"> | ||
<span class="searchbox-result-title">${title ? title : translation.untitled}</span> | ||
<span class="searchbox-result-preview">${excerpt ? excerpt : translation.empty_preview}</span> | ||
</span> | ||
</a> | ||
</section>`; | ||
} | ||
} | ||
})); | ||
|
||
search.addWidget(instantsearch.widgets.pagination({ | ||
container: '.searchbox-footer', | ||
cssClasses: { | ||
list: 'searchbox-pagination', | ||
item: 'searchbox-pagination-item', | ||
link: 'searchbox-pagination-link', | ||
selectedItem: 'active', | ||
disabledItem: 'disabled' | ||
} | ||
})); | ||
|
||
search.start(); | ||
|
||
if (location.hash.trim() === '#algolia-search') { | ||
$('.searchbox').addClass('show'); | ||
} | ||
|
||
$(document).on('click', '.navbar-main .search', () => { | ||
$('.searchbox').toggleClass('show'); | ||
$('.searchbox-input').focus(); | ||
}).on('click', '.searchbox .searchbox-mask', () => { | ||
$('.searchbox').removeClass('show'); | ||
}).on('click', '.searchbox-close', () => { | ||
$('.searchbox').removeClass('show'); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* global google */ | ||
(function(document, $) { | ||
function debounce(func, wait, immediate) { | ||
let timeout; | ||
return function() { | ||
const context = this; | ||
const args = arguments; | ||
const later = function() { | ||
timeout = null; | ||
if (!immediate) func.apply(context, args); | ||
}; | ||
const callNow = immediate && !timeout; | ||
clearTimeout(timeout); | ||
timeout = setTimeout(later, wait); | ||
if (callNow) func.apply(context, args); | ||
}; | ||
} | ||
|
||
$(document).on('click', '.navbar-main .search', () => { | ||
$('.searchbox').toggleClass('show'); | ||
}).on('click', '.searchbox .searchbox-mask', () => { | ||
$('.searchbox').removeClass('show'); | ||
}).on('click', '.searchbox-close', () => { | ||
$('.searchbox').removeClass('show'); | ||
}).on('keydown', '.searchbox-input', debounce(function() { | ||
const value = $(this).val(); | ||
try { | ||
const element = google.search.cse.element.getElement('searchresults-only0'); | ||
if (value.trim() === '') { | ||
element.clearAllResults(); | ||
} else { | ||
element.execute(value); | ||
} | ||
} catch (e) {} | ||
}, 300)); | ||
}(document, jQuery)); |
Oops, something went wrong.