doc_picker.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Sanity-check the conversion and remove this comment.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS101: Remove unnecessary use of Array.from
  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. * DS207: Consider shorter variations of null checks
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
  11. */
  12. app.views.DocPicker = class DocPicker extends app.View {
  13. static initClass() {
  14. this.className = "_list _list-picker";
  15. this.events = {
  16. mousedown: "onMouseDown",
  17. mouseup: "onMouseUp",
  18. };
  19. }
  20. init() {
  21. this.addSubview((this.listFold = new app.views.ListFold(this.el)));
  22. }
  23. activate() {
  24. if (super.activate(...arguments)) {
  25. this.render();
  26. this.onDOMFocus = this.onDOMFocus.bind(this);
  27. $.on(this.el, "focus", this.onDOMFocus, true);
  28. }
  29. }
  30. deactivate() {
  31. if (super.deactivate(...arguments)) {
  32. this.empty();
  33. $.off(this.el, "focus", this.onDOMFocus, true);
  34. this.focusEl = null;
  35. }
  36. }
  37. render() {
  38. let doc;
  39. let html = this.tmpl("docPickerHeader");
  40. let docs = app.docs
  41. .all()
  42. .concat(...Array.from(app.disabledDocs.all() || []));
  43. while ((doc = docs.shift())) {
  44. if (doc.version != null) {
  45. var versions;
  46. [docs, versions] = Array.from(this.extractVersions(docs, doc));
  47. html += this.tmpl(
  48. "sidebarVersionedDoc",
  49. doc,
  50. this.renderVersions(versions),
  51. { open: app.docs.contains(doc) },
  52. );
  53. } else {
  54. html += this.tmpl("sidebarLabel", doc, {
  55. checked: app.docs.contains(doc),
  56. });
  57. }
  58. }
  59. this.html(html + this.tmpl("docPickerNote"));
  60. $.requestAnimationFrame(() =>
  61. __guard__(this.findByTag("input"), (x) => x.focus()),
  62. );
  63. }
  64. renderVersions(docs) {
  65. let html = "";
  66. for (var doc of Array.from(docs)) {
  67. html += this.tmpl("sidebarLabel", doc, {
  68. checked: app.docs.contains(doc),
  69. });
  70. }
  71. return html;
  72. }
  73. extractVersions(originalDocs, version) {
  74. const docs = [];
  75. const versions = [version];
  76. for (var doc of Array.from(originalDocs)) {
  77. (doc.name === version.name ? versions : docs).push(doc);
  78. }
  79. return [docs, versions];
  80. }
  81. empty() {
  82. this.resetClass();
  83. super.empty(...arguments);
  84. }
  85. getSelectedDocs() {
  86. return Array.from(this.findAllByTag("input"))
  87. .filter((input) => (input != null ? input.checked : undefined))
  88. .map((input) => input.name);
  89. }
  90. onMouseDown() {
  91. this.mouseDown = Date.now();
  92. }
  93. onMouseUp() {
  94. this.mouseUp = Date.now();
  95. }
  96. onDOMFocus(event) {
  97. const { target } = event;
  98. if (target.tagName === "INPUT") {
  99. if (
  100. (!this.mouseDown || !(Date.now() < this.mouseDown + 100)) &&
  101. (!this.mouseUp || !(Date.now() < this.mouseUp + 100))
  102. ) {
  103. $.scrollTo(target.parentNode, null, "continuous");
  104. }
  105. } else if (target.classList.contains(app.views.ListFold.targetClass)) {
  106. target.blur();
  107. if (!this.mouseDown || !(Date.now() < this.mouseDown + 100)) {
  108. if (this.focusEl === $("input", target.nextElementSibling)) {
  109. if (target.classList.contains(app.views.ListFold.activeClass)) {
  110. this.listFold.close(target);
  111. }
  112. let prev = target.previousElementSibling;
  113. while (
  114. prev.tagName !== "LABEL" &&
  115. !prev.classList.contains(app.views.ListFold.targetClass)
  116. ) {
  117. prev = prev.previousElementSibling;
  118. }
  119. if (prev.classList.contains(app.views.ListFold.activeClass)) {
  120. prev = $.makeArray($$("input", prev.nextElementSibling)).pop();
  121. }
  122. this.delay(() => prev.focus());
  123. } else {
  124. if (!target.classList.contains(app.views.ListFold.activeClass)) {
  125. this.listFold.open(target);
  126. }
  127. this.delay(() => $("input", target.nextElementSibling).focus());
  128. }
  129. }
  130. }
  131. this.focusEl = target;
  132. }
  133. };
  134. app.views.DocPicker.initClass();
  135. function __guard__(value, transform) {
  136. return typeof value !== "undefined" && value !== null
  137. ? transform(value)
  138. : undefined;
  139. }