reposearch/script.template.js

133 lines
3.8 KiB
JavaScript
Raw Normal View History

2023-02-02 17:07:19 +00:00
var searchBar = document.getElementById('search');
var resultsDiv = document.getElementById('results');
var accessTokenInputsDiv = document.getElementById('access_token_inputs');
var setAccessTokenA = document.getElementById('set_access_token');
var options = {
findAllMatches: true,
keys: [
'name',
'description',
]
}
var results = [];
var REPOS = [];
var fuse = new Fuse(REPOS, options)
function setCookie(name, value, seconds) {
var expires = "";
if (seconds) {
var date = new Date();
date.setTime(date.getTime() + (seconds*1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
function get(url, cb, errcb) {
var xhr = new XMLHttpRequest();
var u = new URL(url);
u.searchParams.set('access_token', getCookie('access_token'))
xhr.responseType = 'json';
xhr.open('GET', u);
xhr.send()
xhr.onload = cb;
xhr.onerror = errcb
}
function loadRepos() {
get('GITEA_SITE/api/v1/user/repos?limit=50000',
function(x) {
console.log('loadRepos');
console.log(x);
REPOS = x.target.response;
console.log(REPOS);
fuse = new Fuse(REPOS, options);
applySearch();
}
);
}
function applySearch() {
results = fuse.search(searchBar.value);
console.log(results)
resultsDiv.innerHTML = "";
results.forEach(function(r) {
p = document.createElement('div');
p.classList.add('searchResult');
fullNameDiv = document.createElement('div');
fullNameDiv.innerHTML = r.item.full_name;
aDiv = document.createElement('a');
aDiv.href = r.item.html_url;
aDiv.appendChild(fullNameDiv);
descriptionDiv = document.createElement('div');
descriptionDiv.classList.add('searchSmallText');
descriptionDiv.innerHTML = 'description: ' + r.item.description;
propertiesDiv = document.createElement('div');
propertiesDiv.classList.add('searchSmallText');
propertiesDiv.innerHTML += r.item.private ? "private" : "";
propertiesDiv.innerHTML += r.item.archived ? " archived" : "";
p.appendChild(aDiv);
p.appendChild(descriptionDiv);
p.appendChild(propertiesDiv);
resultsDiv.appendChild(p);
});
}
function openAccessTokenInput() {
var accessTokenInput = document.createElement('input');
accessTokenInput.placeholder = 'access token';
accessTokenInput.type = 'password';
function setAccessToken(ev) {
if (ev.key != "Enter") return;
setCookie('access_token', accessTokenInput.value);
setAccessTokenA = document.createElement('a');
setAccessTokenA.innerHTML = 'set access token';
setAccessTokenA.addEventListener('click', openAccessTokenInput);
accessTokenInputsDiv.innerHTML = '';
accessTokenInputsDiv.appendChild(setAccessTokenA);
}
accessTokenInput.addEventListener('keyup', setAccessToken);
accessTokenInputsDiv.innerHTML = '';
accessTokenInputsDiv.appendChild(accessTokenInput);
}
setAccessTokenA.addEventListener('click', openAccessTokenInput);
searchBar.focus();
searchBar.addEventListener('change', applySearch);
searchBar.addEventListener('keyup', applySearch);
searchBar.addEventListener('keypress', function(ev) {
if (ev.key != "Enter") return
if (ev.shiftKey) window.open(results[0].item.html_url, '_blank');
else window.location.href = results[0].item.html_url;
});
// attempt to load repos if user is authenticated
if (getCookie('access_token')) loadRepos();