Skip to content

Commit af57ac1

Browse files
author
Sebastian Benjamin
committed
Fix URL encoding behavior and tests
1 parent 0343b45 commit af57ac1

File tree

4 files changed

+47
-36
lines changed

4 files changed

+47
-36
lines changed

jbrowse/src/client/JBrowse/VariantSearch/components/VariantTableWidget.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
GridToolbarExport
1414
} from '@mui/x-data-grid';
1515
import SearchIcon from '@mui/icons-material/Search';
16+
import { OperatorKey } from '../operators'
1617
import LinkIcon from '@mui/icons-material/Link';
1718
import DownloadIcon from '@mui/icons-material/Download'
1819
import { ActionURL } from '@labkey/api';
@@ -99,7 +100,7 @@ const VariantTableWidget = observer(props => {
99100
const handleExport = () => {
100101
const currentUrl = new URL(window.location.href);
101102

102-
const searchString = buildLuceneQuery(filters);
103+
const searchString = encodeURIComponent(buildLuceneQuery(filters));
103104
const sortField = sortModel[0]?.field ?? 'genomicPosition';
104105
const sortDirection = sortModel[0]?.sort ?? false;
105106

@@ -553,7 +554,7 @@ const VariantTableWidget = observer(props => {
553554
<div style={{ marginBottom: "10px", display: "flex", alignItems: "center" }}>
554555
<div style={{ flex: 1 }}>
555556
{filters.map((filter, index) => {
556-
if ((filter as any).field && ((filter as any).operator === "is empty" || (filter as any).operator === "is not empty") && !(filter as any).value) {
557+
if ((filter as any).field && ((filter as any).operator.key === OperatorKey.IsEmpty || (filter as any).operator.key === OperatorKey.IsNotEmpty) && !(filter as any).value) {
557558
return (
558559
<Button
559560
key={index}

jbrowse/src/client/JBrowse/VariantSearch/operators.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,15 @@ export interface Operator<T extends Value = Value> {
3131
generateLucene(field: string, value: T): string
3232
}
3333

34-
function fuzzyNumRange(field: string, v: number) {
35-
return `[${v - 0.000001} TO ${v + 0.000001}]`
34+
function fuzzyNumRange(field: string, raw: string | number) {
35+
const floatValue = parseFloat(String(raw))
36+
const intValue = parseInt(String(raw), 10)
37+
38+
if (floatValue === intValue) {
39+
return `[${intValue} TO ${intValue}]`
40+
}
41+
42+
return `[${floatValue - 0.000001} TO ${floatValue + 0.000001}]`
3643
}
3744

3845
export const OperatorRegistry: Record<OperatorKey, Operator> = {

jbrowse/src/client/JBrowse/utils.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ export function navigateToSearch(sessionId, locString, trackId, isValidRefNameFo
240240
const start = parsedLocString.start;
241241
const end = parsedLocString.end;
242242

243-
searchString = serializeLocationToLuceneQuery(contig, start, end)
243+
searchString = serializeLocationToEncodedSearchString(contig, start, end)
244244
}
245245

246246
window.location.href = ActionURL.buildURL("jbrowse", "variantSearch.view", null, {session: sessionId, location: locString, trackId: trackId, activeTracks: trackId, sampleFilters: sampleFilterURL, infoFilters: infoFilterURL, searchString: searchString})
@@ -318,7 +318,7 @@ export function getGenotypeURL(trackId, contig, start, end) {
318318
return ActionURL.buildURL("jbrowse", "genotypeTable.view", null, {trackId: trackId, chr: contig, start: start, stop: end})
319319
}
320320

321-
export function serializeLocationToLuceneQuery(contig, start, end) {
321+
export function serializeLocationToEncodedSearchString(contig, start, end) {
322322
const filters = [
323323
new Filter("contig", OperatorKey.Equals, contig.toString()),
324324
new Filter("start", OperatorKey.NumericGte, start.toString()),
@@ -348,6 +348,9 @@ export async function fetchLuceneQuery(filters, sessionId, trackGUID, offset, pa
348348
return
349349
}
350350

351+
const lucene = buildLuceneQuery(filters)
352+
const encoded = encodeURIComponent(lucene)
353+
351354
let sortReverse;
352355
if(sortReverseString == "asc") {
353356
sortReverse = true
@@ -366,7 +369,7 @@ export async function fetchLuceneQuery(filters, sessionId, trackGUID, offset, pa
366369
failureCallback("There was an error: " + res.status + "\n Status Body: " + res.responseText + "\n Session ID:" + sessionId)
367370
},
368371
params: {
369-
"searchString": buildLuceneQuery(filters),
372+
"searchString": encoded,
370373
"sessionId": sessionId,
371374
"trackId": trackGUID,
372375
"offset": offset,
@@ -513,11 +516,11 @@ export function createEncodedFilterString(filters: Filter[]): string {
513516
}
514517

515518
export function buildLuceneQuery(filters: Filter[]): string {
516-
if (!filters.length || filters.every(f => f.isEmpty())) return 'all'
517-
return filters
518-
.filter(f => !f.isEmpty())
519-
.map(f => f.toLucene())
520-
.join('&')
519+
if (!filters.length || filters.every(f => f.isEmpty())) return 'all'
520+
return filters
521+
.filter(f => !f.isEmpty())
522+
.map(f => f.toLucene())
523+
.join('&')
521524
}
522525

523526
export async function fetchFieldTypeInfo(sessionId: string, trackId: string, successCallback: (fields: FieldModel[], groups: string[], promotedFilters: Map<string, Filter[]>) => void, failureCallback) {

0 commit comments

Comments
 (0)