You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
devdocs/assets/javascripts/views/sidebar/doc_picker.js

132 lines
3.4 KiB

app.views.DocPicker = class DocPicker extends app.View {
static className = "_list _list-picker";
1 year ago
static events = {
mousedown: "onMouseDown",
mouseup: "onMouseUp",
};
init() {
1 year ago
this.addSubview((this.listFold = new app.views.ListFold(this.el)));
}
activate() {
if (super.activate(...arguments)) {
this.render();
this.onDOMFocus = this.onDOMFocus.bind(this);
1 year ago
$.on(this.el, "focus", this.onDOMFocus, true);
}
}
deactivate() {
if (super.deactivate(...arguments)) {
this.empty();
1 year ago
$.off(this.el, "focus", this.onDOMFocus, true);
this.focusEl = null;
}
}
render() {
let doc;
1 year ago
let html = this.tmpl("docPickerHeader");
let docs = app.docs.all().concat(...(app.disabledDocs.all() || []));
while ((doc = docs.shift())) {
if (doc.version != null) {
var versions;
[docs, versions] = this.extractVersions(docs, doc);
1 year ago
html += this.tmpl(
"sidebarVersionedDoc",
doc,
this.renderVersions(versions),
{ open: app.docs.contains(doc) },
);
} else {
1 year ago
html += this.tmpl("sidebarLabel", doc, {
checked: app.docs.contains(doc),
});
}
}
1 year ago
this.html(html + this.tmpl("docPickerNote"));
requestAnimationFrame(() => this.findByTag("input")?.focus());
}
renderVersions(docs) {
1 year ago
let html = "";
for (var doc of docs) {
1 year ago
html += this.tmpl("sidebarLabel", doc, {
checked: app.docs.contains(doc),
});
}
return html;
}
extractVersions(originalDocs, version) {
const docs = [];
const versions = [version];
for (var doc of originalDocs) {
(doc.name === version.name ? versions : docs).push(doc);
}
return [docs, versions];
}
empty() {
this.resetClass();
super.empty(...arguments);
}
getSelectedDocs() {
return [...this.findAllByTag("input")]
.filter((input) => input?.checked)
1 year ago
.map((input) => input.name);
}
onMouseDown() {
this.mouseDown = Date.now();
}
onMouseUp() {
this.mouseUp = Date.now();
}
onDOMFocus(event) {
1 year ago
const { target } = event;
if (target.tagName === "INPUT") {
if (
(!this.mouseDown || !(Date.now() < this.mouseDown + 100)) &&
(!this.mouseUp || !(Date.now() < this.mouseUp + 100))
) {
$.scrollTo(target.parentNode, null, "continuous");
}
} else if (target.classList.contains(app.views.ListFold.targetClass)) {
target.blur();
1 year ago
if (!this.mouseDown || !(Date.now() < this.mouseDown + 100)) {
if (this.focusEl === $("input", target.nextElementSibling)) {
if (target.classList.contains(app.views.ListFold.activeClass)) {
this.listFold.close(target);
}
let prev = target.previousElementSibling;
1 year ago
while (
prev.tagName !== "LABEL" &&
!prev.classList.contains(app.views.ListFold.targetClass)
) {
prev = prev.previousElementSibling;
}
if (prev.classList.contains(app.views.ListFold.activeClass)) {
prev = $.makeArray($$("input", prev.nextElementSibling)).pop();
}
this.delay(() => prev.focus());
} else {
1 year ago
if (!target.classList.contains(app.views.ListFold.activeClass)) {
this.listFold.open(target);
}
this.delay(() => $("input", target.nextElementSibling).focus());
}
}
}
this.focusEl = target;
}
};