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

Emit popupMenu/palette/contextPad.trigger events #772

Merged
merged 3 commits into from
Apr 5, 2023
Merged
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
4 changes: 4 additions & 0 deletions lib/features/context-pad/ContextPad.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ ContextPad.prototype.triggerEntry = function(entryId, action, event, autoActivat

var handler = entry.action;

if (this._eventBus.fire('contextPad.trigger', { entry, event }) === false) {
return;
}

// simple action (via callback function)
if (isFunction(handler)) {
if (action === 'click') {
Expand Down
4 changes: 4 additions & 0 deletions lib/features/palette/Palette.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ Palette.prototype.triggerEntry = function(entryId, action, event, autoActivate)

handler = entry.action;

if (this._eventBus.fire('palette.trigger', { entry, event }) === false) {
return;
}

// simple action (via callback function)
if (isFunction(handler)) {
if (action === 'click') {
Expand Down
4 changes: 4 additions & 0 deletions lib/features/popup-menu/PopupMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,10 @@ PopupMenu.prototype.trigger = function(event, entry, action = 'click') {

const handler = entry.action;

if (this._emit('trigger', { entry, event }) === false) {
return;
}

if (isFunction(handler)) {
if (action === 'click') {
return handler(event, entry);
Expand Down
33 changes: 33 additions & 0 deletions test/spec/features/context-pad/ContextPadSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,39 @@ describe('features/context-pad', function() {
});


describe('event integration', function() {

beforeEach(bootstrapDiagram({ modules: [ contextPadModule, providerModule ] }));

it('should fire "contextPad.trigger"', inject(function(canvas, contextPad, eventBus) {

// given
var shape = canvas.addShape({ id: 's1', width: 100, height: 100, x: 10, y: 10 });
var triggerSpy = sinon.spy();

eventBus.on('contextPad.trigger', triggerSpy);

contextPad.open(shape);

var pad = contextPad.getPad(shape),
html = pad.html,
target = domQuery('[data-action="action.c"]', html);

var event = globalEvent(target, { x: 0, y: 0 });

// when
contextPad.trigger('click', event);

// then
const entry = contextPad._current.entries['action.c'];

expect(triggerSpy).to.have.been.calledOnce;
expect(triggerSpy.getCall(0).args[1]).to.eql({ entry, event });
}));

});


describe('scaling', function() {

var NUM_REGEX = /([+-]?\d*[.]?\d+)(?=,|\))/g;
Expand Down
42 changes: 42 additions & 0 deletions test/spec/features/palette/PaletteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,48 @@ describe('features/palette', function() {
}));
});


describe('event integration', function() {

const entry = {
label: 'My Entry',
action: () => {}
};

const paletteProvider = {
getPaletteEntries: function() {
return {
'entry-1': entry
};
}
};

beforeEach(bootstrapDiagram({ modules: [ paletteModule ] }));

beforeEach(inject(function(palette) {
palette.registerProvider(800, paletteProvider);
}));


it('should fire "palette.trigger"', inject(function(palette, eventBus) {

// given
var target = domQuery('.djs-palette [data-action="entry-1"]');
var event = globalEvent(target, { x: 0, y: 0 });
var triggerSpy = sinon.spy();

eventBus.on('palette.trigger', triggerSpy);

// when
palette.trigger('click', event);

// then
expect(triggerSpy).to.have.been.calledOnce;
expect(triggerSpy.getCall(0).args[1]).to.eql({ entry, event });
}));

});

});


Expand Down
38 changes: 38 additions & 0 deletions test/spec/features/popup-menu/PopupMenuSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,44 @@ describe('features/popup-menu', function() {
});


describe('events integration', function() {

it('should fire "popupMenu.trigger"', inject(function(popupMenu, eventBus) {

// given
var triggerSpy = sinon.spy();
eventBus.on('popupMenu.trigger', triggerSpy);

popupMenu.registerProvider('test-menu', {
getEntries: function() {
return [ {
id: '1',
label: 'Entry 1',
action: () => {}
} ];
}
});

popupMenu.open({}, 'test-menu', { x: 100, y: 100 });

var entry = queryEntry('1');
var event = globalEvent(entry, { x: 0, y: 0 });

// when
popupMenu.trigger(event);

// then
expect(triggerSpy).to.have.been.calledOnce;
expect(triggerSpy.getCall(0).args[1]).to.eql({
entry: popupMenu._getEntry('1'),
event
});
}));


});


describe('with updater', function() {

it('should allow to add entries', inject(function(popupMenu) {
Expand Down