woocommerce/assets/js/admin/wc-enhanced-select.js

138 lines
4.5 KiB
JavaScript
Raw Normal View History

2015-01-12 11:56:44 +00:00
jQuery( function( $ ) {
$('body')
.on( 'wc-enhanced-select-init', function() {
// Regular select boxes
2015-01-12 15:43:13 +00:00
$(":input.wc-enhanced-select, :input.chosen_select").each(function() {
2015-01-12 11:56:44 +00:00
$( this ).select2({
2015-01-12 13:04:19 +00:00
minimumResultsForSearch: 10,
2015-01-12 11:56:44 +00:00
allowClear: $( this ).data( 'allow_clear' ) ? true : false,
placeholder: $( this ).data( 'placeholder' )
});
});
2015-01-12 16:29:01 +00:00
$(":input.wc-enhanced-select-nostd, :input.chosen_select_nostd").each(function() {
$( this ).select2({
minimumResultsForSearch: 10,
allowClear: true,
placeholder: $( this ).data( 'placeholder' )
});
});
2015-01-12 11:56:44 +00:00
// Ajax product search box
2015-01-12 15:43:13 +00:00
$(":input.wc-product-search").each(function() {
var select2_args = {
2015-01-12 13:04:19 +00:00
allowClear: $( this ).data( 'allow_clear' ) ? true : false,
2015-01-12 11:56:44 +00:00
placeholder: $( this ).data( 'placeholder' ),
2015-01-12 13:04:19 +00:00
minimumInputLength: $( this ).data( 'minimum_input_length' ) ? $( this ).data( 'minimum_input_length' ) : '3',
2015-01-12 15:43:13 +00:00
escapeMarkup: function(m) { return m; },
2015-01-12 11:56:44 +00:00
ajax: {
url: wc_enhanced_select_params.ajax_url,
dataType: 'json',
quietMillis: 250,
data: function( term, page ) {
return {
term: term,
action: $(this).data( 'action' ) || 'woocommerce_json_search_products_and_variations',
security: wc_enhanced_select_params.search_products_nonce
};
},
results: function( data, page ) {
var terms = [];
if ( data ) {
$.each( data, function( id, text ) {
terms.push( { id: id, text: text } );
});
}
return { results: terms };
},
cache: true
2015-01-12 15:43:13 +00:00
}
};
2015-01-12 16:39:49 +00:00
if ( $( this ).data( 'multiple' ) === true ) {
2015-01-12 15:43:13 +00:00
select2_args.multiple = true;
select2_args.initSelection = function( element, callback ) {
2015-01-12 11:56:44 +00:00
var data = $.parseJSON( element.attr( 'data-selected' ) );
var selected = [];
$( element.val().split( "," ) ).each( function( i, val ) {
selected.push( { id: val, text: data[ val ] } );
});
return callback( selected );
2015-01-12 15:43:13 +00:00
};
select2_args.formatSelection = function( data ) {
2015-01-12 11:56:44 +00:00
return '<div class="selected-option" data-id="' + data.id + '">' + data.text + '</div>';
2015-01-12 15:43:13 +00:00
};
} else {
select2_args.multiple = false;
select2_args.initSelection = function( element, callback ) {
var data = {id: element.val(), text: element.attr( 'data-selected' )};
return callback( data );
};
}
$( this ).select2( select2_args );
2015-01-12 11:56:44 +00:00
});
2015-01-12 13:04:19 +00:00
// Ajax customer search boxes
2015-01-12 15:43:13 +00:00
$(":input.wc-customer-search").each(function() {
2015-01-12 13:04:19 +00:00
var select2_args = {
allowClear: $( this ).data( 'allow_clear' ) ? true : false,
placeholder: $( this ).data( 'placeholder' ),
minimumInputLength: $( this ).data( 'minimum_input_length' ) ? $( this ).data( 'minimum_input_length' ) : '3',
escapeMarkup: function(m) { return m; },
ajax: {
url: wc_enhanced_select_params.ajax_url,
dataType: 'json',
quietMillis: 250,
data: function( term, page ) {
return {
term: term,
action: 'woocommerce_json_search_customers',
security: wc_enhanced_select_params.search_customers_nonce
};
},
results: function( data, page ) {
var terms = [];
if ( data ) {
$.each( data, function( id, text ) {
terms.push( { id: id, text: text } );
});
}
return { results: terms };
},
cache: true
}
};
2015-01-12 16:39:49 +00:00
if ( $( this ).data( 'multiple' ) === true ) {
2015-01-12 13:04:19 +00:00
select2_args.multiple = true;
select2_args.initSelection = function( element, callback ) {
var data = $.parseJSON( element.attr( 'data-selected' ) );
var selected = [];
$( element.val().split( "," ) ).each( function( i, val ) {
selected.push( { id: val, text: data[ val ] } );
});
return callback( selected );
};
select2_args.formatSelection = function( data ) {
return '<div class="selected-option" data-id="' + data.id + '">' + data.text + '</div>';
};
} else {
select2_args.multiple = false;
select2_args.initSelection = function( element, callback ) {
var data = {id: element.val(), text: element.attr( 'data-selected' )};
return callback( data );
};
}
$( this ).select2( select2_args );
});
2015-01-12 11:56:44 +00:00
} )
.trigger( 'wc-enhanced-select-init' );
});