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)';
|
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
|
|
|
|
OC.unregisterMenu($('.crumbmenu'), $('.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
|
|
|
// in case svg is not supported by the browser we need to execute the fallback mechanism
|
2014-04-04 13:32:07 +04:00
|
|
|
if (!OC.Util.hasSVGSupport()) {
|
|
|
|
OC.Util.replaceSVG(this.$el);
|
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
|
|
|
|
OC.registerMenu($('.crumbmenu'), $('.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
|
|
|
|
* @return {Object.<String, String>} map of {dir: path, name: displayName}
|
2013-10-28 23:22:06 +04: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 15:02:15 +03:00
|
|
|
// menu part
|
|
|
|
crumbs.push({
|
2017-11-08 20:05:01 +03:00
|
|
|
class: 'crumbmenu hidden',
|
2017-11-04 15:02:15 +03:00
|
|
|
linkclass: 'icon-more'
|
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',
|
2017-11-04 19:47:38 +03:00
|
|
|
linkclass: 'icon-home'
|
|
|
|
});
|
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
|
|
|
/**
|
|
|
|
* Show/hide breadcrumbs to fit the given width
|
|
|
|
* Mostly used by tests
|
|
|
|
*
|
|
|
|
* @param {int} availableWidth available width
|
|
|
|
*/
|
|
|
|
setMaxWidth: function (availableWidth) {
|
|
|
|
if (this.availableWidth !== availableWidth) {
|
|
|
|
this.availableWidth = availableWidth;
|
|
|
|
this._resize();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate real width based on individual crumbs
|
|
|
|
* More accurate and works with tests
|
|
|
|
*
|
|
|
|
* @param {boolean} ignoreHidden ignore hidden crumbs
|
|
|
|
*/
|
|
|
|
getTotalWidth: function(ignoreHidden) {
|
|
|
|
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) {
|
2017-11-08 22:12:38 +03:00
|
|
|
totalWidth += $crumb.outerWidth();
|
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() {
|
2017-11-08 16:01:57 +03:00
|
|
|
var hidden = this.$el.find('.crumb.hidden').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);
|
2017-11-04 19:47:38 +03:00
|
|
|
return this.$el.find('.crumb.hidden:eq('+elmt+')');
|
|
|
|
},
|
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() {
|
|
|
|
if(this.$el.find('.crumb.hidden').length === 1) {
|
|
|
|
this.$el.find('.crumb.hidden').removeClass('hidden');
|
|
|
|
}
|
|
|
|
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() {
|
2017-11-08 20:05:01 +03:00
|
|
|
var menuItems = this.$el.find('.crumb.hidden');
|
2017-11-04 19:47:38 +03:00
|
|
|
// Hide the crumb menu if no elements
|
2017-11-08 21:12:56 +03:00
|
|
|
this.$el.find('.crumbmenu').toggleClass('hidden', menuItems.length === 0);
|
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;
|
|
|
|
}
|
|
|
|
|
2017-11-08 18:00:40 +03:00
|
|
|
// Used for testing since this.$el.parent fails
|
2014-05-23 21:02:50 +04:00
|
|
|
if (!this.availableWidth) {
|
2017-11-10 10:23:24 +03:00
|
|
|
this.usedWidth = this.$el.parent().width() - (this.$el.parent().find('.button').length + 1) * 44;
|
2017-11-08 18:00:40 +03:00
|
|
|
} else {
|
|
|
|
this.usedWidth = this.availableWidth;
|
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
|
|
|
|
while (this.getTotalWidth() > this.usedWidth
|
2017-11-10 10:23:24 +03:00
|
|
|
&& this.$el.find(this.crumbSelector).length > 0) {
|
2017-11-04 11:45:29 +03:00
|
|
|
this._hideCrumb();
|
|
|
|
}
|
|
|
|
// If container is bigger than content + element to be shown
|
|
|
|
// AND if there is at least one hidden crumb
|
|
|
|
while (this.$el.find('.crumb.hidden').length > 0
|
2017-11-08 18:00:40 +03:00
|
|
|
&& this.getTotalWidth() + this._getCrumbElement().width() < this.usedWidth) {
|
2017-11-04 11:45:29 +03:00
|
|
|
this._showCrumb();
|
|
|
|
}
|
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
|
|
|
})();
|