-
Notifications
You must be signed in to change notification settings - Fork 42
Build placeholders with JS and optimize spinner animations #3531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /** | ||
| * Restore placeholder attributes before the builder scans its grid or starts requests. | ||
| * | ||
| * @since x.x | ||
| * @return {void} | ||
| */ | ||
| export function hydrateFieldPlaceholders() { | ||
| const manifestElement = document.getElementById( 'frm-field-placeholders' ); | ||
| const fieldsContainer = document.getElementById( 'frm-show-fields' ); | ||
| if ( ! manifestElement || ! fieldsContainer ) { | ||
| return; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-blocking: the call at the top of |
||
|
|
||
| const { fields, definitions } = JSON.parse( manifestElement.textContent ); | ||
| fieldsContainer.querySelectorAll( 'li[data-frm-placeholder]' ).forEach( field => { | ||
| const [ fieldId, definitionIndex ] = fields[ field.dataset.frmPlaceholder ]; | ||
| const [ className, formId, type ] = definitions[ definitionIndex ]; | ||
| field.id = `frm_field_id_${ fieldId }`; | ||
| field.className = className; | ||
| field.dataset.fid = fieldId; | ||
| field.dataset.formid = formId; | ||
| field.dataset.ftype = type; | ||
| delete field.dataset.frmPlaceholder; | ||
| } ); | ||
| manifestElement.remove(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,86 @@ | |
| #[\PHPUnit\Framework\Attributes\CoversClass( FrmFieldsController::class )] | ||
| class test_FrmFieldsController extends FrmUnitTest { | ||
|
|
||
| public function test_builder_placeholder_manifest_preserves_attributes_and_order() { | ||
| $form_id = $this->factory->form->create(); | ||
| $manifest = (object) array( | ||
| 'definitions' => array(), | ||
| 'fields' => array(), | ||
| ); | ||
| $expected = array(); | ||
|
|
||
| foreach ( array( 'text', 'text', 'html' ) as $type ) { | ||
| $field = $this->factory->field->create_and_get( | ||
| array( | ||
| 'form_id' => $form_id, | ||
| 'type' => $type, | ||
| ) | ||
| ); | ||
| ob_start(); | ||
| FrmFieldsController::load_single_field( | ||
| $field, | ||
| array( | ||
| 'ajax_load' => true, | ||
| 'count' => 11, | ||
| ) | ||
| ); | ||
| $legacy_html = ob_get_clean(); | ||
|
|
||
| ob_start(); | ||
| FrmFieldsController::load_single_field( | ||
| $field, | ||
| array( | ||
| 'ajax_load' => true, | ||
| 'count' => 11, | ||
| 'placeholder_manifest' => $manifest, | ||
| ) | ||
| ); | ||
| $placeholder = ob_get_clean(); | ||
| $index = count( $expected ); | ||
| $this->assertSame( '<li data-frm-placeholder="' . $index . '"></li>', $placeholder ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| $this->assertSame( (int) $field->id, $manifest->fields[ $index ][0] ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| $definition = $manifest->definitions[ $manifest->fields[ $index ][1] ]; | ||
| $this->assertStringContainsString( 'class="' . esc_attr( $definition[0] ) . '"', $legacy_html ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| $this->assertStringContainsString( 'data-formid="' . esc_attr( $definition[1] ) . '"', $legacy_html ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| $this->assertStringContainsString( 'data-ftype="' . esc_attr( $definition[2] ) . '"', $legacy_html ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| $expected[] = (int) $field->id; | ||
| } | ||
|
|
||
| $this->assertCount( 2, $manifest->definitions ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| $this->assertSame( $expected, array_column( $manifest->fields, 0 ) ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| $this->assertSame( $manifest->fields[0][1], $manifest->fields[1][1] ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| public function test_builder_placeholder_manifest_keeps_initial_and_non_ajax_fields_rendered() { | ||
| $form_id = $this->factory->form->create(); | ||
| $field = $this->factory->field->create_and_get( | ||
| array( | ||
| 'form_id' => $form_id, | ||
| 'type' => 'text', | ||
| ) | ||
| ); | ||
| $manifest = (object) array( | ||
| 'definitions' => array(), | ||
| 'fields' => array(), | ||
| ); | ||
|
|
||
| foreach ( array( array( true, 10 ), array( false, 11 ) ) as $settings ) { | ||
| ob_start(); | ||
| FrmFieldsController::load_single_field( | ||
| $field, | ||
| array( | ||
| 'ajax_load' => $settings[0], | ||
| 'count' => $settings[1], | ||
| 'placeholder_manifest' => $manifest, | ||
| ) | ||
| ); | ||
| $html = ob_get_clean(); | ||
| $this->assertStringContainsString( 'id="frm_field_id_' . $field->id . '"', $html ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| $this->assertStringNotContainsString( 'data-frm-placeholder=', $html ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| $this->assertSame( array(), $manifest->fields ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| public function test_builder_batches_omit_only_received_definitions() { | ||
| $definitions = array( | ||
| 'known' => array( 'value' => 'Saved option' ), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocking: this second call looks redundant.
formidable_adminis registered within_footer = true, so it always runs after the builder markup, and the call at the top of this file already hydrates and removes the manifest. By the timebuildInit()runs, this just does agetElementByIdthat finds nothing. One call site makes it clearer which one guarantees hydration beforeobserveFieldPlaceholders().