Skip to content

Commit

Permalink
refactor(admin-tool): move js function to admin export file impress-o…
Browse files Browse the repository at this point in the history
  • Loading branch information
raftaar1191 committed Apr 3, 2018
1 parent 4e241ca commit e9c1a01
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 123 deletions.
138 changes: 138 additions & 0 deletions assets/src/js/admin/admin-export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*!
* Give Admin Export JS
*
* @description: The Give Admin Settings scripts. Only enqueued on the give-settings and give-tools page; used for exporting CSV
* @package: Give
* @since: 2.1
* @subpackage: Assets/JS
* @copyright: Copyright (c) 2016, WordImpress
* @license: http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/

jQuery.noConflict();
jQuery( document ).ready( function ( $ ) {
/**
* Ajax call to get donation fields.
*/
$( '.give-export_donations #give-export_donations-form #give_form_for_csv_export' ).chosen().change( function () {

$( '.give-export-donations-hide' ).addClass( 'give-hidden' );

jQuery( document ).trigger( 'give_export_donations_form_change' );

var give_form_id;

// Check for form ID.
if ( ! (
give_form_id = $( this ).val()
) ) {
return false;
}

// Ajax.
$.ajax( {
type: 'POST',
url: ajaxurl,
data: {
form_id: give_form_id,
action: 'give_export_donations_get_custom_fields'
},
success: function ( response ) {
if ( response ) {
output_give_donations_fields( response );
} else {
alert( give_vars.error_message );
}

jQuery( document ).trigger( 'give_export_donations_form_response', response );
}
} );
} );


/**
* Outputs the custom field checkboxes.
*
* @param response
*/
function output_give_donations_fields( response ) {

/**
* FFM Fields
*/
var ffm_fields = (
typeof response.ffm_fields !== 'undefined'
) ? response.ffm_fields : '';

if ( ffm_fields ) {

var ffm_field_list = $( '.give-export-donations-ffm ul' );

ffm_field_list.closest( 'tr' ).removeClass( 'give-hidden' );

// Loop through FFM fields & output
$( ffm_fields ).each( function ( index, value ) {

// Repeater sections.
var repeater_sections = (
typeof value.repeaters !== 'undefined'
) ? value.repeaters : '';

if ( repeater_sections ) {

var parent_title = '';
// Repeater section field.
$( repeater_sections ).each( function ( index, value ) {
if ( parent_title !== value.parent_title ) {
ffm_field_list.append( '<li class="repeater-section-title" data-parent-meta="' + value.parent_meta + '"><label for="give-give-donations-ffm-field-' + value.parent_meta + '"><input type="checkbox" name="give_give_donations_export_parent[' + value.parent_meta + ']" id="give-give-donations-ffm-field-' + value.parent_meta + '">' + value.parent_title + '</label></li>' );
}
parent_title = value.parent_title;
ffm_field_list.append( '<li class="repeater-section repeater-section-' + value.parent_meta + '"><label for="give-give-donations-ffm-field-' + value.subkey + '"><input type="checkbox" name="give_give_donations_export_option[' + value.subkey + ']" id="give-give-donations-ffm-field-' + value.subkey + '">' + value.label + '</label></li>' );
} );
}
// Repeater sections.
var single_repeaters = (
typeof value.single !== 'undefined'
) ? value.single : '';

if ( single_repeaters ) {
// Repeater section field.
$( single_repeaters ).each( function ( index, value ) {
ffm_field_list.append( '<li><label for="give-give-donations-ffm-field-' + value.subkey + '"><input type="checkbox" name="give_give_donations_export_option[' + value.metakey + ']" id="give-give-donations-ffm-field-' + value.subkey + '">' + value.label + '</label> </li>' );
} );
}
} );

}

/**
* Standard Fields
*/
var standard_fields = (
typeof response.standard_fields !== 'undefined'
) ? response.standard_fields : '';
var standard_field_list = $( '.give-export-donations-standard-fields ul' );
if ( standard_fields.length > 0 ) {
standard_field_list.closest( 'tr' ).removeClass( 'give-hidden' );
// Loop through STANDARD fields & output
$( standard_fields ).each( function ( index, value ) {
standard_field_list.append( '<li><label for="give-give-donations-standard-field-' + value + '"><input type="checkbox" name="give_give_donations_export_option[' + value + ']" id="give-give-donations-standard-field-' + value + '">' + value + '</label> </li>' );
} );
}

/**
* Hidden Fields
*/
var hidden_fields = response.hidden_fields ? response.hidden_fields : '';
var hidden_field_list = $( '.give-export-donations-hidden-fields ul' );

if ( hidden_fields ) {
hidden_field_list.closest( 'tr' ).removeClass( 'give-hidden' );

// Loop through HIDDEN fields & output.
$( hidden_fields ).each( function ( index, value ) {
hidden_field_list.append( '<li><label for="give-give-donations-hidden-field-' + value + '"><input type="checkbox" name="give_give_donations_export_option[' + value + ']" id="give-give-donations-hidden-field-' + value + '">' + value + '</label> </li>' );
} );
}
}
} );
123 changes: 0 additions & 123 deletions assets/src/js/admin/admin-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,127 +150,4 @@ jQuery(document).ready(function ($) {
}
})
});

/**
* Ajax call to get donation fields.
*/
$( '.give-export_donations #give-export_donations-form #give_form_for_csv_export' ).chosen().change( function () {

$( '.give-export-donations-hide' ).addClass( 'give-hidden' );

jQuery( document ).trigger( 'give_export_donations_form_change' );

var give_form_id;

// Check for form ID.
if ( ! ( give_form_id = $( this ).val() ) ) {
return false;
}

// Ajax.
$.ajax( {
type: 'POST',
url: ajaxurl,
data: {
form_id: give_form_id,
action: 'give_export_donations_get_custom_fields'
},
success: function ( response ) {
if ( response ) {
output_give_donations_fields( response );
} else {
alert( give_vars.error_message );
}

jQuery( document ).trigger( 'give_export_donations_form_response', response );
}
} );
} );


/**
* Outputs the custom field checkboxes.
*
* @param response
*/
function output_give_donations_fields( response ) {

/**
* FFM Fields
*/
var ffm_fields = (
typeof response.ffm_fields !== 'undefined'
) ? response.ffm_fields : '';

if ( ffm_fields ) {

var ffm_field_list = $( '.give-export-donations-ffm ul' );

ffm_field_list.closest( 'tr' ).removeClass( 'give-hidden' );

// Loop through FFM fields & output
$( ffm_fields ).each( function ( index, value ) {

// Repeater sections.
var repeater_sections = (
typeof value.repeaters !== 'undefined'
) ? value.repeaters : '';

if ( repeater_sections ) {

var parent_title = '';
// Repeater section field.
$( repeater_sections ).each( function ( index, value ) {
if ( parent_title !== value.parent_title ) {
ffm_field_list.append( '<li class="repeater-section-title" data-parent-meta="' + value.parent_meta + '"><label for="give-give-donations-ffm-field-' + value.parent_meta + '"><input type="checkbox" name="give_give_donations_export_parent[' + value.parent_meta + ']" id="give-give-donations-ffm-field-' + value.parent_meta + '">' + value.parent_title + '</label></li>' );
}
parent_title = value.parent_title;
ffm_field_list.append( '<li class="repeater-section repeater-section-' + value.parent_meta + '"><label for="give-give-donations-ffm-field-' + value.subkey + '"><input type="checkbox" name="give_give_donations_export_option[' + value.subkey + ']" id="give-give-donations-ffm-field-' + value.subkey + '">' + value.label + '</label></li>' );
} );
}
// Repeater sections.
var single_repeaters = (
typeof value.single !== 'undefined'
) ? value.single : '';

if ( single_repeaters ) {
// Repeater section field.
$( single_repeaters ).each( function ( index, value ) {
ffm_field_list.append( '<li><label for="give-give-donations-ffm-field-' + value.subkey + '"><input type="checkbox" name="give_give_donations_export_option[' + value.metakey + ']" id="give-give-donations-ffm-field-' + value.subkey + '">' + value.label + '</label> </li>' );
} );
}
} );

}

/**
* Standard Fields
*/
var standard_fields = (
typeof response.standard_fields !== 'undefined'
) ? response.standard_fields : '';
var standard_field_list = $( '.give-export-donations-standard-fields ul' );
if ( standard_fields.length > 0 ) {
standard_field_list.closest( 'tr' ).removeClass( 'give-hidden' );
// Loop through STANDARD fields & output
$( standard_fields ).each( function ( index, value ) {
standard_field_list.append( '<li><label for="give-give-donations-standard-field-' + value + '"><input type="checkbox" name="give_give_donations_export_option[' + value + ']" id="give-give-donations-standard-field-' + value + '">' + value + '</label> </li>' );
} );
}

/**
* Hidden Fields
*/
var hidden_fields = response.hidden_fields ? response.hidden_fields : '';
var hidden_field_list = $( '.give-export-donations-hidden-fields ul' );

if ( hidden_fields ) {
hidden_field_list.closest( 'tr' ).removeClass( 'give-hidden' );

// Loop through HIDDEN fields & output.
$( hidden_fields ).each( function ( index, value ) {
hidden_field_list.append( '<li><label for="give-give-donations-hidden-field-' + value + '"><input type="checkbox" name="give_give_donations_export_option[' + value + ']" id="give-give-donations-hidden-field-' + value + '">' + value + '</label> </li>' );
} );
}
}
});
1 change: 1 addition & 0 deletions assets/src/js/admin/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ import '../plugins/give-hint.css';
import './admin-forms';
import './admin-scripts';
import './admin-settings';
import './admin-export';
import './admin-widgets';

0 comments on commit e9c1a01

Please sign in to comment.