-
Notifications
You must be signed in to change notification settings - Fork 0
/
widgets.py
31 lines (25 loc) · 826 Bytes
/
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
from django import forms
from django.conf import settings
from django.core.serializers.json import json
css = 'css/codemirror.css'
js = 'js/codemirror.js'
default_config = getattr(settings, 'CODEMIRROR_CONFIG', {})
class CodeMirror(forms.Textarea):
class Media:
js = js
css = {
'all': css
}
def __init__(self, **kwargs):
self.config = default_config
self.config.update(kwargs)
super(CodeMirror, self).__init__()
def render(self, name, value, attrs=None, renderer=None):
field = super(CodeMirror, self).render(name, value, attrs)
return """%s
<script type="text/javascript">
var code_editor = CodeMirror.fromTextArea(
document.getElementById('%s'), %s
);
</script>
""" % (field, attrs['id'], json.dumps(self.config))