select-advanced.js (3151B)
1 ( function ( $, rwmb ) { 2 'use strict'; 3 4 // Cache ajax requests: https://github.com/select2/select2/issues/110#issuecomment-419247158 5 var cache = {}; 6 7 /** 8 * Reorder selected values in correct order that they were selected. 9 * @param $select2 jQuery element of the select2. 10 */ 11 function reorderSelected( $select2 ) { 12 var selected = $select2.data( 'selected' ); 13 if ( ! selected ) { 14 return; 15 } 16 selected.forEach( function ( value ) { 17 var option = $select2.children( '[value="' + value + '"]' ); 18 option.detach(); 19 $select2.append( option ); 20 } ); 21 $select2.trigger( 'change' ); 22 } 23 24 /** 25 * Transform select fields into beautiful dropdown with select2 library. 26 */ 27 function transform() { 28 var $this = $( this ), 29 options = $this.data( 'options' ); 30 31 $this.removeClass( 'select2-hidden-accessible' ).removeAttr( 'data-select2-id' ); 32 $this.siblings( '.select2-container' ).remove(); 33 $this.find( 'option' ).removeAttr( 'data-select2-id' ); 34 35 if ( options.ajax_data ) { 36 options.ajax.dataType = 'json'; 37 options.ajax.data = function( params ) { 38 return Object.assign( options.ajax_data, params ); 39 }; 40 options.ajax.processResults = function ( response ) { 41 var items = response.data.items.map( function( item ) { 42 return { 43 id: item.value, 44 text: item.label, 45 } 46 } ); 47 48 var results = { 49 results: items 50 } 51 if ( response.data.hasOwnProperty( 'more' ) ) { 52 results.pagination = { more: true }; 53 } 54 55 return results; 56 }; 57 58 options.ajax.transport = function ( params, success, failure ) { 59 if ( params.data._type === 'query' ) { 60 delete params.data.page; 61 } 62 63 // Create cache key from ajax params from only neccessary keys to make cache available for multiple fields. 64 var data = $.extend( true, {}, params.data ); 65 delete data.field.id; 66 delete data.action; 67 if ( ! data.term ) { 68 delete data.term; 69 } 70 71 var key = JSON.stringify( data ); 72 if ( cache[key] ) { 73 success( cache[key] ); 74 return; 75 } 76 77 var actions = { 78 'post' : 'rwmb_get_posts', 79 'taxonomy' : 'rwmb_get_terms', 80 'taxonomy_advanced': 'rwmb_get_terms', 81 'user' : 'rwmb_get_users' 82 }; 83 params.data.action = actions[ params.data.field.type ]; 84 params.method = 'POST'; 85 86 return $.ajax( params ).then( function ( data ) { 87 cache[key] = data; 88 return data; 89 } ).then( success ).fail( failure ); 90 }; 91 } 92 93 $this.show().select2( options ); 94 95 if ( ! $this.attr( 'multiple' ) ) { 96 return; 97 } 98 99 reorderSelected( $this ); 100 101 /** 102 * Preserve the order that options are selected. 103 * @see https://github.com/select2/select2/issues/3106#issuecomment-255492815 104 */ 105 $this.on( 'select2:select', function ( event ) { 106 var option = $this.children( '[value="' + event.params.data.id + '"]' ); 107 option.detach(); 108 $this.append( option ).trigger( 'change' ); 109 } ); 110 } 111 112 function init( e ) { 113 $( e.target ).find( '.rwmb-select_advanced' ).each( transform ); 114 } 115 116 rwmb.$document 117 .on( 'mb_ready', init ) 118 .on( 'clone', '.rwmb-select_advanced', transform ); 119 } )( jQuery, rwmb );