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

The StaticSearch class should emit custom events #331

Open
martindholmes opened this issue Jan 30, 2025 · 4 comments
Open

The StaticSearch class should emit custom events #331

martindholmes opened this issue Jan 30, 2025 · 4 comments
Assignees
Labels
enhancement New feature or request fix committed A fix has been made, so if no problems emerge the issue can be closed.
Milestone

Comments

@martindholmes
Copy link
Collaborator

martindholmes commented Jan 30, 2025

There's a common requirement to be able to do some post-processing or enhancements whenever specific things happen with the instantiated StaticSearch class; examples are:

  • When the Sch object has been fully instantiated, add something to it
  • When a search has been initiated, show some interface signal
  • When a search has been completed, do something
  • When the search results have been rendered onto the page, do something

This would be made much easier if StaticSearch emitted custom events which other code could listen for. Here's an example (untested, off the top of my head, but something I'd like to be able to do in a current project):

//at the end of StaticSearch~processResults: existing code:
      this.isSearching = false;
      this.searchFinishedHook(4);
//new code
      const evt = new CustomEvent('ssResultsRendered', {detail: { 'hits': this.resultSet.getSize()} });
      window.dispatchEvent(evt);

//existing code
      return (this.resultSet.getSize() > 0);

And this could be hooked by the site-specific code:

window.addEventListener('ssResultsRendered', function(evt){console.log(evt.detail.hits + ' results were rendered on the page.');});

(I would use this particular case to insert a special link enabling the searcher to view all the results rendered together in a single page, if the number of results is sufficiently small for this to be manageable.)

@martindholmes martindholmes added the enhancement New feature or request label Jan 30, 2025
@martindholmes martindholmes added this to the Release 2.1 milestone Jan 30, 2025
@martindholmes
Copy link
Collaborator Author

martindholmes commented Feb 1, 2025

Working on this in issue-331-events branch. Custom events so far:

  • ssInstantiated: dispatched at the end of the object constructor.
  • ssJsonRetrieved: dispatched when the initial collection of Json files has been retrieved.
  • ssSearchStarting: dispatched before starting a search.
  • ssFormCleared: dispatched after clearing the search form controls.
  • ssSearchCompleted: dispatched after results are rendered (or the warning message appears for too many results).

@martindholmes
Copy link
Collaborator Author

The events above are now added and working (confirmed by adding a small block of code to the test suite JS).

@martindholmes martindholmes modified the milestones: Release 2.1, Release 2.0 Feb 1, 2025
@martindholmes
Copy link
Collaborator Author

What's required now is some documentation, but that will have to wait until the documentation CSS work is finished and merged.

@martindholmes martindholmes added the fix committed A fix has been made, so if no problems emerge the issue can be closed. label Feb 1, 2025
@martindholmes
Copy link
Collaborator Author

PR is merged; documentation still to do, so keeping this open as a reminder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fix committed A fix has been made, so if no problems emerge the issue can be closed.
Projects
None yet
Development

No branches or pull requests

2 participants