Skip to content

Commit 4ec7efb

Browse files
authored
Merge pull request #22 from tapapax/feature/editor-options
Implement editor_options widget parameter
2 parents a8d8e63 + c22783c commit 4ec7efb

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

django_admin_json_editor/admin.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,44 @@
1-
import copy
1+
import json
22

3-
import collections
43
from django import forms
54
from django.utils.safestring import mark_safe
65
from django.template.loader import render_to_string
7-
import json
86

97

108
class JSONEditorWidget(forms.Widget):
119
template_name = 'django_admin_json_editor/editor.html'
1210

13-
def __init__(self, schema, collapsed=True, sceditor=False):
11+
def __init__(self, schema, collapsed=True, sceditor=False, editor_options=None):
1412
super(JSONEditorWidget, self).__init__()
1513
self._schema = schema
1614
self._collapsed = collapsed
1715
self._sceditor = sceditor
16+
self._editor_options = editor_options or {}
1817

1918
def render(self, name, value, attrs=None, renderer=None):
2019
if callable(self._schema):
2120
schema = self._schema(self)
2221
else:
23-
schema = copy.copy(self._schema)
24-
25-
self.schema_updater(schema)
22+
schema = self._schema
2623

2724
schema['title'] = ' '
2825
schema['options'] = {'collapsed': int(self._collapsed)}
2926

27+
editor_options = {
28+
'theme': 'bootstrap3',
29+
'iconlib': 'fontawesome4',
30+
'schema': schema,
31+
}
32+
editor_options.update(self._editor_options)
33+
3034
context = {
3135
'name': name,
32-
'schema': json.dumps(schema),
3336
'data': value,
3437
'sceditor': int(self._sceditor),
38+
'editor_options': json.dumps(editor_options),
3539
}
3640
return mark_safe(render_to_string(self.template_name, context))
3741

38-
@classmethod
39-
def schema_updater(cls, nested):
40-
"""Updates schema to format allowed by JS"""
41-
for key, value in nested.items():
42-
if isinstance(value, collections.Mapping):
43-
cls.schema_updater(value)
44-
else:
45-
# Replace bool values with integers
46-
nested[key] = int(value) if isinstance(value, bool) else value
47-
4842
@property
4943
def media(self):
5044
css = {

django_admin_json_editor/templates/django_admin_json_editor/editor.html

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
<script>
44
var container = document.getElementById("{{ name }}_editor");
5-
var options = {
6-
theme: "bootstrap3",
7-
iconlib: "fontawesome4",
8-
schema: {{ schema|safe }}
9-
};
5+
var options = {{ editor_options|safe }};
106
var {{ name }}_editor = new JSONEditor(container, options);
117
JSONEditor.plugins.sceditor.emoticonsEnabled = {{ sceditor }};
128
{{ name }}_editor.on('change', function () {

0 commit comments

Comments
 (0)