breadcrumb.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /**
  2. * ownCloud
  3. *
  4. * @author Vincent Petry
  5. * @copyright 2014 Vincent Petry <pvince81@owncloud.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public
  18. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. (function() {
  22. /**
  23. * @class BreadCrumb
  24. * @memberof OCA.Files
  25. * @classdesc Breadcrumbs that represent the current path.
  26. *
  27. * @param {Object} [options] options
  28. * @param {Function} [options.onClick] click event handler
  29. * @param {Function} [options.onDrop] drop event handler
  30. * @param {Function} [options.getCrumbUrl] callback that returns
  31. * the URL of a given breadcrumb
  32. */
  33. var BreadCrumb = function(options){
  34. this.$el = $('<div class="breadcrumb"></div>');
  35. this.$menu = $('<div class="popovermenu menu-center"><ul></ul></div>');
  36. this.crumbSelector = '.crumb:not(.hidden):not(.crumbhome):not(.crumbmenu)';
  37. this.hiddenCrumbSelector = '.crumb.hidden:not(.crumbhome):not(.crumbmenu)';
  38. options = options || {};
  39. if (options.onClick) {
  40. this.onClick = options.onClick;
  41. }
  42. if (options.onDrop) {
  43. this.onDrop = options.onDrop;
  44. this.onOver = options.onOver;
  45. this.onOut = options.onOut;
  46. }
  47. if (options.getCrumbUrl) {
  48. this.getCrumbUrl = options.getCrumbUrl;
  49. }
  50. this._detailViews = [];
  51. };
  52. /**
  53. * @memberof OCA.Files
  54. */
  55. BreadCrumb.prototype = {
  56. $el: null,
  57. dir: null,
  58. dirInfo: null,
  59. /**
  60. * Total width of all breadcrumbs
  61. * @type int
  62. * @private
  63. */
  64. totalWidth: 0,
  65. breadcrumbs: [],
  66. onClick: null,
  67. onDrop: null,
  68. onOver: null,
  69. onOut: null,
  70. /**
  71. * Sets the directory to be displayed as breadcrumb.
  72. * This will re-render the breadcrumb.
  73. * @param dir path to be displayed as breadcrumb
  74. */
  75. setDirectory: function(dir) {
  76. dir = dir.replace(/\\/g, '/');
  77. dir = dir || '/';
  78. if (dir !== this.dir) {
  79. this.dir = dir;
  80. this.render();
  81. }
  82. },
  83. setDirectoryInfo: function(dirInfo) {
  84. if (dirInfo !== this.dirInfo) {
  85. this.dirInfo = dirInfo;
  86. this.render();
  87. }
  88. },
  89. /**
  90. * @param {Backbone.View} detailView
  91. */
  92. addDetailView: function(detailView) {
  93. this._detailViews.push(detailView);
  94. },
  95. /**
  96. * Returns the full URL to the given directory
  97. *
  98. * @param {Object.<String, String>} part crumb data as map
  99. * @param {int} index crumb index
  100. * @return full URL
  101. */
  102. getCrumbUrl: function(part, index) {
  103. return '#';
  104. },
  105. /**
  106. * Renders the breadcrumb elements
  107. */
  108. render: function() {
  109. // Menu is destroyed on every change, we need to init it
  110. OC.unregisterMenu($('.crumbmenu > .icon-more'), $('.crumbmenu > .popovermenu'));
  111. var parts = this._makeCrumbs(this.dir || '/');
  112. var $crumb;
  113. var $menuItem;
  114. this.$el.empty();
  115. this.breadcrumbs = [];
  116. for (var i = 0; i < parts.length; i++) {
  117. var part = parts[i];
  118. var $image;
  119. var $link = $('<a></a>');
  120. $crumb = $('<div class="crumb svg"></div>');
  121. if(part.dir) {
  122. $link.attr('href', this.getCrumbUrl(part, i));
  123. }
  124. if(part.name) {
  125. $link.text(part.name);
  126. }
  127. $link.addClass(part.linkclass);
  128. $crumb.append($link);
  129. $crumb.data('dir', part.dir);
  130. // Ignore menu button
  131. $crumb.data('crumb-id', i - 1);
  132. $crumb.addClass(part.class);
  133. if (part.img) {
  134. $image = $('<img class="svg"></img>');
  135. $image.attr('src', part.img);
  136. $image.attr('alt', part.alt);
  137. $link.append($image);
  138. }
  139. this.breadcrumbs.push($crumb);
  140. this.$el.append($crumb);
  141. // Only add feedback if not menu
  142. if (this.onClick && i !== 0) {
  143. $link.on('click', this.onClick);
  144. }
  145. }
  146. // Menu creation
  147. this._createMenu();
  148. for (var j = 0; j < parts.length; j++) {
  149. var menuPart = parts[j];
  150. if(menuPart.dir) {
  151. $menuItem = $('<li class="crumblist"><a><span class="icon-folder"></span><span></span></a></li>');
  152. $menuItem.data('dir', menuPart.dir);
  153. $menuItem.find('a').attr('href', this.getCrumbUrl(part, j));
  154. $menuItem.find('span:eq(1)').text(menuPart.name);
  155. this.$menu.children('ul').append($menuItem);
  156. if (this.onClick) {
  157. $menuItem.on('click', this.onClick);
  158. }
  159. }
  160. }
  161. _.each(this._detailViews, function(view) {
  162. view.render({
  163. dirInfo: this.dirInfo
  164. });
  165. $crumb.append(view.$el);
  166. }, this);
  167. // setup drag and drop
  168. if (this.onDrop) {
  169. this.$el.find('.crumb:not(:last-child):not(.crumbmenu), .crumblist:not(:last-child)').droppable({
  170. drop: this.onDrop,
  171. over: this.onOver,
  172. out: this.onOut,
  173. tolerance: 'pointer',
  174. hoverClass: 'canDrop',
  175. greedy: true
  176. });
  177. }
  178. // Menu is destroyed on every change, we need to init it
  179. OC.registerMenu($('.crumbmenu > .icon-more'), $('.crumbmenu > .popovermenu'));
  180. this._resize();
  181. },
  182. /**
  183. * Makes a breadcrumb structure based on the given path
  184. *
  185. * @param {String} dir path to split into a breadcrumb structure
  186. * @param {String} [rootIcon=icon-home] icon to use for root
  187. * @return {Object.<String, String>} map of {dir: path, name: displayName}
  188. */
  189. _makeCrumbs: function(dir, rootIcon) {
  190. var crumbs = [];
  191. var pathToHere = '';
  192. // trim leading and trailing slashes
  193. dir = dir.replace(/^\/+|\/+$/g, '');
  194. var parts = dir.split('/');
  195. if (dir === '') {
  196. parts = [];
  197. }
  198. // menu part
  199. crumbs.push({
  200. class: 'crumbmenu hidden',
  201. linkclass: 'icon-more menutoggle'
  202. });
  203. // root part
  204. crumbs.push({
  205. name: t('core', 'Home'),
  206. dir: '/',
  207. class: 'crumbhome',
  208. linkclass: rootIcon || 'icon-home'
  209. });
  210. for (var i = 0; i < parts.length; i++) {
  211. var part = parts[i];
  212. pathToHere = pathToHere + '/' + part;
  213. crumbs.push({
  214. dir: pathToHere,
  215. name: part
  216. });
  217. }
  218. return crumbs;
  219. },
  220. /**
  221. * Calculate real width based on individual crumbs
  222. *
  223. * @param {boolean} ignoreHidden ignore hidden crumbs
  224. */
  225. getTotalWidth: function(ignoreHidden) {
  226. // The width has to be calculated by adding up the width of all the
  227. // crumbs; getting the width of the breadcrumb element is not a
  228. // valid approach, as the returned value could be clamped to its
  229. // parent width.
  230. var totalWidth = 0;
  231. for (var i = 0; i < this.breadcrumbs.length; i++ ) {
  232. var $crumb = $(this.breadcrumbs[i]);
  233. if(!$crumb.hasClass('hidden') || ignoreHidden === true) {
  234. totalWidth += $crumb.outerWidth(true);
  235. }
  236. }
  237. return totalWidth;
  238. },
  239. /**
  240. * Hide the middle crumb
  241. */
  242. _hideCrumb: function() {
  243. var length = this.$el.find(this.crumbSelector).length;
  244. // Get the middle one floored down
  245. var elmt = Math.floor(length / 2 - 0.5);
  246. this.$el.find(this.crumbSelector+':eq('+elmt+')').addClass('hidden');
  247. },
  248. /**
  249. * Get the crumb to show
  250. */
  251. _getCrumbElement: function() {
  252. var hidden = this.$el.find(this.hiddenCrumbSelector).length;
  253. var shown = this.$el.find(this.crumbSelector).length;
  254. // Get the outer one with priority to the highest
  255. var elmt = (1 - shown % 2) * (hidden - 1);
  256. return this.$el.find(this.hiddenCrumbSelector + ':eq('+elmt+')');
  257. },
  258. /**
  259. * Show the middle crumb
  260. */
  261. _showCrumb: function() {
  262. if(this.$el.find(this.hiddenCrumbSelector).length === 1) {
  263. this.$el.find(this.hiddenCrumbSelector).removeClass('hidden');
  264. }
  265. this._getCrumbElement().removeClass('hidden');
  266. },
  267. /**
  268. * Create and append the popovermenu
  269. */
  270. _createMenu: function() {
  271. this.$el.find('.crumbmenu').append(this.$menu);
  272. this.$menu.children('ul').empty();
  273. },
  274. /**
  275. * Update the popovermenu
  276. */
  277. _updateMenu: function() {
  278. var menuItems = this.$el.find(this.hiddenCrumbSelector);
  279. this.$menu.find('li').addClass('in-breadcrumb');
  280. for (var i = 0; i < menuItems.length; i++) {
  281. var crumbId = $(menuItems[i]).data('crumb-id');
  282. this.$menu.find('li:eq('+crumbId+')').removeClass('in-breadcrumb');
  283. }
  284. },
  285. _resize: function() {
  286. if (this.breadcrumbs.length <= 2) {
  287. // home & menu
  288. return;
  289. }
  290. // Always hide the menu to ensure that it does not interfere with
  291. // the width calculations; otherwise, the result could be different
  292. // depending on whether the menu was previously being shown or not.
  293. this.$el.find('.crumbmenu').addClass('hidden');
  294. // Show the crumbs to compress the siblings before hidding again the
  295. // crumbs. This is needed when the siblings expand to fill all the
  296. // available width, as in that case their old width would limit the
  297. // available width for the crumbs.
  298. // Note that the crumbs shown always overflow the parent width
  299. // (except, of course, when they all fit in).
  300. while (this.$el.find(this.hiddenCrumbSelector).length > 0
  301. && this.getTotalWidth() <= this.$el.parent().width()) {
  302. this._showCrumb();
  303. }
  304. var siblingsWidth = 0;
  305. this.$el.prevAll(':visible').each(function () {
  306. siblingsWidth += $(this).outerWidth(true);
  307. });
  308. this.$el.nextAll(':visible').each(function () {
  309. siblingsWidth += $(this).outerWidth(true);
  310. });
  311. var availableWidth = this.$el.parent().width() - siblingsWidth;
  312. // If container is smaller than content
  313. // AND if there are crumbs left to hide
  314. while (this.getTotalWidth() > availableWidth
  315. && this.$el.find(this.crumbSelector).length > 0) {
  316. // As soon as one of the crumbs is hidden the menu will be
  317. // shown. This is needed for proper results in further width
  318. // checks.
  319. // Note that the menu is not shown only when all the crumbs were
  320. // being shown and they all fit the available space; if any of
  321. // the crumbs was not being shown then those shown would
  322. // overflow the available width, so at least one will be hidden
  323. // and thus the menu will be shown.
  324. this.$el.find('.crumbmenu').removeClass('hidden');
  325. this._hideCrumb();
  326. }
  327. this._updateMenu();
  328. }
  329. };
  330. OCA.Files.BreadCrumb = BreadCrumb;
  331. })();