list_focus.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. app.views.ListFocus = class ListFocus extends app.View {
  2. static activeClass = "focus";
  3. static events = { click: "onClick" };
  4. static shortcuts = {
  5. up: "onUp",
  6. down: "onDown",
  7. left: "onLeft",
  8. enter: "onEnter",
  9. superEnter: "onSuperEnter",
  10. escape: "blur",
  11. };
  12. constructor(el) {
  13. super(el);
  14. this.focusOnNextFrame = (el) => requestAnimationFrame(() => this.focus(el));
  15. }
  16. focus(el, options) {
  17. if (options == null) {
  18. options = {};
  19. }
  20. if (el && !el.classList.contains(this.constructor.activeClass)) {
  21. this.blur();
  22. el.classList.add(this.constructor.activeClass);
  23. if (options.silent !== true) {
  24. $.trigger(el, "focus");
  25. }
  26. }
  27. }
  28. blur() {
  29. let cursor;
  30. if ((cursor = this.getCursor())) {
  31. cursor.classList.remove(this.constructor.activeClass);
  32. $.trigger(cursor, "blur");
  33. }
  34. }
  35. getCursor() {
  36. return (
  37. this.findByClass(this.constructor.activeClass) ||
  38. this.findByClass(app.views.ListSelect.activeClass)
  39. );
  40. }
  41. findNext(cursor) {
  42. let next;
  43. if ((next = cursor.nextSibling)) {
  44. if (next.tagName === "A") {
  45. return next;
  46. } else if (next.tagName === "SPAN") {
  47. // pagination link
  48. $.click(next);
  49. return this.findNext(cursor);
  50. } else if (next.tagName === "DIV") {
  51. // sub-list
  52. if (cursor.className.includes(" open")) {
  53. return this.findFirst(next) || this.findNext(next);
  54. } else {
  55. return this.findNext(next);
  56. }
  57. } else if (next.tagName === "H6") {
  58. // title
  59. return this.findNext(next);
  60. }
  61. } else if (cursor.parentNode !== this.el) {
  62. return this.findNext(cursor.parentNode);
  63. }
  64. }
  65. findFirst(cursor) {
  66. let first;
  67. if (!(first = cursor.firstChild)) {
  68. return;
  69. }
  70. if (first.tagName === "A") {
  71. return first;
  72. } else if (first.tagName === "SPAN") {
  73. // pagination link
  74. $.click(first);
  75. return this.findFirst(cursor);
  76. }
  77. }
  78. findPrev(cursor) {
  79. let prev;
  80. if ((prev = cursor.previousSibling)) {
  81. if (prev.tagName === "A") {
  82. return prev;
  83. } else if (prev.tagName === "SPAN") {
  84. // pagination link
  85. $.click(prev);
  86. return this.findPrev(cursor);
  87. } else if (prev.tagName === "DIV") {
  88. // sub-list
  89. if (prev.previousSibling.className.includes("open")) {
  90. return this.findLast(prev) || this.findPrev(prev);
  91. } else {
  92. return this.findPrev(prev);
  93. }
  94. } else if (prev.tagName === "H6") {
  95. // title
  96. return this.findPrev(prev);
  97. }
  98. } else if (cursor.parentNode !== this.el) {
  99. return this.findPrev(cursor.parentNode);
  100. }
  101. }
  102. findLast(cursor) {
  103. let last;
  104. if (!(last = cursor.lastChild)) {
  105. return;
  106. }
  107. if (last.tagName === "A") {
  108. return last;
  109. } else if (last.tagName === "SPAN" || last.tagName === "H6") {
  110. // pagination link or title
  111. return this.findPrev(last);
  112. } else if (last.tagName === "DIV") {
  113. // sub-list
  114. return this.findLast(last);
  115. }
  116. }
  117. onDown() {
  118. let cursor;
  119. if ((cursor = this.getCursor())) {
  120. this.focusOnNextFrame(this.findNext(cursor));
  121. } else {
  122. this.focusOnNextFrame(this.findByTag("a"));
  123. }
  124. }
  125. onUp() {
  126. let cursor;
  127. if ((cursor = this.getCursor())) {
  128. this.focusOnNextFrame(this.findPrev(cursor));
  129. } else {
  130. this.focusOnNextFrame(this.findLastByTag("a"));
  131. }
  132. }
  133. onLeft() {
  134. const cursor = this.getCursor();
  135. if (
  136. cursor &&
  137. !cursor.classList.contains(app.views.ListFold.activeClass) &&
  138. cursor.parentNode !== this.el
  139. ) {
  140. const prev = cursor.parentNode.previousSibling;
  141. if (prev && prev.classList.contains(app.views.ListFold.targetClass)) {
  142. this.focusOnNextFrame(cursor.parentNode.previousSibling);
  143. }
  144. }
  145. }
  146. onEnter() {
  147. let cursor;
  148. if ((cursor = this.getCursor())) {
  149. $.click(cursor);
  150. }
  151. }
  152. onSuperEnter() {
  153. let cursor;
  154. if ((cursor = this.getCursor())) {
  155. $.popup(cursor);
  156. }
  157. }
  158. onClick(event) {
  159. if (event.which !== 1 || event.metaKey || event.ctrlKey) {
  160. return;
  161. }
  162. const target = $.eventTarget(event);
  163. if (target.tagName === "A") {
  164. this.focus(target, { silent: true });
  165. }
  166. }
  167. };