results.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. app.views.Results = class Results extends app.View {
  2. static className = "_list";
  3. static events = { click: "onClick" };
  4. static routes = { after: "afterRoute" };
  5. constructor(sidebar, search) {
  6. super();
  7. this.sidebar = sidebar;
  8. this.search = search;
  9. this.init0(); // needs this.search
  10. this.refreshElements();
  11. }
  12. deactivate() {
  13. if (super.deactivate(...arguments)) {
  14. this.empty();
  15. }
  16. }
  17. init0() {
  18. this.addSubview((this.listFocus = new app.views.ListFocus(this.el)));
  19. this.addSubview((this.listSelect = new app.views.ListSelect(this.el)));
  20. this.search
  21. .on("results", (entries, flags) => this.onResults(entries, flags))
  22. .on("noresults", () => this.onNoResults())
  23. .on("clear", () => this.onClear());
  24. }
  25. onResults(entries, flags) {
  26. if (flags.initialResults) {
  27. this.listFocus?.blur();
  28. }
  29. if (flags.initialResults) {
  30. this.empty();
  31. }
  32. this.append(this.tmpl("sidebarResult", entries));
  33. if (flags.initialResults) {
  34. if (flags.urlSearch) {
  35. this.openFirst();
  36. } else {
  37. this.focusFirst();
  38. }
  39. }
  40. }
  41. onNoResults() {
  42. this.html(this.tmpl("sidebarNoResults"));
  43. }
  44. onClear() {
  45. this.empty();
  46. }
  47. focusFirst() {
  48. if (!app.isMobile()) {
  49. this.listFocus?.focusOnNextFrame(this.el.firstElementChild);
  50. }
  51. }
  52. openFirst() {
  53. this.el.firstElementChild?.click();
  54. }
  55. onDocEnabled(doc) {
  56. app.router.show(doc.fullPath());
  57. return this.sidebar.onDocEnabled();
  58. }
  59. afterRoute(route, context) {
  60. if (route === "entry") {
  61. this.listSelect.selectByHref(context.entry.fullPath());
  62. } else {
  63. this.listSelect.deselect();
  64. }
  65. }
  66. onClick(event) {
  67. if (event.which !== 1) {
  68. return;
  69. }
  70. const slug = $.eventTarget(event).getAttribute("data-enable");
  71. if (slug) {
  72. $.stopEvent(event);
  73. const doc = app.disabledDocs.findBy("slug", slug);
  74. if (doc) {
  75. return app.enableDoc(doc, this.onDocEnabled.bind(this, doc), $.noop);
  76. }
  77. }
  78. }
  79. };