Skip to content

Commit

Permalink
Fixes #15
Browse files Browse the repository at this point in the history
- Handle select2 versions
- Update conditionnal overrides
  • Loading branch information
nlemoine committed Apr 25, 2018
1 parent 8d33a7f commit 8ef02fd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
16 changes: 9 additions & 7 deletions assets/js/acf-country-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
var self = this;

// vars
var key = $field.attr('data-key'),
$ancestors = $field.parents('.acf-field-list'),
$tr = $field.find('.acf-field[data-name="conditional_logic"]:last');
var key = $field.attr('data-key');
var $lists = $field.parents('.acf-field-list');
var $tr = $field.find('.acf-field-setting-conditional_logic:last');


// choices
var choices = [];

// loop over ancestors
$.each( $ancestors, function( i ){

// loop over ancestor lists
$.each( $lists, function( i ){

// vars
var group = (i == 0) ? acf._e('sibling_fields') : acf._e('parent_fields');
Expand All @@ -34,7 +36,7 @@
this_label = $this_field.find('.field-label:first').val();

// validate
if( $.inArray(this_type, ['select', 'checkbox', 'true_false', 'radio', 'country']) === -1 ) {
if( $.inArray(this_type, ['select', 'checkbox', 'true_false', 'radio', 'button_group', 'country']) === -1 ) {

return;

Expand Down Expand Up @@ -101,7 +103,7 @@
});

// select
} else if( field_type == "select" || field_type == "checkbox" || field_type == "radio" || field_type == "country" ) {
} else if( field_type == "select" || field_type == "checkbox" || field_type == "radio" || field_type == "button_group" || field_type == "country" ) {

// vars
var lines = $field.find('.acf-field[data-name="choices"] textarea').val().split("\n");
Expand Down
47 changes: 36 additions & 11 deletions assets/js/acf-country.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,52 @@
(function($){

function format_country(code, country) {
function format_v3_country(code, country) {
return '<span class="acf-country-flag-icon famfamfam-flags ' + code + '"></span> <span class="acf-country-flag-name">' + country + '</span>';
}

function format_country_result(result, container, query, escapeMarkup) {
function format_v3_country_result(result, container, query, escapeMarkup) {
var text = $.fn.select2.defaults.formatResult(result, container, query, escapeMarkup);
return format_country(result.id.toLowerCase(), text);
return format_v3_country(result.id.toLowerCase(), text);
}

function format_country_selection(result, container, query, escapeMarkup) {
return format_country(result.id.toLowerCase(), result.text);
function format_v3_country_selection(result, container, query, escapeMarkup) {
return format_v3_country(result.id.toLowerCase(), result.text);
}

function format_country(state) {
if(!state.id) {
return state.text;
}
return $('<span class="acf-country-flag-icon famfamfam-flags ' + state.id.toLowerCase() + '"></span> <span class="acf-country-flag-name">' + state.text + '</span>');
}

function get_select2_version() {
return typeof acf_select2_version != 'undefined' && acf_select2_version.hasOwnProperty('select2_version') ? parseInt(acf_select2_version.select2_version) : 3;
}

function init_field( $el ) {
var $acf_country = $el.find('[data-ui="1"]');
if( !$acf_country.length ) {
return;
}
$acf_country.select2({
var select2_version = get_select2_version();
var options = {
allowClear: !!$acf_country.data('allow-null'),
formatResult: format_country_result,
formatSelection: format_country_selection
});
width: '100%'
};
if( select2_version === 3 ) {
$.extend(options, {
formatResult: format_v3_country_result,
formatSelection: format_v3_country_selection
});
} else if( select2_version === 4 ) {
$.extend(options, {
templateResult: format_country,
templateSelection: format_country
});
}
console.log(options)
$acf_country.select2(options);
}

if( typeof acf.add_action !== 'undefined' ) {
Expand Down Expand Up @@ -54,7 +78,7 @@

// Extend conditional logic to allow country type
acf.conditional_logic.extend({
calculate : function( rule, $trigger, $target ){
calculate: function( rule, $trigger, $target ){

// bail early if $trigger could not be found
if( !$trigger || !$target ) return false;
Expand All @@ -68,13 +92,14 @@


// input with :checked
if( type == 'true_false' || type == 'checkbox' || type == 'radio' ) {
if( type == 'true_false' || type == 'checkbox' || type == 'radio' || type == 'button_group' ) {

match = this.calculate_checkbox( rule, $trigger );


} else if( type == 'select' || type == 'country' ) {


match = this.calculate_select( rule, $trigger );

}
Expand Down
7 changes: 5 additions & 2 deletions fields/acf-country-v5.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,14 @@ function input_admin_enqueue_scripts() {

// register & include JS
wp_register_script( 'acf-country', "{$url}assets/js/acf-country.js", array('acf-input'), $version );
wp_enqueue_script('acf-country');
wp_enqueue_script( 'acf-country' );
wp_localize_script( 'acf-country', 'acf_select2_version', array(
'select2_version' => acf_get_setting('select2_version'),
));

// register & include CSS
wp_register_style( 'acf-country', "{$url}assets/css/acf-country.css", array('acf-input'), $version );
wp_enqueue_style('acf-country');
wp_enqueue_style( 'acf-country' );

wp_register_style( 'famfamfam-flags', sprintf('%sassets/vendor/famfamfam-flags/dist/sprite/famfamfam-flags%s.css', $url, WP_DEBUG ? '' : '.min'), false, $version);
wp_enqueue_style( 'famfamfam-flags' );
Expand Down

0 comments on commit 8ef02fd

Please sign in to comment.