Skip to content

Commit

Permalink
chore(dia.Paper): extract logic into a method
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinKanera committed Nov 3, 2023
1 parent 07a64a6 commit 17db65a
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions src/dia/Paper.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2204,19 +2204,11 @@ export const Paper = View.extend({
const rootViewEl = view.el;

// Custom event
const eventNode = target.closest('[event]');
if (eventNode && rootViewEl !== eventNode && view.el.contains(eventNode)) {
const eventEvt = normalizeEvent($.Event(evt.originalEvent, {
data: evt.data,
// Originally the event listener was attached to the event element.
currentTarget: eventNode
}));
this.onevent(eventEvt);
if (eventEvt.isDefaultPrevented()) {
evt.preventDefault();
}
// `onevent` can stop propagation
const eventEvt = this.customEventTrigger(evt, rootViewEl);
if (eventEvt) {
// `onevent` could have stopped propagation
if (eventEvt.isPropagationStopped()) return;

evt.data = eventEvt.data;
}

Expand Down Expand Up @@ -2548,26 +2540,19 @@ export const Paper = View.extend({
onlabel: function(evt) {

var labelNode = evt.currentTarget;
const eventNode = evt.target.closest('[event]');

var view = this.findView(labelNode);
if (!view) return;

evt = normalizeEvent(evt);
if (this.guard(evt, view)) return;

if (eventNode && labelNode !== eventNode && view.el.contains(eventNode)) {
const eventEvt = normalizeEvent($.Event(evt.originalEvent, {
data: evt.data,
// Originally the event listener was attached to the event element.
currentTarget: eventNode
}));
this.onevent(eventEvt);
if (eventEvt.isDefaultPrevented()) {
evt.preventDefault();
}
// `onevent` can stop propagation
// Custom event
const eventEvt = this.customEventTrigger(evt, labelNode);
if (eventEvt) {
// `onevent` could have stopped propagation
if (eventEvt.isPropagationStopped()) return;

evt.data = eventEvt.data;
}

Expand Down Expand Up @@ -3098,6 +3083,28 @@ export const Paper = View.extend({
markerContentVEl.appendTo(markerVEl);
markerVEl.appendTo(defs);
return id;
},

customEventTrigger: function(evt, rootNode) {

const eventNode = evt.target.closest('[event]');
const view = this.findView(rootNode);

if (eventNode && rootNode !== eventNode && view.el.contains(eventNode)) {
const eventEvt = normalizeEvent($.Event(evt.originalEvent, {
data: evt.data,
// Originally the event listener was attached to the event element.
currentTarget: eventNode
}));

this.onevent(eventEvt);

if (eventEvt.isDefaultPrevented()) {
evt.preventDefault();
}

return eventEvt;
}
}

}, {
Expand Down

0 comments on commit 17db65a

Please sign in to comment.