Fix form loading hyperscript config

This commit is contained in:
Andrew Godwin 2022-12-11 09:34:44 -07:00
parent 6d473a83cc
commit f504e9c351
2 changed files with 21 additions and 46 deletions

View File

@ -30,19 +30,6 @@ class Compose(FormView):
"autofocus": "autofocus", "autofocus": "autofocus",
"maxlength": Config.lazy_system_value("post_length"), "maxlength": Config.lazy_system_value("post_length"),
"placeholder": "What's on your mind?", "placeholder": "What's on your mind?",
"_": f"""
on load or input
set characters to my.value.trim().length
put { Config.system.post_length } - characters into #character-counter
if characters > { Config.system.post_length } then
set #character-counter's style.color to 'var(--color-text-error)'
add [@disabled=] to #post-button
else
set #character-counter's style.color to ''
remove @disabled from #post-button
end
""",
}, },
) )
) )
@ -67,6 +54,24 @@ class Compose(FormView):
) )
reply_to = forms.CharField(widget=forms.HiddenInput(), required=False) reply_to = forms.CharField(widget=forms.HiddenInput(), required=False)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["text"].widget.attrs[
"_"
] = f"""
on load or input
set characters to my.value.trim().length
put {Config.system.post_length} - characters into #character-counter
if characters > {Config.system.post_length} then
set #character-counter's style.color to 'var(--color-text-error)'
add [@disabled=] to #post-button
else
set #character-counter's style.color to ''
remove @disabled from #post-button
end
"""
def clean_text(self): def clean_text(self):
text = self.cleaned_data.get("text") text = self.cleaned_data.get("text")
if not text: if not text:

View File

@ -1,4 +1,3 @@
from django import forms
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.template.defaultfilters import linebreaks_filter from django.template.defaultfilters import linebreaks_filter
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
@ -6,46 +5,17 @@ from django.views.generic import FormView, ListView
from activities.models import Hashtag, Post, PostInteraction, TimelineEvent from activities.models import Hashtag, Post, PostInteraction, TimelineEvent
from core.decorators import cache_page from core.decorators import cache_page
from core.models import Config
from users.decorators import identity_required from users.decorators import identity_required
from .compose import Compose
@method_decorator(identity_required, name="dispatch") @method_decorator(identity_required, name="dispatch")
class Home(FormView): class Home(FormView):
template_name = "activities/home.html" template_name = "activities/home.html"
class form_class(forms.Form): form_class = Compose.form_class
text = forms.CharField(
widget=forms.Textarea(
attrs={
"placeholder": "What's on your mind?",
"_": f"""
on load or input
set characters to my.value.trim().length
put { Config.system.post_length } - characters into #character-counter
if characters > { Config.system.post_length } then
set #character-counter's style.color to 'var(--color-text-error)'
add [@disabled=] to #post-button
else
set #character-counter's style.color to ''
remove @disabled from #post-button
end
""",
},
)
)
content_warning = forms.CharField(
required=False,
label=Config.lazy_system_value("content_warning_text"),
widget=forms.TextInput(
attrs={
"class": "hidden",
"placeholder": Config.lazy_system_value("content_warning_text"),
},
),
)
def get_context_data(self): def get_context_data(self):
context = super().get_context_data() context = super().get_context_data()