Skip to content

Commit

Permalink
Merge pull request #2 from Bynder/sync/fjorge-latest-changes
Browse files Browse the repository at this point in the history
Dev cleanup and comments
  • Loading branch information
CWDN authored May 13, 2024
2 parents 8efcbd6 + 8cc6d94 commit 8fd10b3
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 74 deletions.
45 changes: 27 additions & 18 deletions assets/js/gathercontent-mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,6 @@ module.exports = function (app, _meta_keys) {

initialize: function initialize() {
this.listenTo(this.model, 'change:field_type', this.render);
console.log("this.model: ", this.model);

// Initiate the metaKeys collection.
this.metaKeys = new (app.collections.base.extend({
Expand All @@ -442,10 +441,16 @@ module.exports = function (app, _meta_keys) {
}))(_meta_keys);
},

/**
* 1st Dropdown - event change
*/
changeType: function changeType(evt) {
this.model.set('field_type', jQuery(evt.target).val());
},

/**
* 2nd Dropdown - event change
*/
changeValue: function changeValue(evt) {
var value = jQuery(evt.target).val();
var type = this.model.get('type');
Expand All @@ -471,6 +476,9 @@ module.exports = function (app, _meta_keys) {
}
},

/**
* 3rd Dropdown - event change
*/
changeField: function changeField(evt) {
var value = jQuery(evt.target).val();
var component = jQuery(evt.target).closest('.component-table-wrapper').attr('id');
Expand All @@ -486,6 +494,9 @@ module.exports = function (app, _meta_keys) {
}
},

/**
* LVL 2: Subfield Dropdown - event change
*/
changeSubfield: function changeSubfield(evt) {
var value = jQuery(evt.target).val();
var index = jQuery(evt.target).attr('data-index');
Expand All @@ -497,29 +508,29 @@ module.exports = function (app, _meta_keys) {
this.model.set('field_subfields', subfield_data);
},

/**
* Helper function: build option html elements for AJAX funtions
*/
optionBuilder: function optionBuilder(data) {
console.log('optionBuilder');
var options_html = "<option value=''>Unused</option>";
jQuery.each(data.field_data, function (i, field) {
// console.log('data: ',data);
options_html += "<option class='hidden' value='" + field.key + "' data-type='" + field.type + "'>" + field.label + "</option>";
});
return options_html;
},

/**
* AJAX Update: "Field" - ACF Field group's field
*
* - "Field" refers to the 3rd dropdown of the component fields top level
* - After selecting the field group from the 2nd dropdown, call WP_AJAX to get the relevent fields from the group selected and populate the 3rd dropdown (aka "Field")
*
* @param {string} component - ID without the "#" of the component parent row
* @param {string} field_name - Parent field name/key of the sub fields, should be a repeater
* @param {object} saved_fields - OPTIONAL: Pass saved subfields if you want to set pre-existing values
*/
updateAjax_Field: function updateAjax_Field(component, field_name, saved_fields) {
saved_fields = typeof saved_fields !== 'undefined' ? saved_fields : "";
var $this = this;
// console.log('updateAjax_Field');
// console.log('field_name: ',field_name);
// console.log('saved_fields: ',saved_fields);

// Update UI
jQuery('#' + component + ' .wp-type-field-select ~ span.select2').addClass('ajax-disabled');
Expand All @@ -532,7 +543,6 @@ module.exports = function (app, _meta_keys) {
}, function (response) {
// Update UI
jQuery('#' + component + ' .wp-type-field-select ~ span.select2').removeClass('ajax-disabled');
console.log('RESPONSE: ', response);

// SUCCESS
if (response.success) {
Expand All @@ -551,23 +561,23 @@ module.exports = function (app, _meta_keys) {
}
// ERROR
else {
console.log('AJAX ERROR!');
window.alert('Please refresh and try again. If the issue persists, reach out to support');
}
});
},

/**
* AJAX Update: "Field" - ACF Field group's repeater subfields
*
* AJAX Update: "Subfields" - ACF Field group's repeater subfields
* - "Subfields" are in the component accordion
* - After selecting the field group from the 3rd dropdown, call WP_AJAX to get the relevent subfields from the ACF Repeater selected and populate the subfields
*
* @param {string} component - ID without the "#" of the component parent row
* @param {string} field_name - Parent field name/key of the sub fields, should be a repeater
* @param {object} saved_fields - OPTIONAL: Pass saved subfields if you want to set pre-existing values
*/
updateAjax_ComponentSubfields: function updateAjax_ComponentSubfields(component, field_name, saved_fields) {
saved_fields = typeof saved_fields !== 'undefined' ? saved_fields : {};
var $this = this;
// console.log('updateSubFields: ',field_name);
// console.log('saved_fields: ',saved_fields);

// Update UI
jQuery('#' + component + ' .component-table-inner').find('select').addClass('ajax-disabled');
Expand All @@ -580,7 +590,6 @@ module.exports = function (app, _meta_keys) {
}, function (response) {
// Update UI
jQuery('#' + component + ' .component-table-inner').find('select').removeClass('ajax-disabled');
console.log('RESPONSE: ', response);

// SUCCESS
if (response.success) {
Expand All @@ -603,19 +612,19 @@ module.exports = function (app, _meta_keys) {
}
// ERROR
else {
console.log('AJAX ERROR!');
window.alert('Please refresh and try again. If the issue persists, reach out to support');
}
});
},

/**
* Init
*/
render: function render() {
var val = this.model.get('field_value');
var valField = this.model.get('field_field');
var valSubfields = this.model.get('field_subfields');
var component;
// console.log("render - val: ",val);
// console.log("render - valField: ",valField);
// console.log("render - valSubfields: ",valSubfields);

if (val && !this.metaKeys.getByValue(val)) {
this.metaKeys.add({ value: val });
Expand Down
9 changes: 8 additions & 1 deletion assets/js/gathercontent.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ window.GatherContent = window.GatherContent || {};
gc.$id($(this).attr('href').substring(1)).removeClass('hidden');
};

/**
* Accordion Toggle > Template Mapping: Field Description
* - Opens the drawer for a single field's description
*/
main.maybeReveal = function (evt) {
var $this = $(this);
evt.preventDefault();
Expand All @@ -79,10 +83,13 @@ window.GatherContent = window.GatherContent || {};
}
};

/**
* Accordion Toggle > Template Mapping: Component Fields
* - Opens the drawer for the component's description and subfields
*/
main.maybeRevealComponent = function (evt) {
var $this = $(this);
evt.preventDefault();
console.log('maybeRevealComponent');

if ($this.hasClass('dashicons-arrow-right')) {
$this.closest('table').find('.gc-component-row').addClass('hidden');
Expand Down
9 changes: 8 additions & 1 deletion assets/js/src/components/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ window.GatherContent = window.GatherContent || {};
gc.$id( $( this ).attr( 'href' ).substring(1) ).removeClass( 'hidden' );
};

/**
* Accordion Toggle > Template Mapping: Field Description
* - Opens the drawer for a single field's description
*/
main.maybeReveal = function( evt ) {
var $this = $( this );
evt.preventDefault();
Expand All @@ -53,10 +57,13 @@ window.GatherContent = window.GatherContent || {};
}
};

/**
* Accordion Toggle > Template Mapping: Component Fields
* - Opens the drawer for the component's description and subfields
*/
main.maybeRevealComponent = function( evt ) {
var $this = $( this );
evt.preventDefault();
console.log('maybeRevealComponent');

if ( $this.hasClass( 'dashicons-arrow-right' ) ) {
$this.closest('table').find('.gc-component-row').addClass( 'hidden' );
Expand Down
41 changes: 25 additions & 16 deletions assets/js/src/components/views/tab-row.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module.exports = function( app, _meta_keys ) {

initialize: function() {
this.listenTo( this.model, 'change:field_type', this.render );
console.log("this.model: ",this.model);

// Initiate the metaKeys collection.
this.metaKeys = new ( app.collections.base.extend( {
Expand All @@ -40,10 +39,16 @@ module.exports = function( app, _meta_keys ) {
} ) )( _meta_keys );
},

/**
* 1st Dropdown - event change
*/
changeType: function( evt ) {
this.model.set( 'field_type', jQuery( evt.target ).val() );
},

/**
* 2nd Dropdown - event change
*/
changeValue: function( evt ) {
var value = jQuery( evt.target ).val();
var type = this.model.get( 'type' );
Expand All @@ -69,6 +74,9 @@ module.exports = function( app, _meta_keys ) {
}
},

/**
* 3rd Dropdown - event change
*/
changeField: function( evt ) {
var value = jQuery( evt.target ).val();
var component = jQuery( evt.target ).closest('.component-table-wrapper').attr('id');
Expand All @@ -84,6 +92,9 @@ module.exports = function( app, _meta_keys ) {
}
},

/**
* LVL 2: Subfield Dropdown - event change
*/
changeSubfield: function( evt ) {
var value = jQuery( evt.target ).val();
var index = jQuery( evt.target ).attr('data-index');
Expand All @@ -93,18 +104,21 @@ module.exports = function( app, _meta_keys ) {
this.model.set( 'field_subfields', subfield_data );
},

/**
* Helper function: build option html elements for AJAX funtions
*/
optionBuilder: function( data ) {
console.log('optionBuilder');
var options_html = "<option value=''>Unused</option>";
jQuery.each(data.field_data, function(i, field) {
// console.log('data: ',data);
options_html += "<option class='hidden' value='"+field.key+"' data-type='"+field.type+"'>"+field.label+"</option>";
});
return options_html;
},

/**
* AJAX Update: "Field" - ACF Field group's field
* - "Field" refers to the 3rd dropdown of the component fields top level
* - After selecting the field group from the 2nd dropdown, call WP_AJAX to get the relevent fields from the group selected and populate the 3rd dropdown (aka "Field")
*
* @param {string} component - ID without the "#" of the component parent row
* @param {string} field_name - Parent field name/key of the sub fields, should be a repeater
Expand All @@ -113,9 +127,6 @@ module.exports = function( app, _meta_keys ) {
updateAjax_Field: function( component, field_name, saved_fields ) {
saved_fields = typeof saved_fields !== 'undefined' ? saved_fields : "";
var $this = this;
// console.log('updateAjax_Field');
// console.log('field_name: ',field_name);
// console.log('saved_fields: ',saved_fields);

// Update UI
jQuery('#'+component+' .wp-type-field-select ~ span.select2').addClass('ajax-disabled');
Expand All @@ -128,7 +139,6 @@ module.exports = function( app, _meta_keys ) {
}, function( response ) {
// Update UI
jQuery('#'+component+' .wp-type-field-select ~ span.select2').removeClass('ajax-disabled');
console.log('RESPONSE: ',response);

// SUCCESS
if( response.success ){
Expand All @@ -147,13 +157,15 @@ module.exports = function( app, _meta_keys ) {
}
// ERROR
else{
console.log('AJAX ERROR!');
window.alert('Please refresh and try again. If the issue persists, reach out to support');
}
});
},

/**
* AJAX Update: "Field" - ACF Field group's repeater subfields
* AJAX Update: "Subfields" - ACF Field group's repeater subfields
* - "Subfields" are in the component accordion
* - After selecting the field group from the 3rd dropdown, call WP_AJAX to get the relevent subfields from the ACF Repeater selected and populate the subfields
*
* @param {string} component - ID without the "#" of the component parent row
* @param {string} field_name - Parent field name/key of the sub fields, should be a repeater
Expand All @@ -162,8 +174,6 @@ module.exports = function( app, _meta_keys ) {
updateAjax_ComponentSubfields: function( component, field_name, saved_fields ) {
saved_fields = typeof saved_fields !== 'undefined' ? saved_fields : {};
var $this = this;
// console.log('updateSubFields: ',field_name);
// console.log('saved_fields: ',saved_fields);

// Update UI
jQuery('#'+component+' .component-table-inner').find('select').addClass('ajax-disabled');
Expand All @@ -176,7 +186,6 @@ module.exports = function( app, _meta_keys ) {
}, function( response ) {
// Update UI
jQuery('#'+component+' .component-table-inner').find('select').removeClass('ajax-disabled');
console.log('RESPONSE: ',response);

// SUCCESS
if( response.success ){
Expand All @@ -199,19 +208,19 @@ module.exports = function( app, _meta_keys ) {
}
// ERROR
else{
console.log('AJAX ERROR!');
window.alert('Please refresh and try again. If the issue persists, reach out to support');
}
});
},

/**
* Init
*/
render : function() {
var val = this.model.get( 'field_value' );
var valField = this.model.get( 'field_field' );
var valSubfields = this.model.get( 'field_subfields' );
var component;
// console.log("render - val: ",val);
// console.log("render - valField: ",valField);
// console.log("render - valSubfields: ",valSubfields);

if ( val && ! this.metaKeys.getByValue( val ) ) {
this.metaKeys.add( { value : val } );
Expand Down
10 changes: 6 additions & 4 deletions includes/classes/admin/ajax/handlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,11 @@ public function gc_dismiss_notice_cb() {
* @return void
*/
public function gc_component_subfields_cb() {
// If data not provided from FE, stop and return error
if ( ! $this->_post_val( 'subfields_data' ) ) {
wp_send_json_error("one");
wp_send_json_error();
}
// Parse data from JS
$data = $this->_post_val( 'subfields_data' );

// If ACF Field group
Expand All @@ -440,9 +442,9 @@ public function gc_component_subfields_cb() {
wp_send_json_success($success_data);
}
}

// If error
wp_send_json_error("two");
// If success conditions were not met above, complete function with error status
wp_send_json_error();
}

/*
Expand Down
Loading

0 comments on commit 8fd10b3

Please sign in to comment.