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>');
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
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,
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() {
|
|
|
|
var parts = this._makeCrumbs(this.dir || '/');
|
|
|
|
var $crumb;
|
|
|
|
this.$el.empty();
|
|
|
|
this.breadcrumbs = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < parts.length; i++) {
|
|
|
|
var part = parts[i];
|
|
|
|
var $image;
|
|
|
|
var $link = $('<a></a>').attr('href', this.getCrumbUrl(part, i));
|
|
|
|
$link.text(part.name);
|
2014-04-03 19:08:06 +04:00
|
|
|
$crumb = $('<div class="crumb svg"></div>');
|
2013-10-28 23:22:06 +04:00
|
|
|
$crumb.append($link);
|
|
|
|
$crumb.attr('data-dir', part.dir);
|
|
|
|
|
|
|
|
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);
|
|
|
|
if (this.onClick) {
|
|
|
|
$crumb.on('click', this.onClick);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$crumb.addClass('last');
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
this.$el.find('.crumb:not(.last)').droppable({
|
|
|
|
drop: this.onDrop,
|
2015-11-04 13:44:47 +03:00
|
|
|
over: this.onOver,
|
|
|
|
out: this.onOut,
|
2013-10-28 23:22:06 +04:00
|
|
|
tolerance: 'pointer'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this._updateTotalWidth();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 = [];
|
|
|
|
}
|
|
|
|
// root part
|
|
|
|
crumbs.push({
|
|
|
|
dir: '/',
|
|
|
|
name: '',
|
2014-12-17 13:26:42 +03:00
|
|
|
alt: t('files', 'Home'),
|
2013-10-28 23:22:06 +04:00
|
|
|
img: OC.imagePath('core', 'places/home.svg')
|
|
|
|
});
|
|
|
|
for (var i = 0; i < parts.length; i++) {
|
|
|
|
var part = parts[i];
|
|
|
|
pathToHere = pathToHere + '/' + part;
|
|
|
|
crumbs.push({
|
|
|
|
dir: pathToHere,
|
|
|
|
name: part
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return crumbs;
|
|
|
|
},
|
|
|
|
|
2014-05-23 21:02:50 +04:00
|
|
|
/**
|
|
|
|
* Calculate the total breadcrumb width when
|
|
|
|
* all crumbs are expanded
|
|
|
|
*/
|
2013-10-28 23:22:06 +04:00
|
|
|
_updateTotalWidth: function () {
|
2014-05-23 21:02:50 +04:00
|
|
|
this.totalWidth = 0;
|
|
|
|
for (var i = 0; i < this.breadcrumbs.length; i++ ) {
|
|
|
|
var $crumb = $(this.breadcrumbs[i]);
|
|
|
|
$crumb.data('real-width', $crumb.width());
|
|
|
|
this.totalWidth += $crumb.width();
|
2014-05-12 21:54:20 +04:00
|
|
|
}
|
2014-05-23 21:02:50 +04:00
|
|
|
this._resize();
|
|
|
|
},
|
2014-05-12 21:54:20 +04:00
|
|
|
|
2014-05-23 21:02:50 +04:00
|
|
|
/**
|
|
|
|
* Show/hide breadcrumbs to fit the given width
|
2014-06-24 01:56:10 +04:00
|
|
|
*
|
|
|
|
* @param {int} availableWidth available width
|
2014-05-23 21:02:50 +04:00
|
|
|
*/
|
|
|
|
setMaxWidth: function (availableWidth) {
|
|
|
|
if (this.availableWidth !== availableWidth) {
|
|
|
|
this.availableWidth = availableWidth;
|
|
|
|
this._resize();
|
2013-10-28 23:22:06 +04:00
|
|
|
}
|
2014-05-23 21:02:50 +04:00
|
|
|
},
|
2013-10-28 23:22:06 +04:00
|
|
|
|
2014-05-23 21:02:50 +04:00
|
|
|
_resize: function() {
|
|
|
|
var i, $crumb, $ellipsisCrumb;
|
|
|
|
|
|
|
|
if (!this.availableWidth) {
|
|
|
|
this.availableWidth = this.$el.width();
|
2013-10-28 23:22:06 +04:00
|
|
|
}
|
|
|
|
|
2014-05-23 21:02:50 +04:00
|
|
|
if (this.breadcrumbs.length <= 1) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-28 23:22:06 +04:00
|
|
|
|
2014-05-23 21:02:50 +04:00
|
|
|
// reset crumbs
|
|
|
|
this.$el.find('.crumb.ellipsized').remove();
|
2013-10-28 23:22:06 +04:00
|
|
|
|
2014-05-23 21:02:50 +04:00
|
|
|
// unhide all
|
|
|
|
this.$el.find('.crumb.hidden').removeClass('hidden');
|
2013-10-28 23:22:06 +04:00
|
|
|
|
2014-05-23 21:02:50 +04:00
|
|
|
if (this.totalWidth <= this.availableWidth) {
|
|
|
|
// no need to compute breadcrumbs, there is enough space
|
2013-10-28 23:22:06 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-23 21:02:50 +04:00
|
|
|
// running width, considering the hidden crumbs
|
|
|
|
var currentTotalWidth = $(this.breadcrumbs[0]).data('real-width');
|
|
|
|
var firstHidden = true;
|
|
|
|
|
|
|
|
// insert ellipsis after root part (root part is always visible)
|
|
|
|
$ellipsisCrumb = $('<div class="crumb ellipsized svg"><span class="ellipsis">...</span></div>');
|
|
|
|
$(this.breadcrumbs[0]).after($ellipsisCrumb);
|
|
|
|
currentTotalWidth += $ellipsisCrumb.width();
|
|
|
|
|
|
|
|
i = this.breadcrumbs.length - 1;
|
|
|
|
|
|
|
|
// find the first section that would cause the overflow
|
|
|
|
// then hide everything in front of that
|
|
|
|
//
|
|
|
|
// this ensures that the last crumb section stays visible
|
|
|
|
// for most of the cases and is always the last one to be
|
|
|
|
// hidden when the screen becomes very narrow
|
|
|
|
while (i > 0) {
|
|
|
|
$crumb = $(this.breadcrumbs[i]);
|
|
|
|
// if the current breadcrumb would cause overflow
|
|
|
|
if (!firstHidden || currentTotalWidth + $crumb.data('real-width') > this.availableWidth) {
|
|
|
|
// hide it
|
2013-10-28 23:22:06 +04:00
|
|
|
$crumb.addClass('hidden');
|
2014-05-23 21:02:50 +04:00
|
|
|
if (firstHidden) {
|
|
|
|
// set the path of this one as title for the ellipsis
|
|
|
|
this.$el.find('.crumb.ellipsized')
|
|
|
|
.attr('title', $crumb.attr('data-dir'))
|
|
|
|
.tipsy();
|
2014-09-05 22:54:53 +04:00
|
|
|
this.$el.find('.ellipsis')
|
|
|
|
.wrap('<a class="ellipsislink" href="' + encodeURI(OC.generateUrl('apps/files/?dir=' + $crumb.attr('data-dir'))) + '"></a>');
|
2013-10-28 23:22:06 +04:00
|
|
|
}
|
2014-05-23 21:02:50 +04:00
|
|
|
// and all the previous ones (going backwards)
|
|
|
|
firstHidden = false;
|
|
|
|
} else {
|
|
|
|
// add to total width
|
|
|
|
currentTotalWidth += $crumb.data('real-width');
|
2013-10-28 23:22:06 +04:00
|
|
|
}
|
2014-05-23 21:02:50 +04:00
|
|
|
i--;
|
2013-10-28 23:22:06 +04:00
|
|
|
}
|
|
|
|
|
2014-05-23 21:02:50 +04:00
|
|
|
if (!OC.Util.hasSVGSupport()) {
|
|
|
|
OC.Util.replaceSVG(this.$el);
|
|
|
|
}
|
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
|
|
|
})();
|
|
|
|
|