list_select.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. * DS103: Rewrite code to no longer use __guard__, or convert again using --optional-chaining
  7. * DS206: Consider reworking classes to avoid initClass
  8. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
  9. */
  10. app.views.ListSelect = class ListSelect extends app.View {
  11. static initClass() {
  12. this.activeClass = "active";
  13. this.events = { click: "onClick" };
  14. }
  15. deactivate() {
  16. if (super.deactivate(...arguments)) {
  17. this.deselect();
  18. }
  19. }
  20. select(el) {
  21. this.deselect();
  22. if (el) {
  23. el.classList.add(this.constructor.activeClass);
  24. $.trigger(el, "select");
  25. }
  26. }
  27. deselect() {
  28. let selection;
  29. if ((selection = this.getSelection())) {
  30. selection.classList.remove(this.constructor.activeClass);
  31. $.trigger(selection, "deselect");
  32. }
  33. }
  34. selectByHref(href) {
  35. if (
  36. __guard__(this.getSelection(), (x) => x.getAttribute("href")) !== href
  37. ) {
  38. this.select(this.find(`a[href='${href}']`));
  39. }
  40. }
  41. selectCurrent() {
  42. this.selectByHref(location.pathname + location.hash);
  43. }
  44. getSelection() {
  45. return this.findByClass(this.constructor.activeClass);
  46. }
  47. onClick(event) {
  48. if (event.which !== 1 || event.metaKey || event.ctrlKey) {
  49. return;
  50. }
  51. const target = $.eventTarget(event);
  52. if (target.tagName === "A") {
  53. this.select(target);
  54. }
  55. }
  56. };
  57. app.views.ListSelect.initClass();
  58. function __guard__(value, transform) {
  59. return typeof value !== "undefined" && value !== null
  60. ? transform(value)
  61. : undefined;
  62. }