'use strict'; { const $ = django.jQuery; $.fn.djangoAdminSelect2 = function() { $.each(this, function(i, element) { const select2_config = {}; select2_config.ajax = { data: (params)=>{ const {fieldName, appLabel, modelName} = element.dataset; const out = { term: params.term, page: params.page, app_label: appLabel, model_name: modelName, field_name: fieldName }; if (element.dataset.hasOwnProperty('drilldown_enabled')) { if (element.dataset.hasOwnProperty('filtered_by')) { const filtered_by_fields = JSON.parse(element.dataset.filtered_by) const filtered_by_dict = {}; let some_obj = false; for (let i in filtered_by_fields) { if (filtered_by_fields.hasOwnProperty(i)) { const filtering_field = filtered_by_fields[i]; const field_id = element.dataset.select2Id; const filtering_field_id = `${field_id.slice(0, field_id.length - fieldName.length)}${filtering_field}`; const filtering_value = document.querySelector(`[data-select2-id="${filtering_field_id}"].admin-autocomplete`).value; if (filtering_value !== '') { filtered_by_dict[filtering_field] = filtering_value; some_obj = true; } } } if (some_obj) { out.filtered_by_dict = JSON.stringify(filtered_by_dict); } } } return out; } }; if (element.dataset.hasOwnProperty('drilldown_enabled')) { select2_config.templateResult = (item,container)=>{ let styleClass = ''; element.classList.add('drilldown'); if (item.ddok === 1) { styleClass = 'drilldown_ok'; } else { styleClass = 'drilldown_ko'; } container.classList.add(styleClass); if (item.hasOwnProperty('autoselect')) { const includedOnly = element.dataset.included_only ?? false; const dataLength = $(element).select2('data').length; if (includedOnly || dataLength == 0) { $(element).select2("trigger", "select", { data: item }) } } return $(``).text(item.text); } } $(element).select2(select2_config); const reset_on_excluded = []; const reset_on_included = []; let all_fields_to_reset; let reset_on_something = false; if (element.dataset.hasOwnProperty('reset_on_excluded')) { reset_on_excluded.push(...JSON.parse(element.dataset.reset_on_excluded)); reset_on_something = true; } if (element.dataset.hasOwnProperty('reset_on_included')) { reset_on_included.push(...JSON.parse(element.dataset.reset_on_included)); reset_on_something = true; } if (reset_on_something) { $(element).on('select2:select', (ev)=>{ const data = $(element).select2('data'); if (Array.isArray(data) && data.length > 0) { const ddok = data[0].ddok; if (ddok !== undefined) { all_fields_to_reset = ddok ? reset_on_included : reset_on_excluded; if (all_fields_to_reset.length > 0) { all_fields_to_reset.forEach((field_to_reset)=>{ const field_name = element.dataset.fieldName; const field_id = element.dataset.select2Id; const id_to_reset = `${field_id.slice(0, field_id.length - field_name.length)}${field_to_reset}`; const element_to_reset = document.querySelector(`#${id_to_reset}.admin-autocomplete`); $(element_to_reset).val(null).trigger('change'); } ); } } } } ); } }); return this; } ; $(function() { // Initialize all autocomplete widgets except the one in the template // form used when a new formset is added. $('.admin-autocomplete').not('[name*=__prefix__]').djangoAdminSelect2(); }); document.addEventListener('formset:added', (event)=>{ $(event.target).find('.admin-autocomplete').djangoAdminSelect2(); } ); }