mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-26 17:26:10 -06:00
Make object selector functional
This commit is contained in:
parent
f83352f2c5
commit
967df9d9b1
@ -30,6 +30,7 @@ class ObjectSelectorView(View):
|
|||||||
return render(request, self.template_name, {
|
return render(request, self.template_name, {
|
||||||
'form': form,
|
'form': form,
|
||||||
'model': model,
|
'model': model,
|
||||||
|
'target_id': request.GET.get('target'),
|
||||||
})
|
})
|
||||||
|
|
||||||
def _get_model(self, label):
|
def _get_model(self, label):
|
||||||
|
BIN
netbox/project-static/dist/netbox.js
vendored
BIN
netbox/project-static/dist/netbox.js
vendored
Binary file not shown.
BIN
netbox/project-static/dist/netbox.js.map
vendored
BIN
netbox/project-static/dist/netbox.js.map
vendored
Binary file not shown.
@ -1,9 +1,10 @@
|
|||||||
import { getElements, isTruthy } from './util';
|
import { getElements, isTruthy } from './util';
|
||||||
import { initButtons } from './buttons';
|
import { initButtons } from './buttons';
|
||||||
import { initSelect } from './select';
|
import { initSelect } from './select';
|
||||||
|
import { initObjectSelector } from './objectSelector';
|
||||||
|
|
||||||
function initDepedencies(): void {
|
function initDepedencies(): void {
|
||||||
for (const init of [initButtons, initSelect]) {
|
for (const init of [initButtons, initSelect, initObjectSelector]) {
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
32
netbox/project-static/src/objectSelector.ts
Normal file
32
netbox/project-static/src/objectSelector.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import { getElements } from './util';
|
||||||
|
|
||||||
|
function handleSelection(link: HTMLAnchorElement): void {
|
||||||
|
const selector_results = document.getElementById('selector_results');
|
||||||
|
if (selector_results == null) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const target_id = selector_results.getAttribute('data-selector-target');
|
||||||
|
if (target_id == null) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const target = document.getElementById(target_id);
|
||||||
|
if (target == null) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const label = link.getAttribute('data-label');
|
||||||
|
const value = link.getAttribute('data-value');
|
||||||
|
|
||||||
|
//@ts-ignore
|
||||||
|
target.slim.setData([
|
||||||
|
{text: label, value: value}
|
||||||
|
]);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export function initObjectSelector(): void {
|
||||||
|
for (const element of getElements<HTMLAnchorElement>('#selector_results a')) {
|
||||||
|
element.addEventListener('click', () => handleSelection(element));
|
||||||
|
}
|
||||||
|
}
|
@ -15,7 +15,7 @@
|
|||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-9">
|
<div class="col-9">
|
||||||
<form hx-get="{% url 'htmx_object_selector' %}?model={{ model|meta:"label_lower" }}" hx-target="#results_list">
|
<form hx-get="{% url 'htmx_object_selector' %}?model={{ model|meta:"label_lower" }}" hx-target="#selector_results">
|
||||||
<div class="tab-content p-1">
|
<div class="tab-content p-1">
|
||||||
{% for field in form.visible_fields %}
|
{% for field in form.visible_fields %}
|
||||||
<div class="collapse{% if forloop.first %} show{% endif %}" id="selector{{ forloop.counter }}">{% render_field field %}</div>
|
<div class="collapse{% if forloop.first %} show{% endif %}" id="selector{{ forloop.counter }}">{% render_field field %}</div>
|
||||||
@ -25,6 +25,6 @@
|
|||||||
<button type="submit" name="_search" value="true" class="btn btn-sm btn-primary">Submit</button>
|
<button type="submit" name="_search" value="true" class="btn btn-sm btn-primary">Submit</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
<div id="results_list" class="mt-3"></div>
|
<div id="selector_results" class="mt-3" data-selector-target="{{ target_id }}"></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<div class="list-group">
|
<div class="list-group">
|
||||||
{% for object in results %}
|
{% for object in results %}
|
||||||
<a href="#" class="list-group-item list-group-item-action" data-label="{{ object }}" data-value="{{ object.pk }}">
|
<a href="#" class="list-group-item list-group-item-action" data-label="{{ object }}" data-value="{{ object.pk }}" data-bs-dismiss="modal">
|
||||||
<h6 class="mb-1">
|
<h6 class="mb-1">
|
||||||
{{ object }}
|
{{ object }}
|
||||||
{% if object.status %}{% badge object.get_status_display bg_color=object.get_status_color %}{% endif %}
|
{% if object.status %}{% badge object.get_status_display bg_color=object.get_status_color %}{% endif %}
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
class="btn btn-sm btn-outline-dark border-input ms-1"
|
class="btn btn-sm btn-outline-dark border-input ms-1"
|
||||||
data-bs-toggle="modal"
|
data-bs-toggle="modal"
|
||||||
data-bs-target="#htmx-modal"
|
data-bs-target="#htmx-modal"
|
||||||
hx-get="{% url 'htmx_object_selector' %}?model={{ widget.attrs.selector }}"
|
hx-get="{% url 'htmx_object_selector' %}?model={{ widget.attrs.selector }}&target={{ widget.attrs.id }}"
|
||||||
hx-target="#htmx-modal-content"
|
hx-target="#htmx-modal-content"
|
||||||
>
|
>
|
||||||
<i class="mdi mdi-database-search-outline"></i>
|
<i class="mdi mdi-database-search-outline"></i>
|
||||||
|
Loading…
Reference in New Issue
Block a user