From 823f763c9897b95f96c22eb65d1cd183daa7c5ce Mon Sep 17 00:00:00 2001 From: j Date: Tue, 21 Aug 2018 14:22:29 +0200 Subject: [PATCH] teaser maxlength --- content/admin.py | 16 +++++++++++++++- content/static/js/maxlength_count.js | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 content/static/js/maxlength_count.js diff --git a/content/admin.py b/content/admin.py index 7784cc2..e3a97e8 100644 --- a/content/admin.py +++ b/content/admin.py @@ -39,6 +39,20 @@ class ServerAdmin(admin.ModelAdmin): pass ''' +class MaxLengthAdminMarkdownxWidget(AdminMarkdownxWidget): + def get_context(self, name, value, attrs=None): + if name == 'teaser': + if not attrs: + attrs = {} + attrs['maxlength'] = 250 + print(dir(self)) + return super(MaxLengthAdminMarkdownxWidget, self).get_context(name, value, attrs) + + class Media: + js = ( + 'js/maxlength_count.js', + ) + class GalleryAdminForm(forms.ModelForm): """Users never need to enter a description on a gallery.""" @@ -58,7 +72,7 @@ class ContentAdmin(admin.ModelAdmin): raw_id_fields = ['photo'] inlines = [ContentParentsInline, FileInline, LinkInline] formfield_overrides = { - models.TextField: {'widget': AdminMarkdownxWidget}, + models.TextField: {'widget': MaxLengthAdminMarkdownxWidget}, } diff --git a/content/static/js/maxlength_count.js b/content/static/js/maxlength_count.js new file mode 100644 index 0000000..ce6f05b --- /dev/null +++ b/content/static/js/maxlength_count.js @@ -0,0 +1,18 @@ +django.jQuery(function() { + django.jQuery('textarea[maxlength]').each(function(i, textarea) { + var t = django.jQuery(textarea), + count = t.parent().find('.maxlength-count') + if (count.length == 0) { + count = django.jQuery('
', {'class': 'maxlength-count'} ).insertAfter(t) + t.on({ + keydown: update, + change: update, + drop: update, + }) + } + function update() { + var max = Math.round(t.attr('maxlength')), left = max - t.val().length + count.html(left + ' characters left. (max: ' + max + ')') + } + }); +});