| 1 |
/*global redux_change, redux*/ |
| 2 |
|
| 3 |
(function( $ ) { |
| 4 |
"use strict"; |
| 5 |
|
| 6 |
redux.field_objects = redux.field_objects || {}; |
| 7 |
redux.field_objects.select = redux.field_objects.select || {}; |
| 8 |
|
| 9 |
redux.field_objects.select.init = function( selector ) { |
| 10 |
if ( !selector ) { |
| 11 |
selector = $( document ).find( '.redux-container-select:visible' ); |
| 12 |
} |
| 13 |
|
| 14 |
$( selector ).each( |
| 15 |
function() { |
| 16 |
var el = $( this ); |
| 17 |
var parent = el; |
| 18 |
|
| 19 |
if ( !el.hasClass( 'redux-field-container' ) ) { |
| 20 |
parent = el.parents( '.redux-field-container:first' ); |
| 21 |
} |
| 22 |
if ( parent.is( ":hidden" ) ) { // Skip hidden fields |
| 23 |
return; |
| 24 |
} |
| 25 |
if ( parent.hasClass( 'redux-field-init' ) ) { |
| 26 |
parent.removeClass( 'redux-field-init' ); |
| 27 |
} else { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
el.find( 'select.redux-select-item' ).each( |
| 32 |
function() { |
| 33 |
|
| 34 |
var default_params = { |
| 35 |
width: 'resolve', |
| 36 |
triggerChange: true, |
| 37 |
allowClear: true |
| 38 |
}; |
| 39 |
if ( $(this).attr('multiple') == "multiple" ) { |
| 40 |
default_params.width = "100%"; |
| 41 |
} |
| 42 |
|
| 43 |
if ( $( this ).siblings( '.select2_params' ).size() > 0 ) { |
| 44 |
var select2_params = $( this ).siblings( '.select2_params' ).val(); |
| 45 |
select2_params = JSON.parse( select2_params ); |
| 46 |
default_params = $.extend( {}, default_params, select2_params ); |
| 47 |
} |
| 48 |
|
| 49 |
if ( $( this ).hasClass( 'font-icons' ) ) { |
| 50 |
default_params = $.extend( |
| 51 |
{}, { |
| 52 |
formatResult: redux.field_objects.select.addIcon, |
| 53 |
formatSelection: redux.field_objects.select.addIcon, |
| 54 |
escapeMarkup: function( m ) { |
| 55 |
return m; |
| 56 |
} |
| 57 |
}, default_params |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
$( this ).select2( default_params ); |
| 62 |
|
| 63 |
if ( $( this ).hasClass( 'select2-sortable' ) ) { |
| 64 |
default_params = {}; |
| 65 |
default_params.bindOrder = 'sortableStop'; |
| 66 |
default_params.sortableOptions = {placeholder: 'ui-state-highlight'}; |
| 67 |
$( this ).select2Sortable( default_params ); |
| 68 |
} |
| 69 |
|
| 70 |
$( this ).on( |
| 71 |
"change", function() { |
| 72 |
redux_change( $( $( this ) ) ); |
| 73 |
$( this ).select2SortableOrder(); |
| 74 |
} |
| 75 |
); |
| 76 |
} |
| 77 |
); |
| 78 |
} |
| 79 |
); |
| 80 |
}; |
| 81 |
|
| 82 |
redux.field_objects.select.addIcon = function( icon ) { |
| 83 |
if ( icon.hasOwnProperty( 'id' ) ) { |
| 84 |
return "<span class='elusive'><i class='" + icon.id + "'></i>" + " " + icon.text + "</span>"; |
| 85 |
} |
| 86 |
}; |
| 87 |
})( jQuery ); |