2013-10-28 23:22:06 +04: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-24 01:56:10 +04: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 23:22:06 +04:00
|
|
|
*/
|
|
|
|
var BreadCrumb = function(options){
|
|
|
|
this.$el = $('<div class="breadcrumb"></div>');
|
2017-11-07 07:40:26 +03:00
|
|
|
this.$menu = $('<div class="popovermenu menu-center"><ul></ul></div>');
|
2017-11-08 19:54:38 +03:00
|
|
|
|
|
|
|
this.crumbSelector = '.crumb:not(.hidden):not(.crumbhome):not(.crumbmenu)';
|
2018-02-27 19:14:34 +03:00
|
|
|
this.hiddenCrumbSelector = '.crumb.hidden:not(.crumbhome):not(.crumbmenu)';
|
2013-10-28 23:22:06 +04:00
|
|
|
options = options || {};
|
|
|
|
if (options.onClick) {
|
|
|
|
this.onClick = options.onClick;
|
|
|
|
}
|
|
|
|
if (options.onDrop) {
|
|
|
|
this.onDrop = options.onDrop;
|
2015-11-04 13:44:47 +03:00
|
|
|
this.onOver = options.onOver;
|
|
|
|
this.onOut = options.onOut;
|
2013-10-28 23:22:06 +04:00
|
|
|
}
|
|
|
|
if (options.getCrumbUrl) {
|
|
|
|
this.getCrumbUrl = options.getCrumbUrl;
|
|
|
|
}
|
2016-10-04 13:56:04 +03:00
|
|
|
this._detailViews = [];
|
2013-10-28 23:22:06 +04:00
|
|
|
};
|
2017-11-04 19:47:38 +03:00
|
|
|
|
2014-06-24 01:56:10 +04:00
|
|
|
/**
|
|
|
|
* @memberof OCA.Files
|
|
|
|
*/
|
2013-10-28 23:22:06 +04:00
|
|
|
BreadCrumb.prototype = {
|
|
|
|
$el: null,
|
|
|
|
dir: null,
|
2016-10-04 13:56:04 +03:00
|
|
|
dirInfo: null,
|
2013-10-28 23:22:06 +04:00
|
|
|
|
2014-05-23 21:02:50 +04:00
|
|
|
/**
|
|
|
|
* Total width of all breadcrumbs
|
2014-06-24 01:56:10 +04:00
|
|
|
* @type int
|
|
|
|
* @private
|
2014-05-23 21:02:50 +04:00
|
|
|
*/
|
2013-10-28 23:22:06 +04:00
|
|
|
totalWidth: 0,
|
|
|
|
breadcrumbs: [],
|
|
|
|
onClick: null,
|
|
|
|
onDrop: null,
|
2015-11-04 13:44:47 +03:00
|
|
|
onOver: null,
|
|
|
|
onOut: null,
|
2013-10-28 23:22:06 +04: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 11:56:00 +03:00
|
|
|
dir = dir.replace(/\\/g, '/');
|
2013-10-28 23:22:06 +04:00
|
|
|
dir = dir || '/';
|
|
|
|
if (dir !== this.dir) {
|
|
|
|
this.dir = dir;
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-10-04 13:56:04 +03: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 23:22:06 +04:00
|
|
|
/**
|
|
|
|
* Returns the full URL to the given directory
|
2014-06-24 01:56:10 +04:00
|
|
|
*
|
|
|
|
* @param {Object.<String, String>} part crumb data as map
|
|
|
|
* @param {int} index crumb index
|
2013-10-28 23:22:06 +04:00
|
|
|
* @return full URL
|
|
|
|
*/
|
|
|
|
getCrumbUrl: function(part, index) {
|
|
|
|
return '#';
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the breadcrumb elements
|
|
|
|
*/
|
|
|
|
render: function() {
|
2017-11-10 10:23:24 +03:00
|
|
|
// Menu is destroyed on every change, we need to init it
|
2018-02-28 13:04:27 +03:00
|
|
|
OC.unregisterMenu($('.crumbmenu > .icon-more'), $('.crumbmenu > .popovermenu'));
|
2017-11-07 07:53:17 +03:00
|
|
|
|
2013-10-28 23:22:06 +04:00
|
|
|
var parts = this._makeCrumbs(this.dir || '/');
|
|
|
|
var $crumb;
|
2017-11-04 19:47:38 +03:00
|
|
|
var $menuItem;
|
2013-10-28 23:22:06 +04:00
|
|
|
this.$el.empty();
|
|
|
|
this.breadcrumbs = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < parts.length; i++) {
|
|
|
|
var part = parts[i];
|
|
|
|
var $image;
|
2017-11-04 15:02:15 +03:00
|
|
|
var $link = $('<a></a>');
|
2017-11-04 19:47:38 +03:00
|
|
|
$crumb = $('<div class="crumb svg"></div>');
|
2017-11-04 15:02:15 +03: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 23:22:06 +04:00
|
|
|
$crumb.append($link);
|
2017-11-04 19:47:38 +03:00
|
|
|
$crumb.data('dir', part.dir);
|
|
|
|
// Ignore menu button
|
|
|
|
$crumb.data('crumb-id', i - 1);
|
2017-11-04 15:02:15 +03:00
|
|
|
$crumb.addClass(part.class);
|
2013-10-28 23:22:06 +04:00
|
|
|
|
|
|
|
if (part.img) {
|
|
|
|
$image = $('<img class="svg"></img>');
|
|
|
|
$image.attr('src', part.img);
|
2014-12-17 13:26:42 +03:00
|
|
|
$image.attr('alt', part.alt);
|
2013-10-28 23:22:06 +04:00
|
|
|
$link.append($image);
|
|
|
|
}
|
|
|
|
this.breadcrumbs.push($crumb);
|
|
|
|
this.$el.append($crumb);
|
2017-11-07 07:40:26 +03:00
|
|
|
// Only add feedback if not menu
|
|
|
|
if (this.onClick && i !== 0) {
|
|
|
|
$link.on('click', this.onClick);
|
2013-10-28 23:22:06 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-04 19:47:38 +03:00
|
|
|
// Menu creation
|
|
|
|
this._createMenu();
|
2017-11-08 21:12:56 +03:00
|
|
|
for (var j = 0; j < parts.length; j++) {
|
|
|
|
var menuPart = parts[j];
|
|
|
|
if(menuPart.dir) {
|
2017-11-08 15:10:30 +03:00
|
|
|
$menuItem = $('<li class="crumblist"><a><span class="icon-folder"></span><span></span></a></li>');
|
2017-11-08 21:12:56 +03: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 19:47:38 +03:00
|
|
|
this.$menu.children('ul').append($menuItem);
|
|
|
|
if (this.onClick) {
|
|
|
|
$menuItem.on('click', this.onClick);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-04 13:56:04 +03:00
|
|
|
_.each(this._detailViews, function(view) {
|
|
|
|
view.render({
|
|
|
|
dirInfo: this.dirInfo
|
|
|
|
});
|
|
|
|
$crumb.append(view.$el);
|
|
|
|
}, this);
|
|
|
|
|
2013-10-28 23:22:06 +04:00
|
|
|
// setup drag and drop
|
|
|
|
if (this.onDrop) {
|
2017-11-08 18:56:58 +03:00
|
|
|
this.$el.find('.crumb:not(:last-child):not(.crumbmenu), .crumblist:not(:last-child)').droppable({
|
2013-10-28 23:22:06 +04:00
|
|
|
drop: this.onDrop,
|
2015-11-04 13:44:47 +03:00
|
|
|
over: this.onOver,
|
|
|
|
out: this.onOut,
|
2015-10-29 16:41:37 +03:00
|
|
|
tolerance: 'pointer',
|
2017-11-08 21:03:32 +03:00
|
|
|
hoverClass: 'canDrop',
|
|
|
|
greedy: true
|
2017-11-08 18:56:58 +03:00
|
|
|
});
|
2013-10-28 23:22:06 +04:00
|
|
|
}
|
2017-11-10 10:23:24 +03:00
|
|
|
|
2017-11-07 07:40:26 +03:00
|
|
|
// Menu is destroyed on every change, we need to init it
|
2018-02-28 13:04:27 +03:00
|
|
|
OC.registerMenu($('.crumbmenu > .icon-more'), $('.crumbmenu > .popovermenu'));
|
2017-11-10 10:23:24 +03:00
|
|
|
|
2017-11-04 11:45:29 +03:00
|
|
|
this._resize();
|
2013-10-28 23:22:06 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a breadcrumb structure based on the given path
|
2014-06-24 01:56:10 +04:00
|
|
|
*
|
|
|
|
* @param {String} dir path to split into a breadcrumb structure
|
2019-10-05 10:53:07 +03:00
|
|
|
* @param {String} [rootIcon=icon-home] icon to use for root
|
2014-06-24 01:56:10 +04:00
|
|
|
* @return {Object.<String, String>} map of {dir: path, name: displayName}
|
2013-10-28 23:22:06 +04:00
|
|
|
*/
|
2019-10-05 10:53:07 +03:00
|
|
|
_makeCrumbs: function(dir, rootIcon) {
|
2013-10-28 23:22:06 +04:00
|
|
|
var crumbs = [];
|
|
|
|
var pathToHere = '';
|
|
|
|
// trim leading and trailing slashes
|
|
|
|
dir = dir.replace(/^\/+|\/+$/g, '');
|
|
|
|
var parts = dir.split('/');
|
|
|
|
if (dir === '') {
|
|
|
|
parts = [];
|
|
|
|
}
|
2017-11-04 15:02:15 +03:00
|
|
|
// menu part
|
|
|
|
crumbs.push({
|
2017-11-08 20:05:01 +03:00
|
|
|
class: 'crumbmenu hidden',
|
2018-02-28 13:04:27 +03:00
|
|
|
linkclass: 'icon-more menutoggle'
|
2013-10-28 23:22:06 +04:00
|
|
|
});
|
2017-11-04 19:47:38 +03:00
|
|
|
// root part
|
|
|
|
crumbs.push({
|
|
|
|
name: t('core', 'Home'),
|
|
|
|
dir: '/',
|
2017-11-06 07:27:10 +03:00
|
|
|
class: 'crumbhome',
|
2019-10-05 10:53:07 +03:00
|
|
|
linkclass: rootIcon || 'icon-home'
|
2017-11-04 19:47:38 +03:00
|
|
|
});
|
2013-10-28 23:22:06 +04: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 18:00:40 +03:00
|
|
|
/**
|
|
|
|
* Calculate real width based on individual crumbs
|
|
|
|
*
|
|
|
|
* @param {boolean} ignoreHidden ignore hidden crumbs
|
|
|
|
*/
|
|
|
|
getTotalWidth: function(ignoreHidden) {
|
2018-02-28 16:35:28 +03: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 18:00:40 +03:00
|
|
|
var totalWidth = 0;
|
|
|
|
for (var i = 0; i < this.breadcrumbs.length; i++ ) {
|
|
|
|
var $crumb = $(this.breadcrumbs[i]);
|
2017-11-08 19:54:38 +03:00
|
|
|
if(!$crumb.hasClass('hidden') || ignoreHidden === true) {
|
2018-02-27 15:59:09 +03:00
|
|
|
totalWidth += $crumb.outerWidth(true);
|
2017-11-08 18:00:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return totalWidth;
|
|
|
|
},
|
|
|
|
|
2017-11-04 11:45:29 +03:00
|
|
|
/**
|
|
|
|
* Hide the middle crumb
|
|
|
|
*/
|
2017-11-04 19:47:38 +03:00
|
|
|
_hideCrumb: function() {
|
2017-11-08 19:54:38 +03:00
|
|
|
var length = this.$el.find(this.crumbSelector).length;
|
2017-11-04 19:47:38 +03:00
|
|
|
// Get the middle one floored down
|
|
|
|
var elmt = Math.floor(length / 2 - 0.5);
|
2017-11-08 19:54:38 +03:00
|
|
|
this.$el.find(this.crumbSelector+':eq('+elmt+')').addClass('hidden');
|
2017-11-04 19:47:38 +03:00
|
|
|
},
|
2017-11-04 11:45:29 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the crumb to show
|
|
|
|
*/
|
2017-11-04 19:47:38 +03:00
|
|
|
_getCrumbElement: function() {
|
2018-02-27 19:14:34 +03:00
|
|
|
var hidden = this.$el.find(this.hiddenCrumbSelector).length;
|
2017-11-08 19:54:38 +03:00
|
|
|
var shown = this.$el.find(this.crumbSelector).length;
|
2017-11-04 19:47:38 +03:00
|
|
|
// Get the outer one with priority to the highest
|
2017-11-08 16:01:57 +03:00
|
|
|
var elmt = (1 - shown % 2) * (hidden - 1);
|
2018-02-27 19:14:34 +03:00
|
|
|
return this.$el.find(this.hiddenCrumbSelector + ':eq('+elmt+')');
|
2017-11-04 19:47:38 +03:00
|
|
|
},
|
2017-11-03 14:23:57 +03:00
|
|
|
|
2017-11-04 11:45:29 +03:00
|
|
|
/**
|
|
|
|
* Show the middle crumb
|
|
|
|
*/
|
2017-11-04 19:47:38 +03:00
|
|
|
_showCrumb: function() {
|
2018-02-27 19:14:34 +03:00
|
|
|
if(this.$el.find(this.hiddenCrumbSelector).length === 1) {
|
|
|
|
this.$el.find(this.hiddenCrumbSelector).removeClass('hidden');
|
2017-11-04 19:47:38 +03:00
|
|
|
}
|
|
|
|
this._getCrumbElement().removeClass('hidden');
|
|
|
|
},
|
2017-11-04 11:45:29 +03:00
|
|
|
|
2017-11-04 19:47:38 +03:00
|
|
|
/**
|
2017-11-04 15:02:15 +03:00
|
|
|
* Create and append the popovermenu
|
|
|
|
*/
|
2017-11-04 19:47:38 +03:00
|
|
|
_createMenu: function() {
|
|
|
|
this.$el.find('.crumbmenu').append(this.$menu);
|
|
|
|
this.$menu.children('ul').empty();
|
|
|
|
},
|
2017-11-04 15:02:15 +03:00
|
|
|
|
2017-11-04 19:47:38 +03:00
|
|
|
/**
|
2017-11-04 15:02:15 +03:00
|
|
|
* Update the popovermenu
|
|
|
|
*/
|
2017-11-04 19:47:38 +03:00
|
|
|
_updateMenu: function() {
|
2018-02-27 19:14:34 +03:00
|
|
|
var menuItems = this.$el.find(this.hiddenCrumbSelector);
|
2017-11-04 15:02:15 +03:00
|
|
|
|
2017-11-04 19:47:38 +03: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 15:02:15 +03:00
|
|
|
|
2014-05-23 21:02:50 +04:00
|
|
|
_resize: function() {
|
2017-11-08 19:54:38 +03:00
|
|
|
|
|
|
|
if (this.breadcrumbs.length <= 2) {
|
|
|
|
// home & menu
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-27 19:14:34 +03: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 19:07:26 +03: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 19:14:34 +03: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 19:07:26 +03:00
|
|
|
this._showCrumb();
|
2013-10-28 23:22:06 +04:00
|
|
|
}
|
|
|
|
|
2018-02-20 02:11:40 +03: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 23:22:06 +04:00
|
|
|
|
2017-11-04 11:45:29 +03:00
|
|
|
// If container is smaller than content
|
2017-11-08 19:54:38 +03:00
|
|
|
// AND if there are crumbs left to hide
|
2018-02-19 22:46:49 +03:00
|
|
|
while (this.getTotalWidth() > availableWidth
|
2017-11-10 10:23:24 +03:00
|
|
|
&& this.$el.find(this.crumbSelector).length > 0) {
|
2018-02-27 19:14:34 +03: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 11:45:29 +03:00
|
|
|
this._hideCrumb();
|
|
|
|
}
|
2014-05-23 21:02:50 +04:00
|
|
|
|
2017-11-04 15:02:15 +03:00
|
|
|
this._updateMenu();
|
2013-10-28 23:22:06 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-05-09 00:06:30 +04:00
|
|
|
OCA.Files.BreadCrumb = BreadCrumb;
|
2013-10-28 23:22:06 +04:00
|
|
|
})();
|