Improve uploading/downloading files
This commit is contained in:
parent
5ca163d35a
commit
7005490bf4
18 changed files with 4596 additions and 134 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -155,5 +155,6 @@ cython_debug/
|
|||
# PyCharm
|
||||
.idea/
|
||||
|
||||
# Postgres data directory
|
||||
# FeedVault directories
|
||||
data/
|
||||
media/
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ import feedparser
|
|||
from django.utils import timezone
|
||||
from feedparser import FeedParserDict
|
||||
|
||||
from feedvault.models import Author, Domain, Entry, Feed, Generator, Publisher
|
||||
from feedvault.models import Author, Domain, Entry, Feed, FeedAddResult, Generator, Publisher
|
||||
|
||||
if TYPE_CHECKING:
|
||||
import datetime
|
||||
|
|
@ -171,59 +171,58 @@ def add_entry(feed: Feed, entry: FeedParserDict) -> Entry | None:
|
|||
dateparser.parse(date_string=str(pre_created_parsed)) if pre_created_parsed else None
|
||||
)
|
||||
|
||||
_entry = Entry(
|
||||
entry_id = entry.get("id", "")
|
||||
if not entry_id:
|
||||
logger.error("Entry ID not found: %s", entry)
|
||||
|
||||
added_entry, created = Entry.objects.update_or_create(
|
||||
feed=feed,
|
||||
author=entry.get("author", ""),
|
||||
author_detail=author,
|
||||
comments=entry.get("comments", ""),
|
||||
content=entry.get("content", {}),
|
||||
contributors=entry.get("contributors", {}),
|
||||
created=entry.get("created", ""),
|
||||
created_parsed=created_parsed,
|
||||
enclosures=entry.get("enclosures", []),
|
||||
expired=entry.get("expired", ""),
|
||||
expired_parsed=expired_parsed,
|
||||
_id=entry.get("id", ""),
|
||||
license=entry.get("license", ""),
|
||||
link=entry.get("link", ""),
|
||||
links=entry.get("links", []),
|
||||
published=entry.get("published", ""),
|
||||
published_parsed=published_parsed,
|
||||
publisher=entry.get("publisher", ""),
|
||||
publisher_detail=publisher,
|
||||
source=entry.get("source", {}),
|
||||
summary=entry.get("summary", ""),
|
||||
summary_detail=entry.get("summary_detail", {}),
|
||||
tags=entry.get("tags", []),
|
||||
title=entry.get("title", ""),
|
||||
title_detail=entry.get("title_detail", {}),
|
||||
updated=entry.get("updated", ""),
|
||||
updated_parsed=updated_parsed,
|
||||
entry_id=entry_id,
|
||||
defaults={
|
||||
"author": entry.get("author", ""),
|
||||
"author_detail": author,
|
||||
"comments": entry.get("comments", ""),
|
||||
"content": entry.get("content", {}),
|
||||
"contributors": entry.get("contributors", {}),
|
||||
"created": entry.get("created", ""),
|
||||
"created_parsed": created_parsed,
|
||||
"enclosures": entry.get("enclosures", []),
|
||||
"expired": entry.get("expired", ""),
|
||||
"expired_parsed": expired_parsed,
|
||||
"license": entry.get("license", ""),
|
||||
"link": entry.get("link", ""),
|
||||
"links": entry.get("links", []),
|
||||
"published": entry.get("published", ""),
|
||||
"published_parsed": published_parsed,
|
||||
"publisher": entry.get("publisher", ""),
|
||||
"publisher_detail": publisher,
|
||||
"source": entry.get("source", {}),
|
||||
"summary": entry.get("summary", ""),
|
||||
"summary_detail": entry.get("summary_detail", {}),
|
||||
"tags": entry.get("tags", []),
|
||||
"title": entry.get("title", ""),
|
||||
"title_detail": entry.get("title_detail", {}),
|
||||
"updated": entry.get("updated", ""),
|
||||
"updated_parsed": updated_parsed,
|
||||
},
|
||||
)
|
||||
if created:
|
||||
logger.info("Created entry: %s", added_entry)
|
||||
return added_entry
|
||||
|
||||
# Save the entry.
|
||||
_entry.save()
|
||||
|
||||
logger.info("Created entry: %s", _entry)
|
||||
|
||||
return _entry
|
||||
logger.info("Updated entry: %s", added_entry)
|
||||
return added_entry
|
||||
|
||||
|
||||
def add_feed(url: str | None, user: AbstractBaseUser | AnonymousUser) -> Feed | None: # noqa: PLR0914
|
||||
"""Add a feed to the database.
|
||||
def add_domain_to_db(url: str | None) -> Domain | None:
|
||||
"""Add a domain to the database.
|
||||
|
||||
Args:
|
||||
url: The URL of the feed.
|
||||
user: The user adding the feed.
|
||||
url: The URL of the domain.
|
||||
|
||||
Returns:
|
||||
The feed that was added.
|
||||
The domain that was added.
|
||||
"""
|
||||
# Parse the feed.
|
||||
parsed_feed: dict | None = parse_feed(url=url)
|
||||
if not parsed_feed:
|
||||
return None
|
||||
|
||||
domain_url: None | str = get_domain(url=url)
|
||||
if not domain_url:
|
||||
return None
|
||||
|
|
@ -235,6 +234,28 @@ def add_feed(url: str | None, user: AbstractBaseUser | AnonymousUser) -> Feed |
|
|||
logger.info("Created domain: %s", domain.url)
|
||||
domain.save()
|
||||
|
||||
return domain
|
||||
|
||||
|
||||
def populate_feed(url: str | None, user: AbstractBaseUser | AnonymousUser) -> Feed | None:
|
||||
"""Populate the feed with entries.
|
||||
|
||||
Args:
|
||||
url: The URL of the feed.
|
||||
user: The user adding the feed.
|
||||
|
||||
Returns:
|
||||
The feed that was added.
|
||||
"""
|
||||
domain: Domain | None = add_domain_to_db(url=url)
|
||||
if not domain:
|
||||
return None
|
||||
|
||||
# Parse the feed.
|
||||
parsed_feed: dict | None = parse_feed(url=url)
|
||||
if not parsed_feed:
|
||||
return None
|
||||
|
||||
author: Author = get_author(parsed_feed=parsed_feed)
|
||||
generator: Generator = def_generator(parsed_feed=parsed_feed)
|
||||
publisher: Publisher = get_publisher(parsed_feed=parsed_feed)
|
||||
|
|
@ -252,58 +273,78 @@ def add_feed(url: str | None, user: AbstractBaseUser | AnonymousUser) -> Feed |
|
|||
pre_modified: str = str(parsed_feed.get("modified", ""))
|
||||
modified: timezone.datetime | None = dateparser.parse(date_string=pre_modified) if pre_modified else None
|
||||
|
||||
# Create the feed
|
||||
feed = Feed(
|
||||
# Create or update the feed
|
||||
feed, created = Feed.objects.update_or_create(
|
||||
feed_url=url,
|
||||
user=user,
|
||||
domain=domain,
|
||||
last_checked=timezone.now(),
|
||||
bozo=parsed_feed.get("bozo", 0),
|
||||
bozo_exception=parsed_feed.get("bozo_exception", ""),
|
||||
encoding=parsed_feed.get("encoding", ""),
|
||||
etag=parsed_feed.get("etag", ""),
|
||||
headers=parsed_feed.get("headers", {}),
|
||||
href=parsed_feed.get("href", ""),
|
||||
modified=modified,
|
||||
namespaces=parsed_feed.get("namespaces", {}),
|
||||
status=parsed_feed.get("status", 0),
|
||||
version=parsed_feed.get("version", ""),
|
||||
author=parsed_feed.get("author", ""),
|
||||
author_detail=author,
|
||||
cloud=parsed_feed.get("cloud", {}),
|
||||
contributors=parsed_feed.get("contributors", {}),
|
||||
docs=parsed_feed.get("docs", ""),
|
||||
errorreportsto=parsed_feed.get("errorreportsto", ""),
|
||||
generator=parsed_feed.get("generator", ""),
|
||||
generator_detail=generator,
|
||||
icon=parsed_feed.get("icon", ""),
|
||||
_id=parsed_feed.get("id", ""),
|
||||
image=parsed_feed.get("image", {}),
|
||||
info=parsed_feed.get("info", ""),
|
||||
language=parsed_feed.get("language", ""),
|
||||
license=parsed_feed.get("license", ""),
|
||||
link=parsed_feed.get("link", ""),
|
||||
links=parsed_feed.get("links", []),
|
||||
logo=parsed_feed.get("logo", ""),
|
||||
published=parsed_feed.get("published", ""),
|
||||
published_parsed=published_parsed,
|
||||
publisher=parsed_feed.get("publisher", ""),
|
||||
publisher_detail=publisher,
|
||||
rights=parsed_feed.get("rights", ""),
|
||||
rights_detail=parsed_feed.get("rights_detail", {}),
|
||||
subtitle=parsed_feed.get("subtitle", ""),
|
||||
subtitle_detail=parsed_feed.get("subtitle_detail", {}),
|
||||
tags=parsed_feed.get("tags", []),
|
||||
textinput=parsed_feed.get("textinput", {}),
|
||||
title=parsed_feed.get("title", ""),
|
||||
title_detail=parsed_feed.get("title_detail", {}),
|
||||
ttl=parsed_feed.get("ttl", ""),
|
||||
updated=parsed_feed.get("updated", ""),
|
||||
updated_parsed=updated_parsed,
|
||||
defaults={
|
||||
"user": user,
|
||||
"last_checked": timezone.now(),
|
||||
"bozo": parsed_feed.get("bozo", 0),
|
||||
"bozo_exception": parsed_feed.get("bozo_exception", ""),
|
||||
"encoding": parsed_feed.get("encoding", ""),
|
||||
"etag": parsed_feed.get("etag", ""),
|
||||
"headers": parsed_feed.get("headers", {}),
|
||||
"href": parsed_feed.get("href", ""),
|
||||
"modified": modified,
|
||||
"namespaces": parsed_feed.get("namespaces", {}),
|
||||
"status": parsed_feed.get("status", 0),
|
||||
"version": parsed_feed.get("version", ""),
|
||||
"author": parsed_feed.get("author", ""),
|
||||
"author_detail": author,
|
||||
"cloud": parsed_feed.get("cloud", {}),
|
||||
"contributors": parsed_feed.get("contributors", {}),
|
||||
"docs": parsed_feed.get("docs", ""),
|
||||
"errorreportsto": parsed_feed.get("errorreportsto", ""),
|
||||
"generator": parsed_feed.get("generator", ""),
|
||||
"generator_detail": generator,
|
||||
"icon": parsed_feed.get("icon", ""),
|
||||
"feed_id": parsed_feed.get("id", ""),
|
||||
"image": parsed_feed.get("image", {}),
|
||||
"info": parsed_feed.get("info", ""),
|
||||
"language": parsed_feed.get("language", ""),
|
||||
"license": parsed_feed.get("license", ""),
|
||||
"link": parsed_feed.get("link", ""),
|
||||
"links": parsed_feed.get("links", []),
|
||||
"logo": parsed_feed.get("logo", ""),
|
||||
"published": parsed_feed.get("published", ""),
|
||||
"published_parsed": published_parsed,
|
||||
"publisher": parsed_feed.get("publisher", ""),
|
||||
"publisher_detail": publisher,
|
||||
"rights": parsed_feed.get("rights", ""),
|
||||
"rights_detail": parsed_feed.get("rights_detail", {}),
|
||||
"subtitle": parsed_feed.get("subtitle", ""),
|
||||
"subtitle_detail": parsed_feed.get("subtitle_detail", {}),
|
||||
"tags": parsed_feed.get("tags", []),
|
||||
"textinput": parsed_feed.get("textinput", {}),
|
||||
"title": parsed_feed.get("title", ""),
|
||||
"title_detail": parsed_feed.get("title_detail", {}),
|
||||
"ttl": parsed_feed.get("ttl", ""),
|
||||
"updated": parsed_feed.get("updated", ""),
|
||||
"updated_parsed": updated_parsed,
|
||||
},
|
||||
)
|
||||
|
||||
# Save the feed.
|
||||
feed.save()
|
||||
grab_entries(feed=feed)
|
||||
|
||||
if created:
|
||||
logger.info("Created feed: %s", feed)
|
||||
return feed
|
||||
|
||||
logger.info("Updated feed: %s", feed)
|
||||
return feed
|
||||
|
||||
|
||||
def grab_entries(feed: Feed) -> None:
|
||||
"""Grab the entries from a feed.
|
||||
|
||||
Args:
|
||||
feed: The feed to grab the entries from.
|
||||
"""
|
||||
# Parse the feed.
|
||||
parsed_feed: dict | None = parse_feed(url=feed.feed_url)
|
||||
if not parsed_feed:
|
||||
return
|
||||
|
||||
entries = parsed_feed.get("entries", [])
|
||||
for entry in entries:
|
||||
|
|
@ -311,5 +352,16 @@ def add_feed(url: str | None, user: AbstractBaseUser | AnonymousUser) -> Feed |
|
|||
if not added_entry:
|
||||
continue
|
||||
|
||||
logger.info("Created feed: %s", feed)
|
||||
return feed
|
||||
logger.info("Grabbed entries for feed: %s", feed)
|
||||
return
|
||||
|
||||
|
||||
def add_url(url: str, user: AbstractBaseUser | AnonymousUser) -> FeedAddResult:
|
||||
"""Add a feed to the database so we can grab entries from it later."""
|
||||
domain: Domain | None = add_domain_to_db(url=url)
|
||||
if not domain:
|
||||
return FeedAddResult(feed=None, created=False, error="Domain not found")
|
||||
|
||||
# Add the URL to the database.
|
||||
_feed, _created = Feed.objects.get_or_create(feed_url=url, user=user, domain=domain)
|
||||
return FeedAddResult(feed=_feed, created=_created, error=None)
|
||||
|
|
|
|||
18
feedvault/migrations/0004_alter_feed_bozo.py
Normal file
18
feedvault/migrations/0004_alter_feed_bozo.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 5.0.3 on 2024-03-17 02:49
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('feedvault', '0003_rename__id_entry_entry_id_rename__id_feed_feed_id'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='feed',
|
||||
name='bozo',
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
]
|
||||
32
feedvault/migrations/0005_useruploadedfile.py
Normal file
32
feedvault/migrations/0005_useruploadedfile.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
# Generated by Django 5.0.3 on 2024-03-17 03:19
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('feedvault', '0004_alter_feed_bozo'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='UserUploadedFile',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('file', models.FileField(upload_to='uploads/')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('modified_at', models.DateTimeField(auto_now=True)),
|
||||
('has_been_processed', models.BooleanField(default=False)),
|
||||
('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Uploaded file',
|
||||
'verbose_name_plural': 'Uploaded files',
|
||||
'ordering': ['-created_at'],
|
||||
},
|
||||
),
|
||||
]
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
# Generated by Django 5.0.3 on 2024-03-17 03:29
|
||||
|
||||
import feedvault.models
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('feedvault', '0005_useruploadedfile'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='useruploadedfile',
|
||||
name='original_filename',
|
||||
field=models.TextField(default='a', help_text='The original filename of the file.'),
|
||||
preserve_default=False,
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='useruploadedfile',
|
||||
name='file',
|
||||
field=models.FileField(upload_to=feedvault.models.get_upload_path),
|
||||
),
|
||||
]
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
# Generated by Django 5.0.3 on 2024-03-17 16:00
|
||||
|
||||
import django.db.models.deletion
|
||||
import feedvault.models
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('feedvault', '0006_useruploadedfile_original_filename_and_more'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='useruploadedfile',
|
||||
name='description',
|
||||
field=models.TextField(blank=True, help_text='Description added by user.'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='useruploadedfile',
|
||||
name='notes',
|
||||
field=models.TextField(blank=True, help_text='Notes from admin.'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='useruploadedfile',
|
||||
name='created_at',
|
||||
field=models.DateTimeField(auto_now_add=True, help_text='The time the file was uploaded.'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='useruploadedfile',
|
||||
name='file',
|
||||
field=models.FileField(help_text='The file that was uploaded.', upload_to=feedvault.models.get_upload_path),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='useruploadedfile',
|
||||
name='has_been_processed',
|
||||
field=models.BooleanField(default=False, help_text='Has the file content been added to the archive?'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='useruploadedfile',
|
||||
name='modified_at',
|
||||
field=models.DateTimeField(auto_now=True, help_text='The last time the file was modified.'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='useruploadedfile',
|
||||
name='user',
|
||||
field=models.ForeignKey(blank=True, help_text='The user that uploaded the file.', null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL),
|
||||
),
|
||||
]
|
||||
|
|
@ -2,6 +2,9 @@ from __future__ import annotations
|
|||
|
||||
import logging
|
||||
import typing
|
||||
import uuid
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Literal
|
||||
|
||||
from django.db import models
|
||||
|
|
@ -10,6 +13,15 @@ from django.db.models import JSONField
|
|||
logger: logging.Logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class FeedAddResult:
|
||||
"""The result of adding a feed to the database."""
|
||||
|
||||
feed: Feed | None
|
||||
created: bool
|
||||
error: str | None
|
||||
|
||||
|
||||
class Domain(models.Model):
|
||||
"""A domain that has one or more feeds."""
|
||||
|
||||
|
|
@ -142,7 +154,7 @@ class Feed(models.Model):
|
|||
active = models.BooleanField(default=True)
|
||||
|
||||
# General data
|
||||
bozo = models.BooleanField()
|
||||
bozo = models.BooleanField(default=False)
|
||||
bozo_exception = models.TextField(blank=True)
|
||||
encoding = models.TextField(blank=True)
|
||||
etag = models.TextField(blank=True)
|
||||
|
|
@ -282,3 +294,46 @@ class Entry(models.Model):
|
|||
def __str__(self) -> str:
|
||||
"""Return string representation of the entry."""
|
||||
return f"{self.feed} - {self.title}"
|
||||
|
||||
|
||||
def get_upload_path(instance: UserUploadedFile, filename: str) -> str:
|
||||
"""Don't save the file with the original filename."""
|
||||
ext: str = Path(filename).suffix
|
||||
filename = f"{uuid.uuid4()}{ext}" # For example: 51dc07a7-a299-473c-a737-1ef16bc71609.opml
|
||||
return f"uploads/{instance.user.id}/{filename}" # type: ignore # noqa: PGH003
|
||||
|
||||
|
||||
class UserUploadedFile(models.Model):
|
||||
"""A file uploaded to the server by a user."""
|
||||
|
||||
file = models.FileField(upload_to=get_upload_path, help_text="The file that was uploaded.")
|
||||
original_filename = models.TextField(help_text="The original filename of the file.")
|
||||
created_at = models.DateTimeField(auto_now_add=True, help_text="The time the file was uploaded.")
|
||||
modified_at = models.DateTimeField(auto_now=True, help_text="The last time the file was modified.")
|
||||
user = models.ForeignKey(
|
||||
"auth.User",
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
help_text="The user that uploaded the file.",
|
||||
)
|
||||
has_been_processed = models.BooleanField(default=False, help_text="Has the file content been added to the archive?")
|
||||
description = models.TextField(blank=True, help_text="Description added by user.")
|
||||
notes = models.TextField(blank=True, help_text="Notes from admin.")
|
||||
|
||||
class Meta:
|
||||
"""Meta information for the uploaded file model."""
|
||||
|
||||
ordering: typing.ClassVar[list[str]] = ["-created_at"]
|
||||
verbose_name: str = "Uploaded file"
|
||||
verbose_name_plural: str = "Uploaded files"
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.original_filename} - {self.created_at}"
|
||||
|
||||
def get_absolute_url(self) -> str:
|
||||
"""Return the absolute URL of the uploaded file.
|
||||
|
||||
Note that you will need to be logged in to access the file.
|
||||
"""
|
||||
return f"/download/{self.pk}"
|
||||
|
|
|
|||
|
|
@ -42,13 +42,14 @@ ROOT_URLCONF = "feedvault.urls"
|
|||
WSGI_APPLICATION = "feedvault.wsgi.application"
|
||||
NINJA_PAGINATION_PER_PAGE = 1000
|
||||
STATIC_URL = "static/"
|
||||
STATIC_ROOT = BASE_DIR / "static"
|
||||
STATIC_ROOT: Path = BASE_DIR / "static"
|
||||
STATIC_ROOT.mkdir(parents=True, exist_ok=True)
|
||||
MEDIA_URL = "media/"
|
||||
MEDIA_ROOT = BASE_DIR / "media"
|
||||
MEDIA_ROOT: Path = BASE_DIR / "media"
|
||||
MEDIA_ROOT.mkdir(parents=True, exist_ok=True)
|
||||
LOGIN_REDIRECT_URL = "/"
|
||||
LOGOUT_REDIRECT_URL = "/"
|
||||
|
||||
|
||||
# Is True when running tests, used for not spamming Discord when new users are created
|
||||
TESTING: bool = len(sys.argv) > 1 and sys.argv[1] == "test"
|
||||
|
||||
|
|
@ -56,6 +57,7 @@ INSTALLED_APPS: list[str] = [
|
|||
"feedvault.apps.FeedVaultConfig",
|
||||
"debug_toolbar",
|
||||
"django.contrib.auth",
|
||||
"whitenoise.runserver_nostatic",
|
||||
"django.contrib.staticfiles",
|
||||
"django.contrib.contenttypes",
|
||||
"django.contrib.sessions",
|
||||
|
|
@ -66,6 +68,7 @@ INSTALLED_APPS: list[str] = [
|
|||
MIDDLEWARE: list[str] = [
|
||||
"debug_toolbar.middleware.DebugToolbarMiddleware",
|
||||
"django.middleware.security.SecurityMiddleware",
|
||||
"whitenoise.middleware.WhiteNoiseMiddleware",
|
||||
"django.contrib.sessions.middleware.SessionMiddleware",
|
||||
"django.middleware.common.CommonMiddleware",
|
||||
"django.middleware.csrf.CsrfViewMiddleware",
|
||||
|
|
@ -169,3 +172,12 @@ LOGGING = {
|
|||
},
|
||||
},
|
||||
}
|
||||
|
||||
STORAGES: dict[str, dict[str, str]] = {
|
||||
"default": {
|
||||
"BACKEND": "django.core.files.storage.FileSystemStorage",
|
||||
},
|
||||
"staticfiles": {
|
||||
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,8 +24,11 @@ urlpatterns: list = [
|
|||
path("__debug__/", include("debug_toolbar.urls")),
|
||||
path(route="feed/<int:feed_id>/", view=views.FeedView.as_view(), name="feed"),
|
||||
path(route="feeds/", view=views.FeedsView.as_view(), name="feeds"),
|
||||
path(route="add", view=views.AddView.as_view(), name="add"),
|
||||
path(route="upload", view=views.UploadView.as_view(), name="upload"),
|
||||
path(route="add/", view=views.AddView.as_view(), name="add"),
|
||||
path(route="upload/", view=views.UploadView.as_view(), name="upload"),
|
||||
path(route="download/", view=views.DownloadView.as_view(), name="download"),
|
||||
path(route="delete_upload/", view=views.DeleteUploadView.as_view(), name="delete_upload"),
|
||||
path(route="edit_description/", view=views.EditDescriptionView.as_view(), name="edit_description"),
|
||||
path(route="robots.txt", view=cache_page(timeout=60 * 60 * 365)(views.RobotsView.as_view()), name="robots"),
|
||||
path(
|
||||
"sitemap.xml",
|
||||
|
|
|
|||
|
|
@ -1,14 +1,20 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from mimetypes import guess_type
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth import login
|
||||
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.contrib.auth.views import LoginView, LogoutView, PasswordChangeView
|
||||
from django.contrib.messages.views import SuccessMessageMixin
|
||||
from django.core.exceptions import SuspiciousOperation
|
||||
from django.db.models.manager import BaseManager
|
||||
from django.http import HttpRequest, HttpResponse
|
||||
from django.http import FileResponse, Http404, HttpRequest, HttpResponse
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.template import loader
|
||||
from django.urls import reverse_lazy
|
||||
|
|
@ -16,14 +22,16 @@ from django.views import View
|
|||
from django.views.generic.edit import CreateView
|
||||
from django.views.generic.list import ListView
|
||||
|
||||
from feedvault.add_feeds import add_feed
|
||||
from feedvault.models import Domain, Entry, Feed
|
||||
from feedvault.add_feeds import add_url
|
||||
from feedvault.models import Domain, Entry, Feed, FeedAddResult, UserUploadedFile
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.files.uploadedfile import UploadedFile
|
||||
from django.db.models.manager import BaseManager
|
||||
|
||||
logger: logging.Logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class IndexView(View):
|
||||
"""Index path."""
|
||||
|
|
@ -86,7 +94,7 @@ class FeedsView(ListView):
|
|||
return context
|
||||
|
||||
|
||||
class AddView(View):
|
||||
class AddView(LoginRequiredMixin, View):
|
||||
"""Add a feed."""
|
||||
|
||||
def get(self, request: HttpRequest) -> HttpResponse:
|
||||
|
|
@ -114,22 +122,22 @@ class AddView(View):
|
|||
|
||||
# Split the urls by newline.
|
||||
for url in urls.split("\n"):
|
||||
feed: None | Feed = add_feed(url, request.user)
|
||||
if not feed:
|
||||
messages.error(request, f"{url} - Failed to add")
|
||||
feed_result: FeedAddResult = add_url(url, request.user)
|
||||
feed: Feed | None = feed_result.feed
|
||||
if not feed_result or not feed:
|
||||
messages.error(request, f"{url} - Failed to add, {feed_result.error}")
|
||||
continue
|
||||
# Check if bozo is true.
|
||||
if feed.bozo:
|
||||
messages.warning(request, f"{feed.feed_url} - Bozo: {feed.bozo_exception}")
|
||||
|
||||
messages.success(request, f"{feed.feed_url} added")
|
||||
if feed_result.created:
|
||||
messages.success(request, f"{feed.feed_url} added to queue")
|
||||
else:
|
||||
messages.warning(request, f"{feed.feed_url} already exists")
|
||||
|
||||
# Render the index page.
|
||||
template = loader.get_template(template_name="index.html")
|
||||
return HttpResponse(content=template.render(context={}, request=request))
|
||||
|
||||
|
||||
class UploadView(View):
|
||||
class UploadView(LoginRequiredMixin, View):
|
||||
"""Upload a file."""
|
||||
|
||||
def get(self, request: HttpRequest) -> HttpResponse:
|
||||
|
|
@ -155,23 +163,140 @@ class UploadView(View):
|
|||
if not file:
|
||||
return HttpResponse(content="No file", status=400)
|
||||
|
||||
# Split the urls by newline.
|
||||
for url in file.read().decode("utf-8").split("\n"):
|
||||
feed: None | Feed = add_feed(url, request.user)
|
||||
if not feed:
|
||||
messages.error(request, f"{url} - Failed to add")
|
||||
continue
|
||||
# Check if bozo is true.
|
||||
if feed.bozo:
|
||||
messages.warning(request, f"{feed.feed_url} - Bozo: {feed.bozo_exception}")
|
||||
|
||||
messages.success(request, f"{feed.feed_url} added")
|
||||
# Save file to media folder
|
||||
UserUploadedFile.objects.create(user=request.user, file=file, original_filename=file.name)
|
||||
|
||||
# Render the index page.
|
||||
template = loader.get_template(template_name="index.html")
|
||||
messages.success(request, f"{file.name} uploaded")
|
||||
messages.info(
|
||||
request,
|
||||
"You can find your uploads on your profile page. Files will be parsed and added to the archive when possible. Thanks.", # noqa: E501
|
||||
)
|
||||
return HttpResponse(content=template.render(context={}, request=request))
|
||||
|
||||
|
||||
class DeleteUploadView(LoginRequiredMixin, View):
|
||||
"""Delete an uploaded file."""
|
||||
|
||||
def post(self, request: HttpRequest) -> HttpResponse:
|
||||
"""Delete an uploaded file."""
|
||||
file_id: str | None = request.POST.get("file_id", None)
|
||||
if not file_id:
|
||||
return HttpResponse("No file_id provided", status=400)
|
||||
|
||||
user_file: UserUploadedFile | None = UserUploadedFile.objects.filter(user=request.user, id=file_id).first()
|
||||
if not user_file:
|
||||
msg = "File not found"
|
||||
raise Http404(msg)
|
||||
|
||||
user_upload_dir: Path = Path(settings.MEDIA_ROOT) / "uploads" / f"{request.user.id}" # type: ignore # noqa: PGH003
|
||||
file_path: Path = user_upload_dir / Path(user_file.file.name).name
|
||||
logger.debug("file_path: %s", file_path)
|
||||
|
||||
if not file_path.exists() or not file_path.is_file():
|
||||
logger.error("User '%s' attempted to delete a file that does not exist: %s", request.user, file_path)
|
||||
msg = "File not found"
|
||||
raise Http404(msg)
|
||||
|
||||
if user_upload_dir not in file_path.parents:
|
||||
logger.error(
|
||||
"User '%s' attempted to delete a file that is not in their upload directory: %s",
|
||||
request.user,
|
||||
file_path,
|
||||
)
|
||||
msg = "Attempted unauthorized file access"
|
||||
raise SuspiciousOperation(msg)
|
||||
|
||||
user_file.delete()
|
||||
|
||||
# Go back to the profile page
|
||||
messages.success(request, f"{file_path.name} deleted")
|
||||
return HttpResponse(status=204)
|
||||
|
||||
|
||||
class EditDescriptionView(LoginRequiredMixin, View):
|
||||
"""Edit the description of an uploaded file."""
|
||||
|
||||
def post(self, request: HttpRequest) -> HttpResponse:
|
||||
"""Edit the description of an uploaded file."""
|
||||
new_description: str | None = request.POST.get("description", None)
|
||||
file_id: str | None = request.POST.get("file_id", None)
|
||||
if not new_description:
|
||||
return HttpResponse("No description provided", status=400)
|
||||
if not file_id:
|
||||
return HttpResponse("No file_id provided", status=400)
|
||||
|
||||
user_file: UserUploadedFile | None = UserUploadedFile.objects.filter(user=request.user, id=file_id).first()
|
||||
if not user_file:
|
||||
msg = "File not found"
|
||||
raise Http404(msg)
|
||||
|
||||
user_upload_dir: Path = Path(settings.MEDIA_ROOT) / "uploads" / f"{request.user.id}" # type: ignore # noqa: PGH003
|
||||
file_path: Path = user_upload_dir / Path(user_file.file.name).name
|
||||
logger.debug("file_path: %s", file_path)
|
||||
|
||||
if not file_path.exists() or not file_path.is_file():
|
||||
logger.error("User '%s' attempted to delete a file that does not exist: %s", request.user, file_path)
|
||||
msg = "File not found"
|
||||
raise Http404(msg)
|
||||
|
||||
if user_upload_dir not in file_path.parents:
|
||||
logger.error(
|
||||
"User '%s' attempted to delete a file that is not in their upload directory: %s",
|
||||
request.user,
|
||||
file_path,
|
||||
)
|
||||
msg = "Attempted unauthorized file access"
|
||||
raise SuspiciousOperation(msg)
|
||||
|
||||
old_description: str = user_file.description
|
||||
user_file.description = new_description
|
||||
user_file.save()
|
||||
|
||||
logger.info(
|
||||
"User '%s' updated the description of file '%s' from '%s' to '%s'",
|
||||
request.user,
|
||||
file_path,
|
||||
old_description,
|
||||
new_description,
|
||||
)
|
||||
return HttpResponse(content=new_description, status=200)
|
||||
|
||||
|
||||
class DownloadView(LoginRequiredMixin, View):
|
||||
"""Download a file."""
|
||||
|
||||
def get(self, request: HttpRequest) -> HttpResponse | FileResponse:
|
||||
"""/download/?file_id=1."""
|
||||
file_id: str | None = request.GET.get("file_id", None)
|
||||
|
||||
if not file_id:
|
||||
return HttpResponse("No file_id provided", status=400)
|
||||
|
||||
user_file: UserUploadedFile | None = UserUploadedFile.objects.filter(user=request.user, id=file_id).first()
|
||||
if not user_file:
|
||||
msg = "File not found"
|
||||
raise Http404(msg)
|
||||
|
||||
user_upload_dir: Path = Path(settings.MEDIA_ROOT) / "uploads" / f"{request.user.id}" # type: ignore # noqa: PGH003
|
||||
file_path: Path = user_upload_dir / Path(user_file.file.name).name
|
||||
|
||||
if not file_path.exists() or not file_path.is_file():
|
||||
msg = "File not found"
|
||||
raise Http404(msg)
|
||||
|
||||
if user_upload_dir not in file_path.parents:
|
||||
msg = "Attempted unauthorized file access"
|
||||
raise SuspiciousOperation(msg)
|
||||
|
||||
content_type, _ = guess_type(file_path)
|
||||
response = FileResponse(file_path.open("rb"), content_type=content_type or "application/octet-stream")
|
||||
response["Content-Disposition"] = f'attachment; filename="{user_file.original_filename or file_path.name}"'
|
||||
|
||||
return response
|
||||
|
||||
|
||||
class CustomLoginView(LoginView):
|
||||
"""Custom login view."""
|
||||
|
||||
|
|
@ -228,7 +353,7 @@ class CustomPasswordChangeView(SuccessMessageMixin, PasswordChangeView):
|
|||
return context
|
||||
|
||||
|
||||
class ProfileView(View):
|
||||
class ProfileView(LoginRequiredMixin, View):
|
||||
"""Profile page."""
|
||||
|
||||
def get(self, request: HttpRequest) -> HttpResponse:
|
||||
|
|
@ -236,6 +361,9 @@ class ProfileView(View):
|
|||
template = loader.get_template(template_name="accounts/profile.html")
|
||||
|
||||
user_feeds: BaseManager[Feed] = Feed.objects.filter(user=request.user).order_by("-created_at")[:100]
|
||||
user_uploads: BaseManager[UserUploadedFile] = UserUploadedFile.objects.filter(user=request.user).order_by(
|
||||
"-created_at",
|
||||
)[:100]
|
||||
|
||||
context: dict[str, str | Any] = {
|
||||
"description": f"Profile page for {request.user.get_username()}",
|
||||
|
|
@ -244,6 +372,7 @@ class ProfileView(View):
|
|||
"canonical": "https://feedvault.se/accounts/profile/",
|
||||
"title": f"{request.user.get_username()}",
|
||||
"user_feeds": user_feeds,
|
||||
"user_uploads": user_uploads,
|
||||
}
|
||||
return HttpResponse(content=template.render(context=context, request=request))
|
||||
|
||||
|
|
|
|||
111
poetry.lock
generated
111
poetry.lock
generated
|
|
@ -82,6 +82,98 @@ files = [
|
|||
[package.extras]
|
||||
tests = ["mypy (>=0.800)", "pytest", "pytest-asyncio"]
|
||||
|
||||
[[package]]
|
||||
name = "brotli"
|
||||
version = "1.1.0"
|
||||
description = "Python bindings for the Brotli compression library"
|
||||
optional = false
|
||||
python-versions = "*"
|
||||
files = [
|
||||
{file = "Brotli-1.1.0-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:e1140c64812cb9b06c922e77f1c26a75ec5e3f0fb2bf92cc8c58720dec276752"},
|
||||
{file = "Brotli-1.1.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:c8fd5270e906eef71d4a8d19b7c6a43760c6abcfcc10c9101d14eb2357418de9"},
|
||||
{file = "Brotli-1.1.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:1ae56aca0402a0f9a3431cddda62ad71666ca9d4dc3a10a142b9dce2e3c0cda3"},
|
||||
{file = "Brotli-1.1.0-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:43ce1b9935bfa1ede40028054d7f48b5469cd02733a365eec8a329ffd342915d"},
|
||||
{file = "Brotli-1.1.0-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_12_i686.manylinux2010_i686.whl", hash = "sha256:7c4855522edb2e6ae7fdb58e07c3ba9111e7621a8956f481c68d5d979c93032e"},
|
||||
{file = "Brotli-1.1.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:38025d9f30cf4634f8309c6874ef871b841eb3c347e90b0851f63d1ded5212da"},
|
||||
{file = "Brotli-1.1.0-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:e6a904cb26bfefc2f0a6f240bdf5233be78cd2488900a2f846f3c3ac8489ab80"},
|
||||
{file = "Brotli-1.1.0-cp310-cp310-musllinux_1_1_i686.whl", hash = "sha256:a37b8f0391212d29b3a91a799c8e4a2855e0576911cdfb2515487e30e322253d"},
|
||||
{file = "Brotli-1.1.0-cp310-cp310-musllinux_1_1_ppc64le.whl", hash = "sha256:e84799f09591700a4154154cab9787452925578841a94321d5ee8fb9a9a328f0"},
|
||||
{file = "Brotli-1.1.0-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:f66b5337fa213f1da0d9000bc8dc0cb5b896b726eefd9c6046f699b169c41b9e"},
|
||||
{file = "Brotli-1.1.0-cp310-cp310-win32.whl", hash = "sha256:be36e3d172dc816333f33520154d708a2657ea63762ec16b62ece02ab5e4daf2"},
|
||||
{file = "Brotli-1.1.0-cp310-cp310-win_amd64.whl", hash = "sha256:0c6244521dda65ea562d5a69b9a26120769b7a9fb3db2fe9545935ed6735b128"},
|
||||
{file = "Brotli-1.1.0-cp311-cp311-macosx_10_9_universal2.whl", hash = "sha256:a3daabb76a78f829cafc365531c972016e4aa8d5b4bf60660ad8ecee19df7ccc"},
|
||||
{file = "Brotli-1.1.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:c8146669223164fc87a7e3de9f81e9423c67a79d6b3447994dfb9c95da16e2d6"},
|
||||
{file = "Brotli-1.1.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:30924eb4c57903d5a7526b08ef4a584acc22ab1ffa085faceb521521d2de32dd"},
|
||||
{file = "Brotli-1.1.0-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:ceb64bbc6eac5a140ca649003756940f8d6a7c444a68af170b3187623b43bebf"},
|
||||
{file = "Brotli-1.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:a469274ad18dc0e4d316eefa616d1d0c2ff9da369af19fa6f3daa4f09671fd61"},
|
||||
{file = "Brotli-1.1.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:524f35912131cc2cabb00edfd8d573b07f2d9f21fa824bd3fb19725a9cf06327"},
|
||||
{file = "Brotli-1.1.0-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:5b3cc074004d968722f51e550b41a27be656ec48f8afaeeb45ebf65b561481dd"},
|
||||
{file = "Brotli-1.1.0-cp311-cp311-musllinux_1_1_i686.whl", hash = "sha256:19c116e796420b0cee3da1ccec3b764ed2952ccfcc298b55a10e5610ad7885f9"},
|
||||
{file = "Brotli-1.1.0-cp311-cp311-musllinux_1_1_ppc64le.whl", hash = "sha256:510b5b1bfbe20e1a7b3baf5fed9e9451873559a976c1a78eebaa3b86c57b4265"},
|
||||
{file = "Brotli-1.1.0-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:a1fd8a29719ccce974d523580987b7f8229aeace506952fa9ce1d53a033873c8"},
|
||||
{file = "Brotli-1.1.0-cp311-cp311-win32.whl", hash = "sha256:39da8adedf6942d76dc3e46653e52df937a3c4d6d18fdc94a7c29d263b1f5b50"},
|
||||
{file = "Brotli-1.1.0-cp311-cp311-win_amd64.whl", hash = "sha256:aac0411d20e345dc0920bdec5548e438e999ff68d77564d5e9463a7ca9d3e7b1"},
|
||||
{file = "Brotli-1.1.0-cp312-cp312-macosx_10_9_universal2.whl", hash = "sha256:316cc9b17edf613ac76b1f1f305d2a748f1b976b033b049a6ecdfd5612c70409"},
|
||||
{file = "Brotli-1.1.0-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:caf9ee9a5775f3111642d33b86237b05808dafcd6268faa492250e9b78046eb2"},
|
||||
{file = "Brotli-1.1.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:70051525001750221daa10907c77830bc889cb6d865cc0b813d9db7fefc21451"},
|
||||
{file = "Brotli-1.1.0-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:7f4bf76817c14aa98cc6697ac02f3972cb8c3da93e9ef16b9c66573a68014f91"},
|
||||
{file = "Brotli-1.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d0c5516f0aed654134a2fc936325cc2e642f8a0e096d075209672eb321cff408"},
|
||||
{file = "Brotli-1.1.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:6c3020404e0b5eefd7c9485ccf8393cfb75ec38ce75586e046573c9dc29967a0"},
|
||||
{file = "Brotli-1.1.0-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:4ed11165dd45ce798d99a136808a794a748d5dc38511303239d4e2363c0695dc"},
|
||||
{file = "Brotli-1.1.0-cp312-cp312-musllinux_1_1_i686.whl", hash = "sha256:4093c631e96fdd49e0377a9c167bfd75b6d0bad2ace734c6eb20b348bc3ea180"},
|
||||
{file = "Brotli-1.1.0-cp312-cp312-musllinux_1_1_ppc64le.whl", hash = "sha256:7e4c4629ddad63006efa0ef968c8e4751c5868ff0b1c5c40f76524e894c50248"},
|
||||
{file = "Brotli-1.1.0-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:861bf317735688269936f755fa136a99d1ed526883859f86e41a5d43c61d8966"},
|
||||
{file = "Brotli-1.1.0-cp312-cp312-win32.whl", hash = "sha256:5f4d5ea15c9382135076d2fb28dde923352fe02951e66935a9efaac8f10e81b0"},
|
||||
{file = "Brotli-1.1.0-cp312-cp312-win_amd64.whl", hash = "sha256:906bc3a79de8c4ae5b86d3d75a8b77e44404b0f4261714306e3ad248d8ab0951"},
|
||||
{file = "Brotli-1.1.0-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:a090ca607cbb6a34b0391776f0cb48062081f5f60ddcce5d11838e67a01928d1"},
|
||||
{file = "Brotli-1.1.0-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:2de9d02f5bda03d27ede52e8cfe7b865b066fa49258cbab568720aa5be80a47d"},
|
||||
{file = "Brotli-1.1.0-cp36-cp36m-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:2333e30a5e00fe0fe55903c8832e08ee9c3b1382aacf4db26664a16528d51b4b"},
|
||||
{file = "Brotli-1.1.0-cp36-cp36m-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:4d4a848d1837973bf0f4b5e54e3bec977d99be36a7895c61abb659301b02c112"},
|
||||
{file = "Brotli-1.1.0-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl", hash = "sha256:fdc3ff3bfccdc6b9cc7c342c03aa2400683f0cb891d46e94b64a197910dc4064"},
|
||||
{file = "Brotli-1.1.0-cp36-cp36m-musllinux_1_1_aarch64.whl", hash = "sha256:5eeb539606f18a0b232d4ba45adccde4125592f3f636a6182b4a8a436548b914"},
|
||||
{file = "Brotli-1.1.0-cp36-cp36m-musllinux_1_1_i686.whl", hash = "sha256:fd5f17ff8f14003595ab414e45fce13d073e0762394f957182e69035c9f3d7c2"},
|
||||
{file = "Brotli-1.1.0-cp36-cp36m-musllinux_1_1_ppc64le.whl", hash = "sha256:069a121ac97412d1fe506da790b3e69f52254b9df4eb665cd42460c837193354"},
|
||||
{file = "Brotli-1.1.0-cp36-cp36m-musllinux_1_1_x86_64.whl", hash = "sha256:e93dfc1a1165e385cc8239fab7c036fb2cd8093728cbd85097b284d7b99249a2"},
|
||||
{file = "Brotli-1.1.0-cp36-cp36m-win32.whl", hash = "sha256:a599669fd7c47233438a56936988a2478685e74854088ef5293802123b5b2460"},
|
||||
{file = "Brotli-1.1.0-cp36-cp36m-win_amd64.whl", hash = "sha256:d143fd47fad1db3d7c27a1b1d66162e855b5d50a89666af46e1679c496e8e579"},
|
||||
{file = "Brotli-1.1.0-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:11d00ed0a83fa22d29bc6b64ef636c4552ebafcef57154b4ddd132f5638fbd1c"},
|
||||
{file = "Brotli-1.1.0-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:f733d788519c7e3e71f0855c96618720f5d3d60c3cb829d8bbb722dddce37985"},
|
||||
{file = "Brotli-1.1.0-cp37-cp37m-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:929811df5462e182b13920da56c6e0284af407d1de637d8e536c5cd00a7daf60"},
|
||||
{file = "Brotli-1.1.0-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:0b63b949ff929fbc2d6d3ce0e924c9b93c9785d877a21a1b678877ffbbc4423a"},
|
||||
{file = "Brotli-1.1.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl", hash = "sha256:d192f0f30804e55db0d0e0a35d83a9fead0e9a359a9ed0285dbacea60cc10a84"},
|
||||
{file = "Brotli-1.1.0-cp37-cp37m-musllinux_1_1_aarch64.whl", hash = "sha256:f296c40e23065d0d6650c4aefe7470d2a25fffda489bcc3eb66083f3ac9f6643"},
|
||||
{file = "Brotli-1.1.0-cp37-cp37m-musllinux_1_1_i686.whl", hash = "sha256:919e32f147ae93a09fe064d77d5ebf4e35502a8df75c29fb05788528e330fe74"},
|
||||
{file = "Brotli-1.1.0-cp37-cp37m-musllinux_1_1_ppc64le.whl", hash = "sha256:23032ae55523cc7bccb4f6a0bf368cd25ad9bcdcc1990b64a647e7bbcce9cb5b"},
|
||||
{file = "Brotli-1.1.0-cp37-cp37m-musllinux_1_1_x86_64.whl", hash = "sha256:224e57f6eac61cc449f498cc5f0e1725ba2071a3d4f48d5d9dffba42db196438"},
|
||||
{file = "Brotli-1.1.0-cp37-cp37m-win32.whl", hash = "sha256:587ca6d3cef6e4e868102672d3bd9dc9698c309ba56d41c2b9c85bbb903cdb95"},
|
||||
{file = "Brotli-1.1.0-cp37-cp37m-win_amd64.whl", hash = "sha256:2954c1c23f81c2eaf0b0717d9380bd348578a94161a65b3a2afc62c86467dd68"},
|
||||
{file = "Brotli-1.1.0-cp38-cp38-macosx_10_9_universal2.whl", hash = "sha256:efa8b278894b14d6da122a72fefcebc28445f2d3f880ac59d46c90f4c13be9a3"},
|
||||
{file = "Brotli-1.1.0-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:03d20af184290887bdea3f0f78c4f737d126c74dc2f3ccadf07e54ceca3bf208"},
|
||||
{file = "Brotli-1.1.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:6172447e1b368dcbc458925e5ddaf9113477b0ed542df258d84fa28fc45ceea7"},
|
||||
{file = "Brotli-1.1.0-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:a743e5a28af5f70f9c080380a5f908d4d21d40e8f0e0c8901604d15cfa9ba751"},
|
||||
{file = "Brotli-1.1.0-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:0541e747cce78e24ea12d69176f6a7ddb690e62c425e01d31cc065e69ce55b48"},
|
||||
{file = "Brotli-1.1.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl", hash = "sha256:cdbc1fc1bc0bff1cef838eafe581b55bfbffaed4ed0318b724d0b71d4d377619"},
|
||||
{file = "Brotli-1.1.0-cp38-cp38-musllinux_1_1_aarch64.whl", hash = "sha256:890b5a14ce214389b2cc36ce82f3093f96f4cc730c1cffdbefff77a7c71f2a97"},
|
||||
{file = "Brotli-1.1.0-cp38-cp38-musllinux_1_1_i686.whl", hash = "sha256:1ab4fbee0b2d9098c74f3057b2bc055a8bd92ccf02f65944a241b4349229185a"},
|
||||
{file = "Brotli-1.1.0-cp38-cp38-musllinux_1_1_ppc64le.whl", hash = "sha256:141bd4d93984070e097521ed07e2575b46f817d08f9fa42b16b9b5f27b5ac088"},
|
||||
{file = "Brotli-1.1.0-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:fce1473f3ccc4187f75b4690cfc922628aed4d3dd013d047f95a9b3919a86596"},
|
||||
{file = "Brotli-1.1.0-cp38-cp38-win32.whl", hash = "sha256:db85ecf4e609a48f4b29055f1e144231b90edc90af7481aa731ba2d059226b1b"},
|
||||
{file = "Brotli-1.1.0-cp38-cp38-win_amd64.whl", hash = "sha256:3d7954194c36e304e1523f55d7042c59dc53ec20dd4e9ea9d151f1b62b4415c0"},
|
||||
{file = "Brotli-1.1.0-cp39-cp39-macosx_10_9_universal2.whl", hash = "sha256:5fb2ce4b8045c78ebbc7b8f3c15062e435d47e7393cc57c25115cfd49883747a"},
|
||||
{file = "Brotli-1.1.0-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:7905193081db9bfa73b1219140b3d315831cbff0d8941f22da695832f0dd188f"},
|
||||
{file = "Brotli-1.1.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a77def80806c421b4b0af06f45d65a136e7ac0bdca3c09d9e2ea4e515367c7e9"},
|
||||
{file = "Brotli-1.1.0-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:8dadd1314583ec0bf2d1379f7008ad627cd6336625d6679cf2f8e67081b83acf"},
|
||||
{file = "Brotli-1.1.0-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:901032ff242d479a0efa956d853d16875d42157f98951c0230f69e69f9c09bac"},
|
||||
{file = "Brotli-1.1.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl", hash = "sha256:22fc2a8549ffe699bfba2256ab2ed0421a7b8fadff114a3d201794e45a9ff578"},
|
||||
{file = "Brotli-1.1.0-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:ae15b066e5ad21366600ebec29a7ccbc86812ed267e4b28e860b8ca16a2bc474"},
|
||||
{file = "Brotli-1.1.0-cp39-cp39-musllinux_1_1_i686.whl", hash = "sha256:949f3b7c29912693cee0afcf09acd6ebc04c57af949d9bf77d6101ebb61e388c"},
|
||||
{file = "Brotli-1.1.0-cp39-cp39-musllinux_1_1_ppc64le.whl", hash = "sha256:89f4988c7203739d48c6f806f1e87a1d96e0806d44f0fba61dba81392c9e474d"},
|
||||
{file = "Brotli-1.1.0-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:de6551e370ef19f8de1807d0a9aa2cdfdce2e85ce88b122fe9f6b2b076837e59"},
|
||||
{file = "Brotli-1.1.0-cp39-cp39-win32.whl", hash = "sha256:f0d8a7a6b5983c2496e364b969f0e526647a06b075d034f3297dc66f3b360c64"},
|
||||
{file = "Brotli-1.1.0-cp39-cp39-win_amd64.whl", hash = "sha256:cdad5b9014d83ca68c25d2e9444e28e967ef16e80f6b436918c700c117a85467"},
|
||||
{file = "Brotli-1.1.0.tar.gz", hash = "sha256:81de08ac11bcb85841e440c13611c00b67d3bf82698314928d0b676362546724"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "certifi"
|
||||
version = "2024.2.2"
|
||||
|
|
@ -1024,7 +1116,24 @@ h2 = ["h2 (>=4,<5)"]
|
|||
socks = ["pysocks (>=1.5.6,!=1.5.7,<2.0)"]
|
||||
zstd = ["zstandard (>=0.18.0)"]
|
||||
|
||||
[[package]]
|
||||
name = "whitenoise"
|
||||
version = "6.6.0"
|
||||
description = "Radically simplified static file serving for WSGI applications"
|
||||
optional = false
|
||||
python-versions = ">=3.8"
|
||||
files = [
|
||||
{file = "whitenoise-6.6.0-py3-none-any.whl", hash = "sha256:b1f9db9bf67dc183484d760b99f4080185633136a273a03f6436034a41064146"},
|
||||
{file = "whitenoise-6.6.0.tar.gz", hash = "sha256:8998f7370973447fac1e8ef6e8ded2c5209a7b1f67c1012866dbcd09681c3251"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
Brotli = {version = "*", optional = true, markers = "extra == \"brotli\""}
|
||||
|
||||
[package.extras]
|
||||
brotli = ["Brotli"]
|
||||
|
||||
[metadata]
|
||||
lock-version = "2.0"
|
||||
python-versions = "^3.12"
|
||||
content-hash = "acbd2c335db0529781e85823f05985272bce298add5c02bf2ddc990d478205db"
|
||||
content-hash = "60d19db756f55b38f910b2167075ae87cea90478904adc217b21d746bec6b728"
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ dateparser = "^1.2.0"
|
|||
discord-webhook = "^1.3.1"
|
||||
django-ninja = "^1.1.0"
|
||||
django-debug-toolbar = "^4.3.0"
|
||||
whitenoise = {extras = ["brotli"], version = "^6.6.0"}
|
||||
|
||||
[tool.poetry.group.dev.dependencies]
|
||||
ruff = "^0.3.0"
|
||||
|
|
|
|||
3922
static/htmx.js
Normal file
3922
static/htmx.js
Normal file
|
|
@ -0,0 +1,3922 @@
|
|||
// UMD insanity
|
||||
// This code sets up support for (in order) AMD, ES6 modules, and globals.
|
||||
(function (root, factory) {
|
||||
//@ts-ignore
|
||||
if (typeof define === 'function' && define.amd) {
|
||||
// AMD. Register as an anonymous module.
|
||||
//@ts-ignore
|
||||
define([], factory);
|
||||
} else if (typeof module === 'object' && module.exports) {
|
||||
// Node. Does not work with strict CommonJS, but
|
||||
// only CommonJS-like environments that support module.exports,
|
||||
// like Node.
|
||||
module.exports = factory();
|
||||
} else {
|
||||
// Browser globals
|
||||
root.htmx = root.htmx || factory();
|
||||
}
|
||||
}(typeof self !== 'undefined' ? self : this, function () {
|
||||
return (function () {
|
||||
'use strict';
|
||||
|
||||
// Public API
|
||||
//** @type {import("./htmx").HtmxApi} */
|
||||
// TODO: list all methods in public API
|
||||
var htmx = {
|
||||
onLoad: onLoadHelper,
|
||||
process: processNode,
|
||||
on: addEventListenerImpl,
|
||||
off: removeEventListenerImpl,
|
||||
trigger : triggerEvent,
|
||||
ajax : ajaxHelper,
|
||||
find : find,
|
||||
findAll : findAll,
|
||||
closest : closest,
|
||||
values : function(elt, type){
|
||||
var inputValues = getInputValues(elt, type || "post");
|
||||
return inputValues.values;
|
||||
},
|
||||
remove : removeElement,
|
||||
addClass : addClassToElement,
|
||||
removeClass : removeClassFromElement,
|
||||
toggleClass : toggleClassOnElement,
|
||||
takeClass : takeClassForElement,
|
||||
defineExtension : defineExtension,
|
||||
removeExtension : removeExtension,
|
||||
logAll : logAll,
|
||||
logNone : logNone,
|
||||
logger : null,
|
||||
config : {
|
||||
historyEnabled:true,
|
||||
historyCacheSize:10,
|
||||
refreshOnHistoryMiss:false,
|
||||
defaultSwapStyle:'innerHTML',
|
||||
defaultSwapDelay:0,
|
||||
defaultSettleDelay:20,
|
||||
includeIndicatorStyles:true,
|
||||
indicatorClass:'htmx-indicator',
|
||||
requestClass:'htmx-request',
|
||||
addedClass:'htmx-added',
|
||||
settlingClass:'htmx-settling',
|
||||
swappingClass:'htmx-swapping',
|
||||
allowEval:true,
|
||||
allowScriptTags:true,
|
||||
inlineScriptNonce:'',
|
||||
attributesToSettle:["class", "style", "width", "height"],
|
||||
withCredentials:false,
|
||||
timeout:0,
|
||||
wsReconnectDelay: 'full-jitter',
|
||||
wsBinaryType: 'blob',
|
||||
disableSelector: "[hx-disable], [data-hx-disable]",
|
||||
useTemplateFragments: false,
|
||||
scrollBehavior: 'smooth',
|
||||
defaultFocusScroll: false,
|
||||
getCacheBusterParam: false,
|
||||
globalViewTransitions: false,
|
||||
methodsThatUseUrlParams: ["get"],
|
||||
selfRequestsOnly: false,
|
||||
ignoreTitle: false,
|
||||
scrollIntoViewOnBoost: true,
|
||||
triggerSpecsCache: null,
|
||||
},
|
||||
parseInterval:parseInterval,
|
||||
_:internalEval,
|
||||
createEventSource: function(url){
|
||||
return new EventSource(url, {withCredentials:true})
|
||||
},
|
||||
createWebSocket: function(url){
|
||||
var sock = new WebSocket(url, []);
|
||||
sock.binaryType = htmx.config.wsBinaryType;
|
||||
return sock;
|
||||
},
|
||||
version: "1.9.11"
|
||||
};
|
||||
|
||||
/** @type {import("./htmx").HtmxInternalApi} */
|
||||
var internalAPI = {
|
||||
addTriggerHandler: addTriggerHandler,
|
||||
bodyContains: bodyContains,
|
||||
canAccessLocalStorage: canAccessLocalStorage,
|
||||
findThisElement: findThisElement,
|
||||
filterValues: filterValues,
|
||||
hasAttribute: hasAttribute,
|
||||
getAttributeValue: getAttributeValue,
|
||||
getClosestAttributeValue: getClosestAttributeValue,
|
||||
getClosestMatch: getClosestMatch,
|
||||
getExpressionVars: getExpressionVars,
|
||||
getHeaders: getHeaders,
|
||||
getInputValues: getInputValues,
|
||||
getInternalData: getInternalData,
|
||||
getSwapSpecification: getSwapSpecification,
|
||||
getTriggerSpecs: getTriggerSpecs,
|
||||
getTarget: getTarget,
|
||||
makeFragment: makeFragment,
|
||||
mergeObjects: mergeObjects,
|
||||
makeSettleInfo: makeSettleInfo,
|
||||
oobSwap: oobSwap,
|
||||
querySelectorExt: querySelectorExt,
|
||||
selectAndSwap: selectAndSwap,
|
||||
settleImmediately: settleImmediately,
|
||||
shouldCancel: shouldCancel,
|
||||
triggerEvent: triggerEvent,
|
||||
triggerErrorEvent: triggerErrorEvent,
|
||||
withExtensions: withExtensions,
|
||||
}
|
||||
|
||||
var VERBS = ['get', 'post', 'put', 'delete', 'patch'];
|
||||
var VERB_SELECTOR = VERBS.map(function(verb){
|
||||
return "[hx-" + verb + "], [data-hx-" + verb + "]"
|
||||
}).join(", ");
|
||||
|
||||
var HEAD_TAG_REGEX = makeTagRegEx('head'),
|
||||
TITLE_TAG_REGEX = makeTagRegEx('title'),
|
||||
SVG_TAGS_REGEX = makeTagRegEx('svg', true);
|
||||
|
||||
//====================================================================
|
||||
// Utilities
|
||||
//====================================================================
|
||||
|
||||
/**
|
||||
* @param {string} tag
|
||||
* @param {boolean} global
|
||||
* @returns {RegExp}
|
||||
*/
|
||||
function makeTagRegEx(tag, global = false) {
|
||||
return new RegExp(`<${tag}(\\s[^>]*>|>)([\\s\\S]*?)<\\/${tag}>`,
|
||||
global ? 'gim' : 'im');
|
||||
}
|
||||
|
||||
function parseInterval(str) {
|
||||
if (str == undefined) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let interval = NaN;
|
||||
if (str.slice(-2) == "ms") {
|
||||
interval = parseFloat(str.slice(0, -2));
|
||||
} else if (str.slice(-1) == "s") {
|
||||
interval = parseFloat(str.slice(0, -1)) * 1000;
|
||||
} else if (str.slice(-1) == "m") {
|
||||
interval = parseFloat(str.slice(0, -1)) * 1000 * 60;
|
||||
} else {
|
||||
interval = parseFloat(str);
|
||||
}
|
||||
return isNaN(interval) ? undefined : interval;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} elt
|
||||
* @param {string} name
|
||||
* @returns {(string | null)}
|
||||
*/
|
||||
function getRawAttribute(elt, name) {
|
||||
return elt.getAttribute && elt.getAttribute(name);
|
||||
}
|
||||
|
||||
// resolve with both hx and data-hx prefixes
|
||||
function hasAttribute(elt, qualifiedName) {
|
||||
return elt.hasAttribute && (elt.hasAttribute(qualifiedName) ||
|
||||
elt.hasAttribute("data-" + qualifiedName));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {HTMLElement} elt
|
||||
* @param {string} qualifiedName
|
||||
* @returns {(string | null)}
|
||||
*/
|
||||
function getAttributeValue(elt, qualifiedName) {
|
||||
return getRawAttribute(elt, qualifiedName) || getRawAttribute(elt, "data-" + qualifiedName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} elt
|
||||
* @returns {HTMLElement | null}
|
||||
*/
|
||||
function parentElt(elt) {
|
||||
return elt.parentElement;
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {Document}
|
||||
*/
|
||||
function getDocument() {
|
||||
return document;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} elt
|
||||
* @param {(e:HTMLElement) => boolean} condition
|
||||
* @returns {HTMLElement | null}
|
||||
*/
|
||||
function getClosestMatch(elt, condition) {
|
||||
while (elt && !condition(elt)) {
|
||||
elt = parentElt(elt);
|
||||
}
|
||||
|
||||
return elt ? elt : null;
|
||||
}
|
||||
|
||||
function getAttributeValueWithDisinheritance(initialElement, ancestor, attributeName){
|
||||
var attributeValue = getAttributeValue(ancestor, attributeName);
|
||||
var disinherit = getAttributeValue(ancestor, "hx-disinherit");
|
||||
if (initialElement !== ancestor && disinherit && (disinherit === "*" || disinherit.split(" ").indexOf(attributeName) >= 0)) {
|
||||
return "unset";
|
||||
} else {
|
||||
return attributeValue
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} elt
|
||||
* @param {string} attributeName
|
||||
* @returns {string | null}
|
||||
*/
|
||||
function getClosestAttributeValue(elt, attributeName) {
|
||||
var closestAttr = null;
|
||||
getClosestMatch(elt, function (e) {
|
||||
return closestAttr = getAttributeValueWithDisinheritance(elt, e, attributeName);
|
||||
});
|
||||
if (closestAttr !== "unset") {
|
||||
return closestAttr;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} elt
|
||||
* @param {string} selector
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function matches(elt, selector) {
|
||||
// @ts-ignore: non-standard properties for browser compatibility
|
||||
// noinspection JSUnresolvedVariable
|
||||
var matchesFunction = elt.matches || elt.matchesSelector || elt.msMatchesSelector || elt.mozMatchesSelector || elt.webkitMatchesSelector || elt.oMatchesSelector;
|
||||
return matchesFunction && matchesFunction.call(elt, selector);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} str
|
||||
* @returns {string}
|
||||
*/
|
||||
function getStartTag(str) {
|
||||
var tagMatcher = /<([a-z][^\/\0>\x20\t\r\n\f]*)/i
|
||||
var match = tagMatcher.exec( str );
|
||||
if (match) {
|
||||
return match[1].toLowerCase();
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} resp
|
||||
* @param {number} depth
|
||||
* @returns {Element}
|
||||
*/
|
||||
function parseHTML(resp, depth) {
|
||||
var parser = new DOMParser();
|
||||
var responseDoc = parser.parseFromString(resp, "text/html");
|
||||
|
||||
/** @type {Element} */
|
||||
var responseNode = responseDoc.body;
|
||||
while (depth > 0) {
|
||||
depth--;
|
||||
// @ts-ignore
|
||||
responseNode = responseNode.firstChild;
|
||||
}
|
||||
if (responseNode == null) {
|
||||
// @ts-ignore
|
||||
responseNode = getDocument().createDocumentFragment();
|
||||
}
|
||||
return responseNode;
|
||||
}
|
||||
|
||||
function aFullPageResponse(resp) {
|
||||
return /<body/.test(resp)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} response
|
||||
* @returns {Element}
|
||||
*/
|
||||
function makeFragment(response) {
|
||||
var partialResponse = !aFullPageResponse(response);
|
||||
var startTag = getStartTag(response);
|
||||
var content = response;
|
||||
if (startTag === 'head') {
|
||||
content = content.replace(HEAD_TAG_REGEX, '');
|
||||
}
|
||||
if (htmx.config.useTemplateFragments && partialResponse) {
|
||||
var fragment = parseHTML("<body><template>" + content + "</template></body>", 0);
|
||||
// @ts-ignore type mismatch between DocumentFragment and Element.
|
||||
// TODO: Are these close enough for htmx to use interchangeably?
|
||||
var fragmentContent = fragment.querySelector('template').content;
|
||||
if (htmx.config.allowScriptTags) {
|
||||
// if there is a nonce set up, set it on the new script tags
|
||||
forEach(fragmentContent.querySelectorAll("script"), function (script) {
|
||||
if (htmx.config.inlineScriptNonce) {
|
||||
script.nonce = htmx.config.inlineScriptNonce;
|
||||
}
|
||||
// mark as executed due to template insertion semantics on all browsers except firefox fml
|
||||
script.htmxExecuted = navigator.userAgent.indexOf("Firefox") === -1;
|
||||
})
|
||||
} else {
|
||||
forEach(fragmentContent.querySelectorAll("script"), function (script) {
|
||||
// remove all script tags if scripts are disabled
|
||||
removeElement(script);
|
||||
})
|
||||
}
|
||||
return fragmentContent;
|
||||
}
|
||||
switch (startTag) {
|
||||
case "thead":
|
||||
case "tbody":
|
||||
case "tfoot":
|
||||
case "colgroup":
|
||||
case "caption":
|
||||
return parseHTML("<table>" + content + "</table>", 1);
|
||||
case "col":
|
||||
return parseHTML("<table><colgroup>" + content + "</colgroup></table>", 2);
|
||||
case "tr":
|
||||
return parseHTML("<table><tbody>" + content + "</tbody></table>", 2);
|
||||
case "td":
|
||||
case "th":
|
||||
return parseHTML("<table><tbody><tr>" + content + "</tr></tbody></table>", 3);
|
||||
case "script":
|
||||
case "style":
|
||||
return parseHTML("<div>" + content + "</div>", 1);
|
||||
default:
|
||||
return parseHTML(content, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Function} func
|
||||
*/
|
||||
function maybeCall(func){
|
||||
if(func) {
|
||||
func();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {any} o
|
||||
* @param {string} type
|
||||
* @returns
|
||||
*/
|
||||
function isType(o, type) {
|
||||
return Object.prototype.toString.call(o) === "[object " + type + "]";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {*} o
|
||||
* @returns {o is Function}
|
||||
*/
|
||||
function isFunction(o) {
|
||||
return isType(o, "Function");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {*} o
|
||||
* @returns {o is Object}
|
||||
*/
|
||||
function isRawObject(o) {
|
||||
return isType(o, "Object");
|
||||
}
|
||||
|
||||
/**
|
||||
* getInternalData retrieves "private" data stored by htmx within an element
|
||||
* @param {HTMLElement} elt
|
||||
* @returns {*}
|
||||
*/
|
||||
function getInternalData(elt) {
|
||||
var dataProp = 'htmx-internal-data';
|
||||
var data = elt[dataProp];
|
||||
if (!data) {
|
||||
data = elt[dataProp] = {};
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* toArray converts an ArrayLike object into a real array.
|
||||
* @param {ArrayLike} arr
|
||||
* @returns {any[]}
|
||||
*/
|
||||
function toArray(arr) {
|
||||
var returnArr = [];
|
||||
if (arr) {
|
||||
for (var i = 0; i < arr.length; i++) {
|
||||
returnArr.push(arr[i]);
|
||||
}
|
||||
}
|
||||
return returnArr
|
||||
}
|
||||
|
||||
function forEach(arr, func) {
|
||||
if (arr) {
|
||||
for (var i = 0; i < arr.length; i++) {
|
||||
func(arr[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function isScrolledIntoView(el) {
|
||||
var rect = el.getBoundingClientRect();
|
||||
var elemTop = rect.top;
|
||||
var elemBottom = rect.bottom;
|
||||
return elemTop < window.innerHeight && elemBottom >= 0;
|
||||
}
|
||||
|
||||
function bodyContains(elt) {
|
||||
// IE Fix
|
||||
if (elt.getRootNode && elt.getRootNode() instanceof window.ShadowRoot) {
|
||||
return getDocument().body.contains(elt.getRootNode().host);
|
||||
} else {
|
||||
return getDocument().body.contains(elt);
|
||||
}
|
||||
}
|
||||
|
||||
function splitOnWhitespace(trigger) {
|
||||
return trigger.trim().split(/\s+/);
|
||||
}
|
||||
|
||||
/**
|
||||
* mergeObjects takes all of the keys from
|
||||
* obj2 and duplicates them into obj1
|
||||
* @param {Object} obj1
|
||||
* @param {Object} obj2
|
||||
* @returns {Object}
|
||||
*/
|
||||
function mergeObjects(obj1, obj2) {
|
||||
for (var key in obj2) {
|
||||
if (obj2.hasOwnProperty(key)) {
|
||||
obj1[key] = obj2[key];
|
||||
}
|
||||
}
|
||||
return obj1;
|
||||
}
|
||||
|
||||
function parseJSON(jString) {
|
||||
try {
|
||||
return JSON.parse(jString);
|
||||
} catch(error) {
|
||||
logError(error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function canAccessLocalStorage() {
|
||||
var test = 'htmx:localStorageTest';
|
||||
try {
|
||||
localStorage.setItem(test, test);
|
||||
localStorage.removeItem(test);
|
||||
return true;
|
||||
} catch(e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function normalizePath(path) {
|
||||
try {
|
||||
var url = new URL(path);
|
||||
if (url) {
|
||||
path = url.pathname + url.search;
|
||||
}
|
||||
// remove trailing slash, unless index page
|
||||
if (!(/^\/$/.test(path))) {
|
||||
path = path.replace(/\/+$/, '');
|
||||
}
|
||||
return path;
|
||||
} catch (e) {
|
||||
// be kind to IE11, which doesn't support URL()
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
//==========================================================================================
|
||||
// public API
|
||||
//==========================================================================================
|
||||
|
||||
function internalEval(str){
|
||||
return maybeEval(getDocument().body, function () {
|
||||
return eval(str);
|
||||
});
|
||||
}
|
||||
|
||||
function onLoadHelper(callback) {
|
||||
var value = htmx.on("htmx:load", function(evt) {
|
||||
callback(evt.detail.elt);
|
||||
});
|
||||
return value;
|
||||
}
|
||||
|
||||
function logAll(){
|
||||
htmx.logger = function(elt, event, data) {
|
||||
if(console) {
|
||||
console.log(event, elt, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function logNone() {
|
||||
htmx.logger = null
|
||||
}
|
||||
|
||||
function find(eltOrSelector, selector) {
|
||||
if (selector) {
|
||||
return eltOrSelector.querySelector(selector);
|
||||
} else {
|
||||
return find(getDocument(), eltOrSelector);
|
||||
}
|
||||
}
|
||||
|
||||
function findAll(eltOrSelector, selector) {
|
||||
if (selector) {
|
||||
return eltOrSelector.querySelectorAll(selector);
|
||||
} else {
|
||||
return findAll(getDocument(), eltOrSelector);
|
||||
}
|
||||
}
|
||||
|
||||
function removeElement(elt, delay) {
|
||||
elt = resolveTarget(elt);
|
||||
if (delay) {
|
||||
setTimeout(function(){
|
||||
removeElement(elt);
|
||||
elt = null;
|
||||
}, delay);
|
||||
} else {
|
||||
elt.parentElement.removeChild(elt);
|
||||
}
|
||||
}
|
||||
|
||||
function addClassToElement(elt, clazz, delay) {
|
||||
elt = resolveTarget(elt);
|
||||
if (delay) {
|
||||
setTimeout(function(){
|
||||
addClassToElement(elt, clazz);
|
||||
elt = null;
|
||||
}, delay);
|
||||
} else {
|
||||
elt.classList && elt.classList.add(clazz);
|
||||
}
|
||||
}
|
||||
|
||||
function removeClassFromElement(elt, clazz, delay) {
|
||||
elt = resolveTarget(elt);
|
||||
if (delay) {
|
||||
setTimeout(function(){
|
||||
removeClassFromElement(elt, clazz);
|
||||
elt = null;
|
||||
}, delay);
|
||||
} else {
|
||||
if (elt.classList) {
|
||||
elt.classList.remove(clazz);
|
||||
// if there are no classes left, remove the class attribute
|
||||
if (elt.classList.length === 0) {
|
||||
elt.removeAttribute("class");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function toggleClassOnElement(elt, clazz) {
|
||||
elt = resolveTarget(elt);
|
||||
elt.classList.toggle(clazz);
|
||||
}
|
||||
|
||||
function takeClassForElement(elt, clazz) {
|
||||
elt = resolveTarget(elt);
|
||||
forEach(elt.parentElement.children, function(child){
|
||||
removeClassFromElement(child, clazz);
|
||||
})
|
||||
addClassToElement(elt, clazz);
|
||||
}
|
||||
|
||||
function closest(elt, selector) {
|
||||
elt = resolveTarget(elt);
|
||||
if (elt.closest) {
|
||||
return elt.closest(selector);
|
||||
} else {
|
||||
// TODO remove when IE goes away
|
||||
do{
|
||||
if (elt == null || matches(elt, selector)){
|
||||
return elt;
|
||||
}
|
||||
}
|
||||
while (elt = elt && parentElt(elt));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function startsWith(str, prefix) {
|
||||
return str.substring(0, prefix.length) === prefix
|
||||
}
|
||||
|
||||
function endsWith(str, suffix) {
|
||||
return str.substring(str.length - suffix.length) === suffix
|
||||
}
|
||||
|
||||
function normalizeSelector(selector) {
|
||||
var trimmedSelector = selector.trim();
|
||||
if (startsWith(trimmedSelector, "<") && endsWith(trimmedSelector, "/>")) {
|
||||
return trimmedSelector.substring(1, trimmedSelector.length - 2);
|
||||
} else {
|
||||
return trimmedSelector;
|
||||
}
|
||||
}
|
||||
|
||||
function querySelectorAllExt(elt, selector) {
|
||||
if (selector.indexOf("closest ") === 0) {
|
||||
return [closest(elt, normalizeSelector(selector.substr(8)))];
|
||||
} else if (selector.indexOf("find ") === 0) {
|
||||
return [find(elt, normalizeSelector(selector.substr(5)))];
|
||||
} else if (selector === "next") {
|
||||
return [elt.nextElementSibling]
|
||||
} else if (selector.indexOf("next ") === 0) {
|
||||
return [scanForwardQuery(elt, normalizeSelector(selector.substr(5)))];
|
||||
} else if (selector === "previous") {
|
||||
return [elt.previousElementSibling]
|
||||
} else if (selector.indexOf("previous ") === 0) {
|
||||
return [scanBackwardsQuery(elt, normalizeSelector(selector.substr(9)))];
|
||||
} else if (selector === 'document') {
|
||||
return [document];
|
||||
} else if (selector === 'window') {
|
||||
return [window];
|
||||
} else if (selector === 'body') {
|
||||
return [document.body];
|
||||
} else {
|
||||
return getDocument().querySelectorAll(normalizeSelector(selector));
|
||||
}
|
||||
}
|
||||
|
||||
var scanForwardQuery = function(start, match) {
|
||||
var results = getDocument().querySelectorAll(match);
|
||||
for (var i = 0; i < results.length; i++) {
|
||||
var elt = results[i];
|
||||
if (elt.compareDocumentPosition(start) === Node.DOCUMENT_POSITION_PRECEDING) {
|
||||
return elt;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var scanBackwardsQuery = function(start, match) {
|
||||
var results = getDocument().querySelectorAll(match);
|
||||
for (var i = results.length - 1; i >= 0; i--) {
|
||||
var elt = results[i];
|
||||
if (elt.compareDocumentPosition(start) === Node.DOCUMENT_POSITION_FOLLOWING) {
|
||||
return elt;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function querySelectorExt(eltOrSelector, selector) {
|
||||
if (selector) {
|
||||
return querySelectorAllExt(eltOrSelector, selector)[0];
|
||||
} else {
|
||||
return querySelectorAllExt(getDocument().body, eltOrSelector)[0];
|
||||
}
|
||||
}
|
||||
|
||||
function resolveTarget(arg2) {
|
||||
if (isType(arg2, 'String')) {
|
||||
return find(arg2);
|
||||
} else {
|
||||
return arg2;
|
||||
}
|
||||
}
|
||||
|
||||
function processEventArgs(arg1, arg2, arg3) {
|
||||
if (isFunction(arg2)) {
|
||||
return {
|
||||
target: getDocument().body,
|
||||
event: arg1,
|
||||
listener: arg2
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
target: resolveTarget(arg1),
|
||||
event: arg2,
|
||||
listener: arg3
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function addEventListenerImpl(arg1, arg2, arg3) {
|
||||
ready(function(){
|
||||
var eventArgs = processEventArgs(arg1, arg2, arg3);
|
||||
eventArgs.target.addEventListener(eventArgs.event, eventArgs.listener);
|
||||
})
|
||||
var b = isFunction(arg2);
|
||||
return b ? arg2 : arg3;
|
||||
}
|
||||
|
||||
function removeEventListenerImpl(arg1, arg2, arg3) {
|
||||
ready(function(){
|
||||
var eventArgs = processEventArgs(arg1, arg2, arg3);
|
||||
eventArgs.target.removeEventListener(eventArgs.event, eventArgs.listener);
|
||||
})
|
||||
return isFunction(arg2) ? arg2 : arg3;
|
||||
}
|
||||
|
||||
//====================================================================
|
||||
// Node processing
|
||||
//====================================================================
|
||||
|
||||
var DUMMY_ELT = getDocument().createElement("output"); // dummy element for bad selectors
|
||||
function findAttributeTargets(elt, attrName) {
|
||||
var attrTarget = getClosestAttributeValue(elt, attrName);
|
||||
if (attrTarget) {
|
||||
if (attrTarget === "this") {
|
||||
return [findThisElement(elt, attrName)];
|
||||
} else {
|
||||
var result = querySelectorAllExt(elt, attrTarget);
|
||||
if (result.length === 0) {
|
||||
logError('The selector "' + attrTarget + '" on ' + attrName + " returned no matches!");
|
||||
return [DUMMY_ELT]
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function findThisElement(elt, attribute){
|
||||
return getClosestMatch(elt, function (elt) {
|
||||
return getAttributeValue(elt, attribute) != null;
|
||||
})
|
||||
}
|
||||
|
||||
function getTarget(elt) {
|
||||
var targetStr = getClosestAttributeValue(elt, "hx-target");
|
||||
if (targetStr) {
|
||||
if (targetStr === "this") {
|
||||
return findThisElement(elt,'hx-target');
|
||||
} else {
|
||||
return querySelectorExt(elt, targetStr)
|
||||
}
|
||||
} else {
|
||||
var data = getInternalData(elt);
|
||||
if (data.boosted) {
|
||||
return getDocument().body;
|
||||
} else {
|
||||
return elt;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function shouldSettleAttribute(name) {
|
||||
var attributesToSettle = htmx.config.attributesToSettle;
|
||||
for (var i = 0; i < attributesToSettle.length; i++) {
|
||||
if (name === attributesToSettle[i]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function cloneAttributes(mergeTo, mergeFrom) {
|
||||
forEach(mergeTo.attributes, function (attr) {
|
||||
if (!mergeFrom.hasAttribute(attr.name) && shouldSettleAttribute(attr.name)) {
|
||||
mergeTo.removeAttribute(attr.name)
|
||||
}
|
||||
});
|
||||
forEach(mergeFrom.attributes, function (attr) {
|
||||
if (shouldSettleAttribute(attr.name)) {
|
||||
mergeTo.setAttribute(attr.name, attr.value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function isInlineSwap(swapStyle, target) {
|
||||
var extensions = getExtensions(target);
|
||||
for (var i = 0; i < extensions.length; i++) {
|
||||
var extension = extensions[i];
|
||||
try {
|
||||
if (extension.isInlineSwap(swapStyle)) {
|
||||
return true;
|
||||
}
|
||||
} catch(e) {
|
||||
logError(e);
|
||||
}
|
||||
}
|
||||
return swapStyle === "outerHTML";
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} oobValue
|
||||
* @param {HTMLElement} oobElement
|
||||
* @param {*} settleInfo
|
||||
* @returns
|
||||
*/
|
||||
function oobSwap(oobValue, oobElement, settleInfo) {
|
||||
var selector = "#" + getRawAttribute(oobElement, "id");
|
||||
var swapStyle = "outerHTML";
|
||||
if (oobValue === "true") {
|
||||
// do nothing
|
||||
} else if (oobValue.indexOf(":") > 0) {
|
||||
swapStyle = oobValue.substr(0, oobValue.indexOf(":"));
|
||||
selector = oobValue.substr(oobValue.indexOf(":") + 1, oobValue.length);
|
||||
} else {
|
||||
swapStyle = oobValue;
|
||||
}
|
||||
|
||||
var targets = getDocument().querySelectorAll(selector);
|
||||
if (targets) {
|
||||
forEach(
|
||||
targets,
|
||||
function (target) {
|
||||
var fragment;
|
||||
var oobElementClone = oobElement.cloneNode(true);
|
||||
fragment = getDocument().createDocumentFragment();
|
||||
fragment.appendChild(oobElementClone);
|
||||
if (!isInlineSwap(swapStyle, target)) {
|
||||
fragment = oobElementClone; // if this is not an inline swap, we use the content of the node, not the node itself
|
||||
}
|
||||
|
||||
var beforeSwapDetails = {shouldSwap: true, target: target, fragment:fragment };
|
||||
if (!triggerEvent(target, 'htmx:oobBeforeSwap', beforeSwapDetails)) return;
|
||||
|
||||
target = beforeSwapDetails.target; // allow re-targeting
|
||||
if (beforeSwapDetails['shouldSwap']){
|
||||
swap(swapStyle, target, target, fragment, settleInfo);
|
||||
}
|
||||
forEach(settleInfo.elts, function (elt) {
|
||||
triggerEvent(elt, 'htmx:oobAfterSwap', beforeSwapDetails);
|
||||
});
|
||||
}
|
||||
);
|
||||
oobElement.parentNode.removeChild(oobElement);
|
||||
} else {
|
||||
oobElement.parentNode.removeChild(oobElement);
|
||||
triggerErrorEvent(getDocument().body, "htmx:oobErrorNoTarget", {content: oobElement});
|
||||
}
|
||||
return oobValue;
|
||||
}
|
||||
|
||||
function handleOutOfBandSwaps(elt, fragment, settleInfo) {
|
||||
var oobSelects = getClosestAttributeValue(elt, "hx-select-oob");
|
||||
if (oobSelects) {
|
||||
var oobSelectValues = oobSelects.split(",");
|
||||
for (var i = 0; i < oobSelectValues.length; i++) {
|
||||
var oobSelectValue = oobSelectValues[i].split(":", 2);
|
||||
var id = oobSelectValue[0].trim();
|
||||
if (id.indexOf("#") === 0) {
|
||||
id = id.substring(1);
|
||||
}
|
||||
var oobValue = oobSelectValue[1] || "true";
|
||||
var oobElement = fragment.querySelector("#" + id);
|
||||
if (oobElement) {
|
||||
oobSwap(oobValue, oobElement, settleInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
forEach(findAll(fragment, '[hx-swap-oob], [data-hx-swap-oob]'), function (oobElement) {
|
||||
var oobValue = getAttributeValue(oobElement, "hx-swap-oob");
|
||||
if (oobValue != null) {
|
||||
oobSwap(oobValue, oobElement, settleInfo);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function handlePreservedElements(fragment) {
|
||||
forEach(findAll(fragment, '[hx-preserve], [data-hx-preserve]'), function (preservedElt) {
|
||||
var id = getAttributeValue(preservedElt, "id");
|
||||
var oldElt = getDocument().getElementById(id);
|
||||
if (oldElt != null) {
|
||||
preservedElt.parentNode.replaceChild(oldElt, preservedElt);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function handleAttributes(parentNode, fragment, settleInfo) {
|
||||
forEach(fragment.querySelectorAll("[id]"), function (newNode) {
|
||||
var id = getRawAttribute(newNode, "id")
|
||||
if (id && id.length > 0) {
|
||||
var normalizedId = id.replace("'", "\\'");
|
||||
var normalizedTag = newNode.tagName.replace(':', '\\:');
|
||||
var oldNode = parentNode.querySelector(normalizedTag + "[id='" + normalizedId + "']");
|
||||
if (oldNode && oldNode !== parentNode) {
|
||||
var newAttributes = newNode.cloneNode();
|
||||
cloneAttributes(newNode, oldNode);
|
||||
settleInfo.tasks.push(function () {
|
||||
cloneAttributes(newNode, newAttributes);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function makeAjaxLoadTask(child) {
|
||||
return function () {
|
||||
removeClassFromElement(child, htmx.config.addedClass);
|
||||
processNode(child);
|
||||
processScripts(child);
|
||||
processFocus(child)
|
||||
triggerEvent(child, 'htmx:load');
|
||||
};
|
||||
}
|
||||
|
||||
function processFocus(child) {
|
||||
var autofocus = "[autofocus]";
|
||||
var autoFocusedElt = matches(child, autofocus) ? child : child.querySelector(autofocus)
|
||||
if (autoFocusedElt != null) {
|
||||
autoFocusedElt.focus();
|
||||
}
|
||||
}
|
||||
|
||||
function insertNodesBefore(parentNode, insertBefore, fragment, settleInfo) {
|
||||
handleAttributes(parentNode, fragment, settleInfo);
|
||||
while(fragment.childNodes.length > 0){
|
||||
var child = fragment.firstChild;
|
||||
addClassToElement(child, htmx.config.addedClass);
|
||||
parentNode.insertBefore(child, insertBefore);
|
||||
if (child.nodeType !== Node.TEXT_NODE && child.nodeType !== Node.COMMENT_NODE) {
|
||||
settleInfo.tasks.push(makeAjaxLoadTask(child));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// based on https://gist.github.com/hyamamoto/fd435505d29ebfa3d9716fd2be8d42f0,
|
||||
// derived from Java's string hashcode implementation
|
||||
function stringHash(string, hash) {
|
||||
var char = 0;
|
||||
while (char < string.length){
|
||||
hash = (hash << 5) - hash + string.charCodeAt(char++) | 0; // bitwise or ensures we have a 32-bit int
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
function attributeHash(elt) {
|
||||
var hash = 0;
|
||||
// IE fix
|
||||
if (elt.attributes) {
|
||||
for (var i = 0; i < elt.attributes.length; i++) {
|
||||
var attribute = elt.attributes[i];
|
||||
if(attribute.value){ // only include attributes w/ actual values (empty is same as non-existent)
|
||||
hash = stringHash(attribute.name, hash);
|
||||
hash = stringHash(attribute.value, hash);
|
||||
}
|
||||
}
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
function deInitOnHandlers(elt) {
|
||||
var internalData = getInternalData(elt);
|
||||
if (internalData.onHandlers) {
|
||||
for (var i = 0; i < internalData.onHandlers.length; i++) {
|
||||
const handlerInfo = internalData.onHandlers[i];
|
||||
elt.removeEventListener(handlerInfo.event, handlerInfo.listener);
|
||||
}
|
||||
delete internalData.onHandlers
|
||||
}
|
||||
}
|
||||
|
||||
function deInitNode(element) {
|
||||
var internalData = getInternalData(element);
|
||||
if (internalData.timeout) {
|
||||
clearTimeout(internalData.timeout);
|
||||
}
|
||||
if (internalData.webSocket) {
|
||||
internalData.webSocket.close();
|
||||
}
|
||||
if (internalData.sseEventSource) {
|
||||
internalData.sseEventSource.close();
|
||||
}
|
||||
if (internalData.listenerInfos) {
|
||||
forEach(internalData.listenerInfos, function (info) {
|
||||
if (info.on) {
|
||||
info.on.removeEventListener(info.trigger, info.listener);
|
||||
}
|
||||
});
|
||||
}
|
||||
deInitOnHandlers(element);
|
||||
forEach(Object.keys(internalData), function(key) { delete internalData[key] });
|
||||
}
|
||||
|
||||
function cleanUpElement(element) {
|
||||
triggerEvent(element, "htmx:beforeCleanupElement")
|
||||
deInitNode(element);
|
||||
if (element.children) { // IE
|
||||
forEach(element.children, function(child) { cleanUpElement(child) });
|
||||
}
|
||||
}
|
||||
|
||||
function swapOuterHTML(target, fragment, settleInfo) {
|
||||
if (target.tagName === "BODY") {
|
||||
return swapInnerHTML(target, fragment, settleInfo);
|
||||
} else {
|
||||
// @type {HTMLElement}
|
||||
var newElt
|
||||
var eltBeforeNewContent = target.previousSibling;
|
||||
insertNodesBefore(parentElt(target), target, fragment, settleInfo);
|
||||
if (eltBeforeNewContent == null) {
|
||||
newElt = parentElt(target).firstChild;
|
||||
} else {
|
||||
newElt = eltBeforeNewContent.nextSibling;
|
||||
}
|
||||
settleInfo.elts = settleInfo.elts.filter(function(e) { return e != target });
|
||||
while(newElt && newElt !== target) {
|
||||
if (newElt.nodeType === Node.ELEMENT_NODE) {
|
||||
settleInfo.elts.push(newElt);
|
||||
}
|
||||
newElt = newElt.nextElementSibling;
|
||||
}
|
||||
cleanUpElement(target);
|
||||
parentElt(target).removeChild(target);
|
||||
}
|
||||
}
|
||||
|
||||
function swapAfterBegin(target, fragment, settleInfo) {
|
||||
return insertNodesBefore(target, target.firstChild, fragment, settleInfo);
|
||||
}
|
||||
|
||||
function swapBeforeBegin(target, fragment, settleInfo) {
|
||||
return insertNodesBefore(parentElt(target), target, fragment, settleInfo);
|
||||
}
|
||||
|
||||
function swapBeforeEnd(target, fragment, settleInfo) {
|
||||
return insertNodesBefore(target, null, fragment, settleInfo);
|
||||
}
|
||||
|
||||
function swapAfterEnd(target, fragment, settleInfo) {
|
||||
return insertNodesBefore(parentElt(target), target.nextSibling, fragment, settleInfo);
|
||||
}
|
||||
function swapDelete(target, fragment, settleInfo) {
|
||||
cleanUpElement(target);
|
||||
return parentElt(target).removeChild(target);
|
||||
}
|
||||
|
||||
function swapInnerHTML(target, fragment, settleInfo) {
|
||||
var firstChild = target.firstChild;
|
||||
insertNodesBefore(target, firstChild, fragment, settleInfo);
|
||||
if (firstChild) {
|
||||
while (firstChild.nextSibling) {
|
||||
cleanUpElement(firstChild.nextSibling)
|
||||
target.removeChild(firstChild.nextSibling);
|
||||
}
|
||||
cleanUpElement(firstChild)
|
||||
target.removeChild(firstChild);
|
||||
}
|
||||
}
|
||||
|
||||
function maybeSelectFromResponse(elt, fragment, selectOverride) {
|
||||
var selector = selectOverride || getClosestAttributeValue(elt, "hx-select");
|
||||
if (selector) {
|
||||
var newFragment = getDocument().createDocumentFragment();
|
||||
forEach(fragment.querySelectorAll(selector), function (node) {
|
||||
newFragment.appendChild(node);
|
||||
});
|
||||
fragment = newFragment;
|
||||
}
|
||||
return fragment;
|
||||
}
|
||||
|
||||
function swap(swapStyle, elt, target, fragment, settleInfo) {
|
||||
switch (swapStyle) {
|
||||
case "none":
|
||||
return;
|
||||
case "outerHTML":
|
||||
swapOuterHTML(target, fragment, settleInfo);
|
||||
return;
|
||||
case "afterbegin":
|
||||
swapAfterBegin(target, fragment, settleInfo);
|
||||
return;
|
||||
case "beforebegin":
|
||||
swapBeforeBegin(target, fragment, settleInfo);
|
||||
return;
|
||||
case "beforeend":
|
||||
swapBeforeEnd(target, fragment, settleInfo);
|
||||
return;
|
||||
case "afterend":
|
||||
swapAfterEnd(target, fragment, settleInfo);
|
||||
return;
|
||||
case "delete":
|
||||
swapDelete(target, fragment, settleInfo);
|
||||
return;
|
||||
default:
|
||||
var extensions = getExtensions(elt);
|
||||
for (var i = 0; i < extensions.length; i++) {
|
||||
var ext = extensions[i];
|
||||
try {
|
||||
var newElements = ext.handleSwap(swapStyle, target, fragment, settleInfo);
|
||||
if (newElements) {
|
||||
if (typeof newElements.length !== 'undefined') {
|
||||
// if handleSwap returns an array (like) of elements, we handle them
|
||||
for (var j = 0; j < newElements.length; j++) {
|
||||
var child = newElements[j];
|
||||
if (child.nodeType !== Node.TEXT_NODE && child.nodeType !== Node.COMMENT_NODE) {
|
||||
settleInfo.tasks.push(makeAjaxLoadTask(child));
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
} catch (e) {
|
||||
logError(e);
|
||||
}
|
||||
}
|
||||
if (swapStyle === "innerHTML") {
|
||||
swapInnerHTML(target, fragment, settleInfo);
|
||||
} else {
|
||||
swap(htmx.config.defaultSwapStyle, elt, target, fragment, settleInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function findTitle(content) {
|
||||
if (content.indexOf('<title') > -1) {
|
||||
var contentWithSvgsRemoved = content.replace(SVG_TAGS_REGEX, '');
|
||||
var result = contentWithSvgsRemoved.match(TITLE_TAG_REGEX);
|
||||
if (result) {
|
||||
return result[2];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function selectAndSwap(swapStyle, target, elt, responseText, settleInfo, selectOverride) {
|
||||
settleInfo.title = findTitle(responseText);
|
||||
var fragment = makeFragment(responseText);
|
||||
if (fragment) {
|
||||
handleOutOfBandSwaps(elt, fragment, settleInfo);
|
||||
fragment = maybeSelectFromResponse(elt, fragment, selectOverride);
|
||||
handlePreservedElements(fragment);
|
||||
return swap(swapStyle, elt, target, fragment, settleInfo);
|
||||
}
|
||||
}
|
||||
|
||||
function handleTrigger(xhr, header, elt) {
|
||||
var triggerBody = xhr.getResponseHeader(header);
|
||||
if (triggerBody.indexOf("{") === 0) {
|
||||
var triggers = parseJSON(triggerBody);
|
||||
for (var eventName in triggers) {
|
||||
if (triggers.hasOwnProperty(eventName)) {
|
||||
var detail = triggers[eventName];
|
||||
if (!isRawObject(detail)) {
|
||||
detail = {"value": detail}
|
||||
}
|
||||
triggerEvent(elt, eventName, detail);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
var eventNames = triggerBody.split(",")
|
||||
for (var i = 0; i < eventNames.length; i++) {
|
||||
triggerEvent(elt, eventNames[i].trim(), []);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var WHITESPACE = /\s/;
|
||||
var WHITESPACE_OR_COMMA = /[\s,]/;
|
||||
var SYMBOL_START = /[_$a-zA-Z]/;
|
||||
var SYMBOL_CONT = /[_$a-zA-Z0-9]/;
|
||||
var STRINGISH_START = ['"', "'", "/"];
|
||||
var NOT_WHITESPACE = /[^\s]/;
|
||||
var COMBINED_SELECTOR_START = /[{(]/;
|
||||
var COMBINED_SELECTOR_END = /[})]/;
|
||||
function tokenizeString(str) {
|
||||
var tokens = [];
|
||||
var position = 0;
|
||||
while (position < str.length) {
|
||||
if(SYMBOL_START.exec(str.charAt(position))) {
|
||||
var startPosition = position;
|
||||
while (SYMBOL_CONT.exec(str.charAt(position + 1))) {
|
||||
position++;
|
||||
}
|
||||
tokens.push(str.substr(startPosition, position - startPosition + 1));
|
||||
} else if (STRINGISH_START.indexOf(str.charAt(position)) !== -1) {
|
||||
var startChar = str.charAt(position);
|
||||
var startPosition = position;
|
||||
position++;
|
||||
while (position < str.length && str.charAt(position) !== startChar ) {
|
||||
if (str.charAt(position) === "\\") {
|
||||
position++;
|
||||
}
|
||||
position++;
|
||||
}
|
||||
tokens.push(str.substr(startPosition, position - startPosition + 1));
|
||||
} else {
|
||||
var symbol = str.charAt(position);
|
||||
tokens.push(symbol);
|
||||
}
|
||||
position++;
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
|
||||
function isPossibleRelativeReference(token, last, paramName) {
|
||||
return SYMBOL_START.exec(token.charAt(0)) &&
|
||||
token !== "true" &&
|
||||
token !== "false" &&
|
||||
token !== "this" &&
|
||||
token !== paramName &&
|
||||
last !== ".";
|
||||
}
|
||||
|
||||
function maybeGenerateConditional(elt, tokens, paramName) {
|
||||
if (tokens[0] === '[') {
|
||||
tokens.shift();
|
||||
var bracketCount = 1;
|
||||
var conditionalSource = " return (function(" + paramName + "){ return (";
|
||||
var last = null;
|
||||
while (tokens.length > 0) {
|
||||
var token = tokens[0];
|
||||
if (token === "]") {
|
||||
bracketCount--;
|
||||
if (bracketCount === 0) {
|
||||
if (last === null) {
|
||||
conditionalSource = conditionalSource + "true";
|
||||
}
|
||||
tokens.shift();
|
||||
conditionalSource += ")})";
|
||||
try {
|
||||
var conditionFunction = maybeEval(elt,function () {
|
||||
return Function(conditionalSource)();
|
||||
},
|
||||
function(){return true})
|
||||
conditionFunction.source = conditionalSource;
|
||||
return conditionFunction;
|
||||
} catch (e) {
|
||||
triggerErrorEvent(getDocument().body, "htmx:syntax:error", {error:e, source:conditionalSource})
|
||||
return null;
|
||||
}
|
||||
}
|
||||
} else if (token === "[") {
|
||||
bracketCount++;
|
||||
}
|
||||
if (isPossibleRelativeReference(token, last, paramName)) {
|
||||
conditionalSource += "((" + paramName + "." + token + ") ? (" + paramName + "." + token + ") : (window." + token + "))";
|
||||
} else {
|
||||
conditionalSource = conditionalSource + token;
|
||||
}
|
||||
last = tokens.shift();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function consumeUntil(tokens, match) {
|
||||
var result = "";
|
||||
while (tokens.length > 0 && !match.test(tokens[0])) {
|
||||
result += tokens.shift();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function consumeCSSSelector(tokens) {
|
||||
var result;
|
||||
if (tokens.length > 0 && COMBINED_SELECTOR_START.test(tokens[0])) {
|
||||
tokens.shift();
|
||||
result = consumeUntil(tokens, COMBINED_SELECTOR_END).trim();
|
||||
tokens.shift();
|
||||
} else {
|
||||
result = consumeUntil(tokens, WHITESPACE_OR_COMMA);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
var INPUT_SELECTOR = 'input, textarea, select';
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} elt
|
||||
* @param {string} explicitTrigger
|
||||
* @param {cache} cache for trigger specs
|
||||
* @returns {import("./htmx").HtmxTriggerSpecification[]}
|
||||
*/
|
||||
function parseAndCacheTrigger(elt, explicitTrigger, cache) {
|
||||
var triggerSpecs = [];
|
||||
var tokens = tokenizeString(explicitTrigger);
|
||||
do {
|
||||
consumeUntil(tokens, NOT_WHITESPACE);
|
||||
var initialLength = tokens.length;
|
||||
var trigger = consumeUntil(tokens, /[,\[\s]/);
|
||||
if (trigger !== "") {
|
||||
if (trigger === "every") {
|
||||
var every = {trigger: 'every'};
|
||||
consumeUntil(tokens, NOT_WHITESPACE);
|
||||
every.pollInterval = parseInterval(consumeUntil(tokens, /[,\[\s]/));
|
||||
consumeUntil(tokens, NOT_WHITESPACE);
|
||||
var eventFilter = maybeGenerateConditional(elt, tokens, "event");
|
||||
if (eventFilter) {
|
||||
every.eventFilter = eventFilter;
|
||||
}
|
||||
triggerSpecs.push(every);
|
||||
} else if (trigger.indexOf("sse:") === 0) {
|
||||
triggerSpecs.push({trigger: 'sse', sseEvent: trigger.substr(4)});
|
||||
} else {
|
||||
var triggerSpec = {trigger: trigger};
|
||||
var eventFilter = maybeGenerateConditional(elt, tokens, "event");
|
||||
if (eventFilter) {
|
||||
triggerSpec.eventFilter = eventFilter;
|
||||
}
|
||||
while (tokens.length > 0 && tokens[0] !== ",") {
|
||||
consumeUntil(tokens, NOT_WHITESPACE)
|
||||
var token = tokens.shift();
|
||||
if (token === "changed") {
|
||||
triggerSpec.changed = true;
|
||||
} else if (token === "once") {
|
||||
triggerSpec.once = true;
|
||||
} else if (token === "consume") {
|
||||
triggerSpec.consume = true;
|
||||
} else if (token === "delay" && tokens[0] === ":") {
|
||||
tokens.shift();
|
||||
triggerSpec.delay = parseInterval(consumeUntil(tokens, WHITESPACE_OR_COMMA));
|
||||
} else if (token === "from" && tokens[0] === ":") {
|
||||
tokens.shift();
|
||||
if (COMBINED_SELECTOR_START.test(tokens[0])) {
|
||||
var from_arg = consumeCSSSelector(tokens);
|
||||
} else {
|
||||
var from_arg = consumeUntil(tokens, WHITESPACE_OR_COMMA);
|
||||
if (from_arg === "closest" || from_arg === "find" || from_arg === "next" || from_arg === "previous") {
|
||||
tokens.shift();
|
||||
var selector = consumeCSSSelector(tokens);
|
||||
// `next` and `previous` allow a selector-less syntax
|
||||
if (selector.length > 0) {
|
||||
from_arg += " " + selector;
|
||||
}
|
||||
}
|
||||
}
|
||||
triggerSpec.from = from_arg;
|
||||
} else if (token === "target" && tokens[0] === ":") {
|
||||
tokens.shift();
|
||||
triggerSpec.target = consumeCSSSelector(tokens);
|
||||
} else if (token === "throttle" && tokens[0] === ":") {
|
||||
tokens.shift();
|
||||
triggerSpec.throttle = parseInterval(consumeUntil(tokens, WHITESPACE_OR_COMMA));
|
||||
} else if (token === "queue" && tokens[0] === ":") {
|
||||
tokens.shift();
|
||||
triggerSpec.queue = consumeUntil(tokens, WHITESPACE_OR_COMMA);
|
||||
} else if (token === "root" && tokens[0] === ":") {
|
||||
tokens.shift();
|
||||
triggerSpec[token] = consumeCSSSelector(tokens);
|
||||
} else if (token === "threshold" && tokens[0] === ":") {
|
||||
tokens.shift();
|
||||
triggerSpec[token] = consumeUntil(tokens, WHITESPACE_OR_COMMA);
|
||||
} else {
|
||||
triggerErrorEvent(elt, "htmx:syntax:error", {token:tokens.shift()});
|
||||
}
|
||||
}
|
||||
triggerSpecs.push(triggerSpec);
|
||||
}
|
||||
}
|
||||
if (tokens.length === initialLength) {
|
||||
triggerErrorEvent(elt, "htmx:syntax:error", {token:tokens.shift()});
|
||||
}
|
||||
consumeUntil(tokens, NOT_WHITESPACE);
|
||||
} while (tokens[0] === "," && tokens.shift())
|
||||
if (cache) {
|
||||
cache[explicitTrigger] = triggerSpecs
|
||||
}
|
||||
return triggerSpecs
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} elt
|
||||
* @returns {import("./htmx").HtmxTriggerSpecification[]}
|
||||
*/
|
||||
function getTriggerSpecs(elt) {
|
||||
var explicitTrigger = getAttributeValue(elt, 'hx-trigger');
|
||||
var triggerSpecs = [];
|
||||
if (explicitTrigger) {
|
||||
var cache = htmx.config.triggerSpecsCache
|
||||
triggerSpecs = (cache && cache[explicitTrigger]) || parseAndCacheTrigger(elt, explicitTrigger, cache)
|
||||
}
|
||||
|
||||
if (triggerSpecs.length > 0) {
|
||||
return triggerSpecs;
|
||||
} else if (matches(elt, 'form')) {
|
||||
return [{trigger: 'submit'}];
|
||||
} else if (matches(elt, 'input[type="button"], input[type="submit"]')){
|
||||
return [{trigger: 'click'}];
|
||||
} else if (matches(elt, INPUT_SELECTOR)) {
|
||||
return [{trigger: 'change'}];
|
||||
} else {
|
||||
return [{trigger: 'click'}];
|
||||
}
|
||||
}
|
||||
|
||||
function cancelPolling(elt) {
|
||||
getInternalData(elt).cancelled = true;
|
||||
}
|
||||
|
||||
function processPolling(elt, handler, spec) {
|
||||
var nodeData = getInternalData(elt);
|
||||
nodeData.timeout = setTimeout(function () {
|
||||
if (bodyContains(elt) && nodeData.cancelled !== true) {
|
||||
if (!maybeFilterEvent(spec, elt, makeEvent('hx:poll:trigger', {
|
||||
triggerSpec: spec,
|
||||
target: elt
|
||||
}))) {
|
||||
handler(elt);
|
||||
}
|
||||
processPolling(elt, handler, spec);
|
||||
}
|
||||
}, spec.pollInterval);
|
||||
}
|
||||
|
||||
function isLocalLink(elt) {
|
||||
return location.hostname === elt.hostname &&
|
||||
getRawAttribute(elt,'href') &&
|
||||
getRawAttribute(elt,'href').indexOf("#") !== 0;
|
||||
}
|
||||
|
||||
function boostElement(elt, nodeData, triggerSpecs) {
|
||||
if ((elt.tagName === "A" && isLocalLink(elt) && (elt.target === "" || elt.target === "_self")) || elt.tagName === "FORM") {
|
||||
nodeData.boosted = true;
|
||||
var verb, path;
|
||||
if (elt.tagName === "A") {
|
||||
verb = "get";
|
||||
path = getRawAttribute(elt, 'href')
|
||||
} else {
|
||||
var rawAttribute = getRawAttribute(elt, "method");
|
||||
verb = rawAttribute ? rawAttribute.toLowerCase() : "get";
|
||||
if (verb === "get") {
|
||||
}
|
||||
path = getRawAttribute(elt, 'action');
|
||||
}
|
||||
triggerSpecs.forEach(function(triggerSpec) {
|
||||
addEventListener(elt, function(elt, evt) {
|
||||
if (closest(elt, htmx.config.disableSelector)) {
|
||||
cleanUpElement(elt)
|
||||
return
|
||||
}
|
||||
issueAjaxRequest(verb, path, elt, evt)
|
||||
}, nodeData, triggerSpec, true);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Event} evt
|
||||
* @param {HTMLElement} elt
|
||||
* @returns
|
||||
*/
|
||||
function shouldCancel(evt, elt) {
|
||||
if (evt.type === "submit" || evt.type === "click") {
|
||||
if (elt.tagName === "FORM") {
|
||||
return true;
|
||||
}
|
||||
if (matches(elt, 'input[type="submit"], button') && closest(elt, 'form') !== null) {
|
||||
return true;
|
||||
}
|
||||
if (elt.tagName === "A" && elt.href &&
|
||||
(elt.getAttribute('href') === '#' || elt.getAttribute('href').indexOf("#") !== 0)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function ignoreBoostedAnchorCtrlClick(elt, evt) {
|
||||
return getInternalData(elt).boosted && elt.tagName === "A" && evt.type === "click" && (evt.ctrlKey || evt.metaKey);
|
||||
}
|
||||
|
||||
function maybeFilterEvent(triggerSpec, elt, evt) {
|
||||
var eventFilter = triggerSpec.eventFilter;
|
||||
if(eventFilter){
|
||||
try {
|
||||
return eventFilter.call(elt, evt) !== true;
|
||||
} catch(e) {
|
||||
triggerErrorEvent(getDocument().body, "htmx:eventFilter:error", {error: e, source:eventFilter.source});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function addEventListener(elt, handler, nodeData, triggerSpec, explicitCancel) {
|
||||
var elementData = getInternalData(elt);
|
||||
var eltsToListenOn;
|
||||
if (triggerSpec.from) {
|
||||
eltsToListenOn = querySelectorAllExt(elt, triggerSpec.from);
|
||||
} else {
|
||||
eltsToListenOn = [elt];
|
||||
}
|
||||
// store the initial values of the elements, so we can tell if they change
|
||||
if (triggerSpec.changed) {
|
||||
eltsToListenOn.forEach(function (eltToListenOn) {
|
||||
var eltToListenOnData = getInternalData(eltToListenOn);
|
||||
eltToListenOnData.lastValue = eltToListenOn.value;
|
||||
})
|
||||
}
|
||||
forEach(eltsToListenOn, function (eltToListenOn) {
|
||||
var eventListener = function (evt) {
|
||||
if (!bodyContains(elt)) {
|
||||
eltToListenOn.removeEventListener(triggerSpec.trigger, eventListener);
|
||||
return;
|
||||
}
|
||||
if (ignoreBoostedAnchorCtrlClick(elt, evt)) {
|
||||
return;
|
||||
}
|
||||
if (explicitCancel || shouldCancel(evt, elt)) {
|
||||
evt.preventDefault();
|
||||
}
|
||||
if (maybeFilterEvent(triggerSpec, elt, evt)) {
|
||||
return;
|
||||
}
|
||||
var eventData = getInternalData(evt);
|
||||
eventData.triggerSpec = triggerSpec;
|
||||
if (eventData.handledFor == null) {
|
||||
eventData.handledFor = [];
|
||||
}
|
||||
if (eventData.handledFor.indexOf(elt) < 0) {
|
||||
eventData.handledFor.push(elt);
|
||||
if (triggerSpec.consume) {
|
||||
evt.stopPropagation();
|
||||
}
|
||||
if (triggerSpec.target && evt.target) {
|
||||
if (!matches(evt.target, triggerSpec.target)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (triggerSpec.once) {
|
||||
if (elementData.triggeredOnce) {
|
||||
return;
|
||||
} else {
|
||||
elementData.triggeredOnce = true;
|
||||
}
|
||||
}
|
||||
if (triggerSpec.changed) {
|
||||
var eltToListenOnData = getInternalData(eltToListenOn)
|
||||
if (eltToListenOnData.lastValue === eltToListenOn.value) {
|
||||
return;
|
||||
}
|
||||
eltToListenOnData.lastValue = eltToListenOn.value
|
||||
}
|
||||
if (elementData.delayed) {
|
||||
clearTimeout(elementData.delayed);
|
||||
}
|
||||
if (elementData.throttle) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (triggerSpec.throttle > 0) {
|
||||
if (!elementData.throttle) {
|
||||
handler(elt, evt);
|
||||
elementData.throttle = setTimeout(function () {
|
||||
elementData.throttle = null;
|
||||
}, triggerSpec.throttle);
|
||||
}
|
||||
} else if (triggerSpec.delay > 0) {
|
||||
elementData.delayed = setTimeout(function() { handler(elt, evt) }, triggerSpec.delay);
|
||||
} else {
|
||||
triggerEvent(elt, 'htmx:trigger')
|
||||
handler(elt, evt);
|
||||
}
|
||||
}
|
||||
};
|
||||
if (nodeData.listenerInfos == null) {
|
||||
nodeData.listenerInfos = [];
|
||||
}
|
||||
nodeData.listenerInfos.push({
|
||||
trigger: triggerSpec.trigger,
|
||||
listener: eventListener,
|
||||
on: eltToListenOn
|
||||
})
|
||||
eltToListenOn.addEventListener(triggerSpec.trigger, eventListener);
|
||||
});
|
||||
}
|
||||
|
||||
var windowIsScrolling = false // used by initScrollHandler
|
||||
var scrollHandler = null;
|
||||
function initScrollHandler() {
|
||||
if (!scrollHandler) {
|
||||
scrollHandler = function() {
|
||||
windowIsScrolling = true
|
||||
};
|
||||
window.addEventListener("scroll", scrollHandler)
|
||||
setInterval(function() {
|
||||
if (windowIsScrolling) {
|
||||
windowIsScrolling = false;
|
||||
forEach(getDocument().querySelectorAll("[hx-trigger='revealed'],[data-hx-trigger='revealed']"), function (elt) {
|
||||
maybeReveal(elt);
|
||||
})
|
||||
}
|
||||
}, 200);
|
||||
}
|
||||
}
|
||||
|
||||
function maybeReveal(elt) {
|
||||
if (!hasAttribute(elt,'data-hx-revealed') && isScrolledIntoView(elt)) {
|
||||
elt.setAttribute('data-hx-revealed', 'true');
|
||||
var nodeData = getInternalData(elt);
|
||||
if (nodeData.initHash) {
|
||||
triggerEvent(elt, 'revealed');
|
||||
} else {
|
||||
// if the node isn't initialized, wait for it before triggering the request
|
||||
elt.addEventListener("htmx:afterProcessNode", function(evt) { triggerEvent(elt, 'revealed') }, {once: true});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//====================================================================
|
||||
// Web Sockets
|
||||
//====================================================================
|
||||
|
||||
function processWebSocketInfo(elt, nodeData, info) {
|
||||
var values = splitOnWhitespace(info);
|
||||
for (var i = 0; i < values.length; i++) {
|
||||
var value = values[i].split(/:(.+)/);
|
||||
if (value[0] === "connect") {
|
||||
ensureWebSocket(elt, value[1], 0);
|
||||
}
|
||||
if (value[0] === "send") {
|
||||
processWebSocketSend(elt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function ensureWebSocket(elt, wssSource, retryCount) {
|
||||
if (!bodyContains(elt)) {
|
||||
return; // stop ensuring websocket connection when socket bearing element ceases to exist
|
||||
}
|
||||
|
||||
if (wssSource.indexOf("/") == 0) { // complete absolute paths only
|
||||
var base_part = location.hostname + (location.port ? ':'+location.port: '');
|
||||
if (location.protocol == 'https:') {
|
||||
wssSource = "wss://" + base_part + wssSource;
|
||||
} else if (location.protocol == 'http:') {
|
||||
wssSource = "ws://" + base_part + wssSource;
|
||||
}
|
||||
}
|
||||
var socket = htmx.createWebSocket(wssSource);
|
||||
socket.onerror = function (e) {
|
||||
triggerErrorEvent(elt, "htmx:wsError", {error:e, socket:socket});
|
||||
maybeCloseWebSocketSource(elt);
|
||||
};
|
||||
|
||||
socket.onclose = function (e) {
|
||||
if ([1006, 1012, 1013].indexOf(e.code) >= 0) { // Abnormal Closure/Service Restart/Try Again Later
|
||||
var delay = getWebSocketReconnectDelay(retryCount);
|
||||
setTimeout(function() {
|
||||
ensureWebSocket(elt, wssSource, retryCount+1); // creates a websocket with a new timeout
|
||||
}, delay);
|
||||
}
|
||||
};
|
||||
socket.onopen = function (e) {
|
||||
retryCount = 0;
|
||||
}
|
||||
|
||||
getInternalData(elt).webSocket = socket;
|
||||
socket.addEventListener('message', function (event) {
|
||||
if (maybeCloseWebSocketSource(elt)) {
|
||||
return;
|
||||
}
|
||||
|
||||
var response = event.data;
|
||||
withExtensions(elt, function(extension){
|
||||
response = extension.transformResponse(response, null, elt);
|
||||
});
|
||||
|
||||
var settleInfo = makeSettleInfo(elt);
|
||||
var fragment = makeFragment(response);
|
||||
var children = toArray(fragment.children);
|
||||
for (var i = 0; i < children.length; i++) {
|
||||
var child = children[i];
|
||||
oobSwap(getAttributeValue(child, "hx-swap-oob") || "true", child, settleInfo);
|
||||
}
|
||||
|
||||
settleImmediately(settleInfo.tasks);
|
||||
});
|
||||
}
|
||||
|
||||
function maybeCloseWebSocketSource(elt) {
|
||||
if (!bodyContains(elt)) {
|
||||
getInternalData(elt).webSocket.close();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function processWebSocketSend(elt) {
|
||||
var webSocketSourceElt = getClosestMatch(elt, function (parent) {
|
||||
return getInternalData(parent).webSocket != null;
|
||||
});
|
||||
if (webSocketSourceElt) {
|
||||
elt.addEventListener(getTriggerSpecs(elt)[0].trigger, function (evt) {
|
||||
var webSocket = getInternalData(webSocketSourceElt).webSocket;
|
||||
var headers = getHeaders(elt, webSocketSourceElt);
|
||||
var results = getInputValues(elt, 'post');
|
||||
var errors = results.errors;
|
||||
var rawParameters = results.values;
|
||||
var expressionVars = getExpressionVars(elt);
|
||||
var allParameters = mergeObjects(rawParameters, expressionVars);
|
||||
var filteredParameters = filterValues(allParameters, elt);
|
||||
filteredParameters['HEADERS'] = headers;
|
||||
if (errors && errors.length > 0) {
|
||||
triggerEvent(elt, 'htmx:validation:halted', errors);
|
||||
return;
|
||||
}
|
||||
webSocket.send(JSON.stringify(filteredParameters));
|
||||
if(shouldCancel(evt, elt)){
|
||||
evt.preventDefault();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
triggerErrorEvent(elt, "htmx:noWebSocketSourceError");
|
||||
}
|
||||
}
|
||||
|
||||
function getWebSocketReconnectDelay(retryCount) {
|
||||
var delay = htmx.config.wsReconnectDelay;
|
||||
if (typeof delay === 'function') {
|
||||
// @ts-ignore
|
||||
return delay(retryCount);
|
||||
}
|
||||
if (delay === 'full-jitter') {
|
||||
var exp = Math.min(retryCount, 6);
|
||||
var maxDelay = 1000 * Math.pow(2, exp);
|
||||
return maxDelay * Math.random();
|
||||
}
|
||||
logError('htmx.config.wsReconnectDelay must either be a function or the string "full-jitter"');
|
||||
}
|
||||
|
||||
//====================================================================
|
||||
// Server Sent Events
|
||||
//====================================================================
|
||||
|
||||
function processSSEInfo(elt, nodeData, info) {
|
||||
var values = splitOnWhitespace(info);
|
||||
for (var i = 0; i < values.length; i++) {
|
||||
var value = values[i].split(/:(.+)/);
|
||||
if (value[0] === "connect") {
|
||||
processSSESource(elt, value[1]);
|
||||
}
|
||||
|
||||
if ((value[0] === "swap")) {
|
||||
processSSESwap(elt, value[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function processSSESource(elt, sseSrc) {
|
||||
var source = htmx.createEventSource(sseSrc);
|
||||
source.onerror = function (e) {
|
||||
triggerErrorEvent(elt, "htmx:sseError", {error:e, source:source});
|
||||
maybeCloseSSESource(elt);
|
||||
};
|
||||
getInternalData(elt).sseEventSource = source;
|
||||
}
|
||||
|
||||
function processSSESwap(elt, sseEventName) {
|
||||
var sseSourceElt = getClosestMatch(elt, hasEventSource);
|
||||
if (sseSourceElt) {
|
||||
var sseEventSource = getInternalData(sseSourceElt).sseEventSource;
|
||||
var sseListener = function (event) {
|
||||
if (maybeCloseSSESource(sseSourceElt)) {
|
||||
return;
|
||||
}
|
||||
if (!bodyContains(elt)) {
|
||||
sseEventSource.removeEventListener(sseEventName, sseListener);
|
||||
return;
|
||||
}
|
||||
|
||||
///////////////////////////
|
||||
// TODO: merge this code with AJAX and WebSockets code in the future.
|
||||
|
||||
var response = event.data;
|
||||
withExtensions(elt, function(extension){
|
||||
response = extension.transformResponse(response, null, elt);
|
||||
});
|
||||
|
||||
var swapSpec = getSwapSpecification(elt)
|
||||
var target = getTarget(elt)
|
||||
var settleInfo = makeSettleInfo(elt);
|
||||
|
||||
selectAndSwap(swapSpec.swapStyle, target, elt, response, settleInfo)
|
||||
settleImmediately(settleInfo.tasks)
|
||||
triggerEvent(elt, "htmx:sseMessage", event)
|
||||
};
|
||||
|
||||
getInternalData(elt).sseListener = sseListener;
|
||||
sseEventSource.addEventListener(sseEventName, sseListener);
|
||||
} else {
|
||||
triggerErrorEvent(elt, "htmx:noSSESourceError");
|
||||
}
|
||||
}
|
||||
|
||||
function processSSETrigger(elt, handler, sseEventName) {
|
||||
var sseSourceElt = getClosestMatch(elt, hasEventSource);
|
||||
if (sseSourceElt) {
|
||||
var sseEventSource = getInternalData(sseSourceElt).sseEventSource;
|
||||
var sseListener = function () {
|
||||
if (!maybeCloseSSESource(sseSourceElt)) {
|
||||
if (bodyContains(elt)) {
|
||||
handler(elt);
|
||||
} else {
|
||||
sseEventSource.removeEventListener(sseEventName, sseListener);
|
||||
}
|
||||
}
|
||||
};
|
||||
getInternalData(elt).sseListener = sseListener;
|
||||
sseEventSource.addEventListener(sseEventName, sseListener);
|
||||
} else {
|
||||
triggerErrorEvent(elt, "htmx:noSSESourceError");
|
||||
}
|
||||
}
|
||||
|
||||
function maybeCloseSSESource(elt) {
|
||||
if (!bodyContains(elt)) {
|
||||
getInternalData(elt).sseEventSource.close();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function hasEventSource(node) {
|
||||
return getInternalData(node).sseEventSource != null;
|
||||
}
|
||||
|
||||
//====================================================================
|
||||
|
||||
function loadImmediately(elt, handler, nodeData, delay) {
|
||||
var load = function(){
|
||||
if (!nodeData.loaded) {
|
||||
nodeData.loaded = true;
|
||||
handler(elt);
|
||||
}
|
||||
}
|
||||
if (delay > 0) {
|
||||
setTimeout(load, delay);
|
||||
} else {
|
||||
load();
|
||||
}
|
||||
}
|
||||
|
||||
function processVerbs(elt, nodeData, triggerSpecs) {
|
||||
var explicitAction = false;
|
||||
forEach(VERBS, function (verb) {
|
||||
if (hasAttribute(elt,'hx-' + verb)) {
|
||||
var path = getAttributeValue(elt, 'hx-' + verb);
|
||||
explicitAction = true;
|
||||
nodeData.path = path;
|
||||
nodeData.verb = verb;
|
||||
triggerSpecs.forEach(function(triggerSpec) {
|
||||
addTriggerHandler(elt, triggerSpec, nodeData, function (elt, evt) {
|
||||
if (closest(elt, htmx.config.disableSelector)) {
|
||||
cleanUpElement(elt)
|
||||
return
|
||||
}
|
||||
issueAjaxRequest(verb, path, elt, evt)
|
||||
})
|
||||
});
|
||||
}
|
||||
});
|
||||
return explicitAction;
|
||||
}
|
||||
|
||||
function addTriggerHandler(elt, triggerSpec, nodeData, handler) {
|
||||
if (triggerSpec.sseEvent) {
|
||||
processSSETrigger(elt, handler, triggerSpec.sseEvent);
|
||||
} else if (triggerSpec.trigger === "revealed") {
|
||||
initScrollHandler();
|
||||
addEventListener(elt, handler, nodeData, triggerSpec);
|
||||
maybeReveal(elt);
|
||||
} else if (triggerSpec.trigger === "intersect") {
|
||||
var observerOptions = {};
|
||||
if (triggerSpec.root) {
|
||||
observerOptions.root = querySelectorExt(elt, triggerSpec.root)
|
||||
}
|
||||
if (triggerSpec.threshold) {
|
||||
observerOptions.threshold = parseFloat(triggerSpec.threshold);
|
||||
}
|
||||
var observer = new IntersectionObserver(function (entries) {
|
||||
for (var i = 0; i < entries.length; i++) {
|
||||
var entry = entries[i];
|
||||
if (entry.isIntersecting) {
|
||||
triggerEvent(elt, "intersect");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}, observerOptions);
|
||||
observer.observe(elt);
|
||||
addEventListener(elt, handler, nodeData, triggerSpec);
|
||||
} else if (triggerSpec.trigger === "load") {
|
||||
if (!maybeFilterEvent(triggerSpec, elt, makeEvent("load", {elt: elt}))) {
|
||||
loadImmediately(elt, handler, nodeData, triggerSpec.delay);
|
||||
}
|
||||
} else if (triggerSpec.pollInterval > 0) {
|
||||
nodeData.polling = true;
|
||||
processPolling(elt, handler, triggerSpec);
|
||||
} else {
|
||||
addEventListener(elt, handler, nodeData, triggerSpec);
|
||||
}
|
||||
}
|
||||
|
||||
function evalScript(script) {
|
||||
if (!script.htmxExecuted && htmx.config.allowScriptTags &&
|
||||
(script.type === "text/javascript" || script.type === "module" || script.type === "") ) {
|
||||
var newScript = getDocument().createElement("script");
|
||||
forEach(script.attributes, function (attr) {
|
||||
newScript.setAttribute(attr.name, attr.value);
|
||||
});
|
||||
newScript.textContent = script.textContent;
|
||||
newScript.async = false;
|
||||
if (htmx.config.inlineScriptNonce) {
|
||||
newScript.nonce = htmx.config.inlineScriptNonce;
|
||||
}
|
||||
var parent = script.parentElement;
|
||||
|
||||
try {
|
||||
parent.insertBefore(newScript, script);
|
||||
} catch (e) {
|
||||
logError(e);
|
||||
} finally {
|
||||
// remove old script element, but only if it is still in DOM
|
||||
if (script.parentElement) {
|
||||
script.parentElement.removeChild(script);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function processScripts(elt) {
|
||||
if (matches(elt, "script")) {
|
||||
evalScript(elt);
|
||||
}
|
||||
forEach(findAll(elt, "script"), function (script) {
|
||||
evalScript(script);
|
||||
});
|
||||
}
|
||||
|
||||
function shouldProcessHxOn(elt) {
|
||||
var attributes = elt.attributes
|
||||
for (var j = 0; j < attributes.length; j++) {
|
||||
var attrName = attributes[j].name
|
||||
if (startsWith(attrName, "hx-on:") || startsWith(attrName, "data-hx-on:") ||
|
||||
startsWith(attrName, "hx-on-") || startsWith(attrName, "data-hx-on-")) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
function findHxOnWildcardElements(elt) {
|
||||
var node = null
|
||||
var elements = []
|
||||
|
||||
if (shouldProcessHxOn(elt)) {
|
||||
elements.push(elt)
|
||||
}
|
||||
|
||||
if (document.evaluate) {
|
||||
var iter = document.evaluate('.//*[@*[ starts-with(name(), "hx-on:") or starts-with(name(), "data-hx-on:") or' +
|
||||
' starts-with(name(), "hx-on-") or starts-with(name(), "data-hx-on-") ]]', elt)
|
||||
while (node = iter.iterateNext()) elements.push(node)
|
||||
} else {
|
||||
var allElements = elt.getElementsByTagName("*")
|
||||
for (var i = 0; i < allElements.length; i++) {
|
||||
if (shouldProcessHxOn(allElements[i])) {
|
||||
elements.push(allElements[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return elements
|
||||
}
|
||||
|
||||
function findElementsToProcess(elt) {
|
||||
if (elt.querySelectorAll) {
|
||||
var boostedSelector = ", [hx-boost] a, [data-hx-boost] a, a[hx-boost], a[data-hx-boost]";
|
||||
var results = elt.querySelectorAll(VERB_SELECTOR + boostedSelector + ", form, [type='submit'], [hx-sse], [data-hx-sse], [hx-ws]," +
|
||||
" [data-hx-ws], [hx-ext], [data-hx-ext], [hx-trigger], [data-hx-trigger], [hx-on], [data-hx-on]");
|
||||
return results;
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
// Handle submit buttons/inputs that have the form attribute set
|
||||
// see https://developer.mozilla.org/docs/Web/HTML/Element/button
|
||||
function maybeSetLastButtonClicked(evt) {
|
||||
var elt = closest(evt.target, "button, input[type='submit']");
|
||||
var internalData = getRelatedFormData(evt)
|
||||
if (internalData) {
|
||||
internalData.lastButtonClicked = elt;
|
||||
}
|
||||
};
|
||||
function maybeUnsetLastButtonClicked(evt){
|
||||
var internalData = getRelatedFormData(evt)
|
||||
if (internalData) {
|
||||
internalData.lastButtonClicked = null;
|
||||
}
|
||||
}
|
||||
function getRelatedFormData(evt) {
|
||||
var elt = closest(evt.target, "button, input[type='submit']");
|
||||
if (!elt) {
|
||||
return;
|
||||
}
|
||||
var form = resolveTarget('#' + getRawAttribute(elt, 'form')) || closest(elt, 'form');
|
||||
if (!form) {
|
||||
return;
|
||||
}
|
||||
return getInternalData(form);
|
||||
}
|
||||
function initButtonTracking(elt) {
|
||||
// need to handle both click and focus in:
|
||||
// focusin - in case someone tabs in to a button and hits the space bar
|
||||
// click - on OSX buttons do not focus on click see https://bugs.webkit.org/show_bug.cgi?id=13724
|
||||
elt.addEventListener('click', maybeSetLastButtonClicked)
|
||||
elt.addEventListener('focusin', maybeSetLastButtonClicked)
|
||||
elt.addEventListener('focusout', maybeUnsetLastButtonClicked)
|
||||
}
|
||||
|
||||
function countCurlies(line) {
|
||||
var tokens = tokenizeString(line);
|
||||
var netCurlies = 0;
|
||||
for (var i = 0; i < tokens.length; i++) {
|
||||
const token = tokens[i];
|
||||
if (token === "{") {
|
||||
netCurlies++;
|
||||
} else if (token === "}") {
|
||||
netCurlies--;
|
||||
}
|
||||
}
|
||||
return netCurlies;
|
||||
}
|
||||
|
||||
function addHxOnEventHandler(elt, eventName, code) {
|
||||
var nodeData = getInternalData(elt);
|
||||
if (!Array.isArray(nodeData.onHandlers)) {
|
||||
nodeData.onHandlers = [];
|
||||
}
|
||||
var func;
|
||||
var listener = function (e) {
|
||||
return maybeEval(elt, function() {
|
||||
if (!func) {
|
||||
func = new Function("event", code);
|
||||
}
|
||||
func.call(elt, e);
|
||||
});
|
||||
};
|
||||
elt.addEventListener(eventName, listener);
|
||||
nodeData.onHandlers.push({event:eventName, listener:listener});
|
||||
}
|
||||
|
||||
function processHxOn(elt) {
|
||||
var hxOnValue = getAttributeValue(elt, 'hx-on');
|
||||
if (hxOnValue) {
|
||||
var handlers = {}
|
||||
var lines = hxOnValue.split("\n");
|
||||
var currentEvent = null;
|
||||
var curlyCount = 0;
|
||||
while (lines.length > 0) {
|
||||
var line = lines.shift();
|
||||
var match = line.match(/^\s*([a-zA-Z:\-\.]+:)(.*)/);
|
||||
if (curlyCount === 0 && match) {
|
||||
line.split(":")
|
||||
currentEvent = match[1].slice(0, -1); // strip last colon
|
||||
handlers[currentEvent] = match[2];
|
||||
} else {
|
||||
handlers[currentEvent] += line;
|
||||
}
|
||||
curlyCount += countCurlies(line);
|
||||
}
|
||||
|
||||
for (var eventName in handlers) {
|
||||
addHxOnEventHandler(elt, eventName, handlers[eventName]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function processHxOnWildcard(elt) {
|
||||
// wipe any previous on handlers so that this function takes precedence
|
||||
deInitOnHandlers(elt)
|
||||
|
||||
for (var i = 0; i < elt.attributes.length; i++) {
|
||||
var name = elt.attributes[i].name
|
||||
var value = elt.attributes[i].value
|
||||
if (startsWith(name, "hx-on") || startsWith(name, "data-hx-on")) {
|
||||
var afterOnPosition = name.indexOf("-on") + 3;
|
||||
var nextChar = name.slice(afterOnPosition, afterOnPosition + 1);
|
||||
if (nextChar === "-" || nextChar === ":") {
|
||||
var eventName = name.slice(afterOnPosition + 1);
|
||||
// if the eventName starts with a colon or dash, prepend "htmx" for shorthand support
|
||||
if (startsWith(eventName, ":")) {
|
||||
eventName = "htmx" + eventName
|
||||
} else if (startsWith(eventName, "-")) {
|
||||
eventName = "htmx:" + eventName.slice(1);
|
||||
} else if (startsWith(eventName, "htmx-")) {
|
||||
eventName = "htmx:" + eventName.slice(5);
|
||||
}
|
||||
|
||||
addHxOnEventHandler(elt, eventName, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function initNode(elt) {
|
||||
if (closest(elt, htmx.config.disableSelector)) {
|
||||
cleanUpElement(elt)
|
||||
return;
|
||||
}
|
||||
var nodeData = getInternalData(elt);
|
||||
if (nodeData.initHash !== attributeHash(elt)) {
|
||||
// clean up any previously processed info
|
||||
deInitNode(elt);
|
||||
|
||||
nodeData.initHash = attributeHash(elt);
|
||||
|
||||
processHxOn(elt);
|
||||
|
||||
triggerEvent(elt, "htmx:beforeProcessNode")
|
||||
|
||||
if (elt.value) {
|
||||
nodeData.lastValue = elt.value;
|
||||
}
|
||||
|
||||
var triggerSpecs = getTriggerSpecs(elt);
|
||||
var hasExplicitHttpAction = processVerbs(elt, nodeData, triggerSpecs);
|
||||
|
||||
if (!hasExplicitHttpAction) {
|
||||
if (getClosestAttributeValue(elt, "hx-boost") === "true") {
|
||||
boostElement(elt, nodeData, triggerSpecs);
|
||||
} else if (hasAttribute(elt, 'hx-trigger')) {
|
||||
triggerSpecs.forEach(function (triggerSpec) {
|
||||
// For "naked" triggers, don't do anything at all
|
||||
addTriggerHandler(elt, triggerSpec, nodeData, function () {
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Handle submit buttons/inputs that have the form attribute set
|
||||
// see https://developer.mozilla.org/docs/Web/HTML/Element/button
|
||||
if (elt.tagName === "FORM" || (getRawAttribute(elt, "type") === "submit" && hasAttribute(elt, "form"))) {
|
||||
initButtonTracking(elt)
|
||||
}
|
||||
|
||||
var sseInfo = getAttributeValue(elt, 'hx-sse');
|
||||
if (sseInfo) {
|
||||
processSSEInfo(elt, nodeData, sseInfo);
|
||||
}
|
||||
|
||||
var wsInfo = getAttributeValue(elt, 'hx-ws');
|
||||
if (wsInfo) {
|
||||
processWebSocketInfo(elt, nodeData, wsInfo);
|
||||
}
|
||||
triggerEvent(elt, "htmx:afterProcessNode");
|
||||
}
|
||||
}
|
||||
|
||||
function processNode(elt) {
|
||||
elt = resolveTarget(elt);
|
||||
if (closest(elt, htmx.config.disableSelector)) {
|
||||
cleanUpElement(elt)
|
||||
return;
|
||||
}
|
||||
initNode(elt);
|
||||
forEach(findElementsToProcess(elt), function(child) { initNode(child) });
|
||||
// Because it happens second, the new way of adding onHandlers superseeds the old one
|
||||
// i.e. if there are any hx-on:eventName attributes, the hx-on attribute will be ignored
|
||||
forEach(findHxOnWildcardElements(elt), processHxOnWildcard);
|
||||
}
|
||||
|
||||
//====================================================================
|
||||
// Event/Log Support
|
||||
//====================================================================
|
||||
|
||||
function kebabEventName(str) {
|
||||
return str.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
|
||||
}
|
||||
|
||||
function makeEvent(eventName, detail) {
|
||||
var evt;
|
||||
if (window.CustomEvent && typeof window.CustomEvent === 'function') {
|
||||
evt = new CustomEvent(eventName, {bubbles: true, cancelable: true, detail: detail});
|
||||
} else {
|
||||
evt = getDocument().createEvent('CustomEvent');
|
||||
evt.initCustomEvent(eventName, true, true, detail);
|
||||
}
|
||||
return evt;
|
||||
}
|
||||
|
||||
function triggerErrorEvent(elt, eventName, detail) {
|
||||
triggerEvent(elt, eventName, mergeObjects({error:eventName}, detail));
|
||||
}
|
||||
|
||||
function ignoreEventForLogging(eventName) {
|
||||
return eventName === "htmx:afterProcessNode"
|
||||
}
|
||||
|
||||
/**
|
||||
* `withExtensions` locates all active extensions for a provided element, then
|
||||
* executes the provided function using each of the active extensions. It should
|
||||
* be called internally at every extendable execution point in htmx.
|
||||
*
|
||||
* @param {HTMLElement} elt
|
||||
* @param {(extension:import("./htmx").HtmxExtension) => void} toDo
|
||||
* @returns void
|
||||
*/
|
||||
function withExtensions(elt, toDo) {
|
||||
forEach(getExtensions(elt), function(extension){
|
||||
try {
|
||||
toDo(extension);
|
||||
} catch (e) {
|
||||
logError(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function logError(msg) {
|
||||
if(console.error) {
|
||||
console.error(msg);
|
||||
} else if (console.log) {
|
||||
console.log("ERROR: ", msg);
|
||||
}
|
||||
}
|
||||
|
||||
function triggerEvent(elt, eventName, detail) {
|
||||
elt = resolveTarget(elt);
|
||||
if (detail == null) {
|
||||
detail = {};
|
||||
}
|
||||
detail["elt"] = elt;
|
||||
var event = makeEvent(eventName, detail);
|
||||
if (htmx.logger && !ignoreEventForLogging(eventName)) {
|
||||
htmx.logger(elt, eventName, detail);
|
||||
}
|
||||
if (detail.error) {
|
||||
logError(detail.error);
|
||||
triggerEvent(elt, "htmx:error", {errorInfo:detail})
|
||||
}
|
||||
var eventResult = elt.dispatchEvent(event);
|
||||
var kebabName = kebabEventName(eventName);
|
||||
if (eventResult && kebabName !== eventName) {
|
||||
var kebabedEvent = makeEvent(kebabName, event.detail);
|
||||
eventResult = eventResult && elt.dispatchEvent(kebabedEvent)
|
||||
}
|
||||
withExtensions(elt, function (extension) {
|
||||
eventResult = eventResult && (extension.onEvent(eventName, event) !== false && !event.defaultPrevented)
|
||||
});
|
||||
return eventResult;
|
||||
}
|
||||
|
||||
//====================================================================
|
||||
// History Support
|
||||
//====================================================================
|
||||
var currentPathForHistory = location.pathname+location.search;
|
||||
|
||||
function getHistoryElement() {
|
||||
var historyElt = getDocument().querySelector('[hx-history-elt],[data-hx-history-elt]');
|
||||
return historyElt || getDocument().body;
|
||||
}
|
||||
|
||||
function saveToHistoryCache(url, content, title, scroll) {
|
||||
if (!canAccessLocalStorage()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (htmx.config.historyCacheSize <= 0) {
|
||||
// make sure that an eventually already existing cache is purged
|
||||
localStorage.removeItem("htmx-history-cache");
|
||||
return;
|
||||
}
|
||||
|
||||
url = normalizePath(url);
|
||||
|
||||
var historyCache = parseJSON(localStorage.getItem("htmx-history-cache")) || [];
|
||||
for (var i = 0; i < historyCache.length; i++) {
|
||||
if (historyCache[i].url === url) {
|
||||
historyCache.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
var newHistoryItem = {url:url, content: content, title:title, scroll:scroll};
|
||||
triggerEvent(getDocument().body, "htmx:historyItemCreated", {item:newHistoryItem, cache: historyCache})
|
||||
historyCache.push(newHistoryItem)
|
||||
while (historyCache.length > htmx.config.historyCacheSize) {
|
||||
historyCache.shift();
|
||||
}
|
||||
while(historyCache.length > 0){
|
||||
try {
|
||||
localStorage.setItem("htmx-history-cache", JSON.stringify(historyCache));
|
||||
break;
|
||||
} catch (e) {
|
||||
triggerErrorEvent(getDocument().body, "htmx:historyCacheError", {cause:e, cache: historyCache})
|
||||
historyCache.shift(); // shrink the cache and retry
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getCachedHistory(url) {
|
||||
if (!canAccessLocalStorage()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
url = normalizePath(url);
|
||||
|
||||
var historyCache = parseJSON(localStorage.getItem("htmx-history-cache")) || [];
|
||||
for (var i = 0; i < historyCache.length; i++) {
|
||||
if (historyCache[i].url === url) {
|
||||
return historyCache[i];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function cleanInnerHtmlForHistory(elt) {
|
||||
var className = htmx.config.requestClass;
|
||||
var clone = elt.cloneNode(true);
|
||||
forEach(findAll(clone, "." + className), function(child){
|
||||
removeClassFromElement(child, className);
|
||||
});
|
||||
return clone.innerHTML;
|
||||
}
|
||||
|
||||
function saveCurrentPageToHistory() {
|
||||
var elt = getHistoryElement();
|
||||
var path = currentPathForHistory || location.pathname+location.search;
|
||||
|
||||
// Allow history snapshot feature to be disabled where hx-history="false"
|
||||
// is present *anywhere* in the current document we're about to save,
|
||||
// so we can prevent privileged data entering the cache.
|
||||
// The page will still be reachable as a history entry, but htmx will fetch it
|
||||
// live from the server onpopstate rather than look in the localStorage cache
|
||||
var disableHistoryCache
|
||||
try {
|
||||
disableHistoryCache = getDocument().querySelector('[hx-history="false" i],[data-hx-history="false" i]')
|
||||
} catch (e) {
|
||||
// IE11: insensitive modifier not supported so fallback to case sensitive selector
|
||||
disableHistoryCache = getDocument().querySelector('[hx-history="false"],[data-hx-history="false"]')
|
||||
}
|
||||
if (!disableHistoryCache) {
|
||||
triggerEvent(getDocument().body, "htmx:beforeHistorySave", {path: path, historyElt: elt});
|
||||
saveToHistoryCache(path, cleanInnerHtmlForHistory(elt), getDocument().title, window.scrollY);
|
||||
}
|
||||
|
||||
if (htmx.config.historyEnabled) history.replaceState({htmx: true}, getDocument().title, window.location.href);
|
||||
}
|
||||
|
||||
function pushUrlIntoHistory(path) {
|
||||
// remove the cache buster parameter, if any
|
||||
if (htmx.config.getCacheBusterParam) {
|
||||
path = path.replace(/org\.htmx\.cache-buster=[^&]*&?/, '')
|
||||
if (endsWith(path, '&') || endsWith(path, "?")) {
|
||||
path = path.slice(0, -1);
|
||||
}
|
||||
}
|
||||
if(htmx.config.historyEnabled) {
|
||||
history.pushState({htmx:true}, "", path);
|
||||
}
|
||||
currentPathForHistory = path;
|
||||
}
|
||||
|
||||
function replaceUrlInHistory(path) {
|
||||
if(htmx.config.historyEnabled) history.replaceState({htmx:true}, "", path);
|
||||
currentPathForHistory = path;
|
||||
}
|
||||
|
||||
function settleImmediately(tasks) {
|
||||
forEach(tasks, function (task) {
|
||||
task.call();
|
||||
});
|
||||
}
|
||||
|
||||
function loadHistoryFromServer(path) {
|
||||
var request = new XMLHttpRequest();
|
||||
var details = {path: path, xhr:request};
|
||||
triggerEvent(getDocument().body, "htmx:historyCacheMiss", details);
|
||||
request.open('GET', path, true);
|
||||
request.setRequestHeader("HX-Request", "true");
|
||||
request.setRequestHeader("HX-History-Restore-Request", "true");
|
||||
request.setRequestHeader("HX-Current-URL", getDocument().location.href);
|
||||
request.onload = function () {
|
||||
if (this.status >= 200 && this.status < 400) {
|
||||
triggerEvent(getDocument().body, "htmx:historyCacheMissLoad", details);
|
||||
var fragment = makeFragment(this.response);
|
||||
// @ts-ignore
|
||||
fragment = fragment.querySelector('[hx-history-elt],[data-hx-history-elt]') || fragment;
|
||||
var historyElement = getHistoryElement();
|
||||
var settleInfo = makeSettleInfo(historyElement);
|
||||
var title = findTitle(this.response);
|
||||
if (title) {
|
||||
var titleElt = find("title");
|
||||
if (titleElt) {
|
||||
titleElt.innerHTML = title;
|
||||
} else {
|
||||
window.document.title = title;
|
||||
}
|
||||
}
|
||||
// @ts-ignore
|
||||
swapInnerHTML(historyElement, fragment, settleInfo)
|
||||
settleImmediately(settleInfo.tasks);
|
||||
currentPathForHistory = path;
|
||||
triggerEvent(getDocument().body, "htmx:historyRestore", {path: path, cacheMiss:true, serverResponse:this.response});
|
||||
} else {
|
||||
triggerErrorEvent(getDocument().body, "htmx:historyCacheMissLoadError", details);
|
||||
}
|
||||
};
|
||||
request.send();
|
||||
}
|
||||
|
||||
function restoreHistory(path) {
|
||||
saveCurrentPageToHistory();
|
||||
path = path || location.pathname+location.search;
|
||||
var cached = getCachedHistory(path);
|
||||
if (cached) {
|
||||
var fragment = makeFragment(cached.content);
|
||||
var historyElement = getHistoryElement();
|
||||
var settleInfo = makeSettleInfo(historyElement);
|
||||
swapInnerHTML(historyElement, fragment, settleInfo)
|
||||
settleImmediately(settleInfo.tasks);
|
||||
document.title = cached.title;
|
||||
setTimeout(function () {
|
||||
window.scrollTo(0, cached.scroll);
|
||||
}, 0); // next 'tick', so browser has time to render layout
|
||||
currentPathForHistory = path;
|
||||
triggerEvent(getDocument().body, "htmx:historyRestore", {path:path, item:cached});
|
||||
} else {
|
||||
if (htmx.config.refreshOnHistoryMiss) {
|
||||
|
||||
// @ts-ignore: optional parameter in reload() function throws error
|
||||
window.location.reload(true);
|
||||
} else {
|
||||
loadHistoryFromServer(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function addRequestIndicatorClasses(elt) {
|
||||
var indicators = findAttributeTargets(elt, 'hx-indicator');
|
||||
if (indicators == null) {
|
||||
indicators = [elt];
|
||||
}
|
||||
forEach(indicators, function (ic) {
|
||||
var internalData = getInternalData(ic);
|
||||
internalData.requestCount = (internalData.requestCount || 0) + 1;
|
||||
ic.classList["add"].call(ic.classList, htmx.config.requestClass);
|
||||
});
|
||||
return indicators;
|
||||
}
|
||||
|
||||
function disableElements(elt) {
|
||||
var disabledElts = findAttributeTargets(elt, 'hx-disabled-elt');
|
||||
if (disabledElts == null) {
|
||||
disabledElts = [];
|
||||
}
|
||||
forEach(disabledElts, function (disabledElement) {
|
||||
var internalData = getInternalData(disabledElement);
|
||||
internalData.requestCount = (internalData.requestCount || 0) + 1;
|
||||
disabledElement.setAttribute("disabled", "");
|
||||
});
|
||||
return disabledElts;
|
||||
}
|
||||
|
||||
function removeRequestIndicators(indicators, disabled) {
|
||||
forEach(indicators, function (ic) {
|
||||
var internalData = getInternalData(ic);
|
||||
internalData.requestCount = (internalData.requestCount || 0) - 1;
|
||||
if (internalData.requestCount === 0) {
|
||||
ic.classList["remove"].call(ic.classList, htmx.config.requestClass);
|
||||
}
|
||||
});
|
||||
forEach(disabled, function (disabledElement) {
|
||||
var internalData = getInternalData(disabledElement);
|
||||
internalData.requestCount = (internalData.requestCount || 0) - 1;
|
||||
if (internalData.requestCount === 0) {
|
||||
disabledElement.removeAttribute('disabled');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//====================================================================
|
||||
// Input Value Processing
|
||||
//====================================================================
|
||||
|
||||
function haveSeenNode(processed, elt) {
|
||||
for (var i = 0; i < processed.length; i++) {
|
||||
var node = processed[i];
|
||||
if (node.isSameNode(elt)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function shouldInclude(elt) {
|
||||
if(elt.name === "" || elt.name == null || elt.disabled || closest(elt, "fieldset[disabled]")) {
|
||||
return false;
|
||||
}
|
||||
// ignore "submitter" types (see jQuery src/serialize.js)
|
||||
if (elt.type === "button" || elt.type === "submit" || elt.tagName === "image" || elt.tagName === "reset" || elt.tagName === "file" ) {
|
||||
return false;
|
||||
}
|
||||
if (elt.type === "checkbox" || elt.type === "radio" ) {
|
||||
return elt.checked;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function addValueToValues(name, value, values) {
|
||||
// This is a little ugly because both the current value of the named value in the form
|
||||
// and the new value could be arrays, so we have to handle all four cases :/
|
||||
if (name != null && value != null) {
|
||||
var current = values[name];
|
||||
if (current === undefined) {
|
||||
values[name] = value;
|
||||
} else if (Array.isArray(current)) {
|
||||
if (Array.isArray(value)) {
|
||||
values[name] = current.concat(value);
|
||||
} else {
|
||||
current.push(value);
|
||||
}
|
||||
} else {
|
||||
if (Array.isArray(value)) {
|
||||
values[name] = [current].concat(value);
|
||||
} else {
|
||||
values[name] = [current, value];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function processInputValue(processed, values, errors, elt, validate) {
|
||||
if (elt == null || haveSeenNode(processed, elt)) {
|
||||
return;
|
||||
} else {
|
||||
processed.push(elt);
|
||||
}
|
||||
if (shouldInclude(elt)) {
|
||||
var name = getRawAttribute(elt,"name");
|
||||
var value = elt.value;
|
||||
if (elt.multiple && elt.tagName === "SELECT") {
|
||||
value = toArray(elt.querySelectorAll("option:checked")).map(function (e) { return e.value });
|
||||
}
|
||||
// include file inputs
|
||||
if (elt.files) {
|
||||
value = toArray(elt.files);
|
||||
}
|
||||
addValueToValues(name, value, values);
|
||||
if (validate) {
|
||||
validateElement(elt, errors);
|
||||
}
|
||||
}
|
||||
if (matches(elt, 'form')) {
|
||||
var inputs = elt.elements;
|
||||
forEach(inputs, function(input) {
|
||||
processInputValue(processed, values, errors, input, validate);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function validateElement(element, errors) {
|
||||
if (element.willValidate) {
|
||||
triggerEvent(element, "htmx:validation:validate")
|
||||
if (!element.checkValidity()) {
|
||||
errors.push({elt: element, message:element.validationMessage, validity:element.validity});
|
||||
triggerEvent(element, "htmx:validation:failed", {message:element.validationMessage, validity:element.validity})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} elt
|
||||
* @param {string} verb
|
||||
*/
|
||||
function getInputValues(elt, verb) {
|
||||
var processed = [];
|
||||
var values = {};
|
||||
var formValues = {};
|
||||
var errors = [];
|
||||
var internalData = getInternalData(elt);
|
||||
if (internalData.lastButtonClicked && !bodyContains(internalData.lastButtonClicked)) {
|
||||
internalData.lastButtonClicked = null
|
||||
}
|
||||
|
||||
// only validate when form is directly submitted and novalidate or formnovalidate are not set
|
||||
// or if the element has an explicit hx-validate="true" on it
|
||||
var validate = (matches(elt, 'form') && elt.noValidate !== true) || getAttributeValue(elt, "hx-validate") === "true";
|
||||
if (internalData.lastButtonClicked) {
|
||||
validate = validate && internalData.lastButtonClicked.formNoValidate !== true;
|
||||
}
|
||||
|
||||
// for a non-GET include the closest form
|
||||
if (verb !== 'get') {
|
||||
processInputValue(processed, formValues, errors, closest(elt, 'form'), validate);
|
||||
}
|
||||
|
||||
// include the element itself
|
||||
processInputValue(processed, values, errors, elt, validate);
|
||||
|
||||
// if a button or submit was clicked last, include its value
|
||||
if (internalData.lastButtonClicked || elt.tagName === "BUTTON" ||
|
||||
(elt.tagName === "INPUT" && getRawAttribute(elt, "type") === "submit")) {
|
||||
var button = internalData.lastButtonClicked || elt
|
||||
var name = getRawAttribute(button, "name")
|
||||
addValueToValues(name, button.value, formValues)
|
||||
}
|
||||
|
||||
// include any explicit includes
|
||||
var includes = findAttributeTargets(elt, "hx-include");
|
||||
forEach(includes, function(node) {
|
||||
processInputValue(processed, values, errors, node, validate);
|
||||
// if a non-form is included, include any input values within it
|
||||
if (!matches(node, 'form')) {
|
||||
forEach(node.querySelectorAll(INPUT_SELECTOR), function (descendant) {
|
||||
processInputValue(processed, values, errors, descendant, validate);
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
// form values take precedence, overriding the regular values
|
||||
values = mergeObjects(values, formValues);
|
||||
|
||||
return {errors:errors, values:values};
|
||||
}
|
||||
|
||||
function appendParam(returnStr, name, realValue) {
|
||||
if (returnStr !== "") {
|
||||
returnStr += "&";
|
||||
}
|
||||
if (String(realValue) === "[object Object]") {
|
||||
realValue = JSON.stringify(realValue);
|
||||
}
|
||||
var s = encodeURIComponent(realValue);
|
||||
returnStr += encodeURIComponent(name) + "=" + s;
|
||||
return returnStr;
|
||||
}
|
||||
|
||||
function urlEncode(values) {
|
||||
var returnStr = "";
|
||||
for (var name in values) {
|
||||
if (values.hasOwnProperty(name)) {
|
||||
var value = values[name];
|
||||
if (Array.isArray(value)) {
|
||||
forEach(value, function(v) {
|
||||
returnStr = appendParam(returnStr, name, v);
|
||||
});
|
||||
} else {
|
||||
returnStr = appendParam(returnStr, name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnStr;
|
||||
}
|
||||
|
||||
function makeFormData(values) {
|
||||
var formData = new FormData();
|
||||
for (var name in values) {
|
||||
if (values.hasOwnProperty(name)) {
|
||||
var value = values[name];
|
||||
if (Array.isArray(value)) {
|
||||
forEach(value, function(v) {
|
||||
formData.append(name, v);
|
||||
});
|
||||
} else {
|
||||
formData.append(name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return formData;
|
||||
}
|
||||
|
||||
//====================================================================
|
||||
// Ajax
|
||||
//====================================================================
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} elt
|
||||
* @param {HTMLElement} target
|
||||
* @param {string} prompt
|
||||
* @returns {Object} // TODO: Define/Improve HtmxHeaderSpecification
|
||||
*/
|
||||
function getHeaders(elt, target, prompt) {
|
||||
var headers = {
|
||||
"HX-Request" : "true",
|
||||
"HX-Trigger" : getRawAttribute(elt, "id"),
|
||||
"HX-Trigger-Name" : getRawAttribute(elt, "name"),
|
||||
"HX-Target" : getAttributeValue(target, "id"),
|
||||
"HX-Current-URL" : getDocument().location.href,
|
||||
}
|
||||
getValuesForElement(elt, "hx-headers", false, headers)
|
||||
if (prompt !== undefined) {
|
||||
headers["HX-Prompt"] = prompt;
|
||||
}
|
||||
if (getInternalData(elt).boosted) {
|
||||
headers["HX-Boosted"] = "true";
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* filterValues takes an object containing form input values
|
||||
* and returns a new object that only contains keys that are
|
||||
* specified by the closest "hx-params" attribute
|
||||
* @param {Object} inputValues
|
||||
* @param {HTMLElement} elt
|
||||
* @returns {Object}
|
||||
*/
|
||||
function filterValues(inputValues, elt) {
|
||||
var paramsValue = getClosestAttributeValue(elt, "hx-params");
|
||||
if (paramsValue) {
|
||||
if (paramsValue === "none") {
|
||||
return {};
|
||||
} else if (paramsValue === "*") {
|
||||
return inputValues;
|
||||
} else if(paramsValue.indexOf("not ") === 0) {
|
||||
forEach(paramsValue.substr(4).split(","), function (name) {
|
||||
name = name.trim();
|
||||
delete inputValues[name];
|
||||
});
|
||||
return inputValues;
|
||||
} else {
|
||||
var newValues = {}
|
||||
forEach(paramsValue.split(","), function (name) {
|
||||
name = name.trim();
|
||||
newValues[name] = inputValues[name];
|
||||
});
|
||||
return newValues;
|
||||
}
|
||||
} else {
|
||||
return inputValues;
|
||||
}
|
||||
}
|
||||
|
||||
function isAnchorLink(elt) {
|
||||
return getRawAttribute(elt, 'href') && getRawAttribute(elt, 'href').indexOf("#") >=0
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {HTMLElement} elt
|
||||
* @param {string} swapInfoOverride
|
||||
* @returns {import("./htmx").HtmxSwapSpecification}
|
||||
*/
|
||||
function getSwapSpecification(elt, swapInfoOverride) {
|
||||
var swapInfo = swapInfoOverride ? swapInfoOverride : getClosestAttributeValue(elt, "hx-swap");
|
||||
var swapSpec = {
|
||||
"swapStyle" : getInternalData(elt).boosted ? 'innerHTML' : htmx.config.defaultSwapStyle,
|
||||
"swapDelay" : htmx.config.defaultSwapDelay,
|
||||
"settleDelay" : htmx.config.defaultSettleDelay
|
||||
}
|
||||
if (htmx.config.scrollIntoViewOnBoost && getInternalData(elt).boosted && !isAnchorLink(elt)) {
|
||||
swapSpec["show"] = "top"
|
||||
}
|
||||
if (swapInfo) {
|
||||
var split = splitOnWhitespace(swapInfo);
|
||||
if (split.length > 0) {
|
||||
for (var i = 0; i < split.length; i++) {
|
||||
var value = split[i];
|
||||
if (value.indexOf("swap:") === 0) {
|
||||
swapSpec["swapDelay"] = parseInterval(value.substr(5));
|
||||
} else if (value.indexOf("settle:") === 0) {
|
||||
swapSpec["settleDelay"] = parseInterval(value.substr(7));
|
||||
} else if (value.indexOf("transition:") === 0) {
|
||||
swapSpec["transition"] = value.substr(11) === "true";
|
||||
} else if (value.indexOf("ignoreTitle:") === 0) {
|
||||
swapSpec["ignoreTitle"] = value.substr(12) === "true";
|
||||
} else if (value.indexOf("scroll:") === 0) {
|
||||
var scrollSpec = value.substr(7);
|
||||
var splitSpec = scrollSpec.split(":");
|
||||
var scrollVal = splitSpec.pop();
|
||||
var selectorVal = splitSpec.length > 0 ? splitSpec.join(":") : null;
|
||||
swapSpec["scroll"] = scrollVal;
|
||||
swapSpec["scrollTarget"] = selectorVal;
|
||||
} else if (value.indexOf("show:") === 0) {
|
||||
var showSpec = value.substr(5);
|
||||
var splitSpec = showSpec.split(":");
|
||||
var showVal = splitSpec.pop();
|
||||
var selectorVal = splitSpec.length > 0 ? splitSpec.join(":") : null;
|
||||
swapSpec["show"] = showVal;
|
||||
swapSpec["showTarget"] = selectorVal;
|
||||
} else if (value.indexOf("focus-scroll:") === 0) {
|
||||
var focusScrollVal = value.substr("focus-scroll:".length);
|
||||
swapSpec["focusScroll"] = focusScrollVal == "true";
|
||||
} else if (i == 0) {
|
||||
swapSpec["swapStyle"] = value;
|
||||
} else {
|
||||
logError('Unknown modifier in hx-swap: ' + value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return swapSpec;
|
||||
}
|
||||
|
||||
function usesFormData(elt) {
|
||||
return getClosestAttributeValue(elt, "hx-encoding") === "multipart/form-data" ||
|
||||
(matches(elt, "form") && getRawAttribute(elt, 'enctype') === "multipart/form-data");
|
||||
}
|
||||
|
||||
function encodeParamsForBody(xhr, elt, filteredParameters) {
|
||||
var encodedParameters = null;
|
||||
withExtensions(elt, function (extension) {
|
||||
if (encodedParameters == null) {
|
||||
encodedParameters = extension.encodeParameters(xhr, filteredParameters, elt);
|
||||
}
|
||||
});
|
||||
if (encodedParameters != null) {
|
||||
return encodedParameters;
|
||||
} else {
|
||||
if (usesFormData(elt)) {
|
||||
return makeFormData(filteredParameters);
|
||||
} else {
|
||||
return urlEncode(filteredParameters);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Element} target
|
||||
* @returns {import("./htmx").HtmxSettleInfo}
|
||||
*/
|
||||
function makeSettleInfo(target) {
|
||||
return {tasks: [], elts: [target]};
|
||||
}
|
||||
|
||||
function updateScrollState(content, swapSpec) {
|
||||
var first = content[0];
|
||||
var last = content[content.length - 1];
|
||||
if (swapSpec.scroll) {
|
||||
var target = null;
|
||||
if (swapSpec.scrollTarget) {
|
||||
target = querySelectorExt(first, swapSpec.scrollTarget);
|
||||
}
|
||||
if (swapSpec.scroll === "top" && (first || target)) {
|
||||
target = target || first;
|
||||
target.scrollTop = 0;
|
||||
}
|
||||
if (swapSpec.scroll === "bottom" && (last || target)) {
|
||||
target = target || last;
|
||||
target.scrollTop = target.scrollHeight;
|
||||
}
|
||||
}
|
||||
if (swapSpec.show) {
|
||||
var target = null;
|
||||
if (swapSpec.showTarget) {
|
||||
var targetStr = swapSpec.showTarget;
|
||||
if (swapSpec.showTarget === "window") {
|
||||
targetStr = "body";
|
||||
}
|
||||
target = querySelectorExt(first, targetStr);
|
||||
}
|
||||
if (swapSpec.show === "top" && (first || target)) {
|
||||
target = target || first;
|
||||
target.scrollIntoView({block:'start', behavior: htmx.config.scrollBehavior});
|
||||
}
|
||||
if (swapSpec.show === "bottom" && (last || target)) {
|
||||
target = target || last;
|
||||
target.scrollIntoView({block:'end', behavior: htmx.config.scrollBehavior});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} elt
|
||||
* @param {string} attr
|
||||
* @param {boolean=} evalAsDefault
|
||||
* @param {Object=} values
|
||||
* @returns {Object}
|
||||
*/
|
||||
function getValuesForElement(elt, attr, evalAsDefault, values) {
|
||||
if (values == null) {
|
||||
values = {};
|
||||
}
|
||||
if (elt == null) {
|
||||
return values;
|
||||
}
|
||||
var attributeValue = getAttributeValue(elt, attr);
|
||||
if (attributeValue) {
|
||||
var str = attributeValue.trim();
|
||||
var evaluateValue = evalAsDefault;
|
||||
if (str === "unset") {
|
||||
return null;
|
||||
}
|
||||
if (str.indexOf("javascript:") === 0) {
|
||||
str = str.substr(11);
|
||||
evaluateValue = true;
|
||||
} else if (str.indexOf("js:") === 0) {
|
||||
str = str.substr(3);
|
||||
evaluateValue = true;
|
||||
}
|
||||
if (str.indexOf('{') !== 0) {
|
||||
str = "{" + str + "}";
|
||||
}
|
||||
var varsValues;
|
||||
if (evaluateValue) {
|
||||
varsValues = maybeEval(elt,function () {return Function("return (" + str + ")")();}, {});
|
||||
} else {
|
||||
varsValues = parseJSON(str);
|
||||
}
|
||||
for (var key in varsValues) {
|
||||
if (varsValues.hasOwnProperty(key)) {
|
||||
if (values[key] == null) {
|
||||
values[key] = varsValues[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return getValuesForElement(parentElt(elt), attr, evalAsDefault, values);
|
||||
}
|
||||
|
||||
function maybeEval(elt, toEval, defaultVal) {
|
||||
if (htmx.config.allowEval) {
|
||||
return toEval();
|
||||
} else {
|
||||
triggerErrorEvent(elt, 'htmx:evalDisallowedError');
|
||||
return defaultVal;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} elt
|
||||
* @param {*} expressionVars
|
||||
* @returns
|
||||
*/
|
||||
function getHXVarsForElement(elt, expressionVars) {
|
||||
return getValuesForElement(elt, "hx-vars", true, expressionVars);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} elt
|
||||
* @param {*} expressionVars
|
||||
* @returns
|
||||
*/
|
||||
function getHXValsForElement(elt, expressionVars) {
|
||||
return getValuesForElement(elt, "hx-vals", false, expressionVars);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} elt
|
||||
* @returns {Object}
|
||||
*/
|
||||
function getExpressionVars(elt) {
|
||||
return mergeObjects(getHXVarsForElement(elt), getHXValsForElement(elt));
|
||||
}
|
||||
|
||||
function safelySetHeaderValue(xhr, header, headerValue) {
|
||||
if (headerValue !== null) {
|
||||
try {
|
||||
xhr.setRequestHeader(header, headerValue);
|
||||
} catch (e) {
|
||||
// On an exception, try to set the header URI encoded instead
|
||||
xhr.setRequestHeader(header, encodeURIComponent(headerValue));
|
||||
xhr.setRequestHeader(header + "-URI-AutoEncoded", "true");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getPathFromResponse(xhr) {
|
||||
// NB: IE11 does not support this stuff
|
||||
if (xhr.responseURL && typeof(URL) !== "undefined") {
|
||||
try {
|
||||
var url = new URL(xhr.responseURL);
|
||||
return url.pathname + url.search;
|
||||
} catch (e) {
|
||||
triggerErrorEvent(getDocument().body, "htmx:badResponseUrl", {url: xhr.responseURL});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function hasHeader(xhr, regexp) {
|
||||
return regexp.test(xhr.getAllResponseHeaders())
|
||||
}
|
||||
|
||||
function ajaxHelper(verb, path, context) {
|
||||
verb = verb.toLowerCase();
|
||||
if (context) {
|
||||
if (context instanceof Element || isType(context, 'String')) {
|
||||
return issueAjaxRequest(verb, path, null, null, {
|
||||
targetOverride: resolveTarget(context),
|
||||
returnPromise: true
|
||||
});
|
||||
} else {
|
||||
return issueAjaxRequest(verb, path, resolveTarget(context.source), context.event,
|
||||
{
|
||||
handler : context.handler,
|
||||
headers : context.headers,
|
||||
values : context.values,
|
||||
targetOverride: resolveTarget(context.target),
|
||||
swapOverride: context.swap,
|
||||
select: context.select,
|
||||
returnPromise: true
|
||||
});
|
||||
}
|
||||
} else {
|
||||
return issueAjaxRequest(verb, path, null, null, {
|
||||
returnPromise: true
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function hierarchyForElt(elt) {
|
||||
var arr = [];
|
||||
while (elt) {
|
||||
arr.push(elt);
|
||||
elt = elt.parentElement;
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
function verifyPath(elt, path, requestConfig) {
|
||||
var sameHost
|
||||
var url
|
||||
if (typeof URL === "function") {
|
||||
url = new URL(path, document.location.href);
|
||||
var origin = document.location.origin;
|
||||
sameHost = origin === url.origin;
|
||||
} else {
|
||||
// IE11 doesn't support URL
|
||||
url = path
|
||||
sameHost = startsWith(path, document.location.origin)
|
||||
}
|
||||
|
||||
if (htmx.config.selfRequestsOnly) {
|
||||
if (!sameHost) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return triggerEvent(elt, "htmx:validateUrl", mergeObjects({url: url, sameHost: sameHost}, requestConfig));
|
||||
}
|
||||
|
||||
function issueAjaxRequest(verb, path, elt, event, etc, confirmed) {
|
||||
var resolve = null;
|
||||
var reject = null;
|
||||
etc = etc != null ? etc : {};
|
||||
if(etc.returnPromise && typeof Promise !== "undefined"){
|
||||
var promise = new Promise(function (_resolve, _reject) {
|
||||
resolve = _resolve;
|
||||
reject = _reject;
|
||||
});
|
||||
}
|
||||
if(elt == null) {
|
||||
elt = getDocument().body;
|
||||
}
|
||||
var responseHandler = etc.handler || handleAjaxResponse;
|
||||
var select = etc.select || null;
|
||||
|
||||
if (!bodyContains(elt)) {
|
||||
// do not issue requests for elements removed from the DOM
|
||||
maybeCall(resolve);
|
||||
return promise;
|
||||
}
|
||||
var target = etc.targetOverride || getTarget(elt);
|
||||
if (target == null || target == DUMMY_ELT) {
|
||||
triggerErrorEvent(elt, 'htmx:targetError', {target: getAttributeValue(elt, "hx-target")});
|
||||
maybeCall(reject);
|
||||
return promise;
|
||||
}
|
||||
|
||||
var eltData = getInternalData(elt);
|
||||
var submitter = eltData.lastButtonClicked;
|
||||
|
||||
if (submitter) {
|
||||
var buttonPath = getRawAttribute(submitter, "formaction");
|
||||
if (buttonPath != null) {
|
||||
path = buttonPath;
|
||||
}
|
||||
|
||||
var buttonVerb = getRawAttribute(submitter, "formmethod")
|
||||
if (buttonVerb != null) {
|
||||
// ignore buttons with formmethod="dialog"
|
||||
if (buttonVerb.toLowerCase() !== "dialog") {
|
||||
verb = buttonVerb;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var confirmQuestion = getClosestAttributeValue(elt, "hx-confirm");
|
||||
// allow event-based confirmation w/ a callback
|
||||
if (confirmed === undefined) {
|
||||
var issueRequest = function(skipConfirmation) {
|
||||
return issueAjaxRequest(verb, path, elt, event, etc, !!skipConfirmation);
|
||||
}
|
||||
var confirmDetails = {target: target, elt: elt, path: path, verb: verb, triggeringEvent: event, etc: etc, issueRequest: issueRequest, question: confirmQuestion};
|
||||
if (triggerEvent(elt, 'htmx:confirm', confirmDetails) === false) {
|
||||
maybeCall(resolve);
|
||||
return promise;
|
||||
}
|
||||
}
|
||||
|
||||
var syncElt = elt;
|
||||
var syncStrategy = getClosestAttributeValue(elt, "hx-sync");
|
||||
var queueStrategy = null;
|
||||
var abortable = false;
|
||||
if (syncStrategy) {
|
||||
var syncStrings = syncStrategy.split(":");
|
||||
var selector = syncStrings[0].trim();
|
||||
if (selector === "this") {
|
||||
syncElt = findThisElement(elt, 'hx-sync');
|
||||
} else {
|
||||
syncElt = querySelectorExt(elt, selector);
|
||||
}
|
||||
// default to the drop strategy
|
||||
syncStrategy = (syncStrings[1] || 'drop').trim();
|
||||
eltData = getInternalData(syncElt);
|
||||
if (syncStrategy === "drop" && eltData.xhr && eltData.abortable !== true) {
|
||||
maybeCall(resolve);
|
||||
return promise;
|
||||
} else if (syncStrategy === "abort") {
|
||||
if (eltData.xhr) {
|
||||
maybeCall(resolve);
|
||||
return promise;
|
||||
} else {
|
||||
abortable = true;
|
||||
}
|
||||
} else if (syncStrategy === "replace") {
|
||||
triggerEvent(syncElt, 'htmx:abort'); // abort the current request and continue
|
||||
} else if (syncStrategy.indexOf("queue") === 0) {
|
||||
var queueStrArray = syncStrategy.split(" ");
|
||||
queueStrategy = (queueStrArray[1] || "last").trim();
|
||||
}
|
||||
}
|
||||
|
||||
if (eltData.xhr) {
|
||||
if (eltData.abortable) {
|
||||
triggerEvent(syncElt, 'htmx:abort'); // abort the current request and continue
|
||||
} else {
|
||||
if(queueStrategy == null){
|
||||
if (event) {
|
||||
var eventData = getInternalData(event);
|
||||
if (eventData && eventData.triggerSpec && eventData.triggerSpec.queue) {
|
||||
queueStrategy = eventData.triggerSpec.queue;
|
||||
}
|
||||
}
|
||||
if (queueStrategy == null) {
|
||||
queueStrategy = "last";
|
||||
}
|
||||
}
|
||||
if (eltData.queuedRequests == null) {
|
||||
eltData.queuedRequests = [];
|
||||
}
|
||||
if (queueStrategy === "first" && eltData.queuedRequests.length === 0) {
|
||||
eltData.queuedRequests.push(function () {
|
||||
issueAjaxRequest(verb, path, elt, event, etc)
|
||||
});
|
||||
} else if (queueStrategy === "all") {
|
||||
eltData.queuedRequests.push(function () {
|
||||
issueAjaxRequest(verb, path, elt, event, etc)
|
||||
});
|
||||
} else if (queueStrategy === "last") {
|
||||
eltData.queuedRequests = []; // dump existing queue
|
||||
eltData.queuedRequests.push(function () {
|
||||
issueAjaxRequest(verb, path, elt, event, etc)
|
||||
});
|
||||
}
|
||||
maybeCall(resolve);
|
||||
return promise;
|
||||
}
|
||||
}
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
eltData.xhr = xhr;
|
||||
eltData.abortable = abortable;
|
||||
var endRequestLock = function(){
|
||||
eltData.xhr = null;
|
||||
eltData.abortable = false;
|
||||
if (eltData.queuedRequests != null &&
|
||||
eltData.queuedRequests.length > 0) {
|
||||
var queuedRequest = eltData.queuedRequests.shift();
|
||||
queuedRequest();
|
||||
}
|
||||
}
|
||||
var promptQuestion = getClosestAttributeValue(elt, "hx-prompt");
|
||||
if (promptQuestion) {
|
||||
var promptResponse = prompt(promptQuestion);
|
||||
// prompt returns null if cancelled and empty string if accepted with no entry
|
||||
if (promptResponse === null ||
|
||||
!triggerEvent(elt, 'htmx:prompt', {prompt: promptResponse, target:target})) {
|
||||
maybeCall(resolve);
|
||||
endRequestLock();
|
||||
return promise;
|
||||
}
|
||||
}
|
||||
|
||||
if (confirmQuestion && !confirmed) {
|
||||
if(!confirm(confirmQuestion)) {
|
||||
maybeCall(resolve);
|
||||
endRequestLock()
|
||||
return promise;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var headers = getHeaders(elt, target, promptResponse);
|
||||
|
||||
if (verb !== 'get' && !usesFormData(elt)) {
|
||||
headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||
}
|
||||
|
||||
if (etc.headers) {
|
||||
headers = mergeObjects(headers, etc.headers);
|
||||
}
|
||||
var results = getInputValues(elt, verb);
|
||||
var errors = results.errors;
|
||||
var rawParameters = results.values;
|
||||
if (etc.values) {
|
||||
rawParameters = mergeObjects(rawParameters, etc.values);
|
||||
}
|
||||
var expressionVars = getExpressionVars(elt);
|
||||
var allParameters = mergeObjects(rawParameters, expressionVars);
|
||||
var filteredParameters = filterValues(allParameters, elt);
|
||||
|
||||
if (htmx.config.getCacheBusterParam && verb === 'get') {
|
||||
filteredParameters['org.htmx.cache-buster'] = getRawAttribute(target, "id") || "true";
|
||||
}
|
||||
|
||||
// behavior of anchors w/ empty href is to use the current URL
|
||||
if (path == null || path === "") {
|
||||
path = getDocument().location.href;
|
||||
}
|
||||
|
||||
|
||||
var requestAttrValues = getValuesForElement(elt, 'hx-request');
|
||||
|
||||
var eltIsBoosted = getInternalData(elt).boosted;
|
||||
|
||||
var useUrlParams = htmx.config.methodsThatUseUrlParams.indexOf(verb) >= 0
|
||||
|
||||
var requestConfig = {
|
||||
boosted: eltIsBoosted,
|
||||
useUrlParams: useUrlParams,
|
||||
parameters: filteredParameters,
|
||||
unfilteredParameters: allParameters,
|
||||
headers:headers,
|
||||
target:target,
|
||||
verb:verb,
|
||||
errors:errors,
|
||||
withCredentials: etc.credentials || requestAttrValues.credentials || htmx.config.withCredentials,
|
||||
timeout: etc.timeout || requestAttrValues.timeout || htmx.config.timeout,
|
||||
path:path,
|
||||
triggeringEvent:event
|
||||
};
|
||||
|
||||
if(!triggerEvent(elt, 'htmx:configRequest', requestConfig)){
|
||||
maybeCall(resolve);
|
||||
endRequestLock();
|
||||
return promise;
|
||||
}
|
||||
|
||||
// copy out in case the object was overwritten
|
||||
path = requestConfig.path;
|
||||
verb = requestConfig.verb;
|
||||
headers = requestConfig.headers;
|
||||
filteredParameters = requestConfig.parameters;
|
||||
errors = requestConfig.errors;
|
||||
useUrlParams = requestConfig.useUrlParams;
|
||||
|
||||
if(errors && errors.length > 0){
|
||||
triggerEvent(elt, 'htmx:validation:halted', requestConfig)
|
||||
maybeCall(resolve);
|
||||
endRequestLock();
|
||||
return promise;
|
||||
}
|
||||
|
||||
var splitPath = path.split("#");
|
||||
var pathNoAnchor = splitPath[0];
|
||||
var anchor = splitPath[1];
|
||||
|
||||
var finalPath = path
|
||||
if (useUrlParams) {
|
||||
finalPath = pathNoAnchor;
|
||||
var values = Object.keys(filteredParameters).length !== 0;
|
||||
if (values) {
|
||||
if (finalPath.indexOf("?") < 0) {
|
||||
finalPath += "?";
|
||||
} else {
|
||||
finalPath += "&";
|
||||
}
|
||||
finalPath += urlEncode(filteredParameters);
|
||||
if (anchor) {
|
||||
finalPath += "#" + anchor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!verifyPath(elt, finalPath, requestConfig)) {
|
||||
triggerErrorEvent(elt, 'htmx:invalidPath', requestConfig)
|
||||
maybeCall(reject);
|
||||
return promise;
|
||||
};
|
||||
|
||||
xhr.open(verb.toUpperCase(), finalPath, true);
|
||||
xhr.overrideMimeType("text/html");
|
||||
xhr.withCredentials = requestConfig.withCredentials;
|
||||
xhr.timeout = requestConfig.timeout;
|
||||
|
||||
// request headers
|
||||
if (requestAttrValues.noHeaders) {
|
||||
// ignore all headers
|
||||
} else {
|
||||
for (var header in headers) {
|
||||
if (headers.hasOwnProperty(header)) {
|
||||
var headerValue = headers[header];
|
||||
safelySetHeaderValue(xhr, header, headerValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var responseInfo = {
|
||||
xhr: xhr, target: target, requestConfig: requestConfig, etc: etc, boosted: eltIsBoosted, select: select,
|
||||
pathInfo: {
|
||||
requestPath: path,
|
||||
finalRequestPath: finalPath,
|
||||
anchor: anchor
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onload = function () {
|
||||
try {
|
||||
var hierarchy = hierarchyForElt(elt);
|
||||
responseInfo.pathInfo.responsePath = getPathFromResponse(xhr);
|
||||
responseHandler(elt, responseInfo);
|
||||
removeRequestIndicators(indicators, disableElts);
|
||||
triggerEvent(elt, 'htmx:afterRequest', responseInfo);
|
||||
triggerEvent(elt, 'htmx:afterOnLoad', responseInfo);
|
||||
// if the body no longer contains the element, trigger the event on the closest parent
|
||||
// remaining in the DOM
|
||||
if (!bodyContains(elt)) {
|
||||
var secondaryTriggerElt = null;
|
||||
while (hierarchy.length > 0 && secondaryTriggerElt == null) {
|
||||
var parentEltInHierarchy = hierarchy.shift();
|
||||
if (bodyContains(parentEltInHierarchy)) {
|
||||
secondaryTriggerElt = parentEltInHierarchy;
|
||||
}
|
||||
}
|
||||
if (secondaryTriggerElt) {
|
||||
triggerEvent(secondaryTriggerElt, 'htmx:afterRequest', responseInfo);
|
||||
triggerEvent(secondaryTriggerElt, 'htmx:afterOnLoad', responseInfo);
|
||||
}
|
||||
}
|
||||
maybeCall(resolve);
|
||||
endRequestLock();
|
||||
} catch (e) {
|
||||
triggerErrorEvent(elt, 'htmx:onLoadError', mergeObjects({error:e}, responseInfo));
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
xhr.onerror = function () {
|
||||
removeRequestIndicators(indicators, disableElts);
|
||||
triggerErrorEvent(elt, 'htmx:afterRequest', responseInfo);
|
||||
triggerErrorEvent(elt, 'htmx:sendError', responseInfo);
|
||||
maybeCall(reject);
|
||||
endRequestLock();
|
||||
}
|
||||
xhr.onabort = function() {
|
||||
removeRequestIndicators(indicators, disableElts);
|
||||
triggerErrorEvent(elt, 'htmx:afterRequest', responseInfo);
|
||||
triggerErrorEvent(elt, 'htmx:sendAbort', responseInfo);
|
||||
maybeCall(reject);
|
||||
endRequestLock();
|
||||
}
|
||||
xhr.ontimeout = function() {
|
||||
removeRequestIndicators(indicators, disableElts);
|
||||
triggerErrorEvent(elt, 'htmx:afterRequest', responseInfo);
|
||||
triggerErrorEvent(elt, 'htmx:timeout', responseInfo);
|
||||
maybeCall(reject);
|
||||
endRequestLock();
|
||||
}
|
||||
if(!triggerEvent(elt, 'htmx:beforeRequest', responseInfo)){
|
||||
maybeCall(resolve);
|
||||
endRequestLock()
|
||||
return promise
|
||||
}
|
||||
var indicators = addRequestIndicatorClasses(elt);
|
||||
var disableElts = disableElements(elt);
|
||||
|
||||
forEach(['loadstart', 'loadend', 'progress', 'abort'], function(eventName) {
|
||||
forEach([xhr, xhr.upload], function (target) {
|
||||
target.addEventListener(eventName, function(event){
|
||||
triggerEvent(elt, "htmx:xhr:" + eventName, {
|
||||
lengthComputable:event.lengthComputable,
|
||||
loaded:event.loaded,
|
||||
total:event.total
|
||||
});
|
||||
})
|
||||
});
|
||||
});
|
||||
triggerEvent(elt, 'htmx:beforeSend', responseInfo);
|
||||
var params = useUrlParams ? null : encodeParamsForBody(xhr, elt, filteredParameters)
|
||||
xhr.send(params);
|
||||
return promise;
|
||||
}
|
||||
|
||||
function determineHistoryUpdates(elt, responseInfo) {
|
||||
|
||||
var xhr = responseInfo.xhr;
|
||||
|
||||
//===========================================
|
||||
// First consult response headers
|
||||
//===========================================
|
||||
var pathFromHeaders = null;
|
||||
var typeFromHeaders = null;
|
||||
if (hasHeader(xhr,/HX-Push:/i)) {
|
||||
pathFromHeaders = xhr.getResponseHeader("HX-Push");
|
||||
typeFromHeaders = "push";
|
||||
} else if (hasHeader(xhr,/HX-Push-Url:/i)) {
|
||||
pathFromHeaders = xhr.getResponseHeader("HX-Push-Url");
|
||||
typeFromHeaders = "push";
|
||||
} else if (hasHeader(xhr,/HX-Replace-Url:/i)) {
|
||||
pathFromHeaders = xhr.getResponseHeader("HX-Replace-Url");
|
||||
typeFromHeaders = "replace";
|
||||
}
|
||||
|
||||
// if there was a response header, that has priority
|
||||
if (pathFromHeaders) {
|
||||
if (pathFromHeaders === "false") {
|
||||
return {}
|
||||
} else {
|
||||
return {
|
||||
type: typeFromHeaders,
|
||||
path : pathFromHeaders
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//===========================================
|
||||
// Next resolve via DOM values
|
||||
//===========================================
|
||||
var requestPath = responseInfo.pathInfo.finalRequestPath;
|
||||
var responsePath = responseInfo.pathInfo.responsePath;
|
||||
|
||||
var pushUrl = getClosestAttributeValue(elt, "hx-push-url");
|
||||
var replaceUrl = getClosestAttributeValue(elt, "hx-replace-url");
|
||||
var elementIsBoosted = getInternalData(elt).boosted;
|
||||
|
||||
var saveType = null;
|
||||
var path = null;
|
||||
|
||||
if (pushUrl) {
|
||||
saveType = "push";
|
||||
path = pushUrl;
|
||||
} else if (replaceUrl) {
|
||||
saveType = "replace";
|
||||
path = replaceUrl;
|
||||
} else if (elementIsBoosted) {
|
||||
saveType = "push";
|
||||
path = responsePath || requestPath; // if there is no response path, go with the original request path
|
||||
}
|
||||
|
||||
if (path) {
|
||||
// false indicates no push, return empty object
|
||||
if (path === "false") {
|
||||
return {};
|
||||
}
|
||||
|
||||
// true indicates we want to follow wherever the server ended up sending us
|
||||
if (path === "true") {
|
||||
path = responsePath || requestPath; // if there is no response path, go with the original request path
|
||||
}
|
||||
|
||||
// restore any anchor associated with the request
|
||||
if (responseInfo.pathInfo.anchor &&
|
||||
path.indexOf("#") === -1) {
|
||||
path = path + "#" + responseInfo.pathInfo.anchor;
|
||||
}
|
||||
|
||||
return {
|
||||
type:saveType,
|
||||
path: path
|
||||
}
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function handleAjaxResponse(elt, responseInfo) {
|
||||
var xhr = responseInfo.xhr;
|
||||
var target = responseInfo.target;
|
||||
var etc = responseInfo.etc;
|
||||
var requestConfig = responseInfo.requestConfig;
|
||||
var select = responseInfo.select;
|
||||
|
||||
if (!triggerEvent(elt, 'htmx:beforeOnLoad', responseInfo)) return;
|
||||
|
||||
if (hasHeader(xhr, /HX-Trigger:/i)) {
|
||||
handleTrigger(xhr, "HX-Trigger", elt);
|
||||
}
|
||||
|
||||
if (hasHeader(xhr, /HX-Location:/i)) {
|
||||
saveCurrentPageToHistory();
|
||||
var redirectPath = xhr.getResponseHeader("HX-Location");
|
||||
var swapSpec;
|
||||
if (redirectPath.indexOf("{") === 0) {
|
||||
swapSpec = parseJSON(redirectPath);
|
||||
// what's the best way to throw an error if the user didn't include this
|
||||
redirectPath = swapSpec['path'];
|
||||
delete swapSpec['path'];
|
||||
}
|
||||
ajaxHelper('GET', redirectPath, swapSpec).then(function(){
|
||||
pushUrlIntoHistory(redirectPath);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
var shouldRefresh = hasHeader(xhr, /HX-Refresh:/i) && "true" === xhr.getResponseHeader("HX-Refresh");
|
||||
|
||||
if (hasHeader(xhr, /HX-Redirect:/i)) {
|
||||
location.href = xhr.getResponseHeader("HX-Redirect");
|
||||
shouldRefresh && location.reload();
|
||||
return;
|
||||
}
|
||||
|
||||
if (shouldRefresh) {
|
||||
location.reload();
|
||||
return;
|
||||
}
|
||||
|
||||
if (hasHeader(xhr,/HX-Retarget:/i)) {
|
||||
if (xhr.getResponseHeader("HX-Retarget") === "this") {
|
||||
responseInfo.target = elt;
|
||||
} else {
|
||||
responseInfo.target = querySelectorExt(elt, xhr.getResponseHeader("HX-Retarget"));
|
||||
}
|
||||
}
|
||||
|
||||
var historyUpdate = determineHistoryUpdates(elt, responseInfo);
|
||||
|
||||
// by default htmx only swaps on 200 return codes and does not swap
|
||||
// on 204 'No Content'
|
||||
// this can be ovverriden by responding to the htmx:beforeSwap event and
|
||||
// overriding the detail.shouldSwap property
|
||||
var shouldSwap = xhr.status >= 200 && xhr.status < 400 && xhr.status !== 204;
|
||||
var serverResponse = xhr.response;
|
||||
var isError = xhr.status >= 400;
|
||||
var ignoreTitle = htmx.config.ignoreTitle
|
||||
var beforeSwapDetails = mergeObjects({shouldSwap: shouldSwap, serverResponse:serverResponse, isError:isError, ignoreTitle:ignoreTitle }, responseInfo);
|
||||
if (!triggerEvent(target, 'htmx:beforeSwap', beforeSwapDetails)) return;
|
||||
|
||||
target = beforeSwapDetails.target; // allow re-targeting
|
||||
serverResponse = beforeSwapDetails.serverResponse; // allow updating content
|
||||
isError = beforeSwapDetails.isError; // allow updating error
|
||||
ignoreTitle = beforeSwapDetails.ignoreTitle; // allow updating ignoring title
|
||||
|
||||
responseInfo.target = target; // Make updated target available to response events
|
||||
responseInfo.failed = isError; // Make failed property available to response events
|
||||
responseInfo.successful = !isError; // Make successful property available to response events
|
||||
|
||||
if (beforeSwapDetails.shouldSwap) {
|
||||
if (xhr.status === 286) {
|
||||
cancelPolling(elt);
|
||||
}
|
||||
|
||||
withExtensions(elt, function (extension) {
|
||||
serverResponse = extension.transformResponse(serverResponse, xhr, elt);
|
||||
});
|
||||
|
||||
// Save current page if there will be a history update
|
||||
if (historyUpdate.type) {
|
||||
saveCurrentPageToHistory();
|
||||
}
|
||||
|
||||
var swapOverride = etc.swapOverride;
|
||||
if (hasHeader(xhr,/HX-Reswap:/i)) {
|
||||
swapOverride = xhr.getResponseHeader("HX-Reswap");
|
||||
}
|
||||
var swapSpec = getSwapSpecification(elt, swapOverride);
|
||||
|
||||
if (swapSpec.hasOwnProperty('ignoreTitle')) {
|
||||
ignoreTitle = swapSpec.ignoreTitle;
|
||||
}
|
||||
|
||||
target.classList.add(htmx.config.swappingClass);
|
||||
|
||||
// optional transition API promise callbacks
|
||||
var settleResolve = null;
|
||||
var settleReject = null;
|
||||
|
||||
var doSwap = function () {
|
||||
try {
|
||||
var activeElt = document.activeElement;
|
||||
var selectionInfo = {};
|
||||
try {
|
||||
selectionInfo = {
|
||||
elt: activeElt,
|
||||
// @ts-ignore
|
||||
start: activeElt ? activeElt.selectionStart : null,
|
||||
// @ts-ignore
|
||||
end: activeElt ? activeElt.selectionEnd : null
|
||||
};
|
||||
} catch (e) {
|
||||
// safari issue - see https://github.com/microsoft/playwright/issues/5894
|
||||
}
|
||||
|
||||
var selectOverride;
|
||||
if (select) {
|
||||
selectOverride = select;
|
||||
}
|
||||
|
||||
if (hasHeader(xhr, /HX-Reselect:/i)) {
|
||||
selectOverride = xhr.getResponseHeader("HX-Reselect");
|
||||
}
|
||||
|
||||
// if we need to save history, do so, before swapping so that relative resources have the correct base URL
|
||||
if (historyUpdate.type) {
|
||||
triggerEvent(getDocument().body, 'htmx:beforeHistoryUpdate', mergeObjects({ history: historyUpdate }, responseInfo));
|
||||
if (historyUpdate.type === "push") {
|
||||
pushUrlIntoHistory(historyUpdate.path);
|
||||
triggerEvent(getDocument().body, 'htmx:pushedIntoHistory', {path: historyUpdate.path});
|
||||
} else {
|
||||
replaceUrlInHistory(historyUpdate.path);
|
||||
triggerEvent(getDocument().body, 'htmx:replacedInHistory', {path: historyUpdate.path});
|
||||
}
|
||||
}
|
||||
|
||||
var settleInfo = makeSettleInfo(target);
|
||||
selectAndSwap(swapSpec.swapStyle, target, elt, serverResponse, settleInfo, selectOverride);
|
||||
|
||||
if (selectionInfo.elt &&
|
||||
!bodyContains(selectionInfo.elt) &&
|
||||
getRawAttribute(selectionInfo.elt, "id")) {
|
||||
var newActiveElt = document.getElementById(getRawAttribute(selectionInfo.elt, "id"));
|
||||
var focusOptions = { preventScroll: swapSpec.focusScroll !== undefined ? !swapSpec.focusScroll : !htmx.config.defaultFocusScroll };
|
||||
if (newActiveElt) {
|
||||
// @ts-ignore
|
||||
if (selectionInfo.start && newActiveElt.setSelectionRange) {
|
||||
// @ts-ignore
|
||||
try {
|
||||
newActiveElt.setSelectionRange(selectionInfo.start, selectionInfo.end);
|
||||
} catch (e) {
|
||||
// the setSelectionRange method is present on fields that don't support it, so just let this fail
|
||||
}
|
||||
}
|
||||
newActiveElt.focus(focusOptions);
|
||||
}
|
||||
}
|
||||
|
||||
target.classList.remove(htmx.config.swappingClass);
|
||||
forEach(settleInfo.elts, function (elt) {
|
||||
if (elt.classList) {
|
||||
elt.classList.add(htmx.config.settlingClass);
|
||||
}
|
||||
triggerEvent(elt, 'htmx:afterSwap', responseInfo);
|
||||
});
|
||||
|
||||
if (hasHeader(xhr, /HX-Trigger-After-Swap:/i)) {
|
||||
var finalElt = elt;
|
||||
if (!bodyContains(elt)) {
|
||||
finalElt = getDocument().body;
|
||||
}
|
||||
handleTrigger(xhr, "HX-Trigger-After-Swap", finalElt);
|
||||
}
|
||||
|
||||
var doSettle = function () {
|
||||
forEach(settleInfo.tasks, function (task) {
|
||||
task.call();
|
||||
});
|
||||
forEach(settleInfo.elts, function (elt) {
|
||||
if (elt.classList) {
|
||||
elt.classList.remove(htmx.config.settlingClass);
|
||||
}
|
||||
triggerEvent(elt, 'htmx:afterSettle', responseInfo);
|
||||
});
|
||||
|
||||
if (responseInfo.pathInfo.anchor) {
|
||||
var anchorTarget = getDocument().getElementById(responseInfo.pathInfo.anchor);
|
||||
if(anchorTarget) {
|
||||
anchorTarget.scrollIntoView({block:'start', behavior: "auto"});
|
||||
}
|
||||
}
|
||||
|
||||
if(settleInfo.title && !ignoreTitle) {
|
||||
var titleElt = find("title");
|
||||
if(titleElt) {
|
||||
titleElt.innerHTML = settleInfo.title;
|
||||
} else {
|
||||
window.document.title = settleInfo.title;
|
||||
}
|
||||
}
|
||||
|
||||
updateScrollState(settleInfo.elts, swapSpec);
|
||||
|
||||
if (hasHeader(xhr, /HX-Trigger-After-Settle:/i)) {
|
||||
var finalElt = elt;
|
||||
if (!bodyContains(elt)) {
|
||||
finalElt = getDocument().body;
|
||||
}
|
||||
handleTrigger(xhr, "HX-Trigger-After-Settle", finalElt);
|
||||
}
|
||||
maybeCall(settleResolve);
|
||||
}
|
||||
|
||||
if (swapSpec.settleDelay > 0) {
|
||||
setTimeout(doSettle, swapSpec.settleDelay)
|
||||
} else {
|
||||
doSettle();
|
||||
}
|
||||
} catch (e) {
|
||||
triggerErrorEvent(elt, 'htmx:swapError', responseInfo);
|
||||
maybeCall(settleReject);
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
var shouldTransition = htmx.config.globalViewTransitions
|
||||
if(swapSpec.hasOwnProperty('transition')){
|
||||
shouldTransition = swapSpec.transition;
|
||||
}
|
||||
|
||||
if(shouldTransition &&
|
||||
triggerEvent(elt, 'htmx:beforeTransition', responseInfo) &&
|
||||
typeof Promise !== "undefined" && document.startViewTransition){
|
||||
var settlePromise = new Promise(function (_resolve, _reject) {
|
||||
settleResolve = _resolve;
|
||||
settleReject = _reject;
|
||||
});
|
||||
// wrap the original doSwap() in a call to startViewTransition()
|
||||
var innerDoSwap = doSwap;
|
||||
doSwap = function() {
|
||||
document.startViewTransition(function () {
|
||||
innerDoSwap();
|
||||
return settlePromise;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (swapSpec.swapDelay > 0) {
|
||||
setTimeout(doSwap, swapSpec.swapDelay)
|
||||
} else {
|
||||
doSwap();
|
||||
}
|
||||
}
|
||||
if (isError) {
|
||||
triggerErrorEvent(elt, 'htmx:responseError', mergeObjects({error: "Response Status Error Code " + xhr.status + " from " + responseInfo.pathInfo.requestPath}, responseInfo));
|
||||
}
|
||||
}
|
||||
|
||||
//====================================================================
|
||||
// Extensions API
|
||||
//====================================================================
|
||||
|
||||
/** @type {Object<string, import("./htmx").HtmxExtension>} */
|
||||
var extensions = {};
|
||||
|
||||
/**
|
||||
* extensionBase defines the default functions for all extensions.
|
||||
* @returns {import("./htmx").HtmxExtension}
|
||||
*/
|
||||
function extensionBase() {
|
||||
return {
|
||||
init: function(api) {return null;},
|
||||
onEvent : function(name, evt) {return true;},
|
||||
transformResponse : function(text, xhr, elt) {return text;},
|
||||
isInlineSwap : function(swapStyle) {return false;},
|
||||
handleSwap : function(swapStyle, target, fragment, settleInfo) {return false;},
|
||||
encodeParameters : function(xhr, parameters, elt) {return null;}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* defineExtension initializes the extension and adds it to the htmx registry
|
||||
*
|
||||
* @param {string} name
|
||||
* @param {import("./htmx").HtmxExtension} extension
|
||||
*/
|
||||
function defineExtension(name, extension) {
|
||||
if(extension.init) {
|
||||
extension.init(internalAPI)
|
||||
}
|
||||
extensions[name] = mergeObjects(extensionBase(), extension);
|
||||
}
|
||||
|
||||
/**
|
||||
* removeExtension removes an extension from the htmx registry
|
||||
*
|
||||
* @param {string} name
|
||||
*/
|
||||
function removeExtension(name) {
|
||||
delete extensions[name];
|
||||
}
|
||||
|
||||
/**
|
||||
* getExtensions searches up the DOM tree to return all extensions that can be applied to a given element
|
||||
*
|
||||
* @param {HTMLElement} elt
|
||||
* @param {import("./htmx").HtmxExtension[]=} extensionsToReturn
|
||||
* @param {import("./htmx").HtmxExtension[]=} extensionsToIgnore
|
||||
*/
|
||||
function getExtensions(elt, extensionsToReturn, extensionsToIgnore) {
|
||||
|
||||
if (elt == undefined) {
|
||||
return extensionsToReturn;
|
||||
}
|
||||
if (extensionsToReturn == undefined) {
|
||||
extensionsToReturn = [];
|
||||
}
|
||||
if (extensionsToIgnore == undefined) {
|
||||
extensionsToIgnore = [];
|
||||
}
|
||||
var extensionsForElement = getAttributeValue(elt, "hx-ext");
|
||||
if (extensionsForElement) {
|
||||
forEach(extensionsForElement.split(","), function(extensionName){
|
||||
extensionName = extensionName.replace(/ /g, '');
|
||||
if (extensionName.slice(0, 7) == "ignore:") {
|
||||
extensionsToIgnore.push(extensionName.slice(7));
|
||||
return;
|
||||
}
|
||||
if (extensionsToIgnore.indexOf(extensionName) < 0) {
|
||||
var extension = extensions[extensionName];
|
||||
if (extension && extensionsToReturn.indexOf(extension) < 0) {
|
||||
extensionsToReturn.push(extension);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
return getExtensions(parentElt(elt), extensionsToReturn, extensionsToIgnore);
|
||||
}
|
||||
|
||||
//====================================================================
|
||||
// Initialization
|
||||
//====================================================================
|
||||
var isReady = false
|
||||
getDocument().addEventListener('DOMContentLoaded', function() {
|
||||
isReady = true
|
||||
})
|
||||
|
||||
/**
|
||||
* Execute a function now if DOMContentLoaded has fired, otherwise listen for it.
|
||||
*
|
||||
* This function uses isReady because there is no realiable way to ask the browswer whether
|
||||
* the DOMContentLoaded event has already been fired; there's a gap between DOMContentLoaded
|
||||
* firing and readystate=complete.
|
||||
*/
|
||||
function ready(fn) {
|
||||
// Checking readyState here is a failsafe in case the htmx script tag entered the DOM by
|
||||
// some means other than the initial page load.
|
||||
if (isReady || getDocument().readyState === 'complete') {
|
||||
fn();
|
||||
} else {
|
||||
getDocument().addEventListener('DOMContentLoaded', fn);
|
||||
}
|
||||
}
|
||||
|
||||
function insertIndicatorStyles() {
|
||||
if (htmx.config.includeIndicatorStyles !== false) {
|
||||
getDocument().head.insertAdjacentHTML("beforeend",
|
||||
"<style>\
|
||||
." + htmx.config.indicatorClass + "{opacity:0}\
|
||||
." + htmx.config.requestClass + " ." + htmx.config.indicatorClass + "{opacity:1; transition: opacity 200ms ease-in;}\
|
||||
." + htmx.config.requestClass + "." + htmx.config.indicatorClass + "{opacity:1; transition: opacity 200ms ease-in;}\
|
||||
</style>");
|
||||
}
|
||||
}
|
||||
|
||||
function getMetaConfig() {
|
||||
var element = getDocument().querySelector('meta[name="htmx-config"]');
|
||||
if (element) {
|
||||
// @ts-ignore
|
||||
return parseJSON(element.content);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function mergeMetaConfig() {
|
||||
var metaConfig = getMetaConfig();
|
||||
if (metaConfig) {
|
||||
htmx.config = mergeObjects(htmx.config , metaConfig)
|
||||
}
|
||||
}
|
||||
|
||||
// initialize the document
|
||||
ready(function () {
|
||||
mergeMetaConfig();
|
||||
insertIndicatorStyles();
|
||||
var body = getDocument().body;
|
||||
processNode(body);
|
||||
var restoredElts = getDocument().querySelectorAll(
|
||||
"[hx-trigger='restored'],[data-hx-trigger='restored']"
|
||||
);
|
||||
body.addEventListener("htmx:abort", function (evt) {
|
||||
var target = evt.target;
|
||||
var internalData = getInternalData(target);
|
||||
if (internalData && internalData.xhr) {
|
||||
internalData.xhr.abort();
|
||||
}
|
||||
});
|
||||
/** @type {(ev: PopStateEvent) => any} */
|
||||
const originalPopstate = window.onpopstate ? window.onpopstate.bind(window) : null;
|
||||
/** @type {(ev: PopStateEvent) => any} */
|
||||
window.onpopstate = function (event) {
|
||||
if (event.state && event.state.htmx) {
|
||||
restoreHistory();
|
||||
forEach(restoredElts, function(elt){
|
||||
triggerEvent(elt, 'htmx:restored', {
|
||||
'document': getDocument(),
|
||||
'triggerEvent': triggerEvent
|
||||
});
|
||||
});
|
||||
} else {
|
||||
if (originalPopstate) {
|
||||
originalPopstate(event);
|
||||
}
|
||||
}
|
||||
};
|
||||
setTimeout(function () {
|
||||
triggerEvent(body, 'htmx:load', {}); // give ready handlers a chance to load up before firing this event
|
||||
body = null; // kill reference for gc
|
||||
}, 0);
|
||||
})
|
||||
|
||||
return htmx;
|
||||
}
|
||||
)()
|
||||
}));
|
||||
1
static/htmx.min.js
vendored
Normal file
1
static/htmx.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
|
|
@ -13,4 +13,38 @@
|
|||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<h3>Uploads</h3>
|
||||
{% if user_uploads %}
|
||||
<p>Uploaded files:</p>
|
||||
{% else %}
|
||||
<p>No uploaded files yet.</p>
|
||||
{% endif %}
|
||||
<ul id="uploads-list">
|
||||
{% for upload in user_uploads %}
|
||||
<li id="upload-{{ upload.id }}">
|
||||
<a href="{{ upload.get_absolute_url }}">{{ upload }}</a>
|
||||
<p>
|
||||
Description: <span id="description-{{ upload.id }}">{{ upload.description|default:"No description" }}</span>
|
||||
{% if upload.notes %}
|
||||
<br>
|
||||
Notes: {{ upload.notes }}
|
||||
{% endif %}
|
||||
</p>
|
||||
<form method="post" action="{% url 'delete_upload' %}">
|
||||
{% csrf_token %}
|
||||
<input type="hidden" name="file_id" value="{{ upload.id }}">
|
||||
<button type="submit">Delete</button>
|
||||
</form>
|
||||
<form method="post"
|
||||
hx-post="{% url 'edit_description' %}"
|
||||
hx-target="#description-{{ upload.id }}"
|
||||
hx-swap="innerHTML">
|
||||
{% csrf_token %}
|
||||
<input type="hidden" name="file_id" value="{{ upload.id }}">
|
||||
<input type="text" name="description" placeholder="New description" required>
|
||||
<button type="submit">Edit description</button>
|
||||
</form>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endblock %}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
{% load static %}
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
|
|
@ -7,11 +8,8 @@
|
|||
{% if keywords %}<meta name="keywords" content="{{ keywords }}" />{% endif %}
|
||||
{% if author %}<meta name="author" content="{{ author }}" />{% endif %}
|
||||
{% if canonical %}<link rel="canonical" href="{{ canonical }}" />{% endif %}
|
||||
{% if title %}
|
||||
<title>{{ title }}</title>
|
||||
{% else %}
|
||||
<title>FeedVault</title>
|
||||
{% endif %}
|
||||
<title>{{ title|default:"FeedVault" }}</title>
|
||||
<script src="{% static 'htmx.min.js' %}"></script>
|
||||
<style>
|
||||
html {
|
||||
max-width: 88ch;
|
||||
|
|
@ -70,6 +68,10 @@
|
|||
.success {
|
||||
color: green;
|
||||
}
|
||||
|
||||
.warning {
|
||||
color: orange;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
{% if feeds %}
|
||||
{% for feed in feeds %}
|
||||
<a href="{% url 'feed' feed.id %}">{{ feed.feed_url|default:"Unknown Feed" }} →</a>
|
||||
<br>
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<p>No feeds yet. Time to add some!</p>
|
||||
|
|
|
|||
|
|
@ -14,16 +14,29 @@
|
|||
<h2>Upload</h2>
|
||||
<p>
|
||||
You can also upload files containing the feeds you wish to archive.
|
||||
Currently supported file formats: .opml, .xml, .json. Your file will be parsed in the future if not currently supported. Feel free to upload databases, backups, or any other files containing feeds. The service will parse the files and add the feeds to the archive.
|
||||
Currently supported file formats: .opml, .xml, .json.
|
||||
Your file will be parsed in the future if not currently supported.
|
||||
Feel free to upload databases, backups, or any other files containing feeds.
|
||||
</p>
|
||||
<form enctype="multipart/form-data"
|
||||
method="post"
|
||||
action="{% url 'upload' %}">
|
||||
{% csrf_token %}
|
||||
<p>
|
||||
<label for="file">Choose a file to upload</label>
|
||||
<br>
|
||||
<br>
|
||||
<input type="file" name="file" id="file" required>
|
||||
<br>
|
||||
<br>
|
||||
<label for="description">Description (optional)</label>
|
||||
<input type="text"
|
||||
name="description"
|
||||
id="description"
|
||||
size="80"
|
||||
placeholder="Description (optional)">
|
||||
</p>
|
||||
<button type="submit">Upload feeds</button>
|
||||
<button type="submit">Upload file</button>
|
||||
</form>
|
||||
{% else %}
|
||||
<h2>Welcome to FeedVault!</h2>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue