redux-select.js (3475B)
1 /* global redux_change, redux, ajaxurl, jQuery */ 2 3 (function( $ ) { 4 'use strict'; 5 6 redux.field_objects = redux.field_objects || {}; 7 redux.field_objects.select = redux.field_objects.select || {}; 8 9 redux.field_objects.select.init = function( selector ) { 10 selector = $.redux.getSelector( selector, 'select' ); 11 12 $( selector ).each( 13 function() { 14 var default_params = {}; 15 var el = $( this ); 16 var parent = el; 17 18 if ( ! el.hasClass( 'redux-field-container' ) ) { 19 parent = el.parents( '.redux-field-container:first' ); 20 } 21 22 if ( parent.is( ':hidden' ) ) { 23 return; 24 } 25 26 if ( parent.hasClass( 'redux-field-init' ) ) { 27 parent.removeClass( 'redux-field-init' ); 28 } else { 29 return; 30 } 31 32 el.find( 'select.redux-select-item' ).each( 33 function() { 34 var action; 35 var nonce; 36 var wpdata; 37 var min; 38 var data_args; 39 40 if ( $( this ).hasClass( 'font-icons' ) ) { 41 default_params = $.extend( 42 {}, 43 { 44 templateResult: redux.field_objects.select.addIcon, 45 templateSelection: redux.field_objects.select.addIcon, 46 escapeMarkup: function( m ) { 47 return m; 48 } 49 }, 50 default_params 51 ); 52 } 53 if ( $( this ).data( 'ajax' ) ) { 54 action = $( this ).data( 'action' ); 55 nonce = $( this ).data( 'nonce' ); 56 wpdata = $( this ).data( 'wp-data' ); 57 min = $( this ).data( 'min-input-length' ); 58 data_args = {}; 59 if ( $( this ).data( 'args' ) ) { 60 data_args = JSON.stringify( $( this ).data( 'args' ) ); 61 } 62 63 if ( 'true' === min ) { 64 min = 1; 65 } 66 67 default_params = { 68 minimumInputLength: min, 69 ajax: { 70 url: ajaxurl, 71 dataType: 'json', 72 delay: 250, 73 data: function( params ) { 74 return { 75 nonce: nonce, 76 data: wpdata, 77 q: params.term, 78 page: params.page || 1, 79 action: action, 80 data_args: data_args 81 }; 82 }, 83 processResults: function( data, params ) { 84 params.page = params.page || 1; 85 86 if ( true === data.success ) { 87 return { 88 results: data.data // , 89 // We'll revisit this later. 90 // pagination: { 91 // more: ( params.page * 20 ) < data.data.length 92 // }. 93 }; 94 } else if ( false === data.success ) { 95 alert( data.data ); 96 97 return { 98 results: data.data 99 }; 100 } 101 }, 102 cache: true 103 } 104 }; 105 } 106 107 $( this ).select2( default_params ); 108 109 el.find( '.select2-search__field' ).width( 'auto' ); 110 111 if ( $( this ).hasClass( 'select2-sortable' ) ) { 112 113 default_params = {}; 114 default_params.bindOrder = 'sortableStop'; 115 default_params.sortableOptions = { axis: 'x', placeholder: false }; 116 117 $( this ).select2Sortable( default_params ); 118 } 119 120 $( this ).on( 121 'select2:select', 122 function() { 123 redux_change( $( $( this ) ) ); 124 125 $( this ).select2SortableOrder(); 126 } 127 ); 128 } 129 ); 130 } 131 ); 132 }; 133 134 redux.field_objects.select.addIcon = function( icon ) { 135 if ( icon.hasOwnProperty( 'id' ) ) { 136 return '<span class="elusive"><i class="' + icon.id + '"></i> ' + icon.text + '</span>'; 137 } 138 }; 139 })( jQuery );