2015-03-05 17:55:41 +00:00
|
|
|
/*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() {
|
|
|
|
var formatString = {
|
|
|
|
formatMatches: function( matches ) {
|
|
|
|
if ( 1 === matches ) {
|
2015-03-03 10:58:06 +00:00
|
|
|
return wc_enhanced_select_params.i18n_matches_1;
|
2015-01-19 19:31:22 +00:00
|
|
|
}
|
|
|
|
|
2015-03-03 10:58:06 +00:00
|
|
|
return wc_enhanced_select_params.i18n_matches_n.replace( '%qty%', matches );
|
2015-01-19 19:31:22 +00:00
|
|
|
},
|
|
|
|
formatNoMatches: function() {
|
2015-03-03 10:58:06 +00:00
|
|
|
return wc_enhanced_select_params.i18n_no_matches;
|
2015-01-19 19:31:22 +00:00
|
|
|
},
|
2015-07-08 18:33:59 +00:00
|
|
|
formatAjaxError: function() {
|
2015-03-03 10:58:06 +00:00
|
|
|
return wc_enhanced_select_params.i18n_ajax_error;
|
2015-01-19 19:31:22 +00:00
|
|
|
},
|
|
|
|
formatInputTooShort: function( input, min ) {
|
|
|
|
var number = min - input.length;
|
|
|
|
|
|
|
|
if ( 1 === number ) {
|
2015-03-05 17:55:41 +00:00
|
|
|
return wc_enhanced_select_params.i18n_input_too_short_1;
|
2015-01-19 19:31:22 +00:00
|
|
|
}
|
|
|
|
|
2015-03-03 10:58:06 +00:00
|
|
|
return wc_enhanced_select_params.i18n_input_too_short_n.replace( '%qty%', number );
|
2015-01-19 19:31:22 +00:00
|
|
|
},
|
|
|
|
formatInputTooLong: function( input, max ) {
|
|
|
|
var number = input.length - max;
|
|
|
|
|
|
|
|
if ( 1 === number ) {
|
2015-03-05 17:55:41 +00:00
|
|
|
return wc_enhanced_select_params.i18n_input_too_long_1;
|
2015-01-19 19:31:22 +00:00
|
|
|
}
|
|
|
|
|
2015-03-03 10:58:06 +00:00
|
|
|
return wc_enhanced_select_params.i18n_input_too_long_n.replace( '%qty%', number );
|
2015-01-19 19:31:22 +00:00
|
|
|
},
|
|
|
|
formatSelectionTooBig: function( limit ) {
|
|
|
|
if ( 1 === limit ) {
|
2015-03-03 10:58:06 +00:00
|
|
|
return wc_enhanced_select_params.i18n_selection_too_long_1;
|
2015-01-19 19:31:22 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 17:55:41 +00:00
|
|
|
return wc_enhanced_select_params.i18n_selection_too_long_n.replace( '%qty%', limit );
|
2015-01-19 19:31:22 +00:00
|
|
|
},
|
2015-07-08 18:33:59 +00:00
|
|
|
formatLoadMore: function() {
|
2015-03-03 10:58:06 +00:00
|
|
|
return wc_enhanced_select_params.i18n_load_more;
|
2015-01-19 19:31:22 +00:00
|
|
|
},
|
|
|
|
formatSearching: function() {
|
2015-03-03 10:58:06 +00:00
|
|
|
return wc_enhanced_select_params.i18n_searching;
|
2015-01-19 19:31:22 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return formatString;
|
|
|
|
}
|
|
|
|
|
2015-04-13 15:44:54 +00:00
|
|
|
$( document.body )
|
2015-01-12 11:56:44 +00:00
|
|
|
|
|
|
|
.on( 'wc-enhanced-select-init', function() {
|
|
|
|
|
|
|
|
// Regular select boxes
|
2015-01-20 11:46:31 +00:00
|
|
|
$( ':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() );
|
|
|
|
|
2015-01-20 11:46:31 +00:00
|
|
|
$( this ).select2( select2_args ).addClass( 'enhanced' );
|
2015-01-12 11:56:44 +00:00
|
|
|
});
|
|
|
|
|
2015-01-20 11:46:31 +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() );
|
|
|
|
|
2015-01-20 11:46:31 +00:00
|
|
|
$( 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
|
2015-01-20 11:46:31 +00:00
|
|
|
$( ':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,
|
2015-07-08 18:33:59 +00:00
|
|
|
data: function( term ) {
|
2015-01-12 11:56:44 +00:00
|
|
|
return {
|
|
|
|
term: term,
|
2015-01-19 19:31:22 +00:00
|
|
|
action: $( this ).data( 'action' ) || 'woocommerce_json_search_products_and_variations',
|
2015-01-12 11:56:44 +00:00
|
|
|
security: wc_enhanced_select_params.search_products_nonce
|
|
|
|
};
|
|
|
|
},
|
2015-07-08 18:33:59 +00:00
|
|
|
results: function( data ) {
|
2015-01-12 11:56:44 +00:00
|
|
|
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 = [];
|
|
|
|
|
2015-07-08 18:33:59 +00:00
|
|
|
$( element.val().split( ',' ) ).each( function( i, val ) {
|
2015-01-12 11:56:44 +00:00
|
|
|
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 );
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-01-19 19:31:22 +00:00
|
|
|
select2_args = $.extend( select2_args, getEnhancedSelectFormatString() );
|
|
|
|
|
2015-01-20 11:46:31 +00:00
|
|
|
$( this ).select2( select2_args ).addClass( 'enhanced' );
|
2015-01-12 11:56:44 +00:00
|
|
|
});
|
|
|
|
|
2015-01-12 13:04:19 +00:00
|
|
|
// Ajax customer search boxes
|
2015-01-20 11:46:31 +00:00
|
|
|
$( ':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,
|
2015-07-08 18:33:59 +00:00
|
|
|
data: function( term ) {
|
2015-01-12 13:04:19 +00:00
|
|
|
return {
|
|
|
|
term: term,
|
|
|
|
action: 'woocommerce_json_search_customers',
|
|
|
|
security: wc_enhanced_select_params.search_customers_nonce
|
|
|
|
};
|
|
|
|
},
|
2015-07-08 18:33:59 +00:00
|
|
|
results: function( data ) {
|
2015-01-12 13:04:19 +00:00
|
|
|
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 = [];
|
|
|
|
|
2015-03-05 17:55:41 +00:00
|
|
|
$( element.val().split( ',' ) ).each( function( i, val ) {
|
2015-01-12 13:04:19 +00:00
|
|
|
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 );
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-01-19 19:31:22 +00:00
|
|
|
select2_args = $.extend( select2_args, getEnhancedSelectFormatString() );
|
|
|
|
|
2015-01-20 11:46:31 +00:00
|
|
|
$( this ).select2( select2_args ).addClass( 'enhanced' );
|
2015-01-12 13:04:19 +00:00
|
|
|
});
|
2015-02-10 16:57:14 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// WooCommerce Backbone Modal
|
|
|
|
.on( 'wc_backbone_modal_before_remove', function() {
|
|
|
|
$( ':input.wc-enhanced-select, :input.wc-product-search, :input.wc-customer-search' ).select2( 'close' );
|
|
|
|
})
|
2015-01-12 11:56:44 +00:00
|
|
|
|
|
|
|
.trigger( 'wc-enhanced-select-init' );
|
|
|
|
|
2015-01-19 19:31:22 +00:00
|
|
|
});
|