api-keys.js
5 years ago
api-keys.min.js
2 years ago
backbone-modal.js
2 years ago
backbone-modal.min.js
2 years ago
marketplace-suggestions.js
10 months ago
marketplace-suggestions.min.js
10 months ago
meta-boxes-coupon.js
1 year ago
meta-boxes-coupon.min.js
1 year ago
meta-boxes-order.js
1 month ago
meta-boxes-order.min.js
1 month ago
meta-boxes-product-variation.js
1 year ago
meta-boxes-product-variation.min.js
1 year ago
meta-boxes-product.js
1 month ago
meta-boxes-product.min.js
1 month ago
meta-boxes.js
4 months ago
meta-boxes.min.js
4 months ago
network-orders.js
8 years ago
network-orders.min.js
2 years ago
order-attribution-admin.js
2 years ago
order-attribution-admin.min.js
2 years ago
product-editor.js
11 months ago
product-editor.min.js
11 months ago
product-ordering.js
3 months ago
product-ordering.min.js
3 months ago
quick-edit.js
1 year ago
quick-edit.min.js
1 year ago
reports.js
1 year ago
reports.min.js
1 year ago
settings-views-html-settings-tax.js
1 year ago
settings-views-html-settings-tax.min.js
1 year ago
settings.js
1 year ago
settings.min.js
1 year ago
system-status.js
3 months ago
system-status.min.js
3 months ago
term-ordering.js
3 months ago
term-ordering.min.js
3 months ago
users.js
5 years ago
users.min.js
2 years ago
variation-gallery.js
1 month ago
variation-gallery.min.js
1 month ago
wc-brands-enhanced-select.js
1 year ago
wc-brands-enhanced-select.min.js
1 year ago
wc-clipboard.js
5 years ago
wc-clipboard.min.js
5 years ago
wc-customer-stock-notifications.js
10 months ago
wc-customer-stock-notifications.min.js
10 months ago
wc-enhanced-select.js
2 years ago
wc-enhanced-select.min.js
2 years ago
wc-orders.js
3 years ago
wc-orders.min.js
2 years ago
wc-product-export.js
1 year ago
wc-product-export.min.js
1 year ago
wc-product-import.js
3 years ago
wc-product-import.min.js
2 years ago
wc-recent-reviews-widget-async.js
4 months ago
wc-recent-reviews-widget-async.min.js
4 months ago
wc-setup.js
5 years ago
wc-setup.min.js
2 years ago
wc-shipping-classes.js
1 year ago
wc-shipping-classes.min.js
1 year ago
wc-shipping-providers.js
3 months ago
wc-shipping-providers.min.js
3 months ago
wc-shipping-zone-methods.js
5 months ago
wc-shipping-zone-methods.min.js
5 months ago
wc-shipping-zones.js
1 year ago
wc-shipping-zones.min.js
1 year ago
wc-status-widget-async.js
4 months ago
wc-status-widget-async.min.js
4 months ago
wc-status-widget.js
1 year ago
wc-status-widget.min.js
1 year ago
woocommerce_admin.js
1 month ago
woocommerce_admin.min.js
1 month ago
wc-enhanced-select.js
417 lines
| 1 | /*global wc_enhanced_select_params */ |
| 2 | jQuery( function( $ ) { |
| 3 | |
| 4 | function getEnhancedSelectFormatString() { |
| 5 | return { |
| 6 | 'language': { |
| 7 | errorLoading: function() { |
| 8 | // Workaround for https://github.com/select2/select2/issues/4355 instead of i18n_ajax_error. |
| 9 | return wc_enhanced_select_params.i18n_searching; |
| 10 | }, |
| 11 | inputTooLong: function( args ) { |
| 12 | var overChars = args.input.length - args.maximum; |
| 13 | |
| 14 | if ( 1 === overChars ) { |
| 15 | return wc_enhanced_select_params.i18n_input_too_long_1; |
| 16 | } |
| 17 | |
| 18 | return wc_enhanced_select_params.i18n_input_too_long_n.replace( '%qty%', overChars ); |
| 19 | }, |
| 20 | inputTooShort: function( args ) { |
| 21 | var remainingChars = args.minimum - args.input.length; |
| 22 | |
| 23 | if ( 1 === remainingChars ) { |
| 24 | return wc_enhanced_select_params.i18n_input_too_short_1; |
| 25 | } |
| 26 | |
| 27 | return wc_enhanced_select_params.i18n_input_too_short_n.replace( '%qty%', remainingChars ); |
| 28 | }, |
| 29 | loadingMore: function() { |
| 30 | return wc_enhanced_select_params.i18n_load_more; |
| 31 | }, |
| 32 | maximumSelected: function( args ) { |
| 33 | if ( args.maximum === 1 ) { |
| 34 | return wc_enhanced_select_params.i18n_selection_too_long_1; |
| 35 | } |
| 36 | |
| 37 | return wc_enhanced_select_params.i18n_selection_too_long_n.replace( '%qty%', args.maximum ); |
| 38 | }, |
| 39 | noResults: function() { |
| 40 | return wc_enhanced_select_params.i18n_no_matches; |
| 41 | }, |
| 42 | searching: function() { |
| 43 | return wc_enhanced_select_params.i18n_searching; |
| 44 | } |
| 45 | } |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | try { |
| 50 | $( document.body ) |
| 51 | |
| 52 | .on( 'wc-enhanced-select-init', function() { |
| 53 | |
| 54 | // Regular select boxes |
| 55 | $( ':input.wc-enhanced-select, :input.chosen_select' ).filter( ':not(.enhanced)' ).each( function() { |
| 56 | var select2_args = $.extend({ |
| 57 | minimumResultsForSearch: 10, |
| 58 | allowClear: $( this ).data( 'allow_clear' ) ? true : false, |
| 59 | placeholder: $( this ).data( 'placeholder' ) |
| 60 | }, getEnhancedSelectFormatString() ); |
| 61 | |
| 62 | $( this ).selectWoo( select2_args ).addClass( 'enhanced' ); |
| 63 | }); |
| 64 | |
| 65 | $( ':input.wc-enhanced-select-nostd, :input.chosen_select_nostd' ).filter( ':not(.enhanced)' ).each( function() { |
| 66 | var select2_args = $.extend({ |
| 67 | minimumResultsForSearch: 10, |
| 68 | allowClear: true, |
| 69 | placeholder: $( this ).data( 'placeholder' ) |
| 70 | }, getEnhancedSelectFormatString() ); |
| 71 | |
| 72 | $( this ).selectWoo( select2_args ).addClass( 'enhanced' ); |
| 73 | }); |
| 74 | |
| 75 | function display_result( self, select2_args ) { |
| 76 | select2_args = $.extend( select2_args, getEnhancedSelectFormatString() ); |
| 77 | |
| 78 | $( self ).selectWoo( select2_args ).addClass( 'enhanced' ); |
| 79 | |
| 80 | if ( $( self ).data( 'sortable' ) ) { |
| 81 | var $select = $(self); |
| 82 | var $list = $( self ).next( '.select2-container' ).find( 'ul.select2-selection__rendered' ); |
| 83 | |
| 84 | $list.sortable({ |
| 85 | placeholder : 'ui-state-highlight select2-selection__choice', |
| 86 | forcePlaceholderSize: true, |
| 87 | items : 'li:not(.select2-search__field)', |
| 88 | tolerance : 'pointer', |
| 89 | stop: function() { |
| 90 | $( $list.find( '.select2-selection__choice' ).get().reverse() ).each( function() { |
| 91 | var id = $( this ).data( 'data' ).id; |
| 92 | var option = $select.find( 'option[value="' + id + '"]' )[0]; |
| 93 | $select.prepend( option ); |
| 94 | } ); |
| 95 | } |
| 96 | }); |
| 97 | // Keep multiselects ordered alphabetically if they are not sortable. |
| 98 | } else if ( $( self ).prop( 'multiple' ) ) { |
| 99 | $( self ).on( 'change', function(){ |
| 100 | var $children = $( self ).children(); |
| 101 | $children.sort(function(a, b){ |
| 102 | var atext = a.text.toLowerCase(); |
| 103 | var btext = b.text.toLowerCase(); |
| 104 | |
| 105 | if ( atext > btext ) { |
| 106 | return 1; |
| 107 | } |
| 108 | if ( atext < btext ) { |
| 109 | return -1; |
| 110 | } |
| 111 | return 0; |
| 112 | }); |
| 113 | $( self ).html( $children ); |
| 114 | }); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // Ajax product search box |
| 119 | $( ':input.wc-product-search' ).filter( ':not(.enhanced)' ).each( function() { |
| 120 | var select2_args = { |
| 121 | allowClear: $( this ).data( 'allow_clear' ) ? true : false, |
| 122 | placeholder: $( this ).data( 'placeholder' ), |
| 123 | minimumInputLength: $( this ).data( 'minimum_input_length' ) ? $( this ).data( 'minimum_input_length' ) : '3', |
| 124 | escapeMarkup: function( m ) { |
| 125 | return m; |
| 126 | }, |
| 127 | ajax: { |
| 128 | url: wc_enhanced_select_params.ajax_url, |
| 129 | dataType: 'json', |
| 130 | delay: 250, |
| 131 | data: function( params ) { |
| 132 | return { |
| 133 | term : params.term, |
| 134 | action : $( this ).data( 'action' ) || 'woocommerce_json_search_products_and_variations', |
| 135 | security : wc_enhanced_select_params.search_products_nonce, |
| 136 | exclude : $( this ).data( 'exclude' ), |
| 137 | exclude_type : $( this ).data( 'exclude_type' ), |
| 138 | include : $( this ).data( 'include' ), |
| 139 | limit : $( this ).data( 'limit' ), |
| 140 | display_stock: $( this ).data( 'display_stock' ) |
| 141 | }; |
| 142 | }, |
| 143 | processResults: function( data ) { |
| 144 | var terms = []; |
| 145 | if ( data ) { |
| 146 | $.each( data, function( id, text ) { |
| 147 | terms.push( { id: id, text: text } ); |
| 148 | }); |
| 149 | } |
| 150 | return { |
| 151 | results: terms |
| 152 | }; |
| 153 | }, |
| 154 | cache: true |
| 155 | } |
| 156 | }; |
| 157 | |
| 158 | display_result( this, select2_args ); |
| 159 | }); |
| 160 | |
| 161 | // Ajax Page Search. |
| 162 | $( ':input.wc-page-search' ).filter( ':not(.enhanced)' ).each( function() { |
| 163 | var select2_args = { |
| 164 | allowClear: $( this ).data( 'allow_clear' ) ? true : false, |
| 165 | placeholder: $( this ).data( 'placeholder' ), |
| 166 | minimumInputLength: $( this ).data( 'minimum_input_length' ) ? $( this ).data( 'minimum_input_length' ) : '3', |
| 167 | escapeMarkup: function( m ) { |
| 168 | return m; |
| 169 | }, |
| 170 | ajax: { |
| 171 | url: wc_enhanced_select_params.ajax_url, |
| 172 | dataType: 'json', |
| 173 | delay: 250, |
| 174 | data: function( params ) { |
| 175 | return { |
| 176 | term : params.term, |
| 177 | action : $( this ).data( 'action' ) || 'woocommerce_json_search_pages', |
| 178 | security : wc_enhanced_select_params.search_pages_nonce, |
| 179 | exclude : $( this ).data( 'exclude' ), |
| 180 | post_status : $( this ).data( 'post_status' ), |
| 181 | limit : $( this ).data( 'limit' ), |
| 182 | }; |
| 183 | }, |
| 184 | processResults: function( data ) { |
| 185 | var terms = []; |
| 186 | if ( data ) { |
| 187 | $.each( data, function( id, text ) { |
| 188 | terms.push( { id: id, text: text } ); |
| 189 | } ); |
| 190 | } |
| 191 | return { |
| 192 | results: terms |
| 193 | }; |
| 194 | }, |
| 195 | cache: true |
| 196 | } |
| 197 | }; |
| 198 | |
| 199 | $( this ).selectWoo( select2_args ).addClass( 'enhanced' ); |
| 200 | }); |
| 201 | |
| 202 | // Ajax customer search boxes |
| 203 | $( ':input.wc-customer-search' ).filter( ':not(.enhanced)' ).each( function() { |
| 204 | var select2_args = { |
| 205 | allowClear: $( this ).data( 'allow_clear' ) ? true : false, |
| 206 | placeholder: $( this ).data( 'placeholder' ), |
| 207 | minimumInputLength: $( this ).data( 'minimum_input_length' ) ? $( this ).data( 'minimum_input_length' ) : '1', |
| 208 | escapeMarkup: function( m ) { |
| 209 | return m; |
| 210 | }, |
| 211 | ajax: { |
| 212 | url: wc_enhanced_select_params.ajax_url, |
| 213 | dataType: 'json', |
| 214 | delay: 1000, |
| 215 | data: function( params ) { |
| 216 | return { |
| 217 | term: params.term, |
| 218 | action: 'woocommerce_json_search_customers', |
| 219 | security: wc_enhanced_select_params.search_customers_nonce, |
| 220 | exclude: $( this ).data( 'exclude' ) |
| 221 | }; |
| 222 | }, |
| 223 | processResults: function( data ) { |
| 224 | var terms = []; |
| 225 | if ( data ) { |
| 226 | $.each( data, function( id, text ) { |
| 227 | terms.push({ |
| 228 | id: id, |
| 229 | text: text |
| 230 | }); |
| 231 | }); |
| 232 | } |
| 233 | return { |
| 234 | results: terms |
| 235 | }; |
| 236 | }, |
| 237 | cache: true |
| 238 | } |
| 239 | }; |
| 240 | |
| 241 | select2_args = $.extend( select2_args, getEnhancedSelectFormatString() ); |
| 242 | |
| 243 | $( this ).selectWoo( select2_args ).addClass( 'enhanced' ); |
| 244 | |
| 245 | if ( $( this ).data( 'sortable' ) ) { |
| 246 | var $select = $(this); |
| 247 | var $list = $( this ).next( '.select2-container' ).find( 'ul.select2-selection__rendered' ); |
| 248 | |
| 249 | $list.sortable({ |
| 250 | placeholder : 'ui-state-highlight select2-selection__choice', |
| 251 | forcePlaceholderSize: true, |
| 252 | items : 'li:not(.select2-search__field)', |
| 253 | tolerance : 'pointer', |
| 254 | stop: function() { |
| 255 | $( $list.find( '.select2-selection__choice' ).get().reverse() ).each( function() { |
| 256 | var id = $( this ).data( 'data' ).id; |
| 257 | var option = $select.find( 'option[value="' + id + '"]' )[0]; |
| 258 | $select.prepend( option ); |
| 259 | } ); |
| 260 | } |
| 261 | }); |
| 262 | } |
| 263 | }); |
| 264 | |
| 265 | // Ajax category search boxes |
| 266 | $( ':input.wc-category-search' ).filter( ':not(.enhanced)' ).each( function() { |
| 267 | var return_format = $( this ).data( 'return_id' ) ? 'id' : 'slug'; |
| 268 | |
| 269 | var select2_args = $.extend( { |
| 270 | allowClear : $( this ).data( 'allow_clear' ) ? true : false, |
| 271 | placeholder : $( this ).data( 'placeholder' ), |
| 272 | minimumInputLength: $( this ).data( 'minimum_input_length' ) ? $( this ).data( 'minimum_input_length' ) : '3', |
| 273 | escapeMarkup : function( m ) { |
| 274 | return m; |
| 275 | }, |
| 276 | ajax: { |
| 277 | url: wc_enhanced_select_params.ajax_url, |
| 278 | dataType: 'json', |
| 279 | delay: 250, |
| 280 | data: function( params ) { |
| 281 | return { |
| 282 | term: params.term, |
| 283 | action: 'woocommerce_json_search_categories', |
| 284 | security: wc_enhanced_select_params.search_categories_nonce |
| 285 | }; |
| 286 | }, |
| 287 | processResults: function( data ) { |
| 288 | var terms = []; |
| 289 | if ( data ) { |
| 290 | $.each( data, function( id, term ) { |
| 291 | terms.push({ |
| 292 | id: 'id' === return_format ? term.term_id : term.slug, |
| 293 | text: term.formatted_name |
| 294 | }); |
| 295 | }); |
| 296 | } |
| 297 | return { |
| 298 | results: terms |
| 299 | }; |
| 300 | }, |
| 301 | cache: true |
| 302 | } |
| 303 | }, getEnhancedSelectFormatString() ); |
| 304 | |
| 305 | $( this ).selectWoo( select2_args ).addClass( 'enhanced' ); |
| 306 | }); |
| 307 | |
| 308 | // Ajax category search boxes |
| 309 | $( ':input.wc-taxonomy-term-search' ).filter( ':not(.enhanced)' ).each( function() { |
| 310 | var return_format = $( this ).data( 'return_id' ) ? 'id' : 'slug'; |
| 311 | |
| 312 | var select2_args = $.extend( { |
| 313 | allowClear : $( this ).data( 'allow_clear' ) ? true : false, |
| 314 | placeholder : $( this ).data( 'placeholder' ), |
| 315 | minimumInputLength: $( this ).data( 'minimum_input_length' ) !== null && $( this ).data( 'minimum_input_length' ) !== undefined ? $( this ).data( 'minimum_input_length' ) : '3', |
| 316 | escapeMarkup : function( m ) { |
| 317 | return m; |
| 318 | }, |
| 319 | ajax: { |
| 320 | url: wc_enhanced_select_params.ajax_url, |
| 321 | dataType: 'json', |
| 322 | delay: 250, |
| 323 | data: function( params ) { |
| 324 | return { |
| 325 | taxonomy: $( this ).data( 'taxonomy' ), |
| 326 | limit: $( this ).data( 'limit' ), |
| 327 | orderby: $( this ).data( 'orderby'), |
| 328 | term: params.term, |
| 329 | action: 'woocommerce_json_search_taxonomy_terms', |
| 330 | security: wc_enhanced_select_params.search_taxonomy_terms_nonce |
| 331 | }; |
| 332 | }, |
| 333 | processResults: function( data ) { |
| 334 | var terms = []; |
| 335 | if ( data ) { |
| 336 | $.each( data, function( id, term ) { |
| 337 | terms.push({ |
| 338 | id: 'id' === return_format ? term.term_id : term.slug, |
| 339 | text: term.name |
| 340 | }); |
| 341 | }); |
| 342 | } |
| 343 | return { |
| 344 | results: terms |
| 345 | }; |
| 346 | }, |
| 347 | cache: true |
| 348 | } |
| 349 | }, getEnhancedSelectFormatString() ); |
| 350 | |
| 351 | $( this ).selectWoo( select2_args ).addClass( 'enhanced' ); |
| 352 | }); |
| 353 | |
| 354 | $( ':input.wc-attribute-search' ).filter( ':not(.enhanced)' ).each( function() { |
| 355 | var select2Element = this; |
| 356 | var select2_args = $.extend( { |
| 357 | allowClear : $( this ).data( 'allow_clear' ) ? true : false, |
| 358 | placeholder : $( this ).data( 'placeholder' ), |
| 359 | minimumInputLength: $( this ).data( 'minimum_input_length' ) !== null && $( this ).data( 'minimum_input_length' ) !== undefined ? $( this ).data( 'minimum_input_length' ) : '3', |
| 360 | escapeMarkup : function( m ) { |
| 361 | return m; |
| 362 | }, |
| 363 | ajax: { |
| 364 | url: wc_enhanced_select_params.ajax_url, |
| 365 | dataType: 'json', |
| 366 | delay: 250, |
| 367 | data: function( params ) { |
| 368 | return { |
| 369 | term: params.term, |
| 370 | action: 'woocommerce_json_search_product_attributes', |
| 371 | security: wc_enhanced_select_params.search_product_attributes_nonce |
| 372 | }; |
| 373 | }, |
| 374 | processResults: function( data ) { |
| 375 | var disabledItems = $( select2Element ).data('disabled-items') || []; |
| 376 | var terms = []; |
| 377 | if ( data ) { |
| 378 | $.each( data, function( id, term ) { |
| 379 | terms.push({ |
| 380 | id: term.slug, |
| 381 | text: term.name, |
| 382 | disabled: disabledItems.includes( term.slug ) |
| 383 | }); |
| 384 | }); |
| 385 | } |
| 386 | return { |
| 387 | results: terms |
| 388 | }; |
| 389 | }, |
| 390 | cache: true |
| 391 | } |
| 392 | }, getEnhancedSelectFormatString() ); |
| 393 | |
| 394 | $( this ).selectWoo( select2_args ).addClass( 'enhanced' ); |
| 395 | }); |
| 396 | }) |
| 397 | |
| 398 | // WooCommerce Backbone Modal |
| 399 | .on( 'wc_backbone_modal_before_remove', function() { |
| 400 | $( '.wc-enhanced-select, :input.wc-product-search, :input.wc-customer-search' ).filter( '.select2-hidden-accessible' ) |
| 401 | .selectWoo( 'close' ); |
| 402 | }) |
| 403 | |
| 404 | .trigger( 'wc-enhanced-select-init' ); |
| 405 | |
| 406 | $( 'html' ).on( 'click', function( event ) { |
| 407 | if ( this === event.target ) { |
| 408 | $( '.wc-enhanced-select, :input.wc-product-search, :input.wc-customer-search' ).filter( '.select2-hidden-accessible' ) |
| 409 | .selectWoo( 'close' ); |
| 410 | } |
| 411 | } ); |
| 412 | } catch( err ) { |
| 413 | // If select2 failed (conflict?) log the error but don't stop other scripts breaking. |
| 414 | window.console.log( err ); |
| 415 | } |
| 416 | }); |
| 417 |