Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change sidebar outlet #95

Closed
wants to merge 61 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
e2f3867
feat: change sidebar outlet
jonayrodriguezmrf May 7, 2024
beeb212
fixes
jonayrodriguezmrf May 7, 2024
1f1a303
console logs
jonayrodriguezmrf May 7, 2024
a0c1229
fix
jonayrodriguezmrf May 7, 2024
57ac7f1
more console
jonayrodriguezmrf May 7, 2024
6958b5b
fixes
jonayrodriguezmrf May 7, 2024
dbf3eab
fix category
jonayrodriguezmrf May 7, 2024
ce6037b
parent category
jonayrodriguezmrf May 7, 2024
485293d
test sidebar
jonayrodriguezmrf May 7, 2024
73948b9
fix repeated fetch
jonayrodriguezmrf May 7, 2024
8d8e728
fix
jonayrodriguezmrf May 7, 2024
4a9da05
feat: active links
jonayrodriguezmrf May 7, 2024
c2d2e07
revert
jonayrodriguezmrf May 7, 2024
81b82f5
fix activeLink
jonayrodriguezmrf May 7, 2024
9c83451
fix active
jonayrodriguezmrf May 7, 2024
688e942
fix update
jonayrodriguezmrf May 7, 2024
d80ebfc
fix
jonayrodriguezmrf May 7, 2024
0c343d4
more console
jonayrodriguezmrf May 7, 2024
b388d7a
test
jonayrodriguezmrf May 7, 2024
6178625
fix
jonayrodriguezmrf May 7, 2024
9e29549
fix
jonayrodriguezmrf May 7, 2024
1e2f89f
console
jonayrodriguezmrf May 7, 2024
24e5cb4
fix
jonayrodriguezmrf May 7, 2024
7690058
details fix
jonayrodriguezmrf May 7, 2024
ba62394
wait loading
jonayrodriguezmrf May 7, 2024
939c865
fixes
jonayrodriguezmrf May 7, 2024
ad628e4
fix selector
jonayrodriguezmrf May 7, 2024
6f385c1
use observer
jonayrodriguezmrf May 7, 2024
a5334ff
fix update
jonayrodriguezmrf May 7, 2024
00246d8
fixes
jonayrodriguezmrf May 7, 2024
a85cfde
fix observe
jonayrodriguezmrf May 7, 2024
e289cba
console active
jonayrodriguezmrf May 7, 2024
9d7ef52
remove code
jonayrodriguezmrf May 7, 2024
3731374
fix active
jonayrodriguezmrf May 7, 2024
a9c1526
fixes
jonayrodriguezmrf May 7, 2024
0e8b0cb
fix id
jonayrodriguezmrf May 7, 2024
386dde5
use body tag
jonayrodriguezmrf May 7, 2024
5d726ce
remove old code
jonayrodriguezmrf May 7, 2024
08e96d5
remove console
jonayrodriguezmrf May 7, 2024
92d1262
use body
jonayrodriguezmrf May 7, 2024
f78e517
use body
jonayrodriguezmrf May 7, 2024
4bdf20a
fix
jonayrodriguezmrf May 7, 2024
e1d29ab
revert
jonayrodriguezmrf May 7, 2024
8660ad9
render timeline
jonayrodriguezmrf May 7, 2024
0244cbd
console
jonayrodriguezmrf May 7, 2024
fbc5b83
fix active
jonayrodriguezmrf May 7, 2024
e29fd9f
update fetch
jonayrodriguezmrf May 7, 2024
8d1eadf
retry body_tag
jonayrodriguezmrf May 7, 2024
ecb0939
improve
jonayrodriguezmrf May 7, 2024
7532b28
use body tag
jonayrodriguezmrf May 7, 2024
133a131
use didInsert
jonayrodriguezmrf May 7, 2024
7c8ca9b
check console
jonayrodriguezmrf May 7, 2024
609977c
clean observer
jonayrodriguezmrf May 7, 2024
9aaa48b
update link
jonayrodriguezmrf May 7, 2024
f40f345
improve detect url
jonayrodriguezmrf May 7, 2024
3cfe374
use element instead this
jonayrodriguezmrf May 7, 2024
5d3c0ab
fix selector
jonayrodriguezmrf May 7, 2024
3035b7b
fix undefined
jonayrodriguezmrf May 7, 2024
4d8e891
update sidebar
jonayrodriguezmrf May 7, 2024
f5b069e
fix const
jonayrodriguezmrf May 7, 2024
d1401d4
first render
jonayrodriguezmrf May 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions common/body_tag.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<script>
let prevURL = window.location.href;

