forked from 82Flex/DCRM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidgets.py
executable file
·56 lines (45 loc) · 2.02 KB
/
widgets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from django import forms
from django.forms import Textarea, ClearableFileInput
from django.utils.safestring import mark_safe
class AutosizedTextarea(Textarea):
"""
AutoSized TextArea - TextArea height dynamically grows based on user input
"""
def __init__(self, attrs=None):
new_attrs = _make_attrs(attrs, {"rows": 2}, "autosize form-control")
super(AutosizedTextarea, self).__init__(new_attrs)
@property
def media(self):
return forms.Media(js=('suit/js/autosize.min.js',))
def render(self, name, value, attrs=None):
output = super(AutosizedTextarea, self).render(name, value, attrs)
output += mark_safe(
"<script type=\"text/javascript\">django.jQuery(function () { autosize(document.getElementById('id_%s')); });</script>"
% name)
return output
class CharacterCountTextarea(AutosizedTextarea):
"""
TextArea with character count. Supports also twitter specific count.
"""
def render(self, name, value, attrs=None):
output = super(CharacterCountTextarea, self).render(name, value, attrs)
output += mark_safe(
"<script type=\"text/javascript\">django.jQuery(function () { django.jQuery('#id_%s').suitCharactersCount(); });</script>"
% name)
return output
class ImageWidget(ClearableFileInput):
def render(self, name, value, attrs=None):
html = super(ImageWidget, self).render(name, value, attrs)
if not value or not hasattr(value, 'url') or not value.url:
return html
html = u'<div class="ImageWidget"><div class="float-xs-left">' \
u'<a href="%s" target="_blank"><img src="%s" width="75"></a></div>' \
u'%s</div>' % (value.url, value.url, html)
return mark_safe(html)
def _make_attrs(attrs, defaults=None, classes=None):
result = defaults.copy() if defaults else {}
if attrs:
result.update(attrs)
if classes:
result["class"] = " ".join((classes, result.get("class", "")))
return result