{% set id = id ?? "selectize#{random()}" %} {% set selectizeOptions = { dropdownParent: 'body', plugins: ['auto_position', 'selectize-plugin-a11y'], }|merge(selectizeOptions ?? []) %} {% set multi = multi ?? false %} {% if multi %} {% set selectizeOptions = selectizeOptions|merge({ plugins: (selectizeOptions.plugins ?? [])|push('remove_button') }) %} {% else %} {% if selectizeOptions.extraPlugins is defined and selectizeOptions.extraPlugins == false %} {# don't include extra plugins if the settings explicitly tell us not to #} {% else %} {% set selectizeOptions = selectizeOptions|merge({ plugins: (selectizeOptions.plugins ?? [])|push('select_on_focus') }) %} {% endif %} {% endif %} {# Normalize the options #} {% set options = (options ?? [])|map((o, k) => (o.optgroup is defined or o.value is defined) ? o : { value: k, label: o.label is defined ? o.label : o, }) %} {% set options = options|map(o => o|merge({ data: (o.data ?? {})|merge(o.data.data ?? {})|merge({ color: o.data.color ?? o.color ?? null, icon: iconSvg(o.data.icon ?? o.icon ?? null), }|filter) })) %} {% if includeEnvVars ?? false %} {% if allowedEnvValues is not defined %} {% set allowedEnvValues = options|filter(o => o.optgroup is not defined)|map(o => o.value) %} {% endif %} {% set options = options|map(o => o.data.hint is defined ? o : o|merge({ data: { hint: o.value, }, }, recursive=true)) %} {% endif %} {% if addOptionFn is defined and addOptionLabel is defined %} {% if options is empty %} {% set selectizeOptions = selectizeOptions|merge({ allowEmptyOption: true, }) %} {% set options = [ {value: '', label: ' '}, ] %} {% endif %} {% set options = options|push({ label: addOptionLabel , value: '__add__', data: { addOption: true, }, }) %} {% endif %} {% if includeEnvVars ?? false %} {% set options = options|merge(craft.cp.getEnvOptions(allowedEnvValues)) %} {% endif %} {% include (multi ? '_includes/forms/multiselect.twig' : '_includes/forms/select.twig') with { class: (class ?? [])|explodeClass|push('selectize')|unique, inputAttributes: { style: {display: 'none'}, autocomplete: (not multi and autocomplete is defined) ? autocomplete : false }|merge(inputAttributes ?? [], recursive=true), } %} {% js %} (() => { const id = {{ id|namespaceInputId|json_encode|raw }}; const hasData = (data, option) => { return typeof data[option] !== 'undefined' || typeof data[option.toLowerCase()] !== 'undefined'; }; const getData = (data, option) => { if (typeof data[option] !== 'undefined') { return data[option]; } return data[option.toLowerCase()]; }; const label = (data, showHint) => { let label = ''; if (hasData(data, 'addOption')) { label += ' '; } const status = (() => { if (hasData(data, 'status')) { return getData(data, 'status'); } if (hasData(data, 'boolean')) { return getData(data, 'boolean') ? 'enabled' : 'white'; } return null; })(); if (status) { label += ``; } const icon = hasData(data, 'icon') ? getData(data, 'icon') : false; const color = hasData(data, 'color') ? getData(data, 'color') : false; if (icon !== false) { const style = color ? `style="--icon-color: ${color}"` : ''; label += ` `; } else if (color !== false) { label += '