|
1 |
| -import copy |
| 1 | +import json |
2 | 2 |
|
3 |
| -import collections |
4 | 3 | from django import forms
|
5 | 4 | from django.utils.safestring import mark_safe
|
6 | 5 | from django.template.loader import render_to_string
|
7 |
| -import json |
8 | 6 |
|
9 | 7 |
|
10 | 8 | class JSONEditorWidget(forms.Widget):
|
11 | 9 | template_name = 'django_admin_json_editor/editor.html'
|
12 | 10 |
|
13 |
| - def __init__(self, schema, collapsed=True, sceditor=False): |
| 11 | + def __init__(self, schema, collapsed=True, sceditor=False, editor_options=None): |
14 | 12 | super(JSONEditorWidget, self).__init__()
|
15 | 13 | self._schema = schema
|
16 | 14 | self._collapsed = collapsed
|
17 | 15 | self._sceditor = sceditor
|
| 16 | + self._editor_options = editor_options or {} |
18 | 17 |
|
19 | 18 | def render(self, name, value, attrs=None, renderer=None):
|
20 | 19 | if callable(self._schema):
|
21 | 20 | schema = self._schema(self)
|
22 | 21 | else:
|
23 |
| - schema = copy.copy(self._schema) |
24 |
| - |
25 |
| - self.schema_updater(schema) |
| 22 | + schema = self._schema |
26 | 23 |
|
27 | 24 | schema['title'] = ' '
|
28 | 25 | schema['options'] = {'collapsed': int(self._collapsed)}
|
29 | 26 |
|
| 27 | + editor_options = { |
| 28 | + 'theme': 'bootstrap3', |
| 29 | + 'iconlib': 'fontawesome4', |
| 30 | + 'schema': schema, |
| 31 | + } |
| 32 | + editor_options.update(self._editor_options) |
| 33 | + |
30 | 34 | context = {
|
31 | 35 | 'name': name,
|
32 |
| - 'schema': json.dumps(schema), |
33 | 36 | 'data': value,
|
34 | 37 | 'sceditor': int(self._sceditor),
|
| 38 | + 'editor_options': json.dumps(editor_options), |
35 | 39 | }
|
36 | 40 | return mark_safe(render_to_string(self.template_name, context))
|
37 | 41 |
|
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 |
| - |
48 | 42 | @property
|
49 | 43 | def media(self):
|
50 | 44 | css = {
|
|
0 commit comments