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 1 commit
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
35 changes: 33 additions & 2 deletions lib/features/popup-menu/PopupMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
80 changes: 79 additions & 1 deletion test/spec/features/popup-menu/PopupMenuSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,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 +921,6 @@ describe('features/popup-menu', function() {
});
}));


});


Expand Down