Skip to content

Commit

Permalink
Proper handling when there are multiple elements on which llmsPostsSe…
Browse files Browse the repository at this point in the history
…lect2 is called, by looping over each one.
  • Loading branch information
brianhogg committed Nov 14, 2024
1 parent b57be00 commit 7ed3de0
Showing 1 changed file with 82 additions and 79 deletions.
161 changes: 82 additions & 79 deletions assets/js/llms-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,96 +51,99 @@
* @return void
*/
$.fn.llmsPostsSelect2 = function( options ) {
var localOptions = options;

this.each( function() {
var self = $( this ),
options = localOptions || {},
defaults = {
multiple: false,
placeholder: self.attr( 'data-placeholder' ) || ( undefined !== LLMS.l10n ? LLMS.l10n.translate( 'Select a Course/Membership' ) : 'Select a Course/Membership' ),
post_type: self.attr( 'data-post-type' ) || 'post',
post_statuses: self.attr( 'data-post-statuses' ) || 'publish',
instructor_id: null,
allow_clear: self.attr( 'data-post-type' ) || false,
width: null,
};

$.each( defaults, function( setting ) {
if ( self.attr( 'data-' + setting ) ) {
options[ setting ] = self.attr( 'data-' + setting );
}
} );

var self = this,
options = options || {},
defaults = {
multiple: false,
placeholder: undefined !== LLMS.l10n ? LLMS.l10n.translate( 'Select a Course/Membership' ) : 'Select a Course/Membership',
post_type: self.attr( 'data-post-type' ) || 'post',
post_statuses: self.attr( 'data-post-statuses' ) || 'publish',
instructor_id: null,
allow_clear: self.attr( 'data-post-type' ) || false,
width: null,
};

$.each( defaults, function( setting ) {
if ( self.attr( 'data-' + setting ) ) {
options[ setting ] = self.attr( 'data-' + setting );
if ( 'multiple' === self.attr( 'multiple' ) ) {
options.multiple = true;
}
} );

if ( 'multiple' === self.attr( 'multiple' ) ) {
options.multiple = true;
}

options = $.extend( defaults, options );

this.llmsSelect2( {
allowClear: options.allow_clear,
ajax: {
dataType: 'JSON',
delay: 250,
method: 'POST',
url: window.ajaxurl,
data: function( params ) {
return {
action: 'select2_query_posts',
page: ( params.page ) ? params.page - 1 : 0, // 0 index the pages to make it simpler for the database query
post_type: options.post_type,
instructor_id : options.instructor_id,
post_statuses: options.post_statuses,
term: params.term,
_ajax_nonce: window.llms.ajax_nonce,
};
},
processResults: function( data, params ) {
options = $.extend( defaults, options );

$( this ).llmsSelect2( {
allowClear: options.allow_clear,
ajax: {
dataType: 'JSON',
delay: 250,
method: 'POST',
url: window.ajaxurl,
data: function( params ) {
debugger;
return {
action: 'select2_query_posts',
page: ( params.page ) ? params.page - 1 : 0, // 0 index the pages to make it simpler for the database query
post_type: options.post_type,
instructor_id : options.instructor_id,
post_statuses: options.post_statuses,
term: params.term,
_ajax_nonce: window.llms.ajax_nonce,
};
},
processResults: function( data, params ) {

// recursive function for creating
function map_data( items ) {

// this is a flat array of results
// used when only one post type is selected
// and to format children when using optgroups with multiple post types
if ( Array.isArray( items ) ) {
return $.map( items, function( item ) {
return format_item( item );
} );

// this sets up the top level optgroups when using multiple post types
} else {
return $.map( items, function( item ) {
return {
text: item.label,
children: map_data( item.items ),
}
} );
}
}

// recursive function for creating
function map_data( items ) {

// this is a flat array of results
// used when only one post type is selected
// and to format children when using optgroups with multiple post types
if ( Array.isArray( items ) ) {
return $.map( items, function( item ) {
return format_item( item );
} );

// this sets up the top level optgroups when using multiple post types
} else {
return $.map( items, function( item ) {
return {
text: item.label,
children: map_data( item.items ),
}
} );
// format a single result (option)
function format_item( item ) {
return {
text: item.name,
id: item.id,
};
}
}

// format a single result (option)
function format_item( item ) {
return {
text: item.name,
id: item.id,
results: map_data( data.items ),
pagination: {
more: data.more
}
};
}

return {
results: map_data( data.items ),
pagination: {
more: data.more
}
};

},
},
},
cache: true,
placeholder: options.placeholder,
multiple: options.multiple,
width: options.width,
cache: true,
placeholder: options.placeholder,
multiple: options.multiple,
width: options.width,
} );
} );

};

// automatically setup any select with the `llms-posts-select2` class
Expand Down

0 comments on commit 7ed3de0

Please sign in to comment.