Debian Codegraph @

django_json_widget/templates/django_json_widget.html

raw
1 <div {% if not widget.attrs.style %}style="height:{{widget.height|default:'500px'}};width:{{widget.width|default:'90%'}};display:inline-block;"{% endif %}{% include "django/forms/widgets/attrs.html" %}></div>
2
3 <textarea id="{{widget.attrs.id}}_textarea" name="{{ widget.name }}" required="" style="display: none"></textarea>
4
5 {% with script_id=widget.name|add:"_data" %}
6 {{ widget.value|json_script:script_id }}
7 {% endwith %}
8
9 <script>
10 (function() {
11 var container = document.getElementById("{{ widget.attrs.id }}");
12 var textarea = document.getElementById("{{widget.attrs.id}}_textarea");
13
14 var options = {{ widget.options|safe }};
15 options.onChange = function () {
16 var json = editor.get();
17 textarea.value = JSON.stringify(json);
18 }
19
20 var editor = new JSONEditor(container, options);
21 var content = document.getElementById("{{ widget.name }}_data").textContent;
22 textarea.value = content;
23 editor.set(JSON.parse(content));
24
25 // Expose editor instance for external access
26 window['{{ widget.attrs.id }}_editor'] = editor;
27 container.jsonEditor = editor;
28 })();
29 </script>