list_select.js 1.8 KB

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