teaser maxlength

This commit is contained in:
j 2018-08-21 14:22:29 +02:00
parent ab7a4d3b99
commit 823f763c98
2 changed files with 33 additions and 1 deletions

View File

@ -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},
}

View File

@ -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('<div>', {'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 + ')')
}
});
});