results.js 2.8 KB

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