-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d12137
commit 7d893a5
Showing
7 changed files
with
126 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import htmx from './htmx.js' | ||
import { Howl } from 'https://cdn.jsdelivr.net/npm/howler@2.2.4/+esm' | ||
|
||
function init(/** @type {HTMLElement} */ element) { | ||
const title = element.getAttribute('data-notification-title') | ||
if (!title) { | ||
return | ||
} | ||
|
||
const body = element.getAttribute('data-notification-body') ?? '' | ||
const icon = element.getAttribute('data-notification-icon') | ||
const notification = new Notification(title, { body, ...(icon ? { icon } : {}) }) | ||
|
||
let /** @type {Howl} */ howl | ||
const sound = element.getAttribute('data-notification-sound') | ||
if (sound) { | ||
let volume = 1.0 | ||
|
||
const v = element.getAttribute('data-notification-sound-volume') | ||
if (v) { | ||
volume = parseFloat(v) | ||
} | ||
|
||
howl = new Howl({ | ||
src: sound, | ||
autoplay: true, | ||
volume, | ||
}) | ||
} | ||
|
||
function afterSwap() { | ||
if (document.body.contains(element)) { | ||
return | ||
} | ||
|
||
notification.close() | ||
howl.stop() | ||
htmx.off('htmx:afterSwap', afterSwap) | ||
} | ||
|
||
htmx.on('htmx:afterSwap', afterSwap) | ||
} | ||
|
||
htmx.onLoad(element => { | ||
if (!(element instanceof HTMLElement)) return | ||
|
||
if (element.hasAttribute('data-notification-title')) { | ||
init(element) | ||
} | ||
|
||
element.querySelectorAll(`[data-notification-title]`).forEach(element => { | ||
if (element instanceof HTMLElement) { | ||
init(element) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters