Commit a662f472 authored by David Trattnig's avatar David Trattnig
Browse files

Feat: Provide ability to shuffle categories

parent 6604d74c
<script lang="ts">
import { onMount } from 'svelte'
import { fetchApi } from './common/Common.svelte'
import { fetchApi, shuffle } from './common/Common.svelte'
import CategoryCard from './cards/CategoryCard.svelte'
export let api: string = 'https://prog-info.o94.at/api.php'
export let endpointCategories: string = 'categories'
export let categoriesToDisplay: [] // Default: Display all categories
export let shuffleCategories: boolean = false
/* Nested props */
export let urlShowList: string
export let renderhtml: boolean = false // Attention: to avoid XSS attack vectors use with trusted API sources only
......@@ -23,6 +24,8 @@
function processResponse(data: any) {
console.log('Categories API response:', data)
if (!categoriesToDisplay || categoriesToDisplay.length == 0) {
if (shuffleCategories)
data = shuffle(data)
categories = data
} else {
let selectedCats: any[] = Array()
......@@ -33,6 +36,8 @@
}
}
}
if (shuffleCategories)
selectedCats = shuffle(selectedCats)
categories = selectedCats
}
}
......
......@@ -114,6 +114,19 @@
else return false
}
/* Shuffles an array based on the Fisher–Yates shuffle algorithm */
export function shuffle(array) {
let counter = array.length;
while (counter > 0) {
let index = Math.floor(Math.random() * counter);
counter--;
let temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
return array;
}
/*
Load external CSS into a HTML5 Web Component
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment