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(popup-menu): add ability to refresh without reopening #860

Merged
merged 2 commits into from
Feb 21, 2024
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
37 changes: 34 additions & 3 deletions lib/features/popup-menu/PopupMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default function PopupMenu(config, eventBus, canvas) {
const element = this.isOpen() && this._current.target;

if (event.element === element) {
this._render();
this.refresh();
}
});

Expand All @@ -101,7 +101,7 @@ PopupMenu.prototype._render = function() {

const {
position: _position,
className,
providerId: className,
entries,
headerEntries,
emptyPlaceholder,
Expand Down Expand Up @@ -179,7 +179,7 @@ PopupMenu.prototype.open = function(target, providerId, position, options) {

this._current = {
position,
className: providerId,
providerId,
target,
entries,
headerEntries,
Expand All @@ -195,6 +195,37 @@ PopupMenu.prototype.open = function(target, providerId, position, options) {
this._render();
};

/**
* Refresh the popup menu entries without changing the target or position.
*/
PopupMenu.prototype.refresh = function() {
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we can we consolidate existing behavior, i.e. call refresh here?

Looks good otherwise.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, I'll add that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. ✅

if (!this.isOpen()) {
return;
}

const {
target,
providerId
} = this._current;

const {
entries,
headerEntries,
emptyPlaceholder
} = this._getContext(target, providerId);

this._current = {
...this._current,
entries,
headerEntries,
emptyPlaceholder
};

this._emit('refresh');

this._render();
};


PopupMenu.prototype._getContext = function(target, provider) {

Expand Down
129 changes: 125 additions & 4 deletions test/spec/features/popup-menu/PopupMenuSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ describe('features/popup-menu', function() {
eventBus.on('popupMenu.open', openSpy);
eventBus.on('popupMenu.opened', openedSpy);


// when
popupMenu.open({}, 'menu', { x: 100, y: 100 });

Expand Down Expand Up @@ -558,6 +557,85 @@ describe('features/popup-menu', function() {
});


describe('#refresh', function() {

it('should refresh', inject(function(eventBus, popupMenu) {

// given
var refreshSpy = sinon.spy();

eventBus.on('popupMenu.refresh', refreshSpy);

var dynamicMenuProvider = {
getPopupMenuEntries: function() {
return {
foo: { label: 'Foo' },
bar: { label: 'Bar' }
};
},
getHeaderEntries: function() {
return {
foo: { id: 'foo', label: 'Foo' },
bar: { id: 'bar', label: 'Bar' }
};
}
};

popupMenu.registerProvider('menu', dynamicMenuProvider);

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

expect(popupMenu._current).to.exist;
expect(popupMenu._current.entries).to.have.keys('foo', 'bar');
expect(popupMenu._current.entries).not.to.have.keys('baz');
expect(popupMenu._current.headerEntries).to.have.keys('foo', 'bar');
expect(popupMenu._current.headerEntries).not.to.have.keys('baz');

// when
dynamicMenuProvider.getPopupMenuEntries = function() {
return {
foo: { label: 'Foo' },
bar: { label: 'Bar' },
baz: { label: 'Baz' }
};
};

dynamicMenuProvider.getHeaderEntries = function() {
return {
foo: { id: 'foo', label: 'Foo' },
bar: { id: 'bar', label: 'Bar' },
baz: { id: 'baz', label: 'Baz' }
};
};

popupMenu.refresh();

// then
expect(refreshSpy).to.have.been.calledOnce;

expect(popupMenu._current).to.exist;
expect(popupMenu._current.entries).to.have.keys('foo', 'bar', 'baz');
expect(popupMenu._current.headerEntries).to.have.keys('foo', 'bar', 'baz');
}));


it('should not refresh', inject(function(eventBus, popupMenu) {

// given
var refreshSpy = sinon.spy();

eventBus.on('popupMenu.refresh', refreshSpy);

// when
popupMenu.refresh();

// then
expect(refreshSpy).not.to.have.been.called;
}));

});


describe('#close', function() {

beforeEach(inject(async function(eventBus, popupMenu) {
Expand Down Expand Up @@ -842,7 +920,6 @@ describe('features/popup-menu', function() {
});
}));


});


Expand Down Expand Up @@ -948,7 +1025,51 @@ describe('features/popup-menu', function() {
afterEach(sinon.restore);


it('should close menu (contextPad.close)', inject(function(popupMenu, eventBus) {
it('should refresh (element.changed)',inject(function(eventBus, popupMenu) {

// given
popupMenu.registerProvider('menu', menuProvider);

const element = { id: 'foo' };

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

const refreshSpy = sinon.spy();

eventBus.on('popupMenu.refresh', refreshSpy);

// when
eventBus.fire('element.changed', {
element
});

// then
expect(refreshSpy).to.have.been.calledOnce;
}));


it('should not refresh (element.changed)',inject(function(eventBus, popupMenu) {

// given
popupMenu.registerProvider('menu', menuProvider);

popupMenu.open({ id: 'foo' }, 'menu', { x: 100, y: 100 });

const refreshSpy = sinon.spy();

eventBus.on('popupMenu.refresh', refreshSpy);

// when
eventBus.fire('element.changed', {
element: { id: 'bar' }
});

// then
expect(refreshSpy).not.to.have.been.called;
}));


it('should close (contextPad.close)', inject(function(popupMenu, eventBus) {

// given
popupMenu.registerProvider('menu', menuProvider);
Expand All @@ -965,7 +1086,7 @@ describe('features/popup-menu', function() {
}));


it('should close menu (canvas.viewbox.changing)', inject(function(popupMenu, eventBus) {
it('should close (canvas.viewbox.changing)', inject(function(popupMenu, eventBus) {

// given
popupMenu.registerProvider('menu', menuProvider);
Expand Down