Skip to content

Commit

Permalink
feat: add insight & algolia search & export assets
Browse files Browse the repository at this point in the history
  • Loading branch information
ppoffice committed Mar 13, 2020
1 parent e91113e commit 5edcc36
Show file tree
Hide file tree
Showing 18 changed files with 866 additions and 49 deletions.
8 changes: 8 additions & 0 deletions asset/js/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../.eslintrc.json",
"env": {
"browser": true,
"jquery": true,
"node": false
}
}
87 changes: 87 additions & 0 deletions asset/js/algolia.js
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');
});
}
36 changes: 36 additions & 0 deletions asset/js/google_cse.js
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));
Loading

0 comments on commit 5edcc36

Please sign in to comment.