Skip to content

Split out legacy views from admin JS#2331

Merged
Crabcyborg merged 6 commits into
masterfrom
split_legacy_views_scripts_into_a_new_file
Apr 28, 2025
Merged

Split out legacy views from admin JS#2331
Crabcyborg merged 6 commits into
masterfrom
split_legacy_views_scripts_into_a_new_file

Conversation

@Crabcyborg
Copy link
Copy Markdown
Contributor

@Crabcyborg Crabcyborg commented Apr 28, 2025

This update moves over 400 lines of code out of the frm_admin.js, loading it only when we're loading scripts for the legacy views editor now instead.

This code won't likely get touched again. I may eventually move this to the Legacy Views add-on, but it isn't currently in my plans to update Legacy Views again.

Pre-release
formidable-beta.zip

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 28, 2025

Walkthrough

This change migrates all legacy view-related JavaScript logic out of formidable_admin.js into a new dedicated script, legacy-views.js. The PHP controller is updated to enqueue this new script for legacy views pages; an extraneous blank line is also removed in admin_js(). The removed JavaScript logic includes UI handlers, validation, AJAX row management, and TinyMCE shortcode toggling, all now encapsulated within legacy-views.js. No public APIs or method signatures are changed; only internal implementation details are refactored and relocated for improved code organization.

Changes

File(s) Change Summary
classes/controllers/FrmAppController.php Removed an extraneous blank line in admin_js() method; added enqueue of new legacy-views.js script with dependencies for legacy views admin pages.
js/admin/legacy-views.js New file implementing all legacy view settings UI logic, validation for reserved slugs and filter params, AJAX handlers for adding/removing rows, dynamic UI updates, TinyMCE shortcode toggling, and utility functions previously in formidable_admin.js.
js/formidable_admin.js Removed all legacy view-related JavaScript: dynamic menu positioning, reserved word validation, UI event handlers, AJAX row management, TinyMCE shortcode toggling, and the viewInit method. Modified focusin event handler to dispatch a custom event instead.

Sequence Diagram(s)

sequenceDiagram
    participant AdminPage as WP Admin (Legacy Views)
    participant FrmAppController as FrmAppController (PHP)
    participant Browser as Browser (JS)
    participant Server as WP AJAX

    AdminPage->>FrmAppController: Load legacy views admin page
    FrmAppController->>Browser: Enqueue legacy-views.js
    Browser->>Browser: Initialize legacy-views.js (UI handlers, validation, AJAX)
    Browser->>Server: (On user action) AJAX request (e.g., add row, update form)
    Server-->>Browser: AJAX response (HTML, selectors, etc.)
    Browser->>Browser: Update UI dynamically
Loading

Possibly related PRs

  • Strategy11/formidable-forms#2052: Adds new contextual shortcode management functions within formidable_admin.js, which are related to the legacy shortcode toggle logic removed and refactored into legacy-views.js in this PR.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4941c22 and 7d90b43.

📒 Files selected for processing (2)
  • js/admin/legacy-views.js (1 hunks)
  • js/formidable_admin.js (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • js/admin/legacy-views.js
🧰 Additional context used
🧬 Code Graph Analysis (1)
js/formidable_admin.js (1)
js/admin/legacy-views.js (1)
  • event (16-16)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Cypress
  • GitHub Check: PHP 7.4 tests in WP trunk
  • GitHub Check: PHP 8 tests in WP trunk
🔇 Additional comments (1)
js/formidable_admin.js (1)

10640-10642: Great improvement to the code architecture!

Replacing the direct call to toggleAllowedShortcodes(this.id) with a custom event dispatch is an excellent refactoring approach. This change properly decouples the main admin JS from the legacy views functionality, allowing the legacy views code to be loaded only when needed. The event naming convention with the frm_ prefix and the clean data structure make this a maintainable solution.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (3)
classes/controllers/FrmAppController.php (1)

873-875: Explicitly enqueue the script in the footer to keep dependency order predictable

formidable_legacy_views depends on formidable_admin, which is registered with in_footer => true.
When the dependent is enqueued without the 5th argument, WordPress will silently move it to the footer, but only after an internal “group-upgrade” step. Being explicit avoids surprises and makes intent clearer to future maintainers.

- wp_enqueue_script(
-     'formidable_legacy_views',
-     FrmAppHelper::plugin_url() . '/js/admin/legacy-views.js',
-     array( 'jquery', 'formidable_admin' ),
-     FrmAppHelper::plugin_version()
- );
+wp_enqueue_script(
+    'formidable_legacy_views',
+    FrmAppHelper::plugin_url() . '/js/admin/legacy-views.js',
+    array( 'jquery', 'formidable_admin' ),
+    FrmAppHelper::plugin_version(),
+    true            // load in footer – matches dependency group
+);

Check that nothing else on the page still relies on the script being printed in the header before applying.

js/admin/legacy-views.js (2)

118-120: Avoid implicit global DOM

DOM is assigned without const / let, polluting the global scope. Declare it explicitly:

-DOM = tinymce.DOM;
+const DOM = tinymce.DOM;

157-160: Overwriting window.onscroll can clobber other listeners

Using addEventListener keeps existing handlers intact and is the recommended modern pattern:

-window.onscroll = document.documentElement.onscroll = setMenuOffset;
+window.addEventListener( 'scroll', setMenuOffset );
+document.documentElement.addEventListener( 'scroll', setMenuOffset );
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 26cc3fb and 1911d24.

📒 Files selected for processing (3)
  • classes/controllers/FrmAppController.php (1 hunks)
  • js/admin/legacy-views.js (1 hunks)
  • js/formidable_admin.js (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (3)
js/admin/legacy-views.js (2)
js/formidable_admin.js (5)
  • event (10479-10479)
  • event (10642-10642)
  • $advInfo (10137-10137)
  • frmAdminJs (240-240)
  • t (10085-10085)
classes/controllers/FrmAppController.php (1)
  • menu (11-24)
classes/controllers/FrmAppController.php (1)
classes/helpers/FrmAppHelper.php (3)
  • FrmAppHelper (6-4568)
  • plugin_url (65-68)
  • plugin_version (44-46)
js/formidable_admin.js (1)
js/admin/legacy-views.js (1)
  • event (16-16)
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: Run PHP Syntax inspection (8.3)
  • GitHub Check: PHP 7.4 tests in WP trunk
  • GitHub Check: PHP 8 tests in WP trunk
  • GitHub Check: Cypress
  • GitHub Check: Cypress
  • GitHub Check: PHP 8 tests in WP trunk
  • GitHub Check: PHP 7.4 tests in WP trunk
🔇 Additional comments (1)
js/formidable_admin.js (1)

10640-10644: Legacy views code properly moved to custom event pattern 👍

The change correctly moves the legacy views-specific code out of the main admin JS file by replacing a direct function call with a custom event. This is an excellent refactoring approach that keeps the functionality while properly separating concerns.

The dispatched custom event includes the necessary data for the legacy views script to handle this functionality elsewhere.

- toggleAllowedShortcodes( this.id );
+ const event   = new CustomEvent( 'frm_legacy_views_handle_field_focus' );
+ event.frmData = { idAttrValue: this.id };
+ document.dispatchEvent( event );

Comment thread js/admin/legacy-views.js
Comment thread js/admin/legacy-views.js Outdated
Comment thread js/admin/legacy-views.js Outdated
@Crabcyborg Crabcyborg merged commit ebb1f94 into master Apr 28, 2025
15 of 16 checks passed
@Crabcyborg Crabcyborg deleted the split_legacy_views_scripts_into_a_new_file branch April 28, 2025 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant