From 314316b6483c7f0de0fda87aa2539d408dc75295 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Fri, 7 Nov 2014 10:24:52 -0800 Subject: [PATCH 1/3] If datasource is ajax, only load selected items `use_ajax` is intended for taxonomies with lots of terms, where loading all at once would cause performance issues --- php/class-fieldmanager-options.php | 6 +++++- .../class-fieldmanager-datasource-term.php | 18 ++++++++++++++++++ .../class-fieldmanager-datasource.php | 10 ++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/php/class-fieldmanager-options.php b/php/class-fieldmanager-options.php index 80261fe771..8322863f56 100644 --- a/php/class-fieldmanager-options.php +++ b/php/class-fieldmanager-options.php @@ -105,7 +105,11 @@ public function form_data_elements( $value ) { if ( !$this->has_built_data ) { if ( $this->datasource ) { - $this->add_options( $this->datasource->get_items() ); + if ( $this->datasource->use_ajax ) { + $this->add_options( $this->datasource->get_selected_items( $this->data_id ) ); + } else { + $this->add_options( $this->datasource->get_items() ); + } } // Add the first element to the data array. This is useful for database-based data sets that require a first element. diff --git a/php/datasource/class-fieldmanager-datasource-term.php b/php/datasource/class-fieldmanager-datasource-term.php index 4eac645b44..fae93899dd 100644 --- a/php/datasource/class-fieldmanager-datasource-term.php +++ b/php/datasource/class-fieldmanager-datasource-term.php @@ -229,6 +229,24 @@ public function save_taxonomy( $tax_values, $data_id ) { } } + /** + * Get the selected items for this data item + * + * @param int $data_id + * @return array + */ + public function get_selected_items( $data_id ) { + $terms = wp_get_object_terms( $data_id, $this->get_taxonomies() ); + // Put the taxonomy data into the proper data structure to be used for display + $stack = array(); + foreach ( $terms as $term ) { + // Store the label for the taxonomy as the group since it will be used for display + $key = $this->store_term_taxonomy_id ? $term->term_taxonomy_id : $term->term_id; + $stack[ $key ] = $term->name; + } + return $stack; + } + /** * Get taxonomy data per $this->taxonomy_args * @param $value The value(s) currently set for this field diff --git a/php/datasource/class-fieldmanager-datasource.php b/php/datasource/class-fieldmanager-datasource.php index 80f45bc6e1..f64ed8fd4e 100644 --- a/php/datasource/class-fieldmanager-datasource.php +++ b/php/datasource/class-fieldmanager-datasource.php @@ -105,6 +105,16 @@ public function get_value( $id ) { return isset( $this->options[ $id ] ) ? $this->options[ $id ] : ''; } + /** + * Get the selected items given the data ID + * + * @param int $data_id + * @return array + */ + public function get_selected_items( $data_id ) { + return array(); + } + /** * Get available options, optionally filtering by a fragment (e.g. for Autocomplete) * @param string $fragment optional fragment to filter by From 6df38247c83931fac72dbc668af44a5fe123c40c Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Fri, 7 Nov 2014 13:14:40 -0800 Subject: [PATCH 2/3] Fill Chosen dropdown with AJAX results --- js/fieldmanager-select.js | 19 ++++++++++++------- php/class-fieldmanager-select.php | 8 ++++++-- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/js/fieldmanager-select.js b/js/fieldmanager-select.js index 8b549fa650..67229b3b87 100644 --- a/js/fieldmanager-select.js +++ b/js/fieldmanager-select.js @@ -54,10 +54,9 @@ $( document ).ready( function() { $fm_select_field = $(this).parents('.chzn-container').siblings('select'); $fm_text_field = $(this); - if( $fm_select_field.data("taxonomy") != "" && $fm_select_field.data("taxonomyPreload") == false ) { + if( $fm_select_field.data("fm-ajax-search-action").length ) { fm_typeahead_term = $(this).val(); - $.post( ajaxurl, { action: 'fm_search_terms', search_term: $fm_text_field.val(), taxonomy: $fm_select_field.data("taxonomy"), fm_search_terms_nonce: fm_select.nonce }, function ( result ) { - + $.post( ajaxurl, { action: $fm_select_field.data("fm-ajax-search-action"), fm_autocomplete_search: $fm_text_field.val(), fm_search_nonce: fm_select.fm_search_nonce }, function ( result ) { // Clear any non-selected terms before proceeding fm_text_field_val = $fm_text_field.val(); fm_select_clear_terms( $fm_select_field, "", false ); @@ -65,10 +64,16 @@ $( document ).ready( function() { fm_reset_chosen( $fm_text_field, fm_text_field_val ); if( result != "" ) { - $resultObj = $( result ); + var items = $.parseJSON( result ); + $resultObj = $( document.createElement('select') ); + $.each( items, function( key, label ) { + var el = $( document.createElement( 'option' ) ); + el.attr( 'value', key ).text( label ); + $resultObj.append( el ); + }); // If there are optgroups present, use special processing - $resultObj.filter("optgroup").each( function( index, element ) { + $resultObj.find("optgroup").each( function( index, element ) { // See if the optgroup already exists var optgroup_selector = "optgroup[label='" + $(this).attr("label") + "']"; if( $fm_select_field.find(optgroup_selector).length > 0 ) { @@ -81,7 +86,7 @@ $( document ).ready( function() { } ); // Append any options not in an optgroup - fm_append_options( $fm_select_field, $resultObj.filter("option") ); + fm_append_options( $fm_select_field, $resultObj.find("option") ); } // Inform chosen this field has been updated to populate these options in the typeahead dropdown @@ -101,4 +106,4 @@ $( document ).ready( function() { } ); -} )( jQuery ); \ No newline at end of file +} )( jQuery ); diff --git a/php/class-fieldmanager-select.php b/php/class-fieldmanager-select.php index a149265943..264a0ea547 100644 --- a/php/class-fieldmanager-select.php +++ b/php/class-fieldmanager-select.php @@ -42,7 +42,7 @@ public function __construct( $label = '', $options = array() ) { ); // Add the Fieldmanager Select javascript library - fm_add_script( 'fm_select_js', 'js/fieldmanager-select.js', array(), '1.0.1', false, 'fm_select', array( 'nonce' => wp_create_nonce( 'fm_search_terms_nonce' ) ) ); + fm_add_script( 'fm_select_js', 'js/fieldmanager-select.js', array(), '1.0.1', false, 'fm_select', array( 'fm_search_nonce' => wp_create_nonce( 'fm_search_nonce' ) ) ); parent::__construct( $label, $options ); @@ -59,6 +59,10 @@ public function __construct( $label = '', $options = array() ) { if ( $this->type_ahead ) { fm_add_script( 'chosen', 'js/chosen/chosen.jquery.js' ); fm_add_style( 'chosen_css', 'js/chosen/chosen.css' ); + + if ( $this->datasource && $this->datasource->use_ajax ) { + $this->attributes['data-fm-ajax-search-action'] = $this->datasource->get_ajax_action(); + } } } @@ -166,4 +170,4 @@ public function chosen_init() { Date: Sun, 21 Dec 2014 17:57:19 -0500 Subject: [PATCH 3/3] Add context to ajax request --- js/fieldmanager-select.js | 8 +++++++- php/class-fieldmanager-select.php | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/js/fieldmanager-select.js b/js/fieldmanager-select.js index 67229b3b87..0eb9424c0c 100644 --- a/js/fieldmanager-select.js +++ b/js/fieldmanager-select.js @@ -56,7 +56,13 @@ $( document ).ready( function() { if( $fm_select_field.data("fm-ajax-search-action").length ) { fm_typeahead_term = $(this).val(); - $.post( ajaxurl, { action: $fm_select_field.data("fm-ajax-search-action"), fm_autocomplete_search: $fm_text_field.val(), fm_search_nonce: fm_select.fm_search_nonce }, function ( result ) { + $.post( ajaxurl, { + action: $fm_select_field.data("fm-ajax-search-action"), + fm_autocomplete_search: $fm_text_field.val(), + fm_search_nonce: fm_select.fm_search_nonce, + fm_context: $fm_select_field.data( 'context' ), + fm_subcontext: $fm_select_field.data( 'subcontext' ) + }, function ( result ) { // Clear any non-selected terms before proceeding fm_text_field_val = $fm_text_field.val(); fm_select_clear_terms( $fm_select_field, "", false ); diff --git a/php/class-fieldmanager-select.php b/php/class-fieldmanager-select.php index 264a0ea547..ab6418e2d5 100644 --- a/php/class-fieldmanager-select.php +++ b/php/class-fieldmanager-select.php @@ -62,6 +62,9 @@ public function __construct( $label = '', $options = array() ) { if ( $this->datasource && $this->datasource->use_ajax ) { $this->attributes['data-fm-ajax-search-action'] = $this->datasource->get_ajax_action(); + list ( $context, $subcontext ) = fm_get_context(); + $this->attributes['data-context'] = $context; + $this->attributes['data-subcontext'] = $subcontext; } }