results.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Sanity-check the conversion and remove this comment.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS102: Remove unnecessary code created because of implicit returns
  6. * DS206: Consider reworking classes to avoid initClass
  7. * DS207: Consider shorter variations of null checks
  8. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
  9. */
  10. app.views.Results = class Results extends app.View {
  11. static initClass() {
  12. this.className = "_list";
  13. this.events = { click: "onClick" };
  14. this.routes = { after: "afterRoute" };
  15. }
  16. constructor(sidebar, search) {
  17. super();
  18. this.sidebar = sidebar;
  19. this.search = search;
  20. this.init0(); // needs this.search
  21. this.refreshElements();
  22. }
  23. deactivate() {
  24. if (super.deactivate(...arguments)) {
  25. this.empty();
  26. }
  27. }
  28. init0() {
  29. this.addSubview((this.listFocus = new app.views.ListFocus(this.el)));
  30. this.addSubview((this.listSelect = new app.views.ListSelect(this.el)));
  31. this.search
  32. .on("results", (entries, flags) => this.onResults(entries, flags))
  33. .on("noresults", () => this.onNoResults())
  34. .on("clear", () => this.onClear());
  35. }
  36. onResults(entries, flags) {
  37. if (flags.initialResults) {
  38. if (this.listFocus != null) {
  39. this.listFocus.blur();
  40. }
  41. }
  42. if (flags.initialResults) {
  43. this.empty();
  44. }
  45. this.append(this.tmpl("sidebarResult", entries));
  46. if (flags.initialResults) {
  47. if (flags.urlSearch) {
  48. this.openFirst();
  49. } else {
  50. this.focusFirst();
  51. }
  52. }
  53. }
  54. onNoResults() {
  55. this.html(this.tmpl("sidebarNoResults"));
  56. }
  57. onClear() {
  58. this.empty();
  59. }
  60. focusFirst() {
  61. if (!app.isMobile()) {
  62. if (this.listFocus != null) {
  63. this.listFocus.focusOnNextFrame(this.el.firstElementChild);
  64. }
  65. }
  66. }
  67. openFirst() {
  68. if (this.el.firstElementChild != null) {
  69. this.el.firstElementChild.click();
  70. }
  71. }
  72. onDocEnabled(doc) {
  73. app.router.show(doc.fullPath());
  74. return this.sidebar.onDocEnabled();
  75. }
  76. afterRoute(route, context) {
  77. if (route === "entry") {
  78. this.listSelect.selectByHref(context.entry.fullPath());
  79. } else {
  80. this.listSelect.deselect();
  81. }
  82. }
  83. onClick(event) {
  84. let slug;
  85. if (event.which !== 1) {
  86. return;
  87. }
  88. if ((slug = $.eventTarget(event).getAttribute("data-enable"))) {
  89. $.stopEvent(event);
  90. const doc = app.disabledDocs.findBy("slug", slug);
  91. if (doc) {
  92. return app.enableDoc(doc, this.onDocEnabled.bind(this, doc), $.noop);
  93. }
  94. }
  95. }
  96. };
  97. app.views.Results.initClass();