Skip to content

Commit 33c2ee9

Browse files
edi-odooAntoineVDV
andcommitted
[IMP] odoo_theme: show the "On this page" section in mobile
When the page gets too small for the "On this page" section (local tree of content) to fit on the page, the section is now moved above the menu (global tree of content) rather than being hidden. task-2800970 closes #2200 X-original-commit: c0040fa Signed-off-by: Antoine Vandevenne (anv) <[email protected]> Co-authored-by: Antoine Vandevenne (anv) <[email protected]>
1 parent 1d01977 commit 33c2ee9

File tree

4 files changed

+82
-53
lines changed

4 files changed

+82
-53
lines changed

extensions/odoo_theme/layout.html

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,20 @@
6262
</nav>
6363
</noscript>
6464
{# Shown when the JS has properly set all the classes on the TOC elements #}
65-
<nav id="o_main_toctree" class="o_side_nav border-end" hidden>
66-
{%- include "layout_templates/menu.html" %}
65+
<nav id="o_menu" class="o_side_nav border-end">
66+
{%- if 'hide-page-toc' not in meta %}
67+
{# Shown when the JS has properly set all the classes on the TOC elements #}
68+
<aside id="o_page_toc_in_nav" class="o_page_toc o_in_nav_toc border-bottom pb-3 mb-4" hidden>
69+
{%- include "layout_templates/page_toc.html" %}
70+
</aside>
71+
{%- endif %}
72+
<div id="o_main_toctree" class="o_main_toc" hidden>
73+
{%- include "layout_templates/menu.html" %}
74+
</div>
6775
</nav>
6876
<header class="o_main_header border-bottom navbar navbar-light navbar-expand-lg">
6977
{%- include "layout_templates/header.html" %}
70-
<button class="navbar-toggler p-0 border-0" type="button" data-bs-toggle="collapse" data-bs-target="#o_main_toctree" aria-label="Toggle navigation">
78+
<button class="navbar-toggler p-0 border-0" type="button" data-bs-toggle="collapse" data-bs-target="#o_menu" aria-label="Toggle navigation">
7179
<span class="icon-bar"></span>
7280
<span class="icon-bar"></span>
7381
<span class="icon-bar"></span>

extensions/odoo_theme/static/js/menu.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
(function ($) {
33

44
document.addEventListener('DOMContentLoaded', () => {
5-
this.navigationMenu = document.getElementById('o_main_toctree');
5+
const navigationMenu = document.getElementById('o_main_toctree');
66

77
// Allow to automatically collapse and expand TOC entries
8-
_prepareAccordion(this.navigationMenu);
8+
_prepareAccordion(navigationMenu);
99

1010
// Allow to respectively highlight and expand the TOC entries and their related TOC
1111
// entry list whose page is displayed.
12-
_flagActiveTocEntriesAndLists();
12+
_flagActiveTocEntriesAndLists(navigationMenu);
1313

1414
// Show hidden menu when the css classes have been properly specified
15-
this.navigationMenu.removeAttribute('hidden');
15+
navigationMenu.removeAttribute('hidden');
1616
});
1717

1818
/**
@@ -23,13 +23,15 @@
2323
* the `show` (Bootstrap) class.
2424
* Also, the deepest TOC entries of their respective branch receive the
2525
* `o_deepest_active_toc_entry` class, and their child TOC entry lists receive the `show` class.
26+
*
27+
* @param {HTMLElement} navigationMenu - The navigation menu.
2628
*/
27-
const _flagActiveTocEntriesAndLists = () => {
29+
const _flagActiveTocEntriesAndLists = navigationMenu => {
2830
const regexLayer = /\btoctree-l(?<layer>\d+)\b/;
2931
let lastLayer = undefined;
3032
let lastTocEntry = undefined;
3133
const deepestTocEntries = [];
32-
this.navigationMenu.querySelectorAll('.current').forEach(element => {
34+
navigationMenu.querySelectorAll('.current').forEach(element => {
3335
if (element.tagName === 'UL') {
3436
element.classList.add('show'); // Expand all related <ul>
3537
} else if (element.tagName === 'LI') {

extensions/odoo_theme/static/js/page_toc.js

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,48 @@
33

44
// Customize the page TOC
55
document.addEventListener('DOMContentLoaded', () => {
6-
this.pageToc = document.getElementById('o_page_toc'); // The tree of content of the page
7-
if (this.pageToc) { // The local toctree is not included for toctree pages (see layout.html)
8-
this.headingRefs = this.pageToc.querySelectorAll('a'); // The references to all headings
6+
// Loop on all tree of content of the page. There may be from 0 to 2 depending on the page.
7+
document.querySelectorAll('.o_page_toc').forEach(pageToc => {
8+
const headingRefs = pageToc.querySelectorAll('a'); // The references to all headings.
99

10-
// If the page TOC has less than 2 headings, in addition to the title, hide it entirely
11-
if (this.headingRefs.length <= 2) {
12-
_hidePageToc();
10+
// If the page TOC has less than 2 headings, in addition to the title, hide it entirely.
11+
if (headingRefs.length <= 2) {
12+
_hidePageToc(pageToc);
1313
return;
1414
}
1515

1616
// Allow to automatically collapse and expand TOC entries
17-
_prepareAccordion(this.pageToc);
17+
_prepareAccordion(pageToc);
1818

1919
// Allow to respectively highlight and expand the TOC entries and their related TOC
2020
// entry list whose section is focused.
21-
_flagActiveTocEntriesAndLists();
21+
_flagActiveTocEntriesAndLists(pageToc, headingRefs);
2222

2323
// Allow to hide the TOC entry referring the title (<h1> heading)
24-
_flagFirstHeadingRef();
24+
_flagFirstHeadingRef(headingRefs);
2525

2626
// Show hidden menu when the css classes have been properly specified
27-
this.pageToc.removeAttribute('hidden');
28-
}
27+
pageToc.removeAttribute('hidden');
28+
});
2929
});
3030

3131
/**
3232
* Entirely hide the local tree of contents.
33+
*
34+
* @param {HTMLElement} pageToc - The tree of content of the page.
3335
*/
34-
const _hidePageToc = () => this.pageToc.style.display = 'none';
36+
const _hidePageToc = pageToc => pageToc.style.display = 'none';
3537

3638
/**
3739
* Add the relevant classes on the TOC entries (and lists) whose section is focused.
3840
*
3941
* TOC entries whose section is focused (<li> elements) receive the `o_active_toc_entry` class
4042
* and their related TOC entry list (<ul> elements) receive the `show` (Bootstrap) class.
43+
*
44+
* @param {HTMLElement} pageToc - The tree of content of the page.
45+
* @param {NodeList} headingRefs - The references to all headings.
4146
*/
42-
const _flagActiveTocEntriesAndLists = () => {
47+
const _flagActiveTocEntriesAndLists = (pageToc, headingRefs) => {
4348

4449
const _updateFlags = () => {
4550
const activeHeadingRef = clickedHeadingRef || _findActiveHeadingRef();
@@ -56,8 +61,8 @@
5661
};
5762

5863
const _findActiveHeadingRef = () => {
59-
let activeHeadingRef = this.headingRefs[0];
60-
this.headingRefs.forEach(headingRef => {
64+
let activeHeadingRef = headingRefs[0];
65+
headingRefs.forEach(headingRef => {
6166
const href = headingRef.getAttribute('href');
6267
if (href !== '#') {
6368
const sectionId = href.replace('#', '');
@@ -78,17 +83,17 @@
7883
};
7984

8085
const _unflagAll = () => {
81-
this.pageToc.querySelectorAll('li,ul').forEach(element => {
86+
pageToc.querySelectorAll('li,ul').forEach(element => {
8287
element.classList.remove('o_active_toc_entry', 'show');
8388
});
84-
this.pageToc.querySelectorAll('i').forEach(element => {
89+
pageToc.querySelectorAll('i').forEach(element => {
8590
element.setAttribute('aria-expanded', false);
8691
});
8792
};
8893

8994
const _flagActiveHierarchy = (headingRef) => {
9095
let tocEntry = headingRef.parentElement;
91-
while (tocEntry !== this.pageToc) {
96+
while (tocEntry !== pageToc) {
9297
if (tocEntry.tagName === 'LI') {
9398
// Highlight all <li> in the active hierarchy
9499
tocEntry.classList.add('o_active_toc_entry');
@@ -104,7 +109,7 @@
104109
};
105110

106111
let clickedHeadingRef = undefined;
107-
this.pageToc.addEventListener('click', ev => {
112+
pageToc.addEventListener('click', ev => {
108113
clickedHeadingRef = ev.target.closest('a[href^="#"]'); // Highlight the clicked ref
109114
});
110115
let timeoutId = undefined;
@@ -122,9 +127,11 @@
122127

123128
/**
124129
* Add the class `o_page_toc_title` on the first heading reference.
130+
*
131+
* @param {NodeList} headingRefs - The references to all headings.
125132
*/
126-
const _flagFirstHeadingRef = () => {
127-
this.headingRefs[0].parentNode.classList.add('o_page_toc_title');
133+
const _flagFirstHeadingRef = headingRefs => {
134+
headingRefs[0].parentNode.classList.add('o_page_toc_title');
128135
}
129136

130137
})();

extensions/odoo_theme/static/style.scss

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,23 @@ header.o_main_header {
227227
font-weight: 600;
228228
}
229229
}
230+
.o_main_toc {
231+
> ul {
232+
li {
233+
&.o_active_toc_entry {
234+
&:not(.toctree-l1) > .o_toc_entry_wrapper i[class^="i-"]:not(.collapsed), > a , > .o_toc_entry_wrapper a, > .o_toc_entry_wrapper i {
235+
color: $o-violet-dark;
236+
}
237+
}
238+
239+
a {
240+
&.current {
241+
color: $o-violet-dark;
242+
}
243+
}
244+
}
245+
}
246+
}
230247

231248
ul {
232249
.o_deepest_active_toc_entry {
@@ -235,20 +252,6 @@ header.o_main_header {
235252
margin-left: -3px;
236253
}
237254

238-
li {
239-
&.o_active_toc_entry {
240-
&:not(.toctree-l1) > .o_toc_entry_wrapper i[class^="i-"]:not(.collapsed), > a , > .o_toc_entry_wrapper a, > .o_toc_entry_wrapper i {
241-
color: $o-violet-dark;
242-
}
243-
}
244-
245-
a {
246-
&.current {
247-
color: $o-violet-dark;
248-
}
249-
}
250-
}
251-
252255
> .toctree-l1 {
253256
&[class*="o_menu_"] > .o_toc_entry_wrapper > i:before {
254257
@include o-inline-icon($i-doc-apps, 0 5px 0 0);
@@ -292,7 +295,7 @@ header.o_main_header {
292295
}
293296
}
294297

295-
.o_side_nav, .o_page_toc_nav {
298+
.o_main_toc, .o_page_toc_nav {
296299
ul { // all uls in toc
297300
list-style: none;
298301
padding-left: $padding-s;
@@ -350,19 +353,28 @@ header.o_main_header {
350353
//------------------------------------------------------------------------------
351354

352355
aside.o_page_toc {
353-
display: none;
356+
color: $body-color;
354357
@include font-size($font-size-secondary);
355-
@include media-breakpoint-up(xl) {
356-
display: block;
358+
&:not(.o_in_nav_toc) {
359+
display: none;
360+
@include media-breakpoint-up(xl) {
361+
display: block;
362+
top: $o-header-height;
363+
right: 0;
364+
padding: $padding-l $padding-s $padding-l 0;
365+
overflow-y: auto;
366+
}
357367
position: -webkit-sticky;
358368
position: sticky;
359-
top: $o-header-height;
360-
right: 0;
361369
width: $o-on-page-width;
362370
height: 100%;
363371
max-height: calc(100vh - #{$o-header-height});
364-
padding: $padding-l $padding-s $padding-l 0;
365-
overflow-y: auto;
372+
}
373+
&.o_in_nav_toc {
374+
display: block;
375+
@include media-breakpoint-up(xl) {
376+
display: none;
377+
}
366378
}
367379

368380
h3 {
@@ -1071,7 +1083,7 @@ header.o_main_header {
10711083
> div[class^="highlight-"]{
10721084
border: 0 !important;
10731085
}
1074-
1086+
10751087
> *:last-child {
10761088
margin-bottom: 0 !important;
10771089
}

0 commit comments

Comments
 (0)