Files
adsPRO/libraries/adspro-select2-autoinit.js
Jacek Pyziak a1fcbbd0d2 UI: globalny auto-init Select2 z wyszukiwarka dla selectow >4 opcji
- Nowy libraries/adspro-select2-autoinit.js (auto-init na document.ready + ajaxComplete debounce 150ms)
- Wyszukiwarka odblokowana dla "Grupa reklam", "Kampania", "Klient" na /campaign_terms (data-adspro-select2="true")
- Globalne style Select2 w layout/style.scss i style.css (uogolnione z .products-page)
- Usuniety duplikat: blok CSS .products-page .select2-* i funkcja init_products_scope_select_search() w products

PAUL: phase 10-select2-global-search complete (plan 10-01)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-16 16:13:06 +02:00

154 lines
3.1 KiB
JavaScript

/**
* adsPRO — globalny auto-init Select2.
*
* Inicjalizuje Select2 na kazdym <select> z >4 opcjami (oraz na select z
* atrybutem data-adspro-select2="true"). Pomija .no-select2 i juz zainicjalizowane.
* Wlacza allowClear gdy pierwsza opcja ma value="".
*
* Helper: window.adsproSelect2Init($root) — uruchamiany takze automatycznie
* na document.ready oraz po kazdym AJAX (debounce).
*/
(function( $ )
{
if ( typeof $ === 'undefined' || typeof $.fn === 'undefined' || typeof $.fn.select2 === 'undefined' )
{
return;
}
var THRESHOLD = 4;
function should_init( $el )
{
if ( !$el.length )
{
return false;
}
if ( $el.data( 'select2' ) )
{
return false;
}
if ( $el.hasClass( 'no-select2' ) )
{
return false;
}
var force = $el.attr( 'data-adspro-select2' );
if ( force === 'false' )
{
return false;
}
if ( $el.closest( '.select2-container' ).length )
{
return false;
}
if ( $el.closest( '.dataTables_length' ).length )
{
return false;
}
if ( force === 'true' )
{
return true;
}
var option_count = $el.prop( 'options' ) ? $el.prop( 'options' ).length : $el.find( 'option' ).length;
return option_count > THRESHOLD;
}
function build_options( $el )
{
var options = { width: '100%' };
var first_option = $el.find( 'option' ).first();
var first_value = first_option.length ? first_option.attr( 'value' ) : null;
var placeholder = $el.attr( 'placeholder' );
if ( first_option.length && ( first_value === '' || typeof first_value === 'undefined' ) )
{
options.allowClear = true;
if ( !placeholder )
{
var text = $.trim( first_option.text() );
placeholder = text || 'Wybierz...';
}
}
if ( placeholder )
{
options.placeholder = placeholder;
}
if ( $.fn.select2.defaults && typeof $.fn.select2.defaults.set === 'function' )
{
// jezeli polski jezyk dostepny — nie wymuszamy globalnie, Select2 sam dobierze
}
return options;
}
function init_one( $el )
{
if ( !should_init( $el ) )
{
return;
}
try
{
$el.select2( build_options( $el ) );
$el.data( 'adspro-select2-ready', true );
}
catch ( err )
{
if ( window.console && console.warn )
{
console.warn( '[adspro-select2] init failed', err );
}
}
}
function init_all( $root )
{
var $scope = $root && $root.length ? $root : $( document.body );
var $selects = $scope.find( 'select' );
if ( $scope.is( 'select' ) )
{
$selects = $selects.add( $scope );
}
$selects.each( function()
{
init_one( $( this ) );
} );
}
window.adsproSelect2Init = init_all;
$( function()
{
init_all();
} );
var ajax_debounce_timer = null;
$( document ).ajaxComplete( function()
{
if ( ajax_debounce_timer )
{
clearTimeout( ajax_debounce_timer );
}
ajax_debounce_timer = setTimeout( function()
{
init_all();
}, 150 );
} );
})( window.jQuery );