Skip to content
Merged
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
145 changes: 78 additions & 67 deletions js/formidable_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -1584,7 +1584,7 @@ function frmAdminBuildJS() {
* @returns {Object}
*/
function getInsertNewFieldArgs( fieldType, sectionId, formId, hasBreak ) {
const args = {
return {
action: 'frm_insert_field',
form_id: formId,
field_type: fieldType,
Expand All @@ -1593,7 +1593,6 @@ function frmAdminBuildJS() {
has_break: hasBreak,
last_row_field_ids: getFieldIdsInSubmitRow()
};
return wp.hooks.applyFilters( 'frm_insert_field_args', args, fieldType, formId, sectionId, hasBreak );
}

/**
Expand All @@ -1615,8 +1614,10 @@ function frmAdminBuildJS() {
*/
function insertNewFieldByDragging( fieldType ) {
if ( shouldStopInsertingField( fieldType ) ) {
wp.hooks.doAction( 'frm_stopped_inserting_by_dragging', fieldType );
return;
}
Comment thread
Crabcyborg marked this conversation as resolved.

const placeholder = document.getElementById( 'frm_drag_placeholder' );
const loadingID = fieldType.replace( '|', '-' ) + '_' + getAutoId();
const loading = tag(
Expand Down Expand Up @@ -1647,36 +1648,9 @@ function frmAdminBuildJS() {
url: ajaxurl,
data: getInsertNewFieldArgs( fieldType, sectionId, formId, hasBreak ),
success: function( msg ) {
let replaceWith;
document.getElementById( 'frm_form_editor_container' ).classList.add( 'frm-has-fields' );
const $siblings = $placeholder.siblings( 'li.form-field' ).not( '.edit_field_type_end_divider' );
const fieldId = checkMsgForFieldId( msg );

if ( ! $siblings.length ) {
// if dragging into a new row, we need to wrap the li first.
replaceWith = wrapFieldLi( msg );
} else {
replaceWith = msgAsjQueryObject( msg );
if ( ! $placeholder.get( 0 ).parentNode.parentNode.classList.contains( 'ui-draggable' ) ) {
// If a field group wasn't draggable because it only had a single field, make it draggable.
makeDraggable( $placeholder.get( 0 ).parentNode.parentNode, '.frm-move' );
}
}
$placeholder.replaceWith( replaceWith );
updateFieldOrder();
afterAddField( msg, false );
if ( $siblings.length ) {
syncLayoutClasses( $siblings.first() );
}
toggleSectionHolder();

if ( ! $siblings.length ) {
makeDroppable( replaceWith.get( 0 ).querySelector( 'ul.frm_sorting' ) );
makeDraggable( replaceWith.get( 0 ).querySelector( 'li.form-field' ), '.frm-move' );
} else {
makeDraggable( replaceWith.get( 0 ), '.frm-move' );
}
handleInsertFieldByDraggingResponse( msg, $placeholder );

const fieldId = checkMsgForFieldId( msg );
if ( fieldId ) {
/**
* Fires after a field is added.
Expand All @@ -1688,19 +1662,53 @@ function frmAdminBuildJS() {
* @param {String} fieldData.field_type The field type.
* @param {String} fieldData.form_id The form ID.
*/
wp.hooks.doAction( 'frmadmin.afterFieldAddedInFormBuilder', {
wp.hooks.doAction( 'frm_after_field_added_in_form_builder', {
field: msg,
fieldId: fieldId,
fieldType: fieldType,
fieldId,
fieldType,
form_id: formId,
});
}

},
error: handleInsertFieldError
});
}

/**
* @param {String} msg
* @param {Object} $placeholder jQuery object.
*/
function handleInsertFieldByDraggingResponse( msg, $placeholder ) {
let replaceWith;
document.getElementById( 'frm_form_editor_container' ).classList.add( 'frm-has-fields' );
const $siblings = $placeholder.siblings( 'li.form-field' ).not( '.edit_field_type_end_divider' );

if ( ! $siblings.length ) {
// if dragging into a new row, we need to wrap the li first.
replaceWith = wrapFieldLi( msg );
} else {
replaceWith = msgAsjQueryObject( msg );
if ( ! $placeholder.get( 0 ).parentNode.parentNode.classList.contains( 'ui-draggable' ) ) {
// If a field group wasn't draggable because it only had a single field, make it draggable.
makeDraggable( $placeholder.get( 0 ).parentNode.parentNode, '.frm-move' );
}
}
$placeholder.replaceWith( replaceWith );
updateFieldOrder();
afterAddField( msg, false );
if ( $siblings.length ) {
syncLayoutClasses( $siblings.first() );
}
toggleSectionHolder();

if ( ! $siblings.length ) {
makeDroppable( replaceWith.get( 0 ).querySelector( 'ul.frm_sorting' ) );
makeDraggable( replaceWith.get( 0 ).querySelector( 'li.form-field' ), '.frm-move' );
} else {
makeDraggable( replaceWith.get( 0 ), '.frm-move' );
}
}

/**
* Get the field ID from the response message.
*
Expand Down Expand Up @@ -2098,26 +2106,9 @@ function frmAdminBuildJS() {
url: ajaxurl,
data: getInsertNewFieldArgs( fieldType, 0, formId, hasBreak ),
success: function( msg ) {
document.getElementById( 'frm_form_editor_container' ).classList.add( 'frm-has-fields' );
const replaceWith = wrapFieldLi( msg );
const fieldId = checkMsgForFieldId( msg );
const submitField = $newFields[0].querySelector( '.edit_field_type_submit' );

if ( ! submitField ) {
$newFields.append( replaceWith );
} else {
jQuery( submitField.closest( '.frm_field_box:not(.form-field)' ) ).before( replaceWith );
}

afterAddField( msg, true );

replaceWith.each(
function() {
makeDroppable( this.querySelector( 'ul.frm_sorting' ) );
makeDraggable( this.querySelector( '.form-field' ), '.frm-move' );
}
);
handleAddFieldClickResponse( msg );

const fieldId = checkMsgForFieldId( msg );
if ( fieldId ) {
/**
* Fires after a field is added.
Expand All @@ -2129,10 +2120,10 @@ function frmAdminBuildJS() {
* @param {String} fieldData.field_type The field type.
* @param {String} fieldData.form_id The form ID.
*/
wp.hooks.doAction( 'frmadmin.afterFieldAddedInFormBuilder', {
wp.hooks.doAction( 'frm_after_field_added_in_form_builder', {
field: msg,
fieldId: fieldId,
fieldType: fieldType,
fieldId,
fieldType,
form_id: formId,
});
}
Expand All @@ -2142,6 +2133,27 @@ function frmAdminBuildJS() {
return false;
}

function handleAddFieldClickResponse( msg ) {
document.getElementById( 'frm_form_editor_container' ).classList.add( 'frm-has-fields' );
const replaceWith = wrapFieldLi( msg );
const submitField = $newFields[0].querySelector( '.edit_field_type_submit' );

if ( ! submitField ) {
$newFields.append( replaceWith );
} else {
jQuery( submitField.closest( '.frm_field_box:not(.form-field)' ) ).before( replaceWith );
}

afterAddField( msg, true );

replaceWith.each(
function() {
makeDroppable( this.querySelector( 'ul.frm_sorting' ) );
makeDraggable( this.querySelector( '.form-field' ), '.frm-move' );
}
);
}

function insertFormField( fieldType, fieldOptions = {} ) {

return new Promise( ( resolve ) => {
Expand All @@ -2157,17 +2169,13 @@ function frmAdminBuildJS() {
url: ajaxurl,
data: Object.assign( getInsertNewFieldArgs( fieldType, 0, formId, hasBreak ), { field_options: fieldOptions } ),
success: function( msg ) {
const fieldElement = jQuery( msg );
const fieldId = checkMsgForFieldId( msg );
resolve( msg );

fieldElement[0].style.display = 'none';
resolve( fieldElement );
setTimeout( () => {
updateFieldOrder();
afterAddField( msg, true );
syncLayoutClasses( jQuery( fieldElement.closest( 'ul' ).children()[0] ) );
fieldElement[0].style.display = 'block';

const fieldId = checkMsgForFieldId( msg );
if ( fieldId ) {
/**
* Fires after a field is added.
Expand All @@ -2179,10 +2187,10 @@ function frmAdminBuildJS() {
* @param {String} fieldData.field_type The field type.
* @param {String} fieldData.form_id The form ID.
*/
wp.hooks.doAction( 'frmadmin.afterFieldAddedInFormBuilder', {
wp.hooks.doAction( 'frm_after_field_added_in_form_builder', {
field: msg,
fieldId: fieldId,
fieldType: fieldType,
fieldId,
fieldType,
form_id: formId,
});
Comment thread
Crabcyborg marked this conversation as resolved.
}
Expand Down Expand Up @@ -11169,7 +11177,10 @@ function frmAdminBuildJS() {
showSaveAndReloadModal,
deleteField,
insertFormField,
confirmLinkClick
confirmLinkClick,
handleInsertFieldByDraggingResponse,
handleAddFieldClickResponse,
syncLayoutClasses,
};
}

Expand Down
Loading