Changeset View
Changeset View
Standalone View
Standalone View
assets/src/bundles/browse/origin-search.js
/** | /** | ||||
* Copyright (C) 2018-2021 The Software Heritage developers | * Copyright (C) 2018-2021 The Software Heritage developers | ||||
* See the AUTHORS file at the top-level directory of this distribution | * See the AUTHORS file at the top-level directory of this distribution | ||||
* License: GNU Affero General Public License version 3, or any later version | * License: GNU Affero General Public License version 3, or any later version | ||||
* See top-level LICENSE file for more information | * See top-level LICENSE file for more information | ||||
*/ | */ | ||||
import {handleFetchError} from 'utils/functions'; | import {handleFetchError, isArchivedOrigin} from 'utils/functions'; | ||||
const limit = 100; | const limit = 100; | ||||
let linksPrev = []; | let linksPrev = []; | ||||
let linkNext = null; | let linkNext = null; | ||||
let linkCurrent = null; | let linkCurrent = null; | ||||
let inSearch = false; | let inSearch = false; | ||||
function parseLinkHeader(s) { | function parseLinkHeader(s) { | ||||
▲ Show 20 Lines • Show All 140 Lines • ▼ Show 20 Lines | .catch(response => { | ||||
$('.swh-loading').removeClass('show'); | $('.swh-loading').removeClass('show'); | ||||
inSearch = false; | inSearch = false; | ||||
$('#swh-origin-search-results').hide(); | $('#swh-origin-search-results').hide(); | ||||
$('#swh-no-result').text(`Error ${response.status}: ${response.statusText}`); | $('#swh-no-result').text(`Error ${response.status}: ${response.statusText}`); | ||||
$('#swh-no-result').show(); | $('#swh-no-result').show(); | ||||
}); | }); | ||||
} | } | ||||
function doSearch() { | async function doSearch() { | ||||
$('#swh-no-result').hide(); | $('#swh-no-result').hide(); | ||||
let searchQueryText = $('#swh-origins-url-patterns').val(); | let searchQueryText = $('#swh-origins-url-patterns').val(); | ||||
inSearch = true; | inSearch = true; | ||||
if (searchQueryText.startsWith('swh:')) { | if (searchQueryText.startsWith('swh:')) { | ||||
// searchQueryText may be a PID so sending search queries to PID resolve endpoint | // searchQueryText may be a PID so sending search queries to PID resolve endpoint | ||||
let resolveSWHIDUrl = Urls.api_1_resolve_swhid(searchQueryText); | let resolveSWHIDUrl = Urls.api_1_resolve_swhid(searchQueryText); | ||||
fetch(resolveSWHIDUrl) | fetch(resolveSWHIDUrl) | ||||
.then(handleFetchError) | .then(handleFetchError) | ||||
.then(response => response.json()) | .then(response => response.json()) | ||||
.then(data => { | .then(data => { | ||||
// SWHID has been successfully resolved, | // SWHID has been successfully resolved, | ||||
// so redirect to browse page | // so redirect to browse page | ||||
window.location = data.browse_url; | window.location = data.browse_url; | ||||
}) | }) | ||||
.catch(response => { | .catch(response => { | ||||
// display a useful error message if the input | // display a useful error message if the input | ||||
// looks like a SWHID | // looks like a SWHID | ||||
response.json().then(data => { | response.json().then(data => { | ||||
$('#swh-origin-search-results').hide(); | $('#swh-origin-search-results').hide(); | ||||
$('.swh-search-pagination').hide(); | $('.swh-search-pagination').hide(); | ||||
$('#swh-no-result').text(data.reason); | $('#swh-no-result').text(data.reason); | ||||
$('#swh-no-result').show(); | $('#swh-no-result').show(); | ||||
}); | }); | ||||
}); | }); | ||||
} else if (await isArchivedOrigin(searchQueryText)) { | |||||
// redirect to the browse origin | |||||
window.location.href = | |||||
`${Urls.browse_origin()}?origin_url=${encodeURIComponent(searchQueryText)}`; | |||||
} else { | } else { | ||||
// otherwise, proceed with origins search | // otherwise, proceed with origins search irrespective of the error | ||||
$('#swh-origin-search-results').show(); | $('#swh-origin-search-results').show(); | ||||
$('.swh-search-pagination').show(); | $('.swh-search-pagination').show(); | ||||
searchOriginsFirst(searchQueryText, limit); | searchOriginsFirst(searchQueryText, limit); | ||||
} | } | ||||
} | } | ||||
export function initOriginSearch() { | export function initOriginSearch() { | ||||
$(document).ready(() => { | $(document).ready(() => { | ||||
▲ Show 20 Lines • Show All 67 Lines • Show Last 20 Lines |