bindings
6 months ago
commands
1 week ago
pro
1 week ago
_acf-compatibility.js
1 year ago
_acf-condition-types.js
7 months ago
_acf-condition.js
1 year ago
_acf-conditions.js
1 year ago
_acf-field-accordion.js
6 months ago
_acf-field-button-group.js
7 months ago
_acf-field-checkbox.js
1 month ago
_acf-field-color-picker.js
7 months ago
_acf-field-date-picker.js
1 month ago
_acf-field-date-time-picker.js
1 month ago
_acf-field-file.js
1 month ago
_acf-field-google-map.js
6 months ago
_acf-field-icon-picker.js
1 month ago
_acf-field-image.js
1 month ago
_acf-field-link.js
1 year ago
_acf-field-oembed.js
1 month ago
_acf-field-page-link.js
1 year ago
_acf-field-post-object.js
1 year ago
_acf-field-radio.js
1 month ago
_acf-field-range.js
1 year ago
_acf-field-relationship.js
1 month ago
_acf-field-select.js
1 month ago
_acf-field-tab.js
3 weeks ago
_acf-field-taxonomy.js
7 months ago
_acf-field-time-picker.js
1 month ago
_acf-field-true-false.js
1 month ago
_acf-field-url.js
1 year ago
_acf-field-user.js
1 year ago
_acf-field-wysiwyg.js
1 month ago
_acf-field.js
1 year ago
_acf-fields.js
1 year ago
_acf-helpers.js
1 year ago
_acf-hooks.js
1 year ago
_acf-internal-post-type.js
7 months ago
_acf-media.js
1 week ago
_acf-modal.js
1 year ago
_acf-model.js
1 year ago
_acf-notice.js
7 months ago
_acf-panel.js
1 year ago
_acf-popup.js
7 months ago
_acf-postbox.js
1 year ago
_acf-screen.js
10 months ago
_acf-select2.js
1 year ago
_acf-tinymce.js
6 months ago
_acf-tooltip.js
10 months ago
_acf-unload.js
1 year ago
_acf-validation.js
1 month ago
_acf.js
7 months ago
_browse-fields-modal.js
7 months ago
_field-group-compatibility.js
1 year ago
_field-group-conditions.js
1 year ago
_field-group-field.js
3 months ago
_field-group-fields.js
10 months ago
_field-group-locations.js
1 year ago
_field-group-settings.js
1 year ago
_field-group.js
10 months ago
acf-escaped-html-notice.js
1 year ago
acf-field-group.js
1 year ago
acf-input.js
1 year ago
acf-internal-post-type.js
1 year ago
acf.js
1 year ago
_acf-select2.js
916 lines
| 1 | ( function ( $, undefined ) { |
| 2 | /** |
| 3 | * acf.newSelect2 |
| 4 | * |
| 5 | * description |
| 6 | * |
| 7 | * @date 13/1/18 |
| 8 | * @since ACF 5.6.5 |
| 9 | * |
| 10 | * @param type $var Description. Default. |
| 11 | * @return type Description. |
| 12 | */ |
| 13 | |
| 14 | acf.newSelect2 = function ( $select, props ) { |
| 15 | // defaults |
| 16 | props = acf.parseArgs( props, { |
| 17 | allowNull: false, |
| 18 | placeholder: '', |
| 19 | multiple: false, |
| 20 | field: false, |
| 21 | ajax: false, |
| 22 | tags: false, |
| 23 | ajaxAction: '', |
| 24 | ajaxData: function ( data ) { |
| 25 | return data; |
| 26 | }, |
| 27 | ajaxResults: function ( json ) { |
| 28 | return json; |
| 29 | }, |
| 30 | escapeMarkup: false, |
| 31 | templateSelection: false, |
| 32 | templateResult: false, |
| 33 | dropdownCssClass: '', |
| 34 | suppressFilters: false, |
| 35 | } ); |
| 36 | |
| 37 | // initialize |
| 38 | if ( getVersion() == 4 ) { |
| 39 | var select2 = new Select2_4( $select, props ); |
| 40 | } else { |
| 41 | var select2 = new Select2_3( $select, props ); |
| 42 | } |
| 43 | |
| 44 | // actions |
| 45 | acf.doAction( 'new_select2', select2 ); |
| 46 | |
| 47 | // return |
| 48 | return select2; |
| 49 | }; |
| 50 | |
| 51 | /** |
| 52 | * getVersion |
| 53 | * |
| 54 | * description |
| 55 | * |
| 56 | * @date 13/1/18 |
| 57 | * @since ACF 5.6.5 |
| 58 | * |
| 59 | * @param type $var Description. Default. |
| 60 | * @return type Description. |
| 61 | */ |
| 62 | |
| 63 | function getVersion() { |
| 64 | // v4 |
| 65 | if ( acf.isset( window, 'jQuery', 'fn', 'select2', 'amd' ) ) { |
| 66 | return 4; |
| 67 | } |
| 68 | |
| 69 | // v3 |
| 70 | if ( acf.isset( window, 'Select2' ) ) { |
| 71 | return 3; |
| 72 | } |
| 73 | |
| 74 | // return |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Select2 |
| 80 | * |
| 81 | * description |
| 82 | * |
| 83 | * @date 13/1/18 |
| 84 | * @since ACF 5.6.5 |
| 85 | * |
| 86 | * @param type $var Description. Default. |
| 87 | * @return type Description. |
| 88 | */ |
| 89 | |
| 90 | var Select2 = acf.Model.extend( { |
| 91 | setup: function ( $select, props ) { |
| 92 | $.extend( this.data, props ); |
| 93 | this.$el = $select; |
| 94 | }, |
| 95 | |
| 96 | initialize: function () {}, |
| 97 | |
| 98 | selectOption: function ( value ) { |
| 99 | var $option = this.getOption( value ); |
| 100 | if ( ! $option.prop( 'selected' ) ) { |
| 101 | $option.prop( 'selected', true ).trigger( 'change' ); |
| 102 | } |
| 103 | }, |
| 104 | |
| 105 | unselectOption: function ( value ) { |
| 106 | var $option = this.getOption( value ); |
| 107 | if ( $option.prop( 'selected' ) ) { |
| 108 | $option.prop( 'selected', false ).trigger( 'change' ); |
| 109 | } |
| 110 | }, |
| 111 | |
| 112 | getOption: function ( value ) { |
| 113 | return this.$( 'option[value="' + value + '"]' ); |
| 114 | }, |
| 115 | |
| 116 | addOption: function ( option ) { |
| 117 | // defaults |
| 118 | option = acf.parseArgs( option, { |
| 119 | id: '', |
| 120 | text: '', |
| 121 | selected: false, |
| 122 | } ); |
| 123 | |
| 124 | // vars |
| 125 | var $option = this.getOption( option.id ); |
| 126 | |
| 127 | // append |
| 128 | if ( ! $option.length ) { |
| 129 | $option = $( '<option></option>' ); |
| 130 | $option.html( option.text ); |
| 131 | $option.attr( 'value', option.id ); |
| 132 | $option.prop( 'selected', option.selected ); |
| 133 | this.$el.append( $option ); |
| 134 | } |
| 135 | |
| 136 | // chain |
| 137 | return $option; |
| 138 | }, |
| 139 | |
| 140 | getValue: function () { |
| 141 | // vars |
| 142 | var val = []; |
| 143 | var $options = this.$el.find( 'option:selected' ); |
| 144 | |
| 145 | // bail early if no selected |
| 146 | if ( ! $options.exists() ) { |
| 147 | return val; |
| 148 | } |
| 149 | |
| 150 | // sort by attribute |
| 151 | $options = $options.sort( function ( a, b ) { |
| 152 | return ( |
| 153 | +a.getAttribute( 'data-i' ) - +b.getAttribute( 'data-i' ) |
| 154 | ); |
| 155 | } ); |
| 156 | |
| 157 | // loop |
| 158 | $options.each( function () { |
| 159 | var $el = $( this ); |
| 160 | val.push( { |
| 161 | $el: $el, |
| 162 | id: $el.attr( 'value' ), |
| 163 | text: $el.text(), |
| 164 | } ); |
| 165 | } ); |
| 166 | |
| 167 | // return |
| 168 | return val; |
| 169 | }, |
| 170 | |
| 171 | mergeOptions: function () {}, |
| 172 | |
| 173 | getChoices: function () { |
| 174 | // callback |
| 175 | var crawl = function ( $parent ) { |
| 176 | // vars |
| 177 | var choices = []; |
| 178 | |
| 179 | // loop |
| 180 | $parent.children().each( function () { |
| 181 | // vars |
| 182 | var $child = $( this ); |
| 183 | |
| 184 | // optgroup |
| 185 | if ( $child.is( 'optgroup' ) ) { |
| 186 | choices.push( { |
| 187 | text: $child.attr( 'label' ), |
| 188 | children: crawl( $child ), |
| 189 | } ); |
| 190 | |
| 191 | // option |
| 192 | } else { |
| 193 | choices.push( { |
| 194 | id: $child.attr( 'value' ), |
| 195 | text: $child.text(), |
| 196 | } ); |
| 197 | } |
| 198 | } ); |
| 199 | |
| 200 | // return |
| 201 | return choices; |
| 202 | }; |
| 203 | |
| 204 | // crawl |
| 205 | return crawl( this.$el ); |
| 206 | }, |
| 207 | |
| 208 | getAjaxData: function ( params ) { |
| 209 | // vars |
| 210 | var ajaxData = { |
| 211 | action: this.get( 'ajaxAction' ), |
| 212 | s: params.term || '', |
| 213 | paged: params.page || 1, |
| 214 | }; |
| 215 | |
| 216 | // field helper |
| 217 | var field = this.get( 'field' ); |
| 218 | if ( field ) { |
| 219 | ajaxData.field_key = field.get( 'key' ); |
| 220 | |
| 221 | if ( field.get( 'nonce' ) ) { |
| 222 | ajaxData.nonce = field.get( 'nonce' ); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // callback |
| 227 | var callback = this.get( 'ajaxData' ); |
| 228 | if ( callback ) { |
| 229 | ajaxData = callback.apply( this, [ ajaxData, params ] ); |
| 230 | } |
| 231 | |
| 232 | // filter |
| 233 | ajaxData = acf.applyFilters( |
| 234 | 'select2_ajax_data', |
| 235 | ajaxData, |
| 236 | this.data, |
| 237 | this.$el, |
| 238 | field || false, |
| 239 | this |
| 240 | ); |
| 241 | |
| 242 | // return |
| 243 | return acf.prepareForAjax( ajaxData ); |
| 244 | }, |
| 245 | |
| 246 | getAjaxResults: function ( json, params ) { |
| 247 | // defaults |
| 248 | json = acf.parseArgs( json, { |
| 249 | results: false, |
| 250 | more: false, |
| 251 | } ); |
| 252 | |
| 253 | // callback |
| 254 | var callback = this.get( 'ajaxResults' ); |
| 255 | if ( callback ) { |
| 256 | json = callback.apply( this, [ json, params ] ); |
| 257 | } |
| 258 | |
| 259 | // filter |
| 260 | json = acf.applyFilters( |
| 261 | 'select2_ajax_results', |
| 262 | json, |
| 263 | params, |
| 264 | this |
| 265 | ); |
| 266 | |
| 267 | // return |
| 268 | return json; |
| 269 | }, |
| 270 | |
| 271 | processAjaxResults: function ( json, params ) { |
| 272 | // vars |
| 273 | var json = this.getAjaxResults( json, params ); |
| 274 | |
| 275 | // change more to pagination |
| 276 | if ( json.more ) { |
| 277 | json.pagination = { more: true }; |
| 278 | } |
| 279 | |
| 280 | // merge together groups |
| 281 | setTimeout( $.proxy( this.mergeOptions, this ), 1 ); |
| 282 | |
| 283 | // return |
| 284 | return json; |
| 285 | }, |
| 286 | |
| 287 | destroy: function () { |
| 288 | // destroy via api |
| 289 | if ( this.$el.data( 'select2' ) ) { |
| 290 | this.$el.select2( 'destroy' ); |
| 291 | } |
| 292 | |
| 293 | // destroy via HTML (duplicating HTML does not contain data) |
| 294 | this.$el.siblings( '.select2-container' ).remove(); |
| 295 | }, |
| 296 | } ); |
| 297 | |
| 298 | /** |
| 299 | * Select2_4 |
| 300 | * |
| 301 | * description |
| 302 | * |
| 303 | * @date 13/1/18 |
| 304 | * @since ACF 5.6.5 |
| 305 | * |
| 306 | * @param type $var Description. Default. |
| 307 | * @return type Description. |
| 308 | */ |
| 309 | |
| 310 | var Select2_4 = Select2.extend( { |
| 311 | initialize: function () { |
| 312 | // vars |
| 313 | var $select = this.$el; |
| 314 | var options = { |
| 315 | width: '100%', |
| 316 | allowClear: this.get( 'allowNull' ), |
| 317 | placeholder: this.get( 'placeholder' ), |
| 318 | multiple: this.get( 'multiple' ), |
| 319 | escapeMarkup: this.get( 'escapeMarkup' ), |
| 320 | templateSelection: this.get( 'templateSelection' ), |
| 321 | templateResult: this.get( 'templateResult' ), |
| 322 | dropdownCssClass: this.get( 'dropdownCssClass' ), |
| 323 | suppressFilters: this.get( 'suppressFilters' ), |
| 324 | data: [], |
| 325 | }; |
| 326 | |
| 327 | if ( this.get( 'tags' ) ) { |
| 328 | options.tags = true; |
| 329 | options.tokenSeparators = [ ',' ]; |
| 330 | } |
| 331 | |
| 332 | // Clear empty templateSelections, templateResults, or dropdownCssClass. |
| 333 | if ( ! options.templateSelection ) { |
| 334 | delete options.templateSelection; |
| 335 | } |
| 336 | if ( ! options.templateResult ) { |
| 337 | delete options.templateResult; |
| 338 | } |
| 339 | if ( ! options.dropdownCssClass ) { |
| 340 | delete options.dropdownCssClass; |
| 341 | } |
| 342 | |
| 343 | // Only use the template if SelectWoo is not loaded to work around https://github.com/woocommerce/woocommerce/pull/30473 |
| 344 | if ( ! acf.isset( window, 'jQuery', 'fn', 'selectWoo' ) ) { |
| 345 | if ( ! options.templateSelection ) { |
| 346 | options.templateSelection = function ( selection ) { |
| 347 | var $selection = $( |
| 348 | '<span class="acf-selection"></span>' |
| 349 | ); |
| 350 | $selection.html( |
| 351 | options.escapeMarkup( selection.text ) |
| 352 | ); |
| 353 | $selection.data( 'element', selection.element ); |
| 354 | return $selection; |
| 355 | }; |
| 356 | } |
| 357 | } else { |
| 358 | delete options.templateSelection; |
| 359 | delete options.templateResult; |
| 360 | } |
| 361 | |
| 362 | // Use a default, filterable escapeMarkup if not provided. |
| 363 | if ( ! options.escapeMarkup ) { |
| 364 | options.escapeMarkup = function ( markup ) { |
| 365 | if ( typeof markup !== 'string' ) { |
| 366 | return markup; |
| 367 | } |
| 368 | |
| 369 | if ( this.suppressFilters ) { |
| 370 | return acf.strEscape( markup ); |
| 371 | } |
| 372 | |
| 373 | return acf.applyFilters( |
| 374 | 'select2_escape_markup', |
| 375 | acf.strEscape( markup ), |
| 376 | markup, |
| 377 | $select, |
| 378 | this.data, |
| 379 | field || false, |
| 380 | this |
| 381 | ); |
| 382 | }; |
| 383 | } |
| 384 | |
| 385 | // multiple |
| 386 | if ( options.multiple ) { |
| 387 | // reorder options |
| 388 | this.getValue().map( function ( item ) { |
| 389 | item.$el.detach().appendTo( $select ); |
| 390 | } ); |
| 391 | } |
| 392 | |
| 393 | // Temporarily remove conflicting attribute. |
| 394 | var attrAjax = $select.attr( 'data-ajax' ); |
| 395 | if ( attrAjax !== undefined ) { |
| 396 | $select.removeData( 'ajax' ); |
| 397 | $select.removeAttr( 'data-ajax' ); |
| 398 | } |
| 399 | |
| 400 | // ajax |
| 401 | if ( this.get( 'ajax' ) ) { |
| 402 | options.ajax = { |
| 403 | url: acf.get( 'ajaxurl' ), |
| 404 | delay: 250, |
| 405 | dataType: 'json', |
| 406 | type: 'post', |
| 407 | cache: false, |
| 408 | data: $.proxy( this.getAjaxData, this ), |
| 409 | processResults: $.proxy( this.processAjaxResults, this ), |
| 410 | }; |
| 411 | } |
| 412 | |
| 413 | // filter for 3rd party customization |
| 414 | if ( ! options.suppressFilters ) { |
| 415 | var field = this.get( 'field' ); |
| 416 | options = acf.applyFilters( |
| 417 | 'select2_args', |
| 418 | options, |
| 419 | $select, |
| 420 | this.data, |
| 421 | field || false, |
| 422 | this |
| 423 | ); |
| 424 | } |
| 425 | // add select2 |
| 426 | $select.select2( options ); |
| 427 | |
| 428 | // get container (Select2 v4 does not return this from constructor) |
| 429 | var $container = $select.next( '.select2-container' ); |
| 430 | |
| 431 | // multiple |
| 432 | if ( options.multiple ) { |
| 433 | // vars |
| 434 | var $ul = $container.find( 'ul' ); |
| 435 | |
| 436 | // sortable |
| 437 | $ul.sortable( { |
| 438 | stop: function ( e ) { |
| 439 | // loop |
| 440 | $ul.find( '.select2-selection__choice' ).each( |
| 441 | function () { |
| 442 | // Attempt to use .data if it exists (select2 version < 4.0.6) or use our template data instead. |
| 443 | if ( $( this ).data( 'data' ) ) { |
| 444 | var $option = $( |
| 445 | $( this ).data( 'data' ).element |
| 446 | ); |
| 447 | } else { |
| 448 | var $option = $( |
| 449 | $( this ) |
| 450 | .find( 'span.acf-selection' ) |
| 451 | .data( 'element' ) |
| 452 | ); |
| 453 | } |
| 454 | |
| 455 | // detach and re-append to end |
| 456 | $option.detach().appendTo( $select ); |
| 457 | } |
| 458 | ); |
| 459 | |
| 460 | // trigger change on input (JS error if trigger on select) |
| 461 | $select.trigger( 'change' ); |
| 462 | }, |
| 463 | } ); |
| 464 | |
| 465 | // on select, move to end |
| 466 | $select.on( |
| 467 | 'select2:select', |
| 468 | this.proxy( function ( e ) { |
| 469 | this.getOption( e.params.data.id ) |
| 470 | .detach() |
| 471 | .appendTo( this.$el ); |
| 472 | } ) |
| 473 | ); |
| 474 | } |
| 475 | |
| 476 | // add handler to auto-focus searchbox (for jQuery 3.6) |
| 477 | $select.on( 'select2:open', () => { |
| 478 | $( '.select2-container--open .select2-search__field' ) |
| 479 | .get( -1 ) |
| 480 | .focus(); |
| 481 | } ); |
| 482 | |
| 483 | // add class |
| 484 | $container.addClass( '-acf' ); |
| 485 | |
| 486 | // Add back temporarily removed attr. |
| 487 | if ( attrAjax !== undefined ) { |
| 488 | $select.attr( 'data-ajax', attrAjax ); |
| 489 | } |
| 490 | |
| 491 | // action for 3rd party customization |
| 492 | if ( ! options.suppressFilters ) { |
| 493 | acf.doAction( |
| 494 | 'select2_init', |
| 495 | $select, |
| 496 | options, |
| 497 | this.data, |
| 498 | field || false, |
| 499 | this |
| 500 | ); |
| 501 | } |
| 502 | }, |
| 503 | |
| 504 | mergeOptions: function () { |
| 505 | // vars |
| 506 | var $prevOptions = false; |
| 507 | var $prevGroup = false; |
| 508 | |
| 509 | // loop |
| 510 | $( '.select2-results__option[role="group"]' ).each( function () { |
| 511 | // vars |
| 512 | var $options = $( this ).children( 'ul' ); |
| 513 | var $group = $( this ).children( 'strong' ); |
| 514 | |
| 515 | // compare to previous |
| 516 | if ( $prevGroup && $prevGroup.text() === $group.text() ) { |
| 517 | $prevOptions.append( $options.children() ); |
| 518 | $( this ).remove(); |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | // update vars |
| 523 | $prevOptions = $options; |
| 524 | $prevGroup = $group; |
| 525 | } ); |
| 526 | }, |
| 527 | } ); |
| 528 | |
| 529 | /** |
| 530 | * Select2_3 |
| 531 | * |
| 532 | * description |
| 533 | * |
| 534 | * @date 13/1/18 |
| 535 | * @since ACF 5.6.5 |
| 536 | * |
| 537 | * @param type $var Description. Default. |
| 538 | * @return type Description. |
| 539 | */ |
| 540 | |
| 541 | var Select2_3 = Select2.extend( { |
| 542 | initialize: function () { |
| 543 | // vars |
| 544 | var $select = this.$el; |
| 545 | var value = this.getValue(); |
| 546 | var multiple = this.get( 'multiple' ); |
| 547 | var options = { |
| 548 | width: '100%', |
| 549 | allowClear: this.get( 'allowNull' ), |
| 550 | placeholder: this.get( 'placeholder' ), |
| 551 | separator: '||', |
| 552 | multiple: this.get( 'multiple' ), |
| 553 | data: this.getChoices(), |
| 554 | escapeMarkup: function ( string ) { |
| 555 | return acf.escHtml( string ); |
| 556 | }, |
| 557 | dropdownCss: { |
| 558 | 'z-index': '999999999', |
| 559 | }, |
| 560 | initSelection: function ( element, callback ) { |
| 561 | if ( multiple ) { |
| 562 | callback( value ); |
| 563 | } else { |
| 564 | callback( value.shift() ); |
| 565 | } |
| 566 | }, |
| 567 | }; |
| 568 | // get hidden input |
| 569 | var $input = $select.siblings( 'input' ); |
| 570 | if ( ! $input.length ) { |
| 571 | $input = $( '<input type="hidden" />' ); |
| 572 | $select.before( $input ); |
| 573 | } |
| 574 | |
| 575 | // set input value |
| 576 | inputValue = value |
| 577 | .map( function ( item ) { |
| 578 | return item.id; |
| 579 | } ) |
| 580 | .join( '||' ); |
| 581 | $input.val( inputValue ); |
| 582 | |
| 583 | // multiple |
| 584 | if ( options.multiple ) { |
| 585 | // reorder options |
| 586 | value.map( function ( item ) { |
| 587 | item.$el.detach().appendTo( $select ); |
| 588 | } ); |
| 589 | } |
| 590 | |
| 591 | // remove blank option as we have a clear all button |
| 592 | if ( options.allowClear ) { |
| 593 | options.data = options.data.filter( function ( item ) { |
| 594 | return item.id !== ''; |
| 595 | } ); |
| 596 | } |
| 597 | |
| 598 | // remove conflicting atts |
| 599 | $select.removeData( 'ajax' ); |
| 600 | $select.removeAttr( 'data-ajax' ); |
| 601 | |
| 602 | // ajax |
| 603 | if ( this.get( 'ajax' ) ) { |
| 604 | options.ajax = { |
| 605 | url: acf.get( 'ajaxurl' ), |
| 606 | quietMillis: 250, |
| 607 | dataType: 'json', |
| 608 | type: 'post', |
| 609 | cache: false, |
| 610 | data: $.proxy( this.getAjaxData, this ), |
| 611 | results: $.proxy( this.processAjaxResults, this ), |
| 612 | }; |
| 613 | } |
| 614 | |
| 615 | // filter for 3rd party customization |
| 616 | var field = this.get( 'field' ); |
| 617 | options = acf.applyFilters( |
| 618 | 'select2_args', |
| 619 | options, |
| 620 | $select, |
| 621 | this.data, |
| 622 | field || false, |
| 623 | this |
| 624 | ); |
| 625 | |
| 626 | // add select2 |
| 627 | $input.select2( options ); |
| 628 | |
| 629 | // get container |
| 630 | var $container = $input.select2( 'container' ); |
| 631 | |
| 632 | // helper to find this select's option |
| 633 | var getOption = $.proxy( this.getOption, this ); |
| 634 | |
| 635 | // multiple |
| 636 | if ( options.multiple ) { |
| 637 | // vars |
| 638 | var $ul = $container.find( 'ul' ); |
| 639 | |
| 640 | // sortable |
| 641 | $ul.sortable( { |
| 642 | stop: function () { |
| 643 | // loop |
| 644 | $ul.find( '.select2-search-choice' ).each( function () { |
| 645 | // vars |
| 646 | var data = $( this ).data( 'select2Data' ); |
| 647 | var $option = getOption( data.id ); |
| 648 | |
| 649 | // detach and re-append to end |
| 650 | $option.detach().appendTo( $select ); |
| 651 | } ); |
| 652 | |
| 653 | // trigger change on input (JS error if trigger on select) |
| 654 | $select.trigger( 'change' ); |
| 655 | }, |
| 656 | } ); |
| 657 | } |
| 658 | |
| 659 | // on select, create option and move to end |
| 660 | $input.on( 'select2-selecting', function ( e ) { |
| 661 | // vars |
| 662 | var item = e.choice; |
| 663 | var $option = getOption( item.id ); |
| 664 | |
| 665 | // create if doesn't exist |
| 666 | if ( ! $option.length ) { |
| 667 | $option = $( |
| 668 | '<option value="' + |
| 669 | item.id + |
| 670 | '">' + |
| 671 | item.text + |
| 672 | '</option>' |
| 673 | ); |
| 674 | } |
| 675 | |
| 676 | // detach and re-append to end |
| 677 | $option.detach().appendTo( $select ); |
| 678 | } ); |
| 679 | |
| 680 | // add class |
| 681 | $container.addClass( '-acf' ); |
| 682 | |
| 683 | // action for 3rd party customization |
| 684 | acf.doAction( |
| 685 | 'select2_init', |
| 686 | $select, |
| 687 | options, |
| 688 | this.data, |
| 689 | field || false, |
| 690 | this |
| 691 | ); |
| 692 | |
| 693 | // change |
| 694 | $input.on( 'change', function () { |
| 695 | var val = $input.val(); |
| 696 | if ( val.indexOf( '||' ) ) { |
| 697 | val = val.split( '||' ); |
| 698 | } |
| 699 | $select.val( val ).trigger( 'change' ); |
| 700 | } ); |
| 701 | |
| 702 | // hide select |
| 703 | $select.hide(); |
| 704 | }, |
| 705 | |
| 706 | mergeOptions: function () { |
| 707 | // vars |
| 708 | var $prevOptions = false; |
| 709 | var $prevGroup = false; |
| 710 | |
| 711 | // loop |
| 712 | $( '#select2-drop .select2-result-with-children' ).each( |
| 713 | function () { |
| 714 | // vars |
| 715 | var $options = $( this ).children( 'ul' ); |
| 716 | var $group = $( this ).children( '.select2-result-label' ); |
| 717 | |
| 718 | // compare to previous |
| 719 | if ( $prevGroup && $prevGroup.text() === $group.text() ) { |
| 720 | $prevGroup.append( $options.children() ); |
| 721 | $( this ).remove(); |
| 722 | return; |
| 723 | } |
| 724 | |
| 725 | // update vars |
| 726 | $prevOptions = $options; |
| 727 | $prevGroup = $group; |
| 728 | } |
| 729 | ); |
| 730 | }, |
| 731 | |
| 732 | getAjaxData: function ( term, page ) { |
| 733 | // create Select2 v4 params |
| 734 | var params = { |
| 735 | term: term, |
| 736 | page: page, |
| 737 | }; |
| 738 | |
| 739 | // filter |
| 740 | var field = this.get( 'field' ); |
| 741 | params = acf.applyFilters( |
| 742 | 'select2_ajax_data', |
| 743 | params, |
| 744 | this.data, |
| 745 | this.$el, |
| 746 | field || false, |
| 747 | this |
| 748 | ); |
| 749 | // return |
| 750 | return Select2.prototype.getAjaxData.apply( this, [ params ] ); |
| 751 | }, |
| 752 | } ); |
| 753 | |
| 754 | // manager |
| 755 | var select2Manager = new acf.Model( { |
| 756 | priority: 5, |
| 757 | wait: 'prepare', |
| 758 | actions: { |
| 759 | duplicate: 'onDuplicate', |
| 760 | }, |
| 761 | initialize: function () { |
| 762 | // vars |
| 763 | var locale = acf.get( 'locale' ); |
| 764 | var rtl = acf.get( 'rtl' ); |
| 765 | var l10n = acf.get( 'select2L10n' ); |
| 766 | var version = getVersion(); |
| 767 | |
| 768 | // bail early if no l10n |
| 769 | if ( ! l10n ) { |
| 770 | return false; |
| 771 | } |
| 772 | |
| 773 | // bail early if 'en' |
| 774 | if ( locale.indexOf( 'en' ) === 0 ) { |
| 775 | return false; |
| 776 | } |
| 777 | |
| 778 | // initialize |
| 779 | if ( version == 4 ) { |
| 780 | this.addTranslations4(); |
| 781 | } else if ( version == 3 ) { |
| 782 | this.addTranslations3(); |
| 783 | } |
| 784 | }, |
| 785 | |
| 786 | addTranslations4: function () { |
| 787 | // vars |
| 788 | var l10n = acf.get( 'select2L10n' ); |
| 789 | var locale = acf.get( 'locale' ); |
| 790 | |
| 791 | // modify local to match html[lang] attribute (used by Select2) |
| 792 | locale = locale.replace( '_', '-' ); |
| 793 | |
| 794 | // select2L10n |
| 795 | var select2L10n = { |
| 796 | errorLoading: function () { |
| 797 | return l10n.load_fail; |
| 798 | }, |
| 799 | inputTooLong: function ( args ) { |
| 800 | var overChars = args.input.length - args.maximum; |
| 801 | if ( overChars > 1 ) { |
| 802 | return l10n.input_too_long_n.replace( '%d', overChars ); |
| 803 | } |
| 804 | return l10n.input_too_long_1; |
| 805 | }, |
| 806 | inputTooShort: function ( args ) { |
| 807 | var remainingChars = args.minimum - args.input.length; |
| 808 | if ( remainingChars > 1 ) { |
| 809 | return l10n.input_too_short_n.replace( |
| 810 | '%d', |
| 811 | remainingChars |
| 812 | ); |
| 813 | } |
| 814 | return l10n.input_too_short_1; |
| 815 | }, |
| 816 | loadingMore: function () { |
| 817 | return l10n.load_more; |
| 818 | }, |
| 819 | maximumSelected: function ( args ) { |
| 820 | var maximum = args.maximum; |
| 821 | if ( maximum > 1 ) { |
| 822 | return l10n.selection_too_long_n.replace( |
| 823 | '%d', |
| 824 | maximum |
| 825 | ); |
| 826 | } |
| 827 | return l10n.selection_too_long_1; |
| 828 | }, |
| 829 | noResults: function () { |
| 830 | return l10n.matches_0; |
| 831 | }, |
| 832 | searching: function () { |
| 833 | return l10n.searching; |
| 834 | }, |
| 835 | }; |
| 836 | |
| 837 | // append |
| 838 | jQuery.fn.select2.amd.define( |
| 839 | 'select2/i18n/' + locale, |
| 840 | [], |
| 841 | function () { |
| 842 | return select2L10n; |
| 843 | } |
| 844 | ); |
| 845 | }, |
| 846 | |
| 847 | addTranslations3: function () { |
| 848 | // vars |
| 849 | var l10n = acf.get( 'select2L10n' ); |
| 850 | var locale = acf.get( 'locale' ); |
| 851 | |
| 852 | // modify local to match html[lang] attribute (used by Select2) |
| 853 | locale = locale.replace( '_', '-' ); |
| 854 | |
| 855 | // select2L10n |
| 856 | var select2L10n = { |
| 857 | formatMatches: function ( matches ) { |
| 858 | if ( matches > 1 ) { |
| 859 | return l10n.matches_n.replace( '%d', matches ); |
| 860 | } |
| 861 | return l10n.matches_1; |
| 862 | }, |
| 863 | formatNoMatches: function () { |
| 864 | return l10n.matches_0; |
| 865 | }, |
| 866 | formatAjaxError: function () { |
| 867 | return l10n.load_fail; |
| 868 | }, |
| 869 | formatInputTooShort: function ( input, min ) { |
| 870 | var remainingChars = min - input.length; |
| 871 | if ( remainingChars > 1 ) { |
| 872 | return l10n.input_too_short_n.replace( |
| 873 | '%d', |
| 874 | remainingChars |
| 875 | ); |
| 876 | } |
| 877 | return l10n.input_too_short_1; |
| 878 | }, |
| 879 | formatInputTooLong: function ( input, max ) { |
| 880 | var overChars = input.length - max; |
| 881 | if ( overChars > 1 ) { |
| 882 | return l10n.input_too_long_n.replace( '%d', overChars ); |
| 883 | } |
| 884 | return l10n.input_too_long_1; |
| 885 | }, |
| 886 | formatSelectionTooBig: function ( maximum ) { |
| 887 | if ( maximum > 1 ) { |
| 888 | return l10n.selection_too_long_n.replace( |
| 889 | '%d', |
| 890 | maximum |
| 891 | ); |
| 892 | } |
| 893 | return l10n.selection_too_long_1; |
| 894 | }, |
| 895 | formatLoadMore: function () { |
| 896 | return l10n.load_more; |
| 897 | }, |
| 898 | formatSearching: function () { |
| 899 | return l10n.searching; |
| 900 | }, |
| 901 | }; |
| 902 | |
| 903 | // ensure locales exists |
| 904 | $.fn.select2.locales = $.fn.select2.locales || {}; |
| 905 | |
| 906 | // append |
| 907 | $.fn.select2.locales[ locale ] = select2L10n; |
| 908 | $.extend( $.fn.select2.defaults, select2L10n ); |
| 909 | }, |
| 910 | |
| 911 | onDuplicate: function ( $el, $el2 ) { |
| 912 | $el2.find( '.select2-container' ).remove(); |
| 913 | }, |
| 914 | } ); |
| 915 | } )( jQuery ); |
| 916 |