Changeset View
Changeset View
Standalone View
Standalone View
swh/web/assets/src/bundles/browse/origin-search.js
/** | /** | ||||
* Copyright (C) 2018 The Software Heritage developers | * Copyright (C) 2018 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 {heapsPermute} from 'utils/heaps-permute'; | import {heapsPermute} from 'utils/heaps-permute'; | ||||
import {handleFetchError} from 'utils/functions'; | import {handleFetchError} from 'utils/functions'; | ||||
let originPatterns; | let originPatterns; | ||||
let perPage = 100; | let perPage = 2; | ||||
let limit = perPage * 2; | let limit = perPage * 2; | ||||
let offset = 0; | let offset = 0; | ||||
let currentData = null; | let currentData = null; | ||||
let inSearch = false; | let inSearch = false; | ||||
function fixTableRowsStyle() { | function fixTableRowsStyle() { | ||||
setTimeout(() => { | setTimeout(() => { | ||||
$('#origin-search-results tbody tr').removeAttr('style'); | $('#origin-search-results tbody tr').removeAttr('style'); | ||||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | if (data.length - localOffset < perPage || | ||||
$('#origins-next-results-button').removeClass('disabled'); | $('#origins-next-results-button').removeClass('disabled'); | ||||
} | } | ||||
if (offset > 0) { | if (offset > 0) { | ||||
$('#origins-prev-results-button').removeClass('disabled'); | $('#origins-prev-results-button').removeClass('disabled'); | ||||
} else { | } else { | ||||
$('#origins-prev-results-button').addClass('disabled'); | $('#origins-prev-results-button').addClass('disabled'); | ||||
} | } | ||||
inSearch = false; | inSearch = false; | ||||
if (typeof Storage !== 'undefined') { | |||||
sessionStorage.setItem('last-swh-origin-search-offset', offset); | |||||
} | |||||
setTimeout(() => { | setTimeout(() => { | ||||
window.scrollTo(0, 0); | window.scrollTo(0, 0); | ||||
}); | }); | ||||
} | } | ||||
function escapeStringRegexp(str) { | function escapeStringRegexp(str) { | ||||
let matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g; | let matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g; | ||||
return str.replace(matchOperatorsRe, '\\\\\\$&'); | return str.replace(matchOperatorsRe, '\\\\\\$&'); | ||||
Show All 13 Lines | function searchOrigins(patterns, limit, searchOffset, offset) { | ||||
clearOriginSearchResultsTable(); | clearOriginSearchResultsTable(); | ||||
$('.swh-loading').addClass('show'); | $('.swh-loading').addClass('show'); | ||||
fetch(searchUrl) | fetch(searchUrl) | ||||
.then(handleFetchError) | .then(handleFetchError) | ||||
.then(response => response.json()) | .then(response => response.json()) | ||||
.then(data => { | .then(data => { | ||||
currentData = data; | currentData = data; | ||||
if (typeof Storage !== 'undefined') { | |||||
sessionStorage.setItem('last-swh-origin-url-patterns', patterns); | |||||
sessionStorage.setItem('last-swh-origin-search-results', JSON.stringify(data)); | |||||
sessionStorage.setItem('last-swh-origin-search-offset', offset); | |||||
} | |||||
$('.swh-loading').removeClass('show'); | $('.swh-loading').removeClass('show'); | ||||
populateOriginSearchResultsTable(data, offset); | populateOriginSearchResultsTable(data, offset); | ||||
}) | }) | ||||
.catch(response => { | .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}`); | ||||
Show All 33 Lines | .catch(response => { | ||||
$('.swh-search-pagination').show(); | $('.swh-search-pagination').show(); | ||||
searchOrigins(patterns, limit, offset, offset); | searchOrigins(patterns, limit, offset, offset); | ||||
} | } | ||||
}); | }); | ||||
} | } | ||||
export function initOriginSearch() { | export function initOriginSearch() { | ||||
$(document).ready(() => { | $(document).ready(() => { | ||||
if (typeof Storage !== 'undefined') { | |||||
originPatterns = sessionStorage.getItem('last-swh-origin-url-patterns'); | |||||
let data = sessionStorage.getItem('last-swh-origin-search-results'); | |||||
offset = sessionStorage.getItem('last-swh-origin-search-offset'); | |||||
if (data) { | |||||
$('#origins-url-patterns').val(originPatterns); | |||||
offset = parseInt(offset); | |||||
currentData = JSON.parse(data); | |||||
populateOriginSearchResultsTable(currentData, offset); | |||||
} | |||||
let withVisit = sessionStorage.getItem('last-swh-origin-with-visit'); | |||||
if (withVisit !== null) { | |||||
$('#swh-search-origins-with-visit').prop('checked', JSON.parse(withVisit)); | |||||
} | |||||
let filterEmptyVisits = sessionStorage.getItem('last-filter-empty-visits'); | |||||
if (filterEmptyVisits !== null) { | |||||
$('#swh-filter-empty-visits').prop('checked', JSON.parse(filterEmptyVisits)); | |||||
} | |||||
} | |||||
$('#swh-search-origins').submit(event => { | $('#swh-search-origins').submit(event => { | ||||
event.preventDefault(); | event.preventDefault(); | ||||
let patterns = $('#origins-url-patterns').val().trim(); | let patterns = $('#origins-url-patterns').val().trim(); | ||||
if (typeof Storage !== 'undefined') { | |||||
sessionStorage.setItem('last-swh-origin-url-patterns', patterns); | |||||
sessionStorage.setItem('last-swh-origin-search-results', ''); | |||||
sessionStorage.setItem('last-swh-origin-search-offset', ''); | |||||
} | |||||
let withVisit = $('#swh-search-origins-with-visit').prop('checked'); | let withVisit = $('#swh-search-origins-with-visit').prop('checked'); | ||||
let withContent = $('#swh-filter-empty-visits').prop('checked'); | |||||
let queryParameters = '?q=' + encodeURIComponent(patterns); | let queryParameters = '?q=' + encodeURIComponent(patterns); | ||||
if (withVisit) { | if (withVisit) { | ||||
queryParameters += '&with_visit'; | queryParameters += '&with_visit'; | ||||
} | } | ||||
if (withContent) { | |||||
queryParameters += '&with_content'; | |||||
} | |||||
// Update the url, triggering page reload and effective search | // Update the url, triggering page reload and effective search | ||||
window.location.search = queryParameters; | window.location.search = queryParameters; | ||||
}); | }); | ||||
$('#origins-next-results-button').click(event => { | $('#origins-next-results-button').click(event => { | ||||
if ($('#origins-next-results-button').hasClass('disabled') || inSearch) { | if ($('#origins-next-results-button').hasClass('disabled') || inSearch) { | ||||
return; | return; | ||||
} | } | ||||
Show All 22 Lines | $(document).ready(() => { | ||||
}); | }); | ||||
$(document).on('shown.bs.tab', 'a[data-toggle="tab"]', e => { | $(document).on('shown.bs.tab', 'a[data-toggle="tab"]', e => { | ||||
if (e.currentTarget.text.trim() === 'Search') { | if (e.currentTarget.text.trim() === 'Search') { | ||||
fixTableRowsStyle(); | fixTableRowsStyle(); | ||||
} | } | ||||
}); | }); | ||||
$(window).on('unload', () => { | |||||
if (typeof Storage !== 'undefined') { | |||||
sessionStorage.setItem( | |||||
'last-swh-origin-with-visit', | |||||
JSON.stringify($('#swh-search-origins-with-visit').prop('checked'))); | |||||
sessionStorage.setItem( | |||||
'last-filter-empty-visits', | |||||
JSON.stringify($('#swh-filter-empty-visits').prop('checked'))); | |||||
} | |||||
}); | |||||
let urlParams = new URLSearchParams(window.location.search); | let urlParams = new URLSearchParams(window.location.search); | ||||
let query = urlParams.get('q'); | let query = urlParams.get('q'); | ||||
let withVisit = urlParams.has('with_visit'); | let withVisit = urlParams.has('with_visit'); | ||||
let data = sessionStorage.getItem('last-swh-origin-search-results'); | let withContent = urlParams.has('with_content'); | ||||
if (query && !data) { | if (query) { | ||||
$('#origins-url-patterns').val(query); | $('#origins-url-patterns').val(query); | ||||
if (withVisit) { | $('#swh-search-origins-with-visit').prop('checked', withVisit); | ||||
$('#swh-search-origins-with-visit').prop('checked', true); | $('#swh-search-origins-with-content').prop('checked', withContent); | ||||
} | |||||
doSearch(); | doSearch(); | ||||
} | } | ||||
}); | }); | ||||
} | } |