Skip to content

Commit

Permalink
chore: unbuild search integration in popup menu
Browse files Browse the repository at this point in the history
This reverts the integration of the (partially broken) search
into the popup menu.
  • Loading branch information
nikku committed Sep 23, 2024
1 parent 8c452f0 commit 2ce8808
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 23 deletions.
8 changes: 3 additions & 5 deletions lib/features/popup-menu/PopupMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ var DEFAULT_PRIORITY = 1000;
* @param {EventBus} eventBus
* @param {Canvas} canvas
*/
export default function PopupMenu(config, eventBus, canvas, search) {
export default function PopupMenu(config, eventBus, canvas) {
this._eventBus = eventBus;
this._canvas = canvas;
this._search = search;

this._current = null;

Expand Down Expand Up @@ -95,8 +94,7 @@ export default function PopupMenu(config, eventBus, canvas, search) {
PopupMenu.$inject = [
'config.popupMenu',
'eventBus',
'canvas',
'search'
'canvas'
];

PopupMenu.prototype._render = function() {
Expand Down Expand Up @@ -140,7 +138,6 @@ PopupMenu.prototype._render = function() {
scale=${ scale }
onOpened=${ this._onOpened.bind(this) }
onClosed=${ this._onClosed.bind(this) }
searchFn=${ this._search }
...${{ ...options }}
/>
`,
Expand Down Expand Up @@ -551,6 +548,7 @@ PopupMenu.prototype._getHeaderEntries = function(target, providers) {


PopupMenu.prototype._getEmptyPlaceholder = function(providers) {

const provider = providers.find(
provider => isFunction(provider.getEmptyPlaceholder)
);
Expand Down
33 changes: 21 additions & 12 deletions lib/features/popup-menu/PopupMenuComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export default function PopupMenuComponent(props) {
scale,
search,
emptyPlaceholder,
searchFn,
entries: originalEntries,
onOpened,
onClosed
Expand All @@ -76,19 +75,29 @@ export default function PopupMenuComponent(props) {
return originalEntries;
}

if (!value) {
return originalEntries.filter(({ rank = 0 }) => rank >= 0);
}
const filter = entry => {
if (!value) {
return (entry.rank || 0) >= 0;
}

const searchableEntries = originalEntries.filter(({ searchable }) => searchable !== false);
if (entry.searchable === false) {
return false;
}

const searchableFields = [
entry.description || '',
entry.label || '',
entry.search || ''
].map(string => string.toLowerCase());

// every word of `value` should be included in one of the searchable fields
return value
.toLowerCase()
.split(/\s/g)
.every(word => searchableFields.some(field => field.includes(word)));
};

return searchFn(searchableEntries, value, {
keys: [
'label',
'description',
'search'
]
}).map(({ item }) => item);
return originalEntries.filter(filter);
}, [ searchable ]);

const [ entries, setEntries ] = useState(filterEntries(originalEntries, value));
Expand Down
3 changes: 0 additions & 3 deletions lib/features/popup-menu/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import PopupMenu from './PopupMenu';

import Search from '../search';


/**
* @type { import('didi').ModuleDeclaration }
*/
export default {
__depends__: [ Search ],
__init__: [ 'popupMenu' ],
popupMenu: [ 'type', PopupMenu ]
};
3 changes: 0 additions & 3 deletions test/spec/features/popup-menu/PopupMenuComponentSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import {
queryAll as domQueryAll
} from 'min-dom';

import searchFn from 'lib/features/search/search';


const TEST_IMAGE_URL = `data:image/svg+xml;utf8,${
encodeURIComponent(`
Expand Down Expand Up @@ -741,7 +739,6 @@ describe('features/popup-menu - <PopupMenu>', function() {
const props = {
entries: [],
headerEntries: [],
searchFn: searchFn,
position() {
return { x: 0, y: 0 };
},
Expand Down
22 changes: 22 additions & 0 deletions test/spec/features/popup-menu/PopupMenuSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1495,6 +1495,28 @@ describe('features/popup-menu', function() {
}));


it('should show search results (matching label & search)', inject(async function(popupMenu) {

// given
popupMenu.registerProvider('test-menu', testMenuProvider);
popupMenu.open({}, 'test-menu', { x: 100, y: 100 }, { search: true });

// when
await triggerSearch('delta search');

// then
var shownEntries;

await waitFor(() => {
shownEntries = queryPopupAll('.entry');

expect(shownEntries).to.have.length(1);
});

expect(shownEntries[0].querySelector('.djs-popup-label').textContent).to.eql('Delta');
}));


describe('ranking', function() {

it('should hide rank < 0 items', inject(async function(popupMenu) {
Expand Down

0 comments on commit 2ce8808

Please sign in to comment.