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

feat: allow frontend code in events #80

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
27 changes: 18 additions & 9 deletions ipyvue/VueWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,27 @@ def on_event(self, event_and_modifiers, callback, remove=False):
]:
del self._event_handlers_map[existing_event]

self._event_handlers_map[event_and_modifiers] = CallbackDispatcher()

self._event_handlers_map[event_and_modifiers].register_callback(
callback, remove=remove
)

if remove and not self._event_handlers_map[event_and_modifiers].callbacks:
if isinstance(callback, str):
self._event_handlers_map[event_and_modifiers] = callback
else:
self._event_handlers_map[event_and_modifiers] = CallbackDispatcher()

self._event_handlers_map[event_and_modifiers].register_callback(
callback, remove=remove
Copy link
Collaborator

Choose a reason for hiding this comment

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

Slightly unrelated but I think this code (81-95) is very hard to read, or does not work. The register_callback call with remove=True is a no-op or maybe it gives an exception?
Would make me feel more comfortable is we had a test for this.

)

if remove and (
isinstance(self._event_handlers_map[event_and_modifiers], str)
or not self._event_handlers_map[event_and_modifiers].callbacks
):
del self._event_handlers_map[event_and_modifiers]

difference = set(self._event_handlers_map.keys()) ^ set(self._events)
if len(difference) != 0:
self._events = list(self._event_handlers_map.keys())
self._events = {
k: v if isinstance(v, str) else None
for k, v in self._event_handlers_map.items()
}

def fire_event(self, event, data=None):
"""Manually trigger an event handler on the Python side."""
Expand Down Expand Up @@ -162,7 +171,7 @@ class VueWidget(DOMWidget, Events):

slot = Unicode(None, allow_none=True).tag(sync=True)

_events = List(Unicode()).tag(sync=True)
_events = Dict(None, allow_none=True).tag(sync=True)

v_model = Any("!!disabled!!", allow_none=True).tag(sync=True)

Expand Down
15 changes: 9 additions & 6 deletions js/src/VueRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ function createAttrsMapping(model) {
}, {});
}

function addEventWithModifiers(eventAndModifiers, obj, fn) { // eslint-disable-line no-unused-vars
function addEventWithModifiers(eventAndModifiers, content, slotScopes, obj, fn) { // eslint-disable-line no-unused-vars
/* Example Vue.compile output:
* (function anonymous() {
* with (this) {
Expand All @@ -164,11 +164,12 @@ function addEventWithModifiers(eventAndModifiers, obj, fn) { // eslint-disable-l
* }
* )
*/
const { on } = Vue.compile(`<dummy @${eventAndModifiers}="fn"></dummy>`)
const { on } = Vue.compile(`<dummy @${eventAndModifiers}="${content || 'fn' }"></dummy>`)
.render.bind({
_c: (_, data) => data,
_k: Vue.prototype._k,
fn,
...(slotScopes || {}),
})();

return {
Expand All @@ -177,10 +178,12 @@ function addEventWithModifiers(eventAndModifiers, obj, fn) { // eslint-disable-l
};
}

function createEventMapping(model, parentView) {
return (model.get('_events') || [])
.reduce((result, eventAndModifiers) => addEventWithModifiers(
function createEventMapping(model, parentView, slotScopes) {
return Object.entries(model.get('_events') || {})
.reduce((result, [eventAndModifiers, content]) => addEventWithModifiers(
eventAndModifiers,
content,
slotScopes,
result,
(e) => {
model.send({
Expand Down Expand Up @@ -241,7 +244,7 @@ function createContent(createElement, model, vueModel, parentView, slotScopes) {
const scopedSlots = createSlots(createElement, model, vueModel, parentView, slotScopes);

return {
on: { ...createEventMapping(model, parentView), ...slotUseOn(model, slotScopes) },
on: { ...createEventMapping(model, parentView, slotScopes), ...slotUseOn(model, slotScopes) },
...model.get('style_') && { style: model.get('style_') },
...model.get('class_') && { class: model.get('class_') },
...scopedSlots && { scopedSlots: vueModel._u(scopedSlots) },
Expand Down
Loading