mirror of
https://github.com/stashapp/stash.git
synced 2025-12-18 21:04:37 +03:00
Accept random seed from UI for random sorting (#328)
This commit is contained in:
@@ -69,6 +69,7 @@ export class ListFilterModel {
|
|||||||
public displayModeOptions: DisplayMode[] = [];
|
public displayModeOptions: DisplayMode[] = [];
|
||||||
public criterionOptions: ICriterionOption[] = [];
|
public criterionOptions: ICriterionOption[] = [];
|
||||||
public criteria: Array<Criterion<any, any>> = [];
|
public criteria: Array<Criterion<any, any>> = [];
|
||||||
|
public randomSeed = -1;
|
||||||
|
|
||||||
private static createCriterionOption(criterion: CriterionType) {
|
private static createCriterionOption(criterion: CriterionType) {
|
||||||
return new CriterionOption(Criterion.getLabel(criterion), criterion);
|
return new CriterionOption(Criterion.getLabel(criterion), criterion);
|
||||||
@@ -193,6 +194,19 @@ export class ListFilterModel {
|
|||||||
const params = rawParms as IQueryParameters;
|
const params = rawParms as IQueryParameters;
|
||||||
if (params.sortby !== undefined) {
|
if (params.sortby !== undefined) {
|
||||||
this.sortBy = params.sortby;
|
this.sortBy = params.sortby;
|
||||||
|
|
||||||
|
// parse the random seed if provided
|
||||||
|
const randomPrefix = "random_";
|
||||||
|
if (this.sortBy && this.sortBy.startsWith(randomPrefix)) {
|
||||||
|
const seedStr = this.sortBy.substring(randomPrefix.length);
|
||||||
|
|
||||||
|
this.sortBy = "random";
|
||||||
|
try {
|
||||||
|
this.randomSeed = Number.parseInt(seedStr, 10);
|
||||||
|
} catch (err) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (params.sortdir === "asc" || params.sortdir === "desc") {
|
if (params.sortdir === "asc" || params.sortdir === "desc") {
|
||||||
this.sortDirection = params.sortdir;
|
this.sortDirection = params.sortdir;
|
||||||
@@ -227,6 +241,28 @@ export class ListFilterModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private setRandomSeed() {
|
||||||
|
if (this.sortBy === "random") {
|
||||||
|
// #321 - set the random seed if it is not set
|
||||||
|
if (this.randomSeed === -1) {
|
||||||
|
// generate 8-digit seed
|
||||||
|
this.randomSeed = Math.floor(Math.random() * (10 ** 8));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.randomSeed = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private getSortBy(): string | undefined {
|
||||||
|
this.setRandomSeed();
|
||||||
|
|
||||||
|
if (this.sortBy === "random") {
|
||||||
|
return `${this.sortBy}_${this.randomSeed.toString()}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.sortBy;
|
||||||
|
}
|
||||||
|
|
||||||
public makeQueryParameters(): string {
|
public makeQueryParameters(): string {
|
||||||
const encodedCriteria: string[] = [];
|
const encodedCriteria: string[] = [];
|
||||||
this.criteria.forEach(criterion => {
|
this.criteria.forEach(criterion => {
|
||||||
@@ -239,7 +275,7 @@ export class ListFilterModel {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const result = {
|
const result = {
|
||||||
sortby: this.sortBy,
|
sortby: this.getSortBy(),
|
||||||
sortdir: this.sortDirection,
|
sortdir: this.sortDirection,
|
||||||
disp: this.displayMode,
|
disp: this.displayMode,
|
||||||
q: this.searchTerm,
|
q: this.searchTerm,
|
||||||
@@ -256,7 +292,7 @@ export class ListFilterModel {
|
|||||||
q: this.searchTerm,
|
q: this.searchTerm,
|
||||||
page: this.currentPage,
|
page: this.currentPage,
|
||||||
per_page: this.itemsPerPage,
|
per_page: this.itemsPerPage,
|
||||||
sort: this.sortBy,
|
sort: this.getSortBy(),
|
||||||
direction:
|
direction:
|
||||||
this.sortDirection === "asc"
|
this.sortDirection === "asc"
|
||||||
? SortDirectionEnum.Asc
|
? SortDirectionEnum.Asc
|
||||||
|
|||||||
Reference in New Issue
Block a user