Skip to content
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

Refactor form.js to allow custom field initializer callbacks #469

Merged
merged 3 commits into from
Jan 13, 2024

Conversation

jowilf
Copy link
Owner

@jowilf jowilf commented Jan 13, 2024

No description provided.

Copy link

github-actions bot commented Jan 13, 2024

@github-actions github-actions bot temporarily deployed to pull request January 13, 2024 02:24 Inactive
@github-actions github-actions bot temporarily deployed to pull request January 13, 2024 03:05 Inactive
@github-actions github-actions bot temporarily deployed to pull request January 13, 2024 03:10 Inactive
Copy link

codecov bot commented Jan 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (f844878) 100.00% compared to head (5b554c0) 100.00%.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #469   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           75        75           
  Lines         5779      5780    +1     
=========================================
+ Hits          5779      5780    +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jowilf jowilf merged commit 6d45b0b into main Jan 13, 2024
10 checks passed
@jowilf jowilf deleted the refactor-formjs branch January 13, 2024 03:17
@hasansezertasan
Copy link
Contributor

@jowilf I want to ask you something, do we have a way to watch the entire documentation and initialize some JS when an element with a certain selector is created? For example, trigger registerFieldInitializer when an element is created with a class containing "field".

@jowilf
Copy link
Owner Author

jowilf commented Jan 17, 2024

I'm not sure if I fully understand your question but if you create an element you need to call initializeFields on the element. This function will loop through all the registered callbacks and invoke them. Take a look at the ListField implementation that adds dynamically a field template ->

initializeFields(template);

You don't have to watch the entire document if you can control when the element is created.

@hasansezertasan
Copy link
Contributor

hasansezertasan commented Jan 17, 2024

I'm not sure if I fully understand your question but if you create an element you need to call initializeFields on the element.

Let me try to explain one more time. Do you know anything about HTMX? I'm using it in my project.

Here is an example of what I am doing...

</> htmx ~ Examples ~ Modal Dialogs with UIKit. Navigate to this page and click "Show Modal Dialog". The modal content (a form) is fetched from the backend. So the backend returns an HTML, and HTMX puts it where we tell it to put.

Let's say that one of the inputs in that form is a JavaScript-initialized field, let's assume it is a TinyMCE Editor and it has this class: field-tinymce-editor form-control. With HTMX, I can not trigger a function based on the content of the response and I don't want to put an extra "script" file loader into that response HTML, the JavaScript file is already fetched, why fetch it again?

So what I am asking is this, is there any way to "watch" the document and trigger a function (Field initializer) when an element is created with a certain selector, in this example: .field-tinymce-editor.

Even if I can not trigger a function based on the content of the response and I don't want to put an extra "script" file loader into that response, I can still add <script>initializeFields(document);</script> (or something like that). But I want it to be as dynamic as possible and I believe there is only one way to make it: watch the document for the creation of an element with a selector.

I hope this one was clear enough.

@hasansezertasan
Copy link
Contributor

hasansezertasan commented Feb 17, 2024

I have found these two JS packages:

kubetail-org/sentineljs: Detect new DOM nodes using CSS selectors (650 bytes)

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>ArriveJS</title>
        <script src="https://cdn.rawgit.com/muicss/sentineljs/0.0.7/dist/sentinel.min.js"></script>
    </head>
    <body>
        <div class="container"></div>
        <script>
            sentinel.on('.container', function(newElem) {
                console.log(newElem);
                newElem.innerText = "This text was changed by Sentinel.js";
            });
            var newElement = document.createElement("div");
            newElement.className = "target";
            newElement.innerHTML = "This is a new element added to the page.";
            document.querySelector(".container").appendChild(newElement);
            console.log(newElement)
        </script>
    </body>
</html>

uzairfarooq/arrive: Watch for DOM elements creation and removal

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>SentinelJS</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/arrive/2.4.1/arrive.min.js" integrity="sha512-wkU3qYWjenbM+t2cmvw2ADRRh4opbOYBjkhrPGHV7M6dcE/TR0oKpoDkWXfUs3HrulI2JFuTQyqPLRih1V54EQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    </head>
    <body>
        <div class="container"></div>
        <script>
            document.querySelector(".container").arrive(".target", function(newElem) {
                console.log(newElem);
                newElem.innerText = "This text was changed by Arrive.js";
            });
            var newElement = document.createElement("div");
            newElement.className = "target";
            newElement.innerHTML = "This is a new element added to the page.";
            document.querySelector(".container").appendChild(newElement);
            console.log(newElement)
        </script>
    </body>
</html>

They are quite helpful for this purpose: watch the DOM and do something when a target element is created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants