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

213 lines
6.6 KiB
JavaScript
Raw Normal View History

/*global wc_enhanced_select_params */
2015-01-12 11:56:44 +00:00
jQuery( function( $ ) {
2015-01-19 19:31:22 +00:00
function getEnhancedSelectFormatString() {
return {
'language': {
errorLoading: function() {
return wc_enhanced_select_params.i18n_ajax_error;
},
inputTooLong: function( args ) {
var overChars = args.input.length - args.maximum;
if ( 1 === overChars ) {
return wc_enhanced_select_params.i18n_input_too_long_1;
}
return wc_enhanced_select_params.i18n_input_too_long_n.replace( '%qty%', overChars );
},
inputTooShort: function( args ) {
var remainingChars = args.minimum - args.input.length;
if ( 1 === remainingChars ) {
return wc_enhanced_select_params.i18n_input_too_short_1;
}
return wc_enhanced_select_params.i18n_input_too_short_n.replace( '%qty%', remainingChars );
},
loadingMore: function() {
return wc_enhanced_select_params.i18n_load_more;
},
maximumSelected: function( args ) {
if ( args.maximum === 1 ) {
return wc_enhanced_select_params.i18n_selection_too_long_1;
}
return wc_enhanced_select_params.i18n_selection_too_long_n.replace( '%qty%', args.maximum );
},
noResults: function() {
return wc_enhanced_select_params.i18n_no_matches;
},
searching: function() {
return wc_enhanced_select_params.i18n_searching;
2015-01-19 19:31:22 +00:00
}
}
};
}
$( document.body )
2015-01-12 11:56:44 +00:00
.on( 'wc-enhanced-select-init', function() {
// Regular select boxes
$( ':input.wc-enhanced-select, :input.chosen_select' ).filter( ':not(.enhanced)' ).each( function() {
2015-01-19 19:31:22 +00:00
var select2_args = $.extend({
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-19 19:31:22 +00:00
}, getEnhancedSelectFormatString() );
$( this ).select2( select2_args ).addClass( 'enhanced' );
2015-01-12 11:56:44 +00:00
});
$( ':input.wc-enhanced-select-nostd, :input.chosen_select_nostd' ).filter( ':not(.enhanced)' ).each( function() {
2015-01-19 19:31:22 +00:00
var select2_args = $.extend({
2015-01-12 16:29:01 +00:00
minimumResultsForSearch: 10,
allowClear: true,
placeholder: $( this ).data( 'placeholder' )
2015-01-19 19:31:22 +00:00
}, getEnhancedSelectFormatString() );
$( this ).select2( select2_args ).addClass( 'enhanced' );
2015-01-12 16:29:01 +00:00
});
2015-01-12 11:56:44 +00:00
// Ajax product search box
$( ':input.wc-product-search' ).filter( ':not(.enhanced)' ).each( function() {
2015-01-12 15:43:13 +00:00
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-19 19:31:22 +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,
2016-12-21 13:23:26 +00:00
data: function( params ) {
return {
2016-12-21 13:23:26 +00:00
term: params.term,
2015-01-19 19:31:22 +00:00
action: $( this ).data( 'action' ) || 'woocommerce_json_search_products_and_variations',
security: wc_enhanced_select_params.search_products_nonce,
exclude: $( this ).data( 'exclude' ),
include: $( this ).data( 'include' ),
limit: $( this ).data( 'limit' )
};
},
2016-12-21 13:23:26 +00:00
processResults: function( data ) {
var terms = [];
if ( data ) {
2015-01-12 11:56:44 +00:00
$.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
2015-01-19 19:31:22 +00:00
select2_args = $.extend( select2_args, getEnhancedSelectFormatString() );
$( this ).select2( select2_args ).addClass( 'enhanced' );
2016-08-05 13:23:01 +00:00
if ( $( this ).data( 'sortable' ) ) {
2016-12-23 15:14:27 +00:00
var $select = $(this);
var $list = $( this ).next( '.select2-container' ).find( 'ul.select2-selection__rendered' );
$list.sortable({
placeholder : 'ui-state-highlight select2-selection__choice',
forcePlaceholderSize: true,
items : 'li:not(.select2-search__field)',
tolerance : 'pointer',
stop: function() {
$( $list.find( '.select2-selection__choice' ).get().reverse() ).each( function() {
var id = $( this ).data( 'data' ).id;
var option = $select.find( 'option[value="' + id + '"]' )[0];
$select.prepend( option );
} );
}
});
}
2015-01-12 11:56:44 +00:00
});
2015-01-12 13:04:19 +00:00
// Ajax customer search boxes
$( ':input.wc-customer-search' ).filter( ':not(.enhanced)' ).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',
2015-01-19 19:31:22 +00:00
escapeMarkup: function( m ) {
return m;
},
2015-01-12 13:04:19 +00:00
ajax: {
url: wc_enhanced_select_params.ajax_url,
dataType: 'json',
quietMillis: 250,
2016-12-21 14:18:33 +00:00
data: function( params ) {
return {
2016-12-21 14:18:33 +00:00
term: params.term,
2015-01-12 13:04:19 +00:00
action: 'woocommerce_json_search_customers',
security: wc_enhanced_select_params.search_customers_nonce,
exclude: $( this ).data( 'exclude' )
};
},
2016-12-21 14:18:33 +00:00
processResults: function( data ) {
var terms = [];
if ( data ) {
2015-01-12 13:04:19 +00:00
$.each( data, function( id, text ) {
terms.push({
id: id,
text: text
});
2015-01-12 13:04:19 +00:00
});
}
2016-12-21 14:18:33 +00:00
return {
results: terms
};
},
cache: true
}
2015-01-12 13:04:19 +00:00
};
2015-01-19 19:31:22 +00:00
select2_args = $.extend( select2_args, getEnhancedSelectFormatString() );
$( this ).select2( select2_args ).addClass( 'enhanced' );
2016-08-05 13:23:01 +00:00
if ( $( this ).data( 'sortable' ) ) {
2016-12-23 15:14:27 +00:00
var $select = $(this);
var $list = $( this ).next( '.select2-container' ).find( 'ul.select2-selection__rendered' );
$list.sortable({
placeholder : 'ui-state-highlight select2-selection__choice',
forcePlaceholderSize: true,
items : 'li:not(.select2-search__field)',
tolerance : 'pointer',
stop: function() {
$( $list.find( '.select2-selection__choice' ).get().reverse() ).each( function() {
var id = $( this ).data( 'data' ).id;
var option = $select.find( 'option[value="' + id + '"]' )[0];
$select.prepend( option );
} );
}
});
}
2015-01-12 13:04:19 +00:00
});
})
// WooCommerce Backbone Modal
.on( 'wc_backbone_modal_before_remove', function() {
2016-12-23 15:14:27 +00:00
$( '.wc-enhanced-select, :input.wc-product-search, :input.wc-customer-search' ).filter( '.select2-hidden-accessible' ).select2( 'close' );
})
2015-01-12 11:56:44 +00:00
.trigger( 'wc-enhanced-select-init' );
2016-12-21 14:23:17 +00:00
$( 'html' ).on( 'click', function( event ) {
if ( this === event.target ) {
2016-12-23 15:14:27 +00:00
$( '.wc-enhanced-select, :input.wc-product-search, :input.wc-customer-search' ).filter( '.select2-hidden-accessible' ).select2( 'close' );
2016-12-21 14:23:17 +00:00
}
} );
2015-01-19 19:31:22 +00:00
});