-
-
Notifications
You must be signed in to change notification settings - Fork 41
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
JavaScript extensibility for grids and forms #35
Comments
Whatever approach comes up on top, I'll first create a prototype to see how it feels to use it. |
Other alternatives:
|
I do like the |
Note to self: if using B or C there should be a special events that is triggered before and after the initial render.
|
Hyva_Admin Elements that can have events:Grids
Forms
|
I'll start by implementing option B (Generic event child elements with convention based event names). To try this out only grid actions will support events initially. Generic vents for rows and buttons will be added later once things stabilize. If needed an attribute to specify custom events can still be added in a backward compatible manner later. |
First experimental release in 1.1.13 to gather feedbackThe API for the action event node might be removed or changed in future. Specifying event triggers on actions allows creating complex ui customizations. <actions>
<action id="delete" label="Delete" url="*/*/delete">
<event on="click"/>
</action>
</actions> Event NameThe event name is built based on the grid name, the action id and the event trigger: private function getEventName(): string
{
$gridNameInEvent = $this->eventify($this->gridName);
return sprintf('hyva-grid-%s-action-%s-%s', $gridNameInEvent, $this->eventify($this->targetId), $this->on);
} For example, given a grid named products-query-grid, and an action with the id delete, the JavaScript event that is dispatched is
Event SubscribersEvent subscribers can be declared in .phtml template files that are added to the grid page via layout XML. Example: <script>
window.addEventListener('hyva-grid-products-grid-action-delete-click', e => {
if (! confirm('<?= __('Are you sure?') ?>')) {
e.detail.origEvent.preventDefault();
}
});
</script> Event ArgumentsThe event arguments can be retrieved from the events.detail property in subscribers. event.detail.origEventThis is the original event that was triggered by the user interaction. probably this is mainly useful to abort user actions with event.detail.rowThis property is a reference to the clicked grid table row. It might be useful to retrieve the rendered cell values in a kind of hacky way. event.detail.viewModelThis is the Alpine.js view model of the grid. event.detail.actionThis is the grid action id. In the examples on this page it is the string event.detail.paramsThis is the map of parameters that would be passed to the URL. It depends on the action configuration. The following example will add the params <actions idColumn="id">
<action id="delete" label="Delete" url="*/*/delete" idParam="foo">
<event on="click"/>
</action>
</actions> |
Issue Description
This Issue is to document the decisions regarding how to enable developers to trigger JavaScript on visitor actions, and to gather feedback.
Originally I started thinking about it in relation to the forms (issue #27), but @fabian Schmengler has a use case for the grids, too (see #34).
I think a common solution should be used.
A) Event specific attributes with native JS expressions
The idea is to add attributes like
onchange
,onclick
,onfocus
and so on to field and action elements, and the value could be rendered into the generated HTML directly.For example
This would result in an HTML element like this:
Pros of this approach:
Cons of this approach:
showPopup
in the above expression would have to be declared on thewindow
in global scope, unless more work is done to make the alpineJs view model extensible.onclick
values would be overwritten during configuration merging.B) Generic event child elements with convention based event names
In this approach,
event
elements can be added as children to supported grid and form nodes.For example:
This would result in the following HTML:
For grids the event name would include the grid-name and the action-id following the pattern in the example above (the last part would depend on the event, i.e.
click
,keydown
, etc).For forms the event name would follow the pattern
hyva_form_[form-name]_[field-name]_[event-type]
.Grid and form names would need to be normalized so they work in event names, that is, lowercase characters and underscores only.
The above example HTML would actially be slightly more complex because event arguments would need to be added to the dispatch.
Pros of this approach:
tr
row element and the view model, and for forms the field element and also the view model.Cons of this approach:
C) Generic event child elements with configurable event names
This approach is very similar to approach B), except that each
event
can specify a custom event name.For example:
This would result in the following HTML:
The above example HTML would actially be slightly more complex because event arguments would need to be added to the dispatch.
Pros of this approach:
tr
row element and the view model, and for forms the field element and also the view model.Cons of this approach:
enabled
attribute cold be added which would allow disabling events declared by other modules (but is this even needed?)D) Native events only with external subscribers
Any HTML element can be observed by subscribers that add attach programmatically.
This already is possible and require no knowledge beyond native JavaScript and the DOM model.
Pros of this approach:
Cons of this approach:
The text was updated successfully, but these errors were encountered: