v1.0.1
Group fields support Widget area support Use of devbridgeAutocomplete() instead of autocomplete() to avoid errors
This commit is contained in:
parent
95f31838d8
commit
527ddc5195
|
@ -4,7 +4,7 @@ Plugin Name: CMB2 Field Type: Ajax Search
|
||||||
Plugin URI: https://github.com/rubengc/cmb2-field-ajax-search
|
Plugin URI: https://github.com/rubengc/cmb2-field-ajax-search
|
||||||
GitHub Plugin URI: https://github.com/rubengc/cmb2-field-ajax-search
|
GitHub Plugin URI: https://github.com/rubengc/cmb2-field-ajax-search
|
||||||
Description: CMB2 field type to attach posts, users or terms.
|
Description: CMB2 field type to attach posts, users or terms.
|
||||||
Version: 1.0.0
|
Version: 1.0.1
|
||||||
Author: Ruben Garcia
|
Author: Ruben Garcia
|
||||||
Author URI: http://rubengc.com/
|
Author URI: http://rubengc.com/
|
||||||
License: GPLv2+
|
License: GPLv2+
|
||||||
|
@ -23,7 +23,7 @@ if( ! class_exists( 'CMB2_Field_Ajax_Search' ) ) {
|
||||||
/**
|
/**
|
||||||
* Current version number
|
* Current version number
|
||||||
*/
|
*/
|
||||||
const VERSION = '1.0.0';
|
const VERSION = '1.0.1';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize the plugin by hooking into CMB2
|
* Initialize the plugin by hooking into CMB2
|
||||||
|
@ -179,8 +179,8 @@ if( ! class_exists( 'CMB2_Field_Ajax_Search' ) ) {
|
||||||
*/
|
*/
|
||||||
public function setup_admin_scripts() {
|
public function setup_admin_scripts() {
|
||||||
|
|
||||||
wp_register_script( 'jquery-autocomplete', plugins_url( 'js/jquery.autocomplete.min.js', __FILE__ ), array( 'jquery' ), self::VERSION, true );
|
wp_register_script( 'jquery-autocomplete-ajax-search', plugins_url( 'js/jquery.autocomplete.min.js', __FILE__ ), array( 'jquery' ), self::VERSION, true );
|
||||||
wp_register_script( 'cmb-ajax-search', plugins_url( 'js/ajax-search.js', __FILE__ ), array( 'jquery', 'jquery-autocomplete', 'jquery-ui-sortable' ), self::VERSION, true );
|
wp_register_script( 'cmb-ajax-search', plugins_url( 'js/ajax-search.js', __FILE__ ), array( 'jquery', 'jquery-autocomplete-ajax-search', 'jquery-ui-sortable' ), self::VERSION, true );
|
||||||
|
|
||||||
wp_localize_script( 'cmb-ajax-search', 'cmb_ajax_search', array(
|
wp_localize_script( 'cmb-ajax-search', 'cmb_ajax_search', array(
|
||||||
'ajaxurl' => admin_url( 'admin-ajax.php' ),
|
'ajaxurl' => admin_url( 'admin-ajax.php' ),
|
||||||
|
|
|
@ -6,8 +6,8 @@
|
||||||
.autocomplete-group { padding: 2px 5px; }
|
.autocomplete-group { padding: 2px 5px; }
|
||||||
.autocomplete-group strong { display: block; border-bottom: 1px solid #000; }
|
.autocomplete-group strong { display: block; border-bottom: 1px solid #000; }
|
||||||
|
|
||||||
input.cmb-ajax-search { width: 30em; }
|
input.cmb-ajax-search { width: 25em; }
|
||||||
.cmb-ajax-search-results { width: 30em; padding: 0 1px; box-sizing: border-box; margin-bottom: 5px; }
|
.cmb-ajax-search-results { width: 25em; padding: 0 1px; box-sizing: border-box; margin-bottom: 5px; }
|
||||||
.cmb-ajax-search-results li { margin: 0; border-bottom: 1px solid #f4f4f4; padding: 7px 5px; background: #fff; }
|
.cmb-ajax-search-results li { margin: 0; border-bottom: 1px solid #f4f4f4; padding: 7px 5px; background: #fff; }
|
||||||
.cmb-ajax-search-results li:last-child { border-bottom: none; }
|
.cmb-ajax-search-results li:last-child { border-bottom: none; }
|
||||||
.cmb-ajax-search-results li span.hndl { font-size: 13px; line-height: 16px; color: #ccc; margin: 0 8px 0 0; height: 16px; cursor: s-resize; }
|
.cmb-ajax-search-results li span.hndl { font-size: 13px; line-height: 16px; color: #ccc; margin: 0 8px 0 0; height: 16px; cursor: s-resize; }
|
||||||
|
@ -27,3 +27,6 @@ input.cmb-ajax-search { width: 30em; }
|
||||||
|
|
||||||
#side-sortables .cmb2-wrap input + input.cmb-ajax-search { margin-top: 1px; } /* Fix extra margin from CMB2 */
|
#side-sortables .cmb2-wrap input + input.cmb-ajax-search { margin-top: 1px; } /* Fix extra margin from CMB2 */
|
||||||
|
|
||||||
|
/* Repeatable groups */
|
||||||
|
.cmb-repeatable-group .cmb-type-post-ajax-search .cmb-td, .cmb-repeatable-group .cmb-type-user-ajax-search .cmb-td, .cmb-repeatable-group .cmb-type-term-ajax-search .cmb-td { position: relative; }
|
||||||
|
.cmb-repeatable-group .cmb-ajax-search-spinner { position: absolute; top: 50%; right: 10px; margin: -7px 0 0 0; }
|
||||||
|
|
|
@ -1,99 +1,119 @@
|
||||||
(function( $ ) {
|
(function( document, $ ) {
|
||||||
$('.cmb-ajax-search').each(function () {
|
function init_ajax_search() {
|
||||||
var input_id = $(this).attr('id'); // Field id with '_input' sufix (the searchable field)
|
$('.cmb-ajax-search:not([data-ajax-search="true"])').each(function () {
|
||||||
var field_id = $(this).attr('id').replace( new RegExp('_input$'), '' ); // Field id, the true one field
|
$(this).attr('data-ajax-search', true);
|
||||||
var object_type = $(this).attr('data-object-type');
|
|
||||||
var query_args = $(this).attr('data-query-args');
|
|
||||||
|
|
||||||
$(this).autocomplete({
|
var input_id = $(this).attr('id'); // Field id with '_input' sufix (the searchable field)
|
||||||
serviceUrl: cmb_ajax_search.ajaxurl,
|
var field_id = $(this).attr('id').replace( new RegExp('_input$'), '' ); // Field id, the true one field
|
||||||
type: 'POST',
|
var object_type = $(this).attr('data-object-type');
|
||||||
triggerSelectOnValidInput: false,
|
var query_args = $(this).attr('data-query-args');
|
||||||
showNoSuggestionNotice: true,
|
|
||||||
params: {
|
|
||||||
action : 'cmb_ajax_search_get_results',
|
|
||||||
nonce : cmb_ajax_search.nonce, // nonce
|
|
||||||
field_id : field_id, // Field id for hook purposes
|
|
||||||
object_type : object_type, // post, user or term
|
|
||||||
query_args : query_args, // Query args passed to field
|
|
||||||
},
|
|
||||||
transformResult: function( results ) {
|
|
||||||
var suggestions = $.parseJSON( results );
|
|
||||||
|
|
||||||
if( $('#' + field_id + '_results li').length ) {
|
$(this).devbridgeAutocomplete({
|
||||||
var selected_vals = [];
|
serviceUrl: cmb_ajax_search.ajaxurl,
|
||||||
var d = 0;
|
type: 'POST',
|
||||||
|
triggerSelectOnValidInput: false,
|
||||||
|
showNoSuggestionNotice: true,
|
||||||
|
params: {
|
||||||
|
action : 'cmb_ajax_search_get_results',
|
||||||
|
nonce : cmb_ajax_search.nonce, // nonce
|
||||||
|
field_id : field_id, // Field id for hook purposes
|
||||||
|
object_type : object_type, // post, user or term
|
||||||
|
query_args : query_args, // Query args passed to field
|
||||||
|
},
|
||||||
|
transformResult: function( results ) {
|
||||||
|
var suggestions = $.parseJSON( results );
|
||||||
|
|
||||||
$('#' + field_id + '_results input').each(function( index, element ) {
|
if( $('#' + field_id + '_results li').length ) {
|
||||||
selected_vals.push( $(this).val() );
|
var selected_vals = [];
|
||||||
});
|
var d = 0;
|
||||||
|
|
||||||
// Remove already picked suggestions
|
$('#' + field_id + '_results input').each(function( index, element ) {
|
||||||
$(suggestions).each(function( index, suggestion ) {
|
selected_vals.push( $(this).val() );
|
||||||
if( $.inArray( ( suggestion.id ).toString(), selected_vals ) > -1 ) {
|
});
|
||||||
suggestions.splice( index - d, 1 );
|
|
||||||
d++;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return { suggestions: suggestions };
|
// Remove already picked suggestions
|
||||||
},
|
$(suggestions).each(function( index, suggestion ) {
|
||||||
onSearchStart: function(){
|
if( $.inArray( ( suggestion.id ).toString(), selected_vals ) > -1 ) {
|
||||||
$(this).next('img.cmb-ajax-search-spinner').css( 'display', 'inline-block' );
|
suggestions.splice( index - d, 1 );
|
||||||
},
|
d++;
|
||||||
onSearchComplete: function(){
|
}
|
||||||
$(this).next('img.cmb-ajax-search-spinner').hide();
|
});
|
||||||
},
|
}
|
||||||
onSelect: function ( suggestion ) {
|
|
||||||
$(this).autocomplete('clearCache');
|
return { suggestions: suggestions };
|
||||||
|
},
|
||||||
var field_name = $(this).attr('id').replace( new RegExp('_input$'), '' );
|
onSearchStart: function(){
|
||||||
var multiple = $(this).attr('data-multiple');
|
$(this).next('img.cmb-ajax-search-spinner').css( 'display', 'inline-block' );
|
||||||
var limit = parseInt( $(this).attr('data-limit') );
|
},
|
||||||
var sortable = $(this).attr('data-sortable');
|
onSearchComplete: function(){
|
||||||
|
$(this).next('img.cmb-ajax-search-spinner').hide();
|
||||||
if( multiple == 1 ) {
|
},
|
||||||
// Multiple
|
onSelect: function ( suggestion ) {
|
||||||
$('#' + field_name + '_results' ).append( '<li>' +
|
$(this).devbridgeAutocomplete('clearCache');
|
||||||
( ( sortable == 1 ) ? '<span class="hndl"></span>' : '' ) +
|
|
||||||
'<input type="hidden" name="' + field_name + '[]" value="' + suggestion.id + '">' +
|
var field_name = $(this).attr('id').replace( new RegExp('_input$'), '' );
|
||||||
'<a href="' + suggestion.link + '" target="_blank" class="edit-link">' + suggestion.value + '</a>' +
|
var multiple = $(this).attr('data-multiple');
|
||||||
'<a class="remover"><span class="dashicons dashicons-no"></span><span class="dashicons dashicons-dismiss"></span></a>' +
|
var limit = parseInt( $(this).attr('data-limit') );
|
||||||
'</li>' );
|
var sortable = $(this).attr('data-sortable');
|
||||||
|
|
||||||
$(this).val( '' );
|
if( multiple == 1 ) {
|
||||||
|
// Multiple
|
||||||
// Checks if there is the max allowed results, limit < 0 means unlimited
|
$('#' + field_name + '_results' ).append( '<li>' +
|
||||||
if( limit > 0 && limit == $('#' + field_name + '_results li').length ) {
|
( ( sortable == 1 ) ? '<span class="hndl"></span>' : '' ) +
|
||||||
$(this).prop( 'disabled', 'disabled' );
|
'<input type="hidden" name="' + field_name + '[]" value="' + suggestion.id + '">' +
|
||||||
} else {
|
'<a href="' + suggestion.link + '" target="_blank" class="edit-link">' + suggestion.value + '</a>' +
|
||||||
$(this).focus();
|
'<a class="remover"><span class="dashicons dashicons-no"></span><span class="dashicons dashicons-dismiss"></span></a>' +
|
||||||
|
'</li>' );
|
||||||
|
|
||||||
|
$(this).val( '' );
|
||||||
|
|
||||||
|
// Checks if there is the max allowed results, limit < 0 means unlimited
|
||||||
|
if( limit > 0 && limit == $('#' + field_name + '_results li').length ) {
|
||||||
|
$(this).prop( 'disabled', 'disabled' );
|
||||||
|
} else {
|
||||||
|
$(this).focus();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Singular
|
||||||
|
$('input[name=' + field_name + ']').val( suggestion.id ).change();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// Singular
|
|
||||||
$('input[name=' + field_name + ']').val( suggestion.id ).change();
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if( $(this).attr('data-sortable') == 1 ){
|
||||||
|
$('#' + field_id + '_results').sortable({
|
||||||
|
handle : '.hndl',
|
||||||
|
placeholder : 'ui-state-highlight',
|
||||||
|
forcePlaceholderSize : true
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if( $(this).attr('data-sortable') == 1 ){
|
// Initialize ajax search
|
||||||
$('#' + field_id + '_results').sortable({
|
init_ajax_search();
|
||||||
handle : '.hndl',
|
|
||||||
placeholder : 'ui-state-highlight',
|
// Initialize on group fields add row
|
||||||
forcePlaceholderSize : true
|
$( document ).on( 'cmb2_add_row', function( evt, $row ) {
|
||||||
});
|
$row.find('.cmb-ajax-search').attr('data-ajax-search', false);
|
||||||
}
|
|
||||||
|
init_ajax_search();
|
||||||
});
|
});
|
||||||
|
|
||||||
$('.cmb-ajax-search-results').on( 'click', 'a.remover', function() {
|
// Initialize on widgets area
|
||||||
|
$(document).on('widget-updated widget-added', function() {
|
||||||
|
init_ajax_search();
|
||||||
|
});
|
||||||
|
|
||||||
|
// On click remover listener
|
||||||
|
$('body').on( 'click', '.cmb-ajax-search-results a.remover', function() {
|
||||||
$(this).parent('li').fadeOut( 400, function(){
|
$(this).parent('li').fadeOut( 400, function(){
|
||||||
var field_id = $(this).parents('ul').attr('id').replace('_results', '');
|
var field_id = $(this).parents('ul').attr('id').replace('_results', '');
|
||||||
|
|
||||||
$('#' + field_id).removeProp( 'disabled' );
|
$('#' + field_id).removeProp( 'disabled' );
|
||||||
$('#' + field_id).autocomplete('clearCache');
|
$('#' + field_id).devbridgeAutocomplete('clearCache');
|
||||||
|
|
||||||
$(this).remove();
|
$(this).remove();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
})(jQuery);
|
})(document, jQuery);
|
|
@ -156,5 +156,10 @@ If multiple == true will return an array of IDs of attached object:
|
||||||
|
|
||||||
## Changelog
|
## Changelog
|
||||||
|
|
||||||
|
### 1.0.1
|
||||||
|
* Group fields support
|
||||||
|
* Widget area support
|
||||||
|
* Use of devbridgeAutocomplete() instead of autocomplete() to avoid errors
|
||||||
|
|
||||||
### 1.0.0
|
### 1.0.0
|
||||||
* Initial commit
|
* Initial commit
|
||||||
|
|
Loading…
Reference in New Issue