Group fields support
Widget area support
Use of devbridgeAutocomplete() instead of autocomplete() to avoid errors
This commit is contained in:
Ruben Garcia 2017-06-01 16:26:57 +02:00
parent 95f31838d8
commit 527ddc5195
4 changed files with 114 additions and 86 deletions

View File

@ -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' ),

View File

@ -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; }

View File

@ -1,11 +1,14 @@
(function( $ ) { (function( document, $ ) {
$('.cmb-ajax-search').each(function () { function init_ajax_search() {
$('.cmb-ajax-search:not([data-ajax-search="true"])').each(function () {
$(this).attr('data-ajax-search', true);
var input_id = $(this).attr('id'); // Field id with '_input' sufix (the searchable field) var input_id = $(this).attr('id'); // Field id with '_input' sufix (the searchable field)
var field_id = $(this).attr('id').replace( new RegExp('_input$'), '' ); // Field id, the true one field var field_id = $(this).attr('id').replace( new RegExp('_input$'), '' ); // Field id, the true one field
var object_type = $(this).attr('data-object-type'); var object_type = $(this).attr('data-object-type');
var query_args = $(this).attr('data-query-args'); var query_args = $(this).attr('data-query-args');
$(this).autocomplete({ $(this).devbridgeAutocomplete({
serviceUrl: cmb_ajax_search.ajaxurl, serviceUrl: cmb_ajax_search.ajaxurl,
type: 'POST', type: 'POST',
triggerSelectOnValidInput: false, triggerSelectOnValidInput: false,
@ -46,7 +49,7 @@
$(this).next('img.cmb-ajax-search-spinner').hide(); $(this).next('img.cmb-ajax-search-spinner').hide();
}, },
onSelect: function ( suggestion ) { onSelect: function ( suggestion ) {
$(this).autocomplete('clearCache'); $(this).devbridgeAutocomplete('clearCache');
var field_name = $(this).attr('id').replace( new RegExp('_input$'), '' ); var field_name = $(this).attr('id').replace( new RegExp('_input$'), '' );
var multiple = $(this).attr('data-multiple'); var multiple = $(this).attr('data-multiple');
@ -85,15 +88,32 @@
}); });
} }
}); });
}
$('.cmb-ajax-search-results').on( 'click', 'a.remover', function() { // Initialize ajax search
init_ajax_search();
// Initialize on group fields add row
$( document ).on( 'cmb2_add_row', function( evt, $row ) {
$row.find('.cmb-ajax-search').attr('data-ajax-search', false);
init_ajax_search();
});
// 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);

View File

@ -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