breadcrumb.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 {number} 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. $menuItem.append(view.$el.clone(true));
  167. }, this);
  168. // setup drag and drop
  169. if (this.onDrop) {
  170. this.$el.find('.crumb:not(:last-child):not(.crumbmenu), .crumblist:not(:last-child)').droppable({
  171. drop: this.onDrop,
  172. over: this.onOver,
  173. out: this.onOut,
  174. tolerance: 'pointer',
  175. hoverClass: 'canDrop',
  176. greedy: true
  177. });
  178. }
  179. // Menu is destroyed on every change, we need to init it
  180. OC.registerMenu($('.crumbmenu > .icon-more'), $('.crumbmenu > .popovermenu'));
  181. this._resize();
  182. },
  183. /**
  184. * Makes a breadcrumb structure based on the given path
  185. *
  186. * @param {String} dir path to split into a breadcrumb structure
  187. * @param {String} [rootIcon=icon-home] icon to use for root
  188. * @return {Object.<String, String>} map of {dir: path, name: displayName}
  189. */
  190. _makeCrumbs: function(dir, rootIcon) {
  191. var crumbs = [];
  192. var pathToHere = '';
  193. // trim leading and trailing slashes
  194. dir = dir.replace(/^\/+|\/+$/g, '');
  195. var parts = dir.split('/');
  196. if (dir === '') {
  197. parts = [];
  198. }
  199. // menu part
  200. crumbs.push({
  201. class: 'crumbmenu hidden',
  202. linkclass: 'icon-more menutoggle'
  203. });
  204. // root part
  205. crumbs.push({
  206. name: t('files', 'Home'),
  207. dir: '/',
  208. class: 'crumbhome',
  209. linkclass: rootIcon || 'icon-home'
  210. });
  211. for (var i = 0; i < parts.length; i++) {
  212. var part = parts[i];
  213. pathToHere = pathToHere + '/' + part;
  214. crumbs.push({
  215. dir: pathToHere,
  216. name: part
  217. });
  218. }
  219. return crumbs;
  220. },
  221. /**
  222. * Calculate real width based on individual crumbs
  223. *
  224. * @param {boolean} ignoreHidden ignore hidden crumbs
  225. */
  226. getTotalWidth: function(ignoreHidden) {
  227. // The width has to be calculated by adding up the width of all the
  228. // crumbs; getting the width of the breadcrumb element is not a
  229. // valid approach, as the returned value could be clamped to its
  230. // parent width.
  231. var totalWidth = 0;
  232. for (var i = 0; i < this.breadcrumbs.length; i++ ) {
  233. var $crumb = $(this.breadcrumbs[i]);
  234. if(!$crumb.hasClass('hidden') || ignoreHidden === true) {
  235. totalWidth += $crumb.outerWidth(true);
  236. }
  237. }
  238. return totalWidth;
  239. },
  240. /**
  241. * Hide the middle crumb
  242. */
  243. _hideCrumb: function() {
  244. var length = this.$el.find(this.crumbSelector).length;
  245. // Get the middle one floored down
  246. var elmt = Math.floor(length / 2 - 0.5);
  247. this.$el.find(this.crumbSelector+':eq('+elmt+')').addClass('hidden');
  248. },
  249. /**
  250. * Get the crumb to show
  251. */
  252. _getCrumbElement: function() {
  253. var hidden = this.$el.find(this.hiddenCrumbSelector).length;
  254. var shown = this.$el.find(this.crumbSelector).length;
  255. // Get the outer one with priority to the highest
  256. var elmt = (1 - shown % 2) * (hidden - 1);
  257. return this.$el.find(this.hiddenCrumbSelector + ':eq('+elmt+')');
  258. },
  259. /**
  260. * Show the middle crumb
  261. */
  262. _showCrumb: function() {
  263. if(this.$el.find(this.hiddenCrumbSelector).length === 1) {
  264. this.$el.find(this.hiddenCrumbSelector).removeClass('hidden');
  265. }
  266. this._getCrumbElement().removeClass('hidden');
  267. },
  268. /**
  269. * Create and append the popovermenu
  270. */
  271. _createMenu: function() {
  272. this.$el.find('.crumbmenu').append(this.$menu);
  273. this.$menu.children('ul').empty();
  274. },
  275. /**
  276. * Update the popovermenu
  277. */
  278. _updateMenu: function() {
  279. var menuItems = this.$el.find(this.hiddenCrumbSelector);
  280. this.$menu.find('li').addClass('in-breadcrumb');
  281. for (var i = 0; i < menuItems.length; i++) {
  282. var crumbId = $(menuItems[i]).data('crumb-id');
  283. this.$menu.find('li:eq('+crumbId+')').removeClass('in-breadcrumb');
  284. }
  285. },
  286. _resize: function() {
  287. if (this.breadcrumbs.length <= 2) {
  288. // home & menu
  289. return;
  290. }
  291. // Always hide the menu to ensure that it does not interfere with
  292. // the width calculations; otherwise, the result could be different
  293. // depending on whether the menu was previously being shown or not.
  294. this.$el.find('.crumbmenu').addClass('hidden');
  295. // Show the crumbs to compress the siblings before hiding again the
  296. // crumbs. This is needed when the siblings expand to fill all the
  297. // available width, as in that case their old width would limit the
  298. // available width for the crumbs.
  299. // Note that the crumbs shown always overflow the parent width
  300. // (except, of course, when they all fit in).
  301. while (this.$el.find(this.hiddenCrumbSelector).length > 0
  302. && Math.round(this.getTotalWidth()) <= Math.round(this.$el.parent().width())) {
  303. this._showCrumb();
  304. }
  305. var siblingsWidth = 0;
  306. this.$el.prevAll(':visible').each(function () {
  307. siblingsWidth += $(this).outerWidth(true);
  308. });
  309. this.$el.nextAll(':visible').each(function () {
  310. siblingsWidth += $(this).outerWidth(true);
  311. });
  312. var availableWidth = this.$el.parent().width() - siblingsWidth;
  313. // If container is smaller than content
  314. // AND if there are crumbs left to hide
  315. while (Math.round(this.getTotalWidth()) > Math.round(availableWidth)
  316. && this.$el.find(this.crumbSelector).length > 0) {
  317. // As soon as one of the crumbs is hidden the menu will be
  318. // shown. This is needed for proper results in further width
  319. // checks.
  320. // Note that the menu is not shown only when all the crumbs were
  321. // being shown and they all fit the available space; if any of
  322. // the crumbs was not being shown then those shown would
  323. // overflow the available width, so at least one will be hidden
  324. // and thus the menu will be shown.
  325. this.$el.find('.crumbmenu').removeClass('hidden');
  326. this._hideCrumb();
  327. }
  328. this._updateMenu();
  329. }
  330. };
  331. OCA.Files.BreadCrumb = BreadCrumb;
  332. })();