list_fold.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. const Cls = (app.views.ListFold = class ListFold extends app.View {
  12. static initClass() {
  13. this.targetClass = '_list-dir';
  14. this.handleClass = '_list-arrow';
  15. this.activeClass = 'open';
  16. this.events =
  17. {click: 'onClick'};
  18. this.shortcuts = {
  19. left: 'onLeft',
  20. right: 'onRight'
  21. };
  22. }
  23. constructor(el) { this.onLeft = this.onLeft.bind(this); this.onRight = this.onRight.bind(this); this.onClick = this.onClick.bind(this); this.el = el; super(...arguments); }
  24. open(el) {
  25. if (el && !el.classList.contains(this.constructor.activeClass)) {
  26. el.classList.add(this.constructor.activeClass);
  27. $.trigger(el, 'open');
  28. }
  29. }
  30. close(el) {
  31. if (el && el.classList.contains(this.constructor.activeClass)) {
  32. el.classList.remove(this.constructor.activeClass);
  33. $.trigger(el, 'close');
  34. }
  35. }
  36. toggle(el) {
  37. if (el.classList.contains(this.constructor.activeClass)) {
  38. this.close(el);
  39. } else {
  40. this.open(el);
  41. }
  42. }
  43. reset() {
  44. let el;
  45. while ((el = this.findByClass(this.constructor.activeClass))) {
  46. this.close(el);
  47. }
  48. }
  49. getCursor() {
  50. return this.findByClass(app.views.ListFocus.activeClass) || this.findByClass(app.views.ListSelect.activeClass);
  51. }
  52. onLeft() {
  53. const cursor = this.getCursor();
  54. if (cursor != null ? cursor.classList.contains(this.constructor.activeClass) : undefined) {
  55. this.close(cursor);
  56. }
  57. }
  58. onRight() {
  59. const cursor = this.getCursor();
  60. if (cursor != null ? cursor.classList.contains(this.constructor.targetClass) : undefined) {
  61. this.open(cursor);
  62. }
  63. }
  64. onClick(event) {
  65. if ((event.which !== 1) || event.metaKey || event.ctrlKey) { return; }
  66. if (!event.pageY) { return; } // ignore fabricated clicks
  67. let el = $.eventTarget(event);
  68. if (el.parentNode.tagName.toUpperCase() === 'SVG') { el = el.parentNode; }
  69. if (el.classList.contains(this.constructor.handleClass)) {
  70. $.stopEvent(event);
  71. this.toggle(el.parentNode);
  72. } else if (el.classList.contains(this.constructor.targetClass)) {
  73. if (el.hasAttribute('href')) {
  74. if (el.classList.contains(this.constructor.activeClass)) {
  75. if (el.classList.contains(app.views.ListSelect.activeClass)) { this.close(el); }
  76. } else {
  77. this.open(el);
  78. }
  79. } else {
  80. this.toggle(el);
  81. }
  82. }
  83. }
  84. });
  85. Cls.initClass();