const observer = new MutationObserver(() => {
if (location.href !== prevURL && /\/t\//.test(location.href)) {
console.log("URL changed");
prevURL = location.href;
const currentRT = window.location.pathname.split("/").pop();
const activeItem = document.querySelector(
"li a.active:not(.sidebar-section-link)"
);

if (activeItem) {
activeItem.classList.remove("active");
let parent = activeItem.closest("details");

while (parent) {
if (parent.tagName === "DETAILS") {
parent.open = false;
}
parent = parent.parentElement.closest("details");
}
}
const currentSidebarItem = document.querySelector(
`.sidebar-sections li > a[href*='${currentRT}']:not(.active):not(.sidebar-section-link)`
);

if (currentSidebarItem) {
currentSidebarItem.classList.add("active");
let element = currentSidebarItem.closest("details");

while (element) {
if (element.tagName === 'DETAILS') {
element.open = true;
}
element = element.parentElement.closest("details");
}
}
}
});

const topicBody = document.getElementById("main-outlet");
observer.observe(topicBody, { childList: true, subtree: true });
</script>
95 changes: 95 additions & 0 deletions javascripts/discourse/components/category-sidebar.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ export default class CategorySidebar extends Component {
@service site;
@tracked sidebarContent;
@tracked loading = true;
@tracked lastFetchedCategory = null;

observer = null;

constructor() {
super(...arguments);
this.router.on('routeDidChange', () => {
this.updateActiveLinks();
});
}

<template>
{{#if this.matchedSetting}}
Expand All @@ -31,6 +41,7 @@ export default class CategorySidebar extends Component {
<div
class="category-sidebar-contents"
data-category-sidebar={{this.category.slug}}
{{didInsert this.setupObserver}}
>
<div class="cooked">
{{#unless this.loading}}
Expand Down Expand Up @@ -69,6 +80,10 @@ export default class CategorySidebar extends Component {
return filteredTargets.includes(this.router.currentRouteName);
}

get categoryIdTopic() {
return this.router?.currentRoute?.parent?.attributes?.category_id;
}

get categorySlugPathWithID() {
return this.router?.currentRoute?.params?.category_slug_path_with_id;
}
Expand All @@ -79,6 +94,10 @@ export default class CategorySidebar extends Component {
: null;
}

get topicCategory() {
return this.categoryIdTopic ? Category.findById(this.categoryIdTopic) : null;
}

get matchedSetting() {
if (this.parsedSetting["all"] && this.isTopRoute) {
// if this is a top_menu route, use the "all" setting
Expand All @@ -101,12 +120,40 @@ export default class CategorySidebar extends Component {
) {
return this.parsedSetting[parentCategorySlug];
}
} else if (this.categoryIdTopic) {
const topicCategorySlug = this.topicCategory?.slug;
const parentTopicCategorySlug = Category.findById(
this.topicCategory?.parent_category_id
)?.slug;

if (topicCategorySlug && this.parsedSetting[topicCategorySlug]) {
return this.parsedSetting[topicCategorySlug];
}

if (
settings.inherit_parent_sidebar &&
parentTopicCategorySlug &&
this.parsedSetting[parentTopicCategorySlug]
) {
return this.parsedSetting[parentTopicCategorySlug];
}
}
}

@action
async fetchPostContent() {
const currentCategory = this.category || Category.findById(
this.topicCategory?.parent_category_id
);

// Check if the category has changed
if (this.lastFetchedCategory === currentCategory?.id) {
// If not, skip fetching
return;
}

this.loading = true;
this.lastFetchedCategory = currentCategory?.id;

try {
if (this.matchedSetting) {
Expand All @@ -122,4 +169,52 @@ export default class CategorySidebar extends Component {

return this.sidebarContent;
}

@action
setupObserver(element) {
if (this.observer) {
this.observer.disconnect();
}
this.observer = new MutationObserver(() => {
this.updateActiveLinks(element);
});

this.observer.observe(element, {
childList: true,
subtree: true
});
}

@action
updateActiveLinks(element) {
if (!element) {
return;
}
const currentPath = window.location.pathname;
const links = element?.querySelectorAll('li > a:not(.sidebar-section-link)');
links.forEach(link => {
const linkPath = new URL(link.href).pathname;
if (linkPath === currentPath) {
link.classList.add('active');
let detailsElement = link.closest('details');
while (detailsElement) {
if (detailsElement.tagName === 'DETAILS') {
detailsElement.setAttribute('open', '');
}

detailsElement = detailsElement.parentElement.closest('details');
}
} else {
link.classList.remove('active');
}
});
}

willDestroy() {
super.willDestroy();
if (this.observer) {
this.observer.disconnect();
}
this.router.off('routeDidChange', this.updateActiveLinks);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<CategorySidebar />

This file was deleted.

This file was deleted.

122 changes: 0 additions & 122 deletions javascripts/discourse/widgets/category-sidebar.js

This file was deleted.

Loading
Loading