list_fold.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. * DS206: Consider reworking classes to avoid initClass
  7. * DS207: Consider shorter variations of null checks
  8. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
  9. */
  10. app.views.ListFold = class ListFold extends app.View {
  11. static initClass() {
  12. this.targetClass = "_list-dir";
  13. this.handleClass = "_list-arrow";
  14. this.activeClass = "open";
  15. this.events = { click: "onClick" };
  16. this.shortcuts = {
  17. left: "onLeft",
  18. right: "onRight",
  19. };
  20. }
  21. open(el) {
  22. if (el && !el.classList.contains(this.constructor.activeClass)) {
  23. el.classList.add(this.constructor.activeClass);
  24. $.trigger(el, "open");
  25. }
  26. }
  27. close(el) {
  28. if (el && el.classList.contains(this.constructor.activeClass)) {
  29. el.classList.remove(this.constructor.activeClass);
  30. $.trigger(el, "close");
  31. }
  32. }
  33. toggle(el) {
  34. if (el.classList.contains(this.constructor.activeClass)) {
  35. this.close(el);
  36. } else {
  37. this.open(el);
  38. }
  39. }
  40. reset() {
  41. let el;
  42. while ((el = this.findByClass(this.constructor.activeClass))) {
  43. this.close(el);
  44. }
  45. }
  46. getCursor() {
  47. return (
  48. this.findByClass(app.views.ListFocus.activeClass) ||
  49. this.findByClass(app.views.ListSelect.activeClass)
  50. );
  51. }
  52. onLeft() {
  53. const cursor = this.getCursor();
  54. if (
  55. cursor != null
  56. ? cursor.classList.contains(this.constructor.activeClass)
  57. : undefined
  58. ) {
  59. this.close(cursor);
  60. }
  61. }
  62. onRight() {
  63. const cursor = this.getCursor();
  64. if (
  65. cursor != null
  66. ? cursor.classList.contains(this.constructor.targetClass)
  67. : undefined
  68. ) {
  69. this.open(cursor);
  70. }
  71. }
  72. onClick(event) {
  73. if (event.which !== 1 || event.metaKey || event.ctrlKey) {
  74. return;
  75. }
  76. if (!event.pageY) {
  77. return;
  78. } // ignore fabricated clicks
  79. let el = $.eventTarget(event);
  80. if (el.parentNode.tagName.toUpperCase() === "SVG") {
  81. el = el.parentNode;
  82. }
  83. if (el.classList.contains(this.constructor.handleClass)) {
  84. $.stopEvent(event);
  85. this.toggle(el.parentNode);
  86. } else if (el.classList.contains(this.constructor.targetClass)) {
  87. if (el.hasAttribute("href")) {
  88. if (el.classList.contains(this.constructor.activeClass)) {
  89. if (el.classList.contains(app.views.ListSelect.activeClass)) {
  90. this.close(el);
  91. }
  92. } else {
  93. this.open(el);
  94. }
  95. } else {
  96. this.toggle(el);
  97. }
  98. }
  99. }
  100. };
  101. app.views.ListFold.initClass();