list_select.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. app.views.ListSelect = class ListSelect extends app.View {
  2. static activeClass = "active";
  3. static events = { click: "onClick" };
  4. deactivate() {
  5. if (super.deactivate(...arguments)) {
  6. this.deselect();
  7. }
  8. }
  9. select(el) {
  10. this.deselect();
  11. if (el) {
  12. el.classList.add(this.constructor.activeClass);
  13. $.trigger(el, "select");
  14. }
  15. }
  16. deselect() {
  17. let selection;
  18. if ((selection = this.getSelection())) {
  19. selection.classList.remove(this.constructor.activeClass);
  20. $.trigger(selection, "deselect");
  21. }
  22. }
  23. selectByHref(href) {
  24. if (this.getSelection()?.getAttribute("href") !== href) {
  25. this.select(this.find(`a[href='${href}']`));
  26. }
  27. }
  28. selectCurrent() {
  29. this.selectByHref(location.pathname + location.hash);
  30. }
  31. getSelection() {
  32. return this.findByClass(this.constructor.activeClass);
  33. }
  34. onClick(event) {
  35. if (event.which !== 1 || event.metaKey || event.ctrlKey) {
  36. return;
  37. }
  38. const target = $.eventTarget(event);
  39. if (target.tagName === "A") {
  40. this.select(target);
  41. }
  42. }
  43. };