Skip to content
Closed
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
25 changes: 18 additions & 7 deletions js/fieldmanager-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,32 @@ $( 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,
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 );
$fm_select_field.trigger("liszt:updated");
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 ) {
Expand All @@ -81,7 +92,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
Expand All @@ -101,4 +112,4 @@ $( document ).ready( function() {

} );

} )( jQuery );
} )( jQuery );
6 changes: 5 additions & 1 deletion php/class-fieldmanager-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 9 additions & 2 deletions php/class-fieldmanager-select.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand All @@ -59,6 +59,13 @@ 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();
list ( $context, $subcontext ) = fm_get_context();
$this->attributes['data-context'] = $context;
$this->attributes['data-subcontext'] = $subcontext;
}
}

}
Expand Down Expand Up @@ -166,4 +173,4 @@ public function chosen_init() {
</script>
<?php
}
}
}
18 changes: 18 additions & 0 deletions php/datasource/class-fieldmanager-datasource-term.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment doesn't seem to apply, but there should be an option to group by taxonomy.

$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
Expand Down
10 changes: 10 additions & 0 deletions php/datasource/class-fieldmanager-datasource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down