Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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