2023-09-28 04:02:42 +02:00
|
|
|
<script lang="ts">
|
2023-09-28 23:50:27 +02:00
|
|
|
import { createEventDispatcher } from "svelte"
|
2023-12-01 15:23:28 +01:00
|
|
|
import Star from "../../assets/svg/Star.svelte"
|
|
|
|
import Star_half from "../../assets/svg/Star_half.svelte"
|
|
|
|
import Star_outline from "../../assets/svg/Star_outline.svelte"
|
2024-01-22 03:42:00 +01:00
|
|
|
import { ariaLabel } from "../../Utils/ariaLabel"
|
2023-12-24 05:01:10 +01:00
|
|
|
import Translations from "../i18n/Translations"
|
2023-09-28 04:02:42 +02:00
|
|
|
|
2023-09-28 23:50:27 +02:00
|
|
|
export let score: number
|
|
|
|
export let cutoff: number
|
|
|
|
export let starSize = "w-h h-4"
|
2023-12-21 17:36:43 +01:00
|
|
|
export let i: number
|
2023-12-24 05:01:10 +01:00
|
|
|
export let readonly = false
|
2023-09-28 04:02:42 +02:00
|
|
|
|
2023-12-30 15:24:30 +01:00
|
|
|
let dispatch = createEventDispatcher<{ hover: { score: number }; click: { score: number } }>()
|
2023-09-28 23:50:27 +02:00
|
|
|
let container: HTMLElement
|
2023-09-28 04:02:42 +02:00
|
|
|
|
|
|
|
function getScore(e: MouseEvent): number {
|
2023-12-24 05:01:10 +01:00
|
|
|
if (e.clientX === 0 && e.clientY === 0) {
|
2023-12-25 22:48:29 +01:00
|
|
|
console.log("Keyboard rated", cutoff)
|
2023-12-24 05:01:10 +01:00
|
|
|
// Keyboard triggered 'click' -> return max value
|
|
|
|
return cutoff
|
|
|
|
}
|
2023-09-28 23:50:27 +02:00
|
|
|
const x = e.clientX - e.target.getBoundingClientRect().x
|
|
|
|
const w = container.getClientRects()[0]?.width
|
|
|
|
return x / w < 0.5 ? cutoff - 10 : cutoff
|
2023-09-28 04:02:42 +02:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
2023-12-24 05:01:10 +01:00
|
|
|
{#if readonly}
|
2023-09-28 04:02:42 +02:00
|
|
|
{#if score >= cutoff}
|
2023-12-01 15:23:28 +01:00
|
|
|
<Star class={starSize} />
|
2023-09-28 04:02:42 +02:00
|
|
|
{:else if score + 10 >= cutoff}
|
2023-12-01 15:23:28 +01:00
|
|
|
<Star_half class={starSize} />
|
2023-09-28 04:02:42 +02:00
|
|
|
{:else}
|
2023-12-01 15:23:28 +01:00
|
|
|
<Star_outline class={starSize} />
|
2023-09-28 04:02:42 +02:00
|
|
|
{/if}
|
2023-12-24 05:01:10 +01:00
|
|
|
{:else}
|
|
|
|
<button
|
2023-12-30 15:24:30 +01:00
|
|
|
use:ariaLabel={Translations.t.reviews.rate.Subs({ n: i + 1 })}
|
|
|
|
class="small soft no-image-background rounded-full"
|
2023-12-24 05:01:10 +01:00
|
|
|
style="padding: 0; border: none;"
|
|
|
|
bind:this={container}
|
2023-12-30 15:24:30 +01:00
|
|
|
on:click={(e) => {
|
|
|
|
console.log("Dispatching click", e)
|
|
|
|
return dispatch("click", { score: getScore(e) })
|
|
|
|
}}
|
|
|
|
on:mousemove={(e) => dispatch("hover", { score: getScore(e) })}
|
2023-12-24 05:01:10 +01:00
|
|
|
>
|
|
|
|
{#if score >= cutoff}
|
|
|
|
<Star class={starSize} />
|
|
|
|
{:else if score + 10 >= cutoff}
|
|
|
|
<Star_half class={starSize} />
|
|
|
|
{:else}
|
|
|
|
<Star_outline class={starSize} />
|
|
|
|
{/if}
|
|
|
|
</button>
|
|
|
|
{/if}
|