mirror of
https://github.com/ae-utbm/sith.git
synced 2026-05-18 23:18:08 +00:00
improve $notifications
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
<title>{% trans %}Slideshow{% endtrans %}</title>
|
||||
<link rel="shortcut icon" href="{{ static('core/img/favicon.ico') }}">
|
||||
<link href="{{ static('css/slideshow.scss') }}" rel="stylesheet" type="text/css" />
|
||||
<script type="module" src="{{ static('bundled/alpine-index.js') }}"></script>
|
||||
<script type="module" src="{{ static('bundled/alpine-index.ts') }}"></script>
|
||||
<script type="module" src="{{ static('bundled/com/slideshow-index.ts') }}"></script>
|
||||
</head>
|
||||
<body x-data="slideshow([
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
import sort from "@alpinejs/sort";
|
||||
import Alpine from "alpinejs";
|
||||
import { limitedChoices } from "#core:alpine/limited-choices.ts";
|
||||
import { alpinePlugin as notificationPlugin } from "#core:utils/notifications.ts";
|
||||
|
||||
Alpine.plugin([sort, limitedChoices]);
|
||||
Alpine.magic("notifications", notificationPlugin);
|
||||
window.Alpine = Alpine;
|
||||
|
||||
window.addEventListener("DOMContentLoaded", () => {
|
||||
Alpine.start();
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
import sort from "@alpinejs/sort";
|
||||
import { Alpine } from "alpinejs";
|
||||
import { limitedChoices } from "#core:alpine/limited-choices";
|
||||
import {
|
||||
type NotificationPlugin,
|
||||
alpinePlugin as notificationPlugin,
|
||||
} from "#core:utils/notifications";
|
||||
|
||||
declare module "alpinejs" {
|
||||
interface Magics<T> {
|
||||
$notifications: NotificationPlugin;
|
||||
}
|
||||
}
|
||||
Alpine.plugin([sort, limitedChoices]);
|
||||
Alpine.magic("notifications", notificationPlugin);
|
||||
// biome-ignore lint/style/useNamingConvention: it's how it's named
|
||||
Object.assign(window, { Alpine });
|
||||
|
||||
window.addEventListener("DOMContentLoaded", () => {
|
||||
Alpine.start();
|
||||
});
|
||||
@@ -1,30 +1,40 @@
|
||||
import { Alpine } from "alpinejs";
|
||||
|
||||
export enum NotificationLevel {
|
||||
Error = "error",
|
||||
Warning = "warning",
|
||||
Success = "success",
|
||||
}
|
||||
|
||||
export function createNotification(message: string, level: NotificationLevel) {
|
||||
const element = document.getElementById("quick-notifications");
|
||||
if (element === null) {
|
||||
return false;
|
||||
}
|
||||
return element.dispatchEvent(
|
||||
new CustomEvent("quick-notification-add", {
|
||||
detail: { text: message, tag: level },
|
||||
}),
|
||||
);
|
||||
export interface NotificationPlugin {
|
||||
error: (message: string) => void;
|
||||
warning: (message: string) => void;
|
||||
success: (message: string) => void;
|
||||
clear: () => void;
|
||||
addMany: (notifs: Notification[]) => void;
|
||||
getAll: () => Notification[];
|
||||
}
|
||||
|
||||
export function deleteNotifications() {
|
||||
const element = document.getElementById("quick-notifications");
|
||||
if (element === null) {
|
||||
return false;
|
||||
}
|
||||
return element.dispatchEvent(new CustomEvent("quick-notification-delete"));
|
||||
export interface Notification {
|
||||
tag: NotificationLevel;
|
||||
text: string;
|
||||
}
|
||||
|
||||
export function alpinePlugin() {
|
||||
Alpine.store("notifications", [] as Notification[]);
|
||||
|
||||
function createNotification(message: string, level: NotificationLevel) {
|
||||
(Alpine.store("notifications") as Notification[]).push({ text: message, tag: level });
|
||||
}
|
||||
|
||||
function deleteNotifications() {
|
||||
Alpine.store("notifications", []);
|
||||
}
|
||||
|
||||
function getNotifications() {
|
||||
return Alpine.store("notifications") as Notification[];
|
||||
}
|
||||
|
||||
export function alpinePlugin(): NotificationPlugin {
|
||||
return {
|
||||
error: (message: string) => createNotification(message, NotificationLevel.Error),
|
||||
warning: (message: string) =>
|
||||
@@ -32,5 +42,10 @@ export function alpinePlugin() {
|
||||
success: (message: string) =>
|
||||
createNotification(message, NotificationLevel.Success),
|
||||
clear: () => deleteNotifications(),
|
||||
addMany: (notifs: Notification[]) =>
|
||||
notifs.forEach((n) => {
|
||||
createNotification(n.text, n.tag);
|
||||
}),
|
||||
getAll: () => getNotifications(),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
<script src="{{ url('javascript-catalog') }}"></script>
|
||||
<script type="module" src="{{ static("bundled/core/navbar-index.ts") }}"></script>
|
||||
<script type="module" src="{{ static("bundled/core/components/include-index.ts") }}"></script>
|
||||
<script type="module" src="{{ static('bundled/alpine-index.js') }}"></script>
|
||||
<script type="module" src="{{ static('bundled/alpine-index.ts') }}"></script>
|
||||
<script type="module" src="{{ static('bundled/htmx-index.js') }}"></script>
|
||||
<script type="module" src="{{ static('bundled/country-flags-index.ts') }}"></script>
|
||||
<script type="module" src="{{ static('bundled/core/tooltips-index.ts') }}"></script>
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
<div id="quick-notifications"
|
||||
x-data='{
|
||||
messages: [
|
||||
x-data='{ messages: $notifications.getAll() }'
|
||||
x-init='$notifications.addMany([
|
||||
{%- for message in messages -%}
|
||||
{%- if not message.extra_tags -%}
|
||||
{ tag: {{ message.tags|string|tojson }}, text: {{ message|string|tojson }} },
|
||||
{%- endif -%}
|
||||
{%- endfor -%}
|
||||
]
|
||||
}'
|
||||
@quick-notification-add="(e) => messages.push(e?.detail)"
|
||||
@quick-notification-delete="messages = []">
|
||||
])'
|
||||
>
|
||||
<template x-for="(message, index) in messages">
|
||||
<div class="alert" :class="`alert-${message.tag}`" x-transition>
|
||||
<span class="alert-main" x-text="message.text"></span>
|
||||
|
||||
@@ -35,8 +35,8 @@ les fichiers sont à mettre dans un dossier `static/bundled` de l'application à
|
||||
Pour accéder au fichier, il faut utiliser `static` comme pour le reste mais en ajouter `bundled/` comme prefix.
|
||||
|
||||
```jinja
|
||||
{# Example pour ajouter sith/core/bundled/alpine-index.js #}
|
||||
<script type="module" src="{{ static('bundled/alpine-index.js') }}"></script>
|
||||
{# Example pour ajouter sith/core/bundled/alpine-index.ts #}
|
||||
<script type="module" src="{{ static('bundled/alpine-index.ts') }}"></script>
|
||||
<script type="module" src="{{ static('bundled/other-index.ts') }}"></script>
|
||||
```
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ class JsBundlerManifestEntry:
|
||||
# because that's what the user types when importing statics and that's what django gives us
|
||||
# This is really similar to what we are doing in the bundler, it uses a similar algorithm
|
||||
# Example:
|
||||
# core/static/bundled/alpine-index.js -> bundled/alpine-index.js
|
||||
# core/static/bundled/alpine-index.ts -> bundled/alpine-index.ts
|
||||
# core/static/bundled/components/include-index.ts -> core/static/bundled/components/include-index.ts
|
||||
def get_relative_src_name(name: str) -> str:
|
||||
original_path = Path(name)
|
||||
|
||||
Reference in New Issue
Block a user