-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathcompanies-table.js
133 lines (115 loc) · 3.66 KB
/
companies-table.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
function setupSearch() {
var table = document.querySelector( 'table#companies-table' );
var searchInput = document.createElement( 'input' );
searchInput.type = 'text';
searchInput.placeholder = 'Search';
searchInput.id = 'search-input';
var searchStatus = document.createElement( 'span' );
searchStatus.id = 'search-status';
var companiesHeading = document.querySelector( 'h2#companies' );
companiesHeading.appendChild( searchInput );
companiesHeading.appendChild( searchStatus );
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>'
);
table.parentNode.insertBefore( searchExplanation, table );
var searchLoading = false;
var searchData = null;
var searchIndex = null;
var updateTimeout = null;
function updateSearch() {
if ( ! searchData || searchLoading ) {
return;
}
var searchValue = searchInput.value.toLowerCase();
var allMatch = !searchValue;
var searchResults = [];
searchData.textData.forEach( function( company, index ) {
var companyName = company.nameText.toLowerCase();
if(companyName.includes(searchValue)){
searchResults.push({ref: index});
}
});
var searchDisplayValue = searchInput.value.trim();
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' );
} );
}
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 );
}
updateTimeout = setTimeout( updateSearch, 100);
} );
document.body.setAttribute(
'class',
document.body.getAttribute( 'class' ) + ' search-enabled'
);
}
document.addEventListener( 'DOMContentLoaded', function( event ) {
setupSearch();
} );