Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

llmsPostsSelect2 improvements #2806

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changelogs/fix_llms-posts-select2-improvements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
significance: patch
type: dev
links:
- "#2805"
entry: Improved llmsPostsSelect2 method when called on multiple elements at
once, each with different options.
158 changes: 80 additions & 78 deletions assets/js/llms-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,96 +51,98 @@
* @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 );

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 ) {
$( 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 ) {

// 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
Loading