Created
January 10, 2018 11:31
-
-
Save scheurta/f720527bd8b976dd50b60046f769c750 to your computer and use it in GitHub Desktop.
adsf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.select2 = { | |
init: function( el ) { | |
if ( 'undefined' === typeof el ) { | |
return; | |
} | |
var scope = this, | |
args = { | |
maximumSelectionLength: 1, | |
templateSelection: function( item ) { | |
return item.text.replace( ' - ', '' ); | |
}, | |
}; | |
el.each( function() { | |
var select = $( this ), | |
label = select.data( 'label' ); | |
if ( '' !== select.data( 'type' ) ) { | |
args.ajax = { | |
url: theme.ajaxurl, | |
type: 'post', | |
delay: 100, | |
data: function( params ) { | |
return { | |
action: 'get_filter', | |
type: select.data( 'type' ), | |
postId: select.data( 'post-id' ), | |
}; | |
}, | |
processResults: function( data, params ) { | |
return { | |
results: data.data.items, | |
}; | |
}, | |
success: function( data ) { | |
delete args.ajax; | |
args.data = data.data.items; | |
args.maximumSelectionLength = scope.setMaximumSelection( select.data( 'type' ) ); | |
if ( 'event-genres' === select.data( 'type' ) ) { | |
args.maximumSelectionLength = 3; | |
} else { | |
args.maximumSelectionLength = 1; | |
} | |
select.select2( args ) | |
.on( 'select2:open', function() { | |
select.next().addClass( 'select2-container--is-open' ); | |
}) | |
.on( 'select2:close', function() { | |
select.next().removeClass( 'select2-container--is-open' ); | |
}) | |
.select2( 'open' ); | |
scope.setLabel( select, label ); | |
}, | |
}; | |
args.maximumSelectionLength = scope.setMaximumSelection( select.data( 'type' ) ); | |
} | |
select.select2( args ); | |
scope.setAttributes(); | |
scope.setLabel( select, label ); | |
}); | |
}, | |
setMaximumSelection: function( type ) { | |
return ( 'event-genres' === type ) ? 3 : 1; | |
}, | |
setAttributes: function( select ) { | |
$( '.select2-container' ).attr( 'aria-hidden', true ); | |
}, | |
setLabel: function( select, label ) { | |
select.next( '.select2' ) | |
.addClass( 'select2-has-label' ) | |
.find( '.select2-selection__rendered' ) | |
.before( '<span class="select2-label">' + label + '</span>' ); | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment