Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[change] Allow multiple jsonschema widgets in one page #333

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions openwisp_controller/config/static/config/js/tabs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
django.jQuery(function ($) {

if ($('.add-form').length || !$('#device_form').length) {
return;
}
Expand Down Expand Up @@ -48,7 +48,7 @@ django.jQuery(function ($) {
label = $el.find('> fieldset.module > h2, ' +
'> .tabular > fieldset.module > h2').text();
tabsContainer.append(
'<li><a class="button" href="#' + tabId + '">' + label + '</a></li>'
'<li class="'+ label.toLowerCase().replace(' ', '-') +'"><a class="button" href="#' + tabId + '">' + label + '</a></li>'
);
});

Expand Down
10 changes: 7 additions & 3 deletions openwisp_controller/config/static/config/js/widget.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';
(function ($) {
django._schemas = new Map();
django._jsonEditors = new Map();
var inFullScreenMode = false,
prevDefaultValues = {},
defaultValuesUrl = window.location.origin + '/admin/config/device/get-template-default-values/',
Expand Down Expand Up @@ -200,14 +202,15 @@
disable_edit_json: true,
startval: startval,
keep_oneof_values: false,
show_errors: 'change',
show_errors: field.data('show-errors') ? field.data('show-errors'): 'change',
// if no backend selected use empty schema
schema: backend ? schemas[backend] : {}
};
if (field.attr("data-options") !== undefined) {
$.extend(options, JSON.parse(field.attr("data-options")));
}
editor = new JSONEditor(document.getElementById(id), options);
django._jsonEditors[id] = editor;
// initialise advanced json editor here (disable schema validation in VPN admin)
advancedEditor = initAdvancedEditor(field, value, options.schema, $('#vpn_form').length === 1);
$advancedEl = $('#advanced_editor');
Expand Down Expand Up @@ -330,8 +333,9 @@
};

var bindLoadUi = function () {
$.getJSON(django._jsonSchemaWidgetUrl, function (schemas) {
$('.jsoneditor-raw').each(function (i, el) {
$('.jsoneditor-raw:not([name*="__prefix__"])').each(function (i, el) {
$.getJSON($(el).data('schema-url'), function (schemas) {
django._schemas[$(el).data('schema-url')] = schemas;
var field = $(el),
schema = field.attr("data-schema"),
schemaSelector = field.attr("data-schema-selector");
Expand Down
2 changes: 2 additions & 0 deletions openwisp_controller/config/widgets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django import forms
from django.contrib.admin.widgets import AdminTextareaWidget
from django.template.loader import get_template
from django.urls import reverse


class JsonSchemaWidget(AdminTextareaWidget):
Expand Down Expand Up @@ -45,5 +46,6 @@ def render(self, name, value, attrs=None, renderer=None):
attrs = attrs or {}
attrs['class'] = 'vLargeTextField jsoneditor-raw'
attrs.update(self.extra_attrs)
attrs.update({'data-schema-url': reverse(self.schema_view_name)})
html += super().render(name, value, attrs, renderer)
return html