Skip to content

Commit

Permalink
feat(popup-menu): add ability to refresh without reopening
Browse files Browse the repository at this point in the history
Closes #804
  • Loading branch information
philippfromme committed Feb 21, 2024
1 parent 2b82e8f commit fe6ae72
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 3 deletions.
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() {
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

0 comments on commit fe6ae72

Please sign in to comment.