mirror of
https://github.com/techgaun/active-forks.git
synced 2024-12-22 21:42:13 +01:00
Merge pull request #9 from alcar/feature/direct-links
Replace query string handling with hash logic
This commit is contained in:
commit
42babe6ec8
1 changed files with 10 additions and 14 deletions
24
js/main.js
24
js/main.js
|
@ -1,7 +1,8 @@
|
||||||
window.addEventListener('load', () => {
|
window.addEventListener('load', () => {
|
||||||
initDT() // Initialize the DatatTable and window.columnNames variables
|
initDT() // Initialize the DatatTable and window.columnNames variables
|
||||||
|
|
||||||
const repo = getQueryParams().q;
|
const repo = getRepoFromUrl();
|
||||||
|
|
||||||
if (repo) {
|
if (repo) {
|
||||||
document.getElementById('q').value = repo;
|
document.getElementById('q').value = repo;
|
||||||
fetchData();
|
fetchData();
|
||||||
|
@ -17,7 +18,11 @@ function fetchData() {
|
||||||
const repo = document.getElementById('q').value;
|
const repo = document.getElementById('q').value;
|
||||||
const re = /[-_\w]+\/[-_.\w]+/;
|
const re = /[-_\w]+\/[-_.\w]+/;
|
||||||
|
|
||||||
window.history.pushState('', '', `?q=${repo}`);
|
const urlRepo = getRepoFromUrl();
|
||||||
|
|
||||||
|
if (!urlRepo || urlRepo !== repo) {
|
||||||
|
window.history.pushState('', '', `/#${repo}`);
|
||||||
|
}
|
||||||
|
|
||||||
if (re.test(repo)) {
|
if (re.test(repo)) {
|
||||||
fetchAndShow(repo);
|
fetchAndShow(repo);
|
||||||
|
@ -156,19 +161,10 @@ function showData(data) {
|
||||||
document.getElementById('footer').innerHTML = `${data.length} ${data.length == 1 ? 'result' : 'results'}`;
|
document.getElementById('footer').innerHTML = `${data.length} ${data.length == 1 ? 'result' : 'results'}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getQueryParams() {
|
function getRepoFromUrl() {
|
||||||
let query = location.search;
|
const urlRepo = location.hash && location.hash.slice(1);
|
||||||
if (!query) {
|
|
||||||
return { };
|
|
||||||
}
|
|
||||||
|
|
||||||
return (/^[?#]/.test(query) ? query.slice(1) : query)
|
return urlRepo && decodeURIComponent(urlRepo);
|
||||||
.split('&')
|
|
||||||
.reduce((params, param) => {
|
|
||||||
let [ key, value ] = param.split('=');
|
|
||||||
params[key] = value ? decodeURIComponent(value.replace(/\+/g, ' ')) : '';
|
|
||||||
return params;
|
|
||||||
}, { });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function timeSince(date_str) {
|
function timeSince(date_str) {
|
||||||
|
|
Loading…
Reference in a new issue