Skip to content

Commit 3e7b92a

Browse files
authored
Merge pull request #416 from canjs/sidebar-fix
Fix an issue with groups not being expanded when a page in a non-Core package is selected
2 parents 62b5233 + c7c2e6d commit 3e7b92a

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

sidebar/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Sidebar Docs
2+
3+
## Architecture
4+
5+
Most of the logic for the sidebar is broken up into three files:
6+
7+
- [page-model.js](page-model.js) represents any documented module/page/etc.
8+
- [sidebar.viewmodel.js](sidebar.viewmodel.js) has everything else that’s needed for the template
9+
- [sidebar.events.js](sidebar.events.js) handles button & links clicks and the animations
10+
11+
### Page model
12+
13+
In addition to storing the data about each module/page/etc., the page model
14+
is responsible for some stateful information about how the page is being
15+
displayed in the template:
16+
17+
- `parentPage` is set by the view-model when the page is initialized
18+
- `unsortedChildren` contains _all_ the children before they’re sorted
19+
- `sortedChildren` contains _all_ the children after sorting
20+
- `childrenInCoreCollection` contains only the children that are part of `can-core`
21+
- `isCollapsible` is only true for pages like Observables, Views, etc.
22+
- `isCollapsed` uses local storage to track whether the user has collapsed a section
23+
- `visibleChildren` returns either `childrenInCoreCollection` or `sortedChildren`, depending on `isCollapsed`
24+
25+
Additionally, the `collapse` method is used to update local storage when
26+
`isCollapsed` is changed by the user. It should only be called when
27+
initiated by the user; any other (programatic) changes to `isCollapsed`
28+
should not be persisted to local storage.
29+
30+
## Debugging
31+
32+
The demo files make it easier to debug issues without having to rebuild the
33+
entire site. For example, you can test how the sidebar starts with an
34+
initial page by changing `selectedPageName: 'canjs'` in [demo.js](demo.js).

sidebar/sidebar.viewmodel.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,16 @@ module.exports = DefineMap.extend({
9898
if (selectedPage) {
9999
if (selectedPage.isCollapsed && selectedPage.visibleChildren.length === 0) {
100100
selectedPage.isCollapsed = false;
101-
} else {
102-
var selectedParent = selectedPage.parentPage;
103-
if (selectedParent && selectedParent.isCollapsible && selectedPage.collection !== 'can-core') {
104-
selectedParent.isCollapsed = false;
101+
102+
} else if (selectedPage.collection !== 'can-core') {
103+
// If the page is not in the core collection, walk up the parents to
104+
// make sure they are not collapsed.
105+
var parent = (selectedPage) ? selectedPage.parentPage : null;
106+
while (parent) {
107+
if (parent.isCollapsible) {
108+
parent.isCollapsed = false;
109+
}
110+
parent = parent.parentPage;
105111
}
106112
}
107113
}

sidebar/test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,17 @@ QUnit.test('When a child item is selected, its parent should not be collapsed',
157157
assert.notOk(vm.shouldShowExpandCollapseButton(canAjaxParent), 'parent does not show expand/collapse button');
158158
});
159159

160+
QUnit.test('When a grandchild item is selected, its grandparent should not be collapsed', function(assert) {
161+
var vm = new ViewModel({searchMap: searchMap});
162+
var pageMap = vm.pageMap;
163+
var grandchildPage = pageMap['can-view-live.attr'];
164+
var grandparentPage = grandchildPage.parentPage.parentPage;
165+
assert.ok(grandparentPage.isCollapsed, 'grandparent is collapsed beforehand');
166+
vm.selectedPage = grandchildPage;
167+
assert.notOk(grandparentPage.isCollapsed, 'grandparent is not collapsed afterwards');
168+
assert.notOk(vm.shouldShowExpandCollapseButton(grandparentPage), 'grandparent does not show expand/collapse button');
169+
});
170+
160171
QUnit.test('When a purpose group page is selected, its expand/collapse button should be shown', function(assert) {
161172
var vm = new ViewModel({searchMap: searchMap});
162173
var canObservablesPage = vm.pageMap['can-observables'];

0 commit comments

Comments
 (0)