2020-05-06 06:42:21 +00:00
|
|
|
function setupSearch() {
|
2018-08-12 01:41:11 +00:00
|
|
|
var table = document.querySelector( 'table#companies-table' );
|
2018-05-28 20:46:25 +00:00
|
|
|
|
2020-05-06 06:42:21 +00:00
|
|
|
var searchInput = document.createElement( 'input' );
|
|
|
|
searchInput.type = 'text';
|
|
|
|
searchInput.placeholder = 'Search';
|
|
|
|
searchInput.id = 'search-input';
|
2018-05-28 20:46:25 +00:00
|
|
|
|
2020-05-06 06:42:21 +00:00
|
|
|
var searchStatus = document.createElement( 'span' );
|
|
|
|
searchStatus.id = 'search-status';
|
2018-05-28 20:46:25 +00:00
|
|
|
|
2018-08-12 01:41:11 +00:00
|
|
|
var companiesHeading = document.querySelector( 'h2#companies' );
|
2020-05-06 06:42:21 +00:00
|
|
|
companiesHeading.appendChild( searchInput );
|
|
|
|
companiesHeading.appendChild( searchStatus );
|
2018-05-28 20:46:25 +00:00
|
|
|
|
2020-05-06 06:42:21 +00:00
|
|
|
var searchExplanation = document.createElement( 'p' );
|
|
|
|
searchExplanation.id = 'search-explanation';
|
|
|
|
searchExplanation.innerHTML = (
|
|
|
|
'Use the text box above to search all of our company data. '
|
|
|
|
+ ' <a href="https://blog.remoteintech.company/search-help/">More info</a>'
|
2018-08-12 01:41:11 +00:00
|
|
|
);
|
2020-05-06 06:42:21 +00:00
|
|
|
table.parentNode.insertBefore( searchExplanation, table );
|
|
|
|
|
|
|
|
var searchLoading = false;
|
|
|
|
var searchData = null;
|
|
|
|
var searchIndex = null;
|
|
|
|
var updateTimeout = null;
|
|
|
|
|
|
|
|
function updateSearch() {
|
|
|
|
if ( ! searchData || searchLoading ) {
|
|
|
|
return;
|
2018-05-28 20:46:25 +00:00
|
|
|
}
|
2024-10-04 16:07:47 +00:00
|
|
|
var searchValue = searchInput.value.toLowerCase();
|
|
|
|
var allMatch = !searchValue;
|
|
|
|
var searchResults = [];
|
2020-05-06 06:42:21 +00:00
|
|
|
|
2024-10-04 16:07:47 +00:00
|
|
|
searchData.textData.forEach( function( company, index ) {
|
|
|
|
var companyName = company.nameText.toLowerCase();
|
|
|
|
if(companyName.includes(searchValue)){
|
|
|
|
searchResults.push({ref: index});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var searchDisplayValue = searchInput.value.trim();
|
2020-05-06 06:42:21 +00:00
|
|
|
if ( allMatch ) {
|
|
|
|
searchStatus.innerHTML = (
|
|
|
|
'Empty search; showing all '
|
|
|
|
+ searchData.textData.length
|
|
|
|
+ ' companies'
|
|
|
|
);
|
|
|
|
} else if ( searchResults.length === 1 ) {
|
|
|
|
searchStatus.innerText = searchDisplayValue + ': 1 result';
|
|
|
|
} else {
|
|
|
|
searchStatus.innerText = (
|
|
|
|
searchDisplayValue + ': '
|
|
|
|
+ searchResults.length + ' results'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
var searchMatches = {};
|
|
|
|
searchResults.forEach( function( r ) {
|
|
|
|
searchMatches[ +r.ref ] = r;
|
|
|
|
} );
|
|
|
|
if ( window.console && console.log ) {
|
|
|
|
console.log( 'search', { value: searchValue, results: searchResults } );
|
|
|
|
}
|
|
|
|
searchData.textData.forEach( function( company, index ) {
|
|
|
|
var match = searchMatches[ index ];
|
|
|
|
var row = document.getElementById( 'company-row-' + index );
|
|
|
|
var rowMatch = row.nextElementSibling;
|
|
|
|
if ( rowMatch && rowMatch.classList.contains( 'company-match' ) ) {
|
|
|
|
rowMatch.parentNode.removeChild( rowMatch );
|
|
|
|
}
|
|
|
|
row.style.display = ( match || allMatch ? '' : 'none' );
|
|
|
|
row.classList.remove( 'has-match' );
|
2024-10-04 16:07:47 +00:00
|
|
|
|
2020-05-06 06:42:21 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
searchInput.addEventListener( 'focus', function() {
|
|
|
|
if ( searchData || searchLoading ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
searchLoading = true;
|
|
|
|
var searchLoadingText = 'Loading search data...';
|
|
|
|
|
|
|
|
searchStatus.innerHTML = searchLoadingText;
|
|
|
|
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.open( 'GET', searchIndexFilename );
|
|
|
|
|
|
|
|
xhr.onprogress = function( e ) {
|
|
|
|
var percentLoaded;
|
|
|
|
if ( e.lengthComputable ) {
|
|
|
|
percentLoaded = Math.round( 100 * e.loaded / e.total );
|
|
|
|
} else {
|
|
|
|
percentLoaded = Math.min(
|
|
|
|
100,
|
|
|
|
Math.round( 100 * e.loaded / searchIndexSize )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
searchStatus.innerHTML = searchLoadingText + ' ' + percentLoaded + '%';
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.onload = function() {
|
|
|
|
searchLoading = false;
|
|
|
|
if ( xhr.status === 200 ) {
|
|
|
|
searchData = JSON.parse( xhr.response );
|
|
|
|
searchIndex = lunr.Index.load( searchData.index );
|
|
|
|
updateSearch();
|
|
|
|
} else {
|
|
|
|
searchStatus.innerHTML = 'Error!';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send();
|
|
|
|
} );
|
|
|
|
|
|
|
|
searchInput.addEventListener( 'keyup', function() {
|
|
|
|
if ( updateTimeout ) {
|
|
|
|
clearTimeout( updateTimeout );
|
|
|
|
}
|
2024-10-04 16:07:47 +00:00
|
|
|
updateTimeout = setTimeout( updateSearch, 100);
|
2020-05-06 06:42:21 +00:00
|
|
|
} );
|
2018-08-12 01:41:11 +00:00
|
|
|
|
2018-08-13 04:12:18 +00:00
|
|
|
document.body.setAttribute(
|
|
|
|
'class',
|
2020-05-06 06:42:21 +00:00
|
|
|
document.body.getAttribute( 'class' ) + ' search-enabled'
|
2018-08-13 04:12:18 +00:00
|
|
|
);
|
2018-05-28 20:46:25 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 01:41:11 +00:00
|
|
|
document.addEventListener( 'DOMContentLoaded', function( event ) {
|
2020-05-06 06:42:21 +00:00
|
|
|
setupSearch();
|
2018-08-12 01:41:11 +00:00
|
|
|
} );
|