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_list.js

250 lines
5.8 KiB

app.views.DocList = class DocList extends app.View {
static className = "_list";
static attributes = { role: "navigation" };
static events = {
open: "onOpen",
close: "onClose",
click: "onClick",
};
static routes = { after: "afterRoute" };
static elements = {
disabledTitle: "._list-title",
disabledList: "._disabled-list",
};
init() {
this.lists = {};
1 year ago
this.addSubview((this.listFocus = new app.views.ListFocus(this.el)));
this.addSubview((this.listFold = new app.views.ListFold(this.el)));
this.addSubview((this.listSelect = new app.views.ListSelect(this.el)));
app.on("ready", () => this.render());
}
activate() {
if (super.activate(...arguments)) {
1 year ago
for (var slug in this.lists) {
var list = this.lists[slug];
list.activate();
}
this.listSelect.selectCurrent();
}
}
deactivate() {
if (super.deactivate(...arguments)) {
1 year ago
for (var slug in this.lists) {
var list = this.lists[slug];
list.deactivate();
}
}
}
render() {
1 year ago
let html = "";
for (var doc of app.docs.all()) {
1 year ago
html += this.tmpl("sidebarDoc", doc, {
fullName: app.docs.countAllBy("name", doc.name) > 1,
});
}
this.html(html);
1 year ago
if (!app.isSingleDoc() && app.disabledDocs.size() !== 0) {
this.renderDisabled();
}
}
renderDisabled() {
1 year ago
this.append(
this.tmpl("sidebarDisabled", { count: app.disabledDocs.size() }),
);
this.refreshElements();
this.renderDisabledList();
}
renderDisabledList() {
1 year ago
if (app.settings.get("hideDisabled")) {
this.removeDisabledList();
} else {
this.appendDisabledList();
}
}
appendDisabledList() {
let doc;
1 year ago
let html = "";
const docs = [].concat(...(app.disabledDocs.all() || []));
while ((doc = docs.shift())) {
if (doc.version != null) {
1 year ago
var versions = "";
while (true) {
1 year ago
versions += this.tmpl("sidebarDoc", doc, { disabled: true });
if (docs[0]?.name !== doc.name) {
1 year ago
break;
}
doc = docs.shift();
}
1 year ago
html += this.tmpl("sidebarDisabledVersionedDoc", doc, versions);
} else {
1 year ago
html += this.tmpl("sidebarDoc", doc, { disabled: true });
}
}
1 year ago
this.append(this.tmpl("sidebarDisabledList", html));
this.disabledTitle.classList.add("open-title");
this.refreshElements();
}
removeDisabledList() {
1 year ago
if (this.disabledList) {
$.remove(this.disabledList);
}
this.disabledTitle.classList.remove("open-title");
this.refreshElements();
}
reset(options) {
1 year ago
if (options == null) {
options = {};
}
this.listSelect.deselect();
if (this.listFocus != null) {
this.listFocus.blur();
}
this.listFold.reset();
1 year ago
if (options.revealCurrent || app.isSingleDoc()) {
this.revealCurrent();
}
}
onOpen(event) {
$.stopEvent(event);
1 year ago
const doc = app.docs.findBy("slug", event.target.getAttribute("data-slug"));
if (doc && !this.lists[doc.slug]) {
1 year ago
this.lists[doc.slug] = doc.types.isEmpty()
? new app.views.EntryList(doc.entries.all())
: new app.views.TypeList(doc);
$.after(event.target, this.lists[doc.slug].el);
}
}
onClose(event) {
$.stopEvent(event);
1 year ago
const doc = app.docs.findBy("slug", event.target.getAttribute("data-slug"));
if (doc && this.lists[doc.slug]) {
this.lists[doc.slug].detach();
delete this.lists[doc.slug];
}
}
select(model) {
this.listSelect.selectByHref(model?.fullPath());
}
reveal(model) {
this.openDoc(model.doc);
1 year ago
if (model.type) {
this.openType(model.getType());
}
this.focus(model);
this.paginateTo(model);
this.scrollTo(model);
}
focus(model) {
if (this.listFocus != null) {
this.listFocus.focus(this.find(`a[href='${model.fullPath()}']`));
}
}
revealCurrent() {
let model;
1 year ago
if ((model = app.router.context.type || app.router.context.entry)) {
this.reveal(model);
this.select(model);
}
}
openDoc(doc) {
1 year ago
if (app.disabledDocs.contains(doc) && doc.version) {
this.listFold.open(
this.find(`[data-slug='${doc.slug_without_version}']`),
);
}
this.listFold.open(this.find(`[data-slug='${doc.slug}']`));
}
closeDoc(doc) {
this.listFold.close(this.find(`[data-slug='${doc.slug}']`));
}
openType(type) {
1 year ago
this.listFold.open(
this.lists[type.doc.slug].find(`[data-slug='${type.slug}']`),
);
}
paginateTo(model) {
if (this.lists[model.doc.slug] != null) {
this.lists[model.doc.slug].paginateTo(model);
}
}
scrollTo(model) {
1 year ago
$.scrollTo(this.find(`a[href='${model.fullPath()}']`), null, "top", {
margin: app.isMobile() ? 48 : 0,
});
}
toggleDisabled() {
1 year ago
if (this.disabledTitle.classList.contains("open-title")) {
this.removeDisabledList();
1 year ago
app.settings.set("hideDisabled", true);
} else {
this.appendDisabledList();
1 year ago
app.settings.set("hideDisabled", false);
}
}
onClick(event) {
let slug;
const target = $.eventTarget(event);
1 year ago
if (
this.disabledTitle &&
$.hasChild(this.disabledTitle, target) &&
target.tagName !== "A"
) {
$.stopEvent(event);
this.toggleDisabled();
1 year ago
} else if ((slug = target.getAttribute("data-enable"))) {
$.stopEvent(event);
1 year ago
const doc = app.disabledDocs.findBy("slug", slug);
if (doc) {
this.onEnabled = this.onEnabled.bind(this);
1 year ago
app.enableDoc(doc, this.onEnabled, this.onEnabled);
}
}
}
onEnabled() {
this.reset();
this.render();
}
afterRoute(route, context) {
if (context.init) {
1 year ago
if (this.activated) {
this.reset({ revealCurrent: true });
}
} else {
this.select(context.type || context.entry);
}
}
};