2013-10-28 19:22:06 +00:00
|
|
|
/**
|
|
|
|
* ownCloud
|
|
|
|
*
|
|
|
|
* @author Vincent Petry
|
|
|
|
* @copyright 2014 Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
/**
|
2014-06-23 21:56:10 +00:00
|
|
|
* @class BreadCrumb
|
|
|
|
* @memberof OCA.Files
|
|
|
|
* @classdesc Breadcrumbs that represent the current path.
|
|
|
|
*
|
|
|
|
* @param {Object} [options] options
|
|
|
|
* @param {Function} [options.onClick] click event handler
|
|
|
|
* @param {Function} [options.onDrop] drop event handler
|
|
|
|
* @param {Function} [options.getCrumbUrl] callback that returns
|
|
|
|
* the URL of a given breadcrumb
|
2013-10-28 19:22:06 +00:00
|
|
|
*/
|
|
|
|
var BreadCrumb = function(options){
|
|
|
|
this.$el = $('<div class="breadcrumb"></div>');
|
2017-11-07 04:40:26 +00:00
|
|
|
this.$menu = $('<div class="popovermenu menu-center"><ul></ul></div>');
|
2017-11-08 16:54:38 +00:00
|
|
|
|
|
|
|
this.crumbSelector = '.crumb:not(.hidden):not(.crumbhome):not(.crumbmenu)';
|
2018-02-27 16:14:34 +00:00
|
|
|
this.hiddenCrumbSelector = '.crumb.hidden:not(.crumbhome):not(.crumbmenu)';
|
2013-10-28 19:22:06 +00:00
|
|
|
options = options || {};
|
|
|
|
if (options.onClick) {
|
|
|
|
this.onClick = options.onClick;
|
|
|
|
}
|
|
|
|
if (options.onDrop) {
|
|
|
|
this.onDrop = options.onDrop;
|
2015-11-04 10:44:47 +00:00
|
|
|
this.onOver = options.onOver;
|
|
|
|
this.onOut = options.onOut;
|
2013-10-28 19:22:06 +00:00
|
|
|
}
|
|
|
|
if (options.getCrumbUrl) {
|
|
|
|
this.getCrumbUrl = options.getCrumbUrl;
|
|
|
|
}
|
2016-10-04 10:56:04 +00:00
|
|
|
this._detailViews = [];
|
2013-10-28 19:22:06 +00:00
|
|
|
};
|
2017-11-04 16:47:38 +00:00
|
|
|
|
2014-06-23 21:56:10 +00:00
|
|
|
/**
|
|
|
|
* @memberof OCA.Files
|
|
|
|
*/
|
2013-10-28 19:22:06 +00:00
|
|
|
BreadCrumb.prototype = {
|
|
|
|
$el: null,
|
|
|
|
dir: null,
|
2016-10-04 10:56:04 +00:00
|
|
|
dirInfo: null,
|
2013-10-28 19:22:06 +00:00
|
|
|
|
2014-05-23 17:02:50 +00:00
|
|
|
/**
|
|
|
|
* Total width of all breadcrumbs
|
2014-06-23 21:56:10 +00:00
|
|
|
* @type int
|
|
|
|
* @private
|
2014-05-23 17:02:50 +00:00
|
|
|
*/
|
2013-10-28 19:22:06 +00:00
|
|
|
totalWidth: 0,
|
|
|
|
breadcrumbs: [],
|
|
|
|
onClick: null,
|
|
|
|
onDrop: null,
|
2015-11-04 10:44:47 +00:00
|
|
|
onOver: null,
|
|
|
|
onOut: null,
|
2013-10-28 19:22:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the directory to be displayed as breadcrumb.
|
|
|
|
* This will re-render the breadcrumb.
|
|
|
|
* @param dir path to be displayed as breadcrumb
|
|
|
|
*/
|
|
|
|
setDirectory: function(dir) {
|
2015-01-24 08:56:00 +00:00
|
|
|
dir = dir.replace(/\\/g, '/');
|
2013-10-28 19:22:06 +00:00
|
|
|
dir = dir || '/';
|
|
|
|
if (dir !== this.dir) {
|
|
|
|
this.dir = dir;
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-10-04 10:56:04 +00:00
|
|
|
setDirectoryInfo: function(dirInfo) {
|
|
|
|
if (dirInfo !== this.dirInfo) {
|
|
|
|
this.dirInfo = dirInfo;
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Backbone.View} detailView
|
|
|
|
*/
|
|
|
|
addDetailView: function(detailView) {
|
|
|
|
this._detailViews.push(detailView);
|
|
|
|
},
|
|
|
|
|
2013-10-28 19:22:06 +00:00
|
|
|
/**
|
|
|
|
* Returns the full URL to the given directory
|
2014-06-23 21:56:10 +00:00
|
|
|
*
|
|
|
|
* @param {Object.<String, String>} part crumb data as map
|
|
|
|
* @param {int} index crumb index
|
2013-10-28 19:22:06 +00:00
|
|
|
* @return full URL
|
|
|
|
*/
|
|
|
|
getCrumbUrl: function(part, index) {
|
|
|
|
return '#';
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the breadcrumb elements
|
|
|
|
*/
|
|
|
|
render: function() {
|
2017-11-10 07:23:24 +00:00
|
|
|
// Menu is destroyed on every change, we need to init it
|
2018-02-28 10:04:27 +00:00
|
|
|
OC.unregisterMenu($('.crumbmenu > .icon-more'), $('.crumbmenu > .popovermenu'));
|
2017-11-07 04:53:17 +00:00
|
|
|
|
2013-10-28 19:22:06 +00:00
|
|
|
var parts = this._makeCrumbs(this.dir || '/');
|
|
|
|
var $crumb;
|
2017-11-04 16:47:38 +00:00
|
|
|
var $menuItem;
|
2013-10-28 19:22:06 +00:00
|
|
|
this.$el.empty();
|
|
|
|
this.breadcrumbs = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < parts.length; i++) {
|
|
|
|
var part = parts[i];
|
|
|
|
var $image;
|
2017-11-04 12:02:15 +00:00
|
|
|
var $link = $('<a></a>');
|
2017-11-04 16:47:38 +00:00
|
|
|
$crumb = $('<div class="crumb svg"></div>');
|
2017-11-04 12:02:15 +00:00
|
|
|
if(part.dir) {
|
|
|
|
$link.attr('href', this.getCrumbUrl(part, i));
|
|
|
|
}
|
|
|
|
if(part.name) {
|
|
|
|
$link.text(part.name);
|
|
|
|
}
|
|
|
|
$link.addClass(part.linkclass);
|
2013-10-28 19:22:06 +00:00
|
|
|
$crumb.append($link);
|
2017-11-04 16:47:38 +00:00
|
|
|
$crumb.data('dir', part.dir);
|
|
|
|
// Ignore menu button
|
|
|
|
$crumb.data('crumb-id', i - 1);
|
2017-11-04 12:02:15 +00:00
|
|
|
$crumb.addClass(part.class);
|
2013-10-28 19:22:06 +00:00
|
|
|
|
|
|
|
if (part.img) {
|
|
|
|
$image = $('<img class="svg"></img>');
|
|
|
|
$image.attr('src', part.img);
|
2014-12-17 10:26:42 +00:00
|
|
|
$image.attr('alt', part.alt);
|
2013-10-28 19:22:06 +00:00
|
|
|
$link.append($image);
|
|
|
|
}
|
|
|
|
this.breadcrumbs.push($crumb);
|
|
|
|
this.$el.append($crumb);
|
2017-11-07 04:40:26 +00:00
|
|
|
// Only add feedback if not menu
|
|
|
|
if (this.onClick && i !== 0) {
|
|
|
|
$link.on('click', this.onClick);
|
2013-10-28 19:22:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-04 16:47:38 +00:00
|
|
|
// Menu creation
|
|
|
|
this._createMenu();
|
2017-11-08 18:12:56 +00:00
|
|
|
for (var j = 0; j < parts.length; j++) {
|
|
|
|
var menuPart = parts[j];
|
|
|
|
if(menuPart.dir) {
|
2017-11-08 12:10:30 +00:00
|
|
|
$menuItem = $('<li class="crumblist"><a><span class="icon-folder"></span><span></span></a></li>');
|
2017-11-08 18:12:56 +00:00
|
|
|
$menuItem.data('dir', menuPart.dir);
|
|
|
|
$menuItem.find('a').attr('href', this.getCrumbUrl(part, j));
|
|
|
|
$menuItem.find('span:eq(1)').text(menuPart.name);
|
2017-11-04 16:47:38 +00:00
|
|
|
this.$menu.children('ul').append($menuItem);
|
|
|
|
if (this.onClick) {
|
|
|
|
$menuItem.on('click', this.onClick);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-04 10:56:04 +00:00
|
|
|
_.each(this._detailViews, function(view) {
|
|
|
|
view.render({
|
|
|
|
dirInfo: this.dirInfo
|
|
|
|
});
|
|
|
|
$crumb.append(view.$el);
|
|
|
|
}, this);
|
|
|
|
|
2013-10-28 19:22:06 +00:00
|
|
|
// setup drag and drop
|
|
|
|
if (this.onDrop) {
|
2017-11-08 15:56:58 +00:00
|
|
|
this.$el.find('.crumb:not(:last-child):not(.crumbmenu), .crumblist:not(:last-child)').droppable({
|
2013-10-28 19:22:06 +00:00
|
|
|
drop: this.onDrop,
|
2015-11-04 10:44:47 +00:00
|
|
|
over: this.onOver,
|
|
|
|
out: this.onOut,
|
2015-10-29 13:41:37 +00:00
|
|
|
tolerance: 'pointer',
|
2017-11-08 18:03:32 +00:00
|
|
|
hoverClass: 'canDrop',
|
|
|
|
greedy: true
|
2017-11-08 15:56:58 +00:00
|
|
|
});
|
2013-10-28 19:22:06 +00:00
|
|
|
}
|
2017-11-10 07:23:24 +00:00
|
|
|
|
2017-11-07 04:40:26 +00:00
|
|
|
// Menu is destroyed on every change, we need to init it
|
2018-02-28 10:04:27 +00:00
|
|
|
OC.registerMenu($('.crumbmenu > .icon-more'), $('.crumbmenu > .popovermenu'));
|
2017-11-10 07:23:24 +00:00
|
|
|
|
2017-11-04 08:45:29 +00:00
|
|
|
this._resize();
|
2013-10-28 19:22:06 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a breadcrumb structure based on the given path
|
2014-06-23 21:56:10 +00:00
|
|
|
*
|
|
|
|
* @param {String} dir path to split into a breadcrumb structure
|
|
|
|
* @return {Object.<String, String>} map of {dir: path, name: displayName}
|
2013-10-28 19:22:06 +00:00
|
|
|
*/
|
|
|
|
_makeCrumbs: function(dir) {
|
|
|
|
var crumbs = [];
|
|
|
|
var pathToHere = '';
|
|
|
|
// trim leading and trailing slashes
|
|
|
|
dir = dir.replace(/^\/+|\/+$/g, '');
|
|
|
|
var parts = dir.split('/');
|
|
|
|
if (dir === '') {
|
|
|
|
parts = [];
|
|
|
|
}
|
2017-11-04 12:02:15 +00:00
|
|
|
// menu part
|
|
|
|
crumbs.push({
|
2017-11-08 17:05:01 +00:00
|
|
|
class: 'crumbmenu hidden',
|
2018-02-28 10:04:27 +00:00
|
|
|
linkclass: 'icon-more menutoggle'
|
2013-10-28 19:22:06 +00:00
|
|
|
});
|
2017-11-04 16:47:38 +00:00
|
|
|
// root part
|
|
|
|
crumbs.push({
|
|
|
|
name: t('core', 'Home'),
|
|
|
|
dir: '/',
|
2017-11-06 04:27:10 +00:00
|
|
|
class: 'crumbhome',
|
2017-11-04 16:47:38 +00:00
|
|
|
linkclass: 'icon-home'
|
|
|
|
});
|
2013-10-28 19:22:06 +00:00
|
|
|
for (var i = 0; i < parts.length; i++) {
|
|
|
|
var part = parts[i];
|
|
|
|
pathToHere = pathToHere + '/' + part;
|
|
|
|
crumbs.push({
|
|
|
|
dir: pathToHere,
|
|
|
|
name: part
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return crumbs;
|
|
|
|
},
|
|
|
|
|
2017-11-08 15:00:40 +00:00
|
|
|
/**
|
|
|
|
* Calculate real width based on individual crumbs
|
|
|
|
*
|
|
|
|
* @param {boolean} ignoreHidden ignore hidden crumbs
|
|
|
|
*/
|
|
|
|
getTotalWidth: function(ignoreHidden) {
|
2018-02-28 13:35:28 +00:00
|
|
|
// The width has to be calculated by adding up the width of all the
|
|
|
|
// crumbs; getting the width of the breadcrumb element is not a
|
|
|
|
// valid approach, as the returned value could be clamped to its
|
|
|
|
// parent width.
|
2017-11-08 15:00:40 +00:00
|
|
|
var totalWidth = 0;
|
|
|
|
for (var i = 0; i < this.breadcrumbs.length; i++ ) {
|
|
|
|
var $crumb = $(this.breadcrumbs[i]);
|
2017-11-08 16:54:38 +00:00
|
|
|
if(!$crumb.hasClass('hidden') || ignoreHidden === true) {
|
2018-02-27 12:59:09 +00:00
|
|
|
totalWidth += $crumb.outerWidth(true);
|
2017-11-08 15:00:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return totalWidth;
|
|
|
|
},
|
|
|
|
|
2017-11-04 08:45:29 +00:00
|
|
|
/**
|
|
|
|
* Hide the middle crumb
|
|
|
|
*/
|
2017-11-04 16:47:38 +00:00
|
|
|
_hideCrumb: function() {
|
2017-11-08 16:54:38 +00:00
|
|
|
var length = this.$el.find(this.crumbSelector).length;
|
2017-11-04 16:47:38 +00:00
|
|
|
// Get the middle one floored down
|
|
|
|
var elmt = Math.floor(length / 2 - 0.5);
|
2017-11-08 16:54:38 +00:00
|
|
|
this.$el.find(this.crumbSelector+':eq('+elmt+')').addClass('hidden');
|
2017-11-04 16:47:38 +00:00
|
|
|
},
|
2017-11-04 08:45:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the crumb to show
|
|
|
|
*/
|
2017-11-04 16:47:38 +00:00
|
|
|
_getCrumbElement: function() {
|
2018-02-27 16:14:34 +00:00
|
|
|
var hidden = this.$el.find(this.hiddenCrumbSelector).length;
|
2017-11-08 16:54:38 +00:00
|
|
|
var shown = this.$el.find(this.crumbSelector).length;
|
2017-11-04 16:47:38 +00:00
|
|
|
// Get the outer one with priority to the highest
|
2017-11-08 13:01:57 +00:00
|
|
|
var elmt = (1 - shown % 2) * (hidden - 1);
|
2018-02-27 16:14:34 +00:00
|
|
|
return this.$el.find(this.hiddenCrumbSelector + ':eq('+elmt+')');
|
2017-11-04 16:47:38 +00:00
|
|
|
},
|
2017-11-03 11:23:57 +00:00
|
|
|
|
2017-11-04 08:45:29 +00:00
|
|
|
/**
|
|
|
|
* Show the middle crumb
|
|
|
|
*/
|
2017-11-04 16:47:38 +00:00
|
|
|
_showCrumb: function() {
|
2018-02-27 16:14:34 +00:00
|
|
|
if(this.$el.find(this.hiddenCrumbSelector).length === 1) {
|
|
|
|
this.$el.find(this.hiddenCrumbSelector).removeClass('hidden');
|
2017-11-04 16:47:38 +00:00
|
|
|
}
|
|
|
|
this._getCrumbElement().removeClass('hidden');
|
|
|
|
},
|
2017-11-04 08:45:29 +00:00
|
|
|
|
2017-11-04 16:47:38 +00:00
|
|
|
/**
|
2017-11-04 12:02:15 +00:00
|
|
|
* Create and append the popovermenu
|
|
|
|
*/
|
2017-11-04 16:47:38 +00:00
|
|
|
_createMenu: function() {
|
|
|
|
this.$el.find('.crumbmenu').append(this.$menu);
|
|
|
|
this.$menu.children('ul').empty();
|
|
|
|
},
|
2017-11-04 12:02:15 +00:00
|
|
|
|
2017-11-04 16:47:38 +00:00
|
|
|
/**
|
2017-11-04 12:02:15 +00:00
|
|
|
* Update the popovermenu
|
|
|
|
*/
|
2017-11-04 16:47:38 +00:00
|
|
|
_updateMenu: function() {
|
2018-02-27 16:14:34 +00:00
|
|
|
var menuItems = this.$el.find(this.hiddenCrumbSelector);
|
2017-11-04 12:02:15 +00:00
|
|
|
|
2017-11-04 16:47:38 +00:00
|
|
|
this.$menu.find('li').addClass('in-breadcrumb');
|
|
|
|
for (var i = 0; i < menuItems.length; i++) {
|
|
|
|
var crumbId = $(menuItems[i]).data('crumb-id');
|
|
|
|
this.$menu.find('li:eq('+crumbId+')').removeClass('in-breadcrumb');
|
|
|
|
}
|
|
|
|
},
|
2017-11-04 12:02:15 +00:00
|
|
|
|
2014-05-23 17:02:50 +00:00
|
|
|
_resize: function() {
|
2017-11-08 16:54:38 +00:00
|
|
|
|
|
|
|
if (this.breadcrumbs.length <= 2) {
|
|
|
|
// home & menu
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-27 16:14:34 +00:00
|
|
|
// Always hide the menu to ensure that it does not interfere with
|
|
|
|
// the width calculations; otherwise, the result could be different
|
|
|
|
// depending on whether the menu was previously being shown or not.
|
|
|
|
this.$el.find('.crumbmenu').addClass('hidden');
|
|
|
|
|
2018-02-20 16:07:26 +00:00
|
|
|
// Show the crumbs to compress the siblings before hidding again the
|
|
|
|
// crumbs. This is needed when the siblings expand to fill all the
|
|
|
|
// available width, as in that case their old width would limit the
|
|
|
|
// available width for the crumbs.
|
2018-02-27 16:14:34 +00:00
|
|
|
// Note that the crumbs shown always overflow the parent width
|
|
|
|
// (except, of course, when they all fit in).
|
|
|
|
while (this.$el.find(this.hiddenCrumbSelector).length > 0
|
|
|
|
&& this.getTotalWidth() <= this.$el.parent().width()) {
|
2018-02-20 16:07:26 +00:00
|
|
|
this._showCrumb();
|
2013-10-28 19:22:06 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 23:11:40 +00:00
|
|
|
var siblingsWidth = 0;
|
|
|
|
this.$el.prevAll(':visible').each(function () {
|
|
|
|
siblingsWidth += $(this).outerWidth(true);
|
|
|
|
});
|
|
|
|
this.$el.nextAll(':visible').each(function () {
|
|
|
|
siblingsWidth += $(this).outerWidth(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
var availableWidth = this.$el.parent().width() - siblingsWidth;
|
2013-10-28 19:22:06 +00:00
|
|
|
|
2017-11-04 08:45:29 +00:00
|
|
|
// If container is smaller than content
|
2017-11-08 16:54:38 +00:00
|
|
|
// AND if there are crumbs left to hide
|
2018-02-19 19:46:49 +00:00
|
|
|
while (this.getTotalWidth() > availableWidth
|
2017-11-10 07:23:24 +00:00
|
|
|
&& this.$el.find(this.crumbSelector).length > 0) {
|
2018-02-27 16:14:34 +00:00
|
|
|
// As soon as one of the crumbs is hidden the menu will be
|
|
|
|
// shown. This is needed for proper results in further width
|
|
|
|
// checks.
|
|
|
|
// Note that the menu is not shown only when all the crumbs were
|
|
|
|
// being shown and they all fit the available space; if any of
|
|
|
|
// the crumbs was not being shown then those shown would
|
|
|
|
// overflow the available width, so at least one will be hidden
|
|
|
|
// and thus the menu will be shown.
|
|
|
|
this.$el.find('.crumbmenu').removeClass('hidden');
|
2017-11-04 08:45:29 +00:00
|
|
|
this._hideCrumb();
|
|
|
|
}
|
2014-05-23 17:02:50 +00:00
|
|
|
|
2017-11-04 12:02:15 +00:00
|
|
|
this._updateMenu();
|
2013-10-28 19:22:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-05-08 20:06:30 +00:00
|
|
|
OCA.Files.BreadCrumb = BreadCrumb;
|
2013-10-28 19:22:06 +00:00
|
|
|
})();
|