Skip to content

Commit 17153aa

Browse files
author
Christian Paier
committed
Show the parent folders in the breadcrumb menu when on a child entry.
Previously, clicking on an menu item in the breadcrumb menu removed the parent entries of the path, i.e.: Clicking on the "to" entry in "/path/to/some/folder" changed the breadcrumb menu to show only the "/path/to" entries. With this change the breadcrumb menu changes this behaviour as the full path is still visible (and usable) but with the "to" entry beeing highlighted. Signed-off-by: Christian Paier <hallo+git@cpaier.com>
1 parent 764e452 commit 17153aa

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

apps/files/js/breadcrumb.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@
5858
BreadCrumb.prototype = {
5959
$el: null,
6060
dir: null,
61+
maxDepthDir: null,
6162
dirInfo: null,
63+
activeItemIndex: 0,
6264

6365
/**
6466
* Total width of all breadcrumbs
@@ -81,11 +83,35 @@
8183
dir = dir.replace(/\\/g, '/');
8284
dir = dir || '/';
8385
if (dir !== this.dir) {
86+
this._getMaxDepthDir(dir, this.maxDepthDir || "");
8487
this.dir = dir;
8588
this.render();
8689
}
8790
},
8891

92+
_getMaxDepthDir: function(dir1, dir2) {
93+
var dir1array = dir1.split("/").filter(item => item.length > 0)
94+
var dir2array = dir2.split("/").filter(item => item.length > 0)
95+
96+
this.activeItemIndex = dir1array.length-1;
97+
98+
let isSubDirectory = true;
99+
for (let index=0; index<Math.min(dir1array.length, dir2array.length); index+=1) {
100+
if (dir1array[index] !== dir2array[index]) {
101+
isSubDirectory = false;
102+
break;
103+
}
104+
}
105+
106+
if (isSubDirectory && dir1array.length < dir2array.length) {
107+
this.maxDepthDir = dir2;
108+
return;
109+
}
110+
111+
this.maxDepthDir = dir1;
112+
},
113+
114+
89115
setDirectoryInfo: function(dirInfo) {
90116
if (dirInfo !== this.dirInfo) {
91117
this.dirInfo = dirInfo;
@@ -118,7 +144,7 @@
118144
// Menu is destroyed on every change, we need to init it
119145
OC.unregisterMenu($('.crumbmenu > .icon-more'), $('.crumbmenu > .popovermenu'));
120146

121-
var parts = this._makeCrumbs(this.dir || '/');
147+
var parts = this._makeCrumbs(this.maxDepthDir || '/');
122148
var $crumb;
123149
var $menuItem;
124150
this.$el.empty();
@@ -163,19 +189,20 @@
163189
if(menuPart.dir) {
164190
$menuItem = $('<li class="crumblist"><a><span class="icon-folder"></span><span></span></a></li>');
165191
$menuItem.data('dir', menuPart.dir);
166-
$menuItem.find('a').attr('href', this.getCrumbUrl(part, j));
192+
$menuItem.find('a').attr('href', this.getCrumbUrl(parts[Math.max(this.activeItemIndex, 0)], j));
167193
$menuItem.find('span:eq(1)').text(menuPart.name);
168194
this.$menu.children('ul').append($menuItem);
169195
if (this.onClick) {
170196
$menuItem.on('click', this.onClick);
171197
}
172198
}
173199
}
200+
174201
_.each(this._detailViews, function(view) {
175202
view.render({
176203
dirInfo: this.dirInfo
177204
});
178-
$crumb.append(view.$el);
205+
this.breadcrumbs[this.activeItemIndex + 2].append(view.$el);
179206
$menuItem.append(view.$el.clone(true));
180207
}, this);
181208

@@ -228,8 +255,14 @@
228255
for (var i = 0; i < parts.length; i++) {
229256
var part = parts[i];
230257
pathToHere = pathToHere + '/' + part;
258+
259+
let classes = "";
260+
if (i === this.activeItemIndex) {
261+
classes = "active";
262+
}
231263
crumbs.push({
232264
dir: pathToHere,
265+
class: classes,
233266
name: part
234267
});
235268
}

apps/files/tests/js/breadcrumbSpec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ describe('OCA.Files.BreadCrumb tests', function() {
5656
expect($crumbs.eq(1).find('a').hasClass('icon-home')).toEqual(true);
5757
expect($crumbs.eq(1).data('dir')).toEqual('/');
5858
});
59-
it('Renders root when switching to root', function() {
59+
it('Renders complete directory when switching to root', function() {
6060
var $crumbs;
6161
bc.setDirectory('/somedir');
6262
bc.setDirectory('/');
6363
$crumbs = bc.$el.find('.crumb');
64-
expect($crumbs.length).toEqual(2);
64+
expect($crumbs.length).toEqual(3);
6565
expect($crumbs.eq(1).data('dir')).toEqual('/');
66+
expect($crumbs.eq(2).data('dir')).toEqual('/somedir');
67+
expect($crumbs.eq(2).attr('class').includes("active")).toEqual(false);
6668
});
6769
it('Renders single path section', function() {
6870
var $crumbs;

core/css/styles.scss

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,14 @@ div.crumb {
12021202
order: 3;
12031203
}
12041204
}
1205+
&.active {
1206+
font-weight: bold;
1207+
margin-right: 10px;
1208+
// Allow multiple span next to the main 'a'
1209+
a ~ span {
1210+
padding-left: 0;
1211+
}
1212+
}
12051213
> a,
12061214
> span {
12071215
position: relative;
@@ -1226,14 +1234,6 @@ div.crumb {
12261234
}
12271235
&:not(:first-child) a {
12281236
}
1229-
&:last-child {
1230-
font-weight: bold;
1231-
margin-right: 10px;
1232-
// Allow multiple span next to the main 'a'
1233-
a ~ span {
1234-
padding-left: 0;
1235-
}
1236-
}
12371237
&:hover, &:focus, a:focus, &:active {
12381238
opacity: 1;
12391239

0 commit comments

Comments
 (0)