files-breadcrumb.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. import Ember from 'ember';
  19. export default Ember.Component.extend({
  20. path: '',
  21. collapseAt: 4,
  22. tagName: 'ul',
  23. classNames: ['breadcrumb'],
  24. collapsingRequired: false,
  25. collapsedCrumbs: [],
  26. expandedCrumbs: [],
  27. crumbs: Ember.on('init', Ember.observer('path', function() {
  28. var path = this.get('path');
  29. var currentPath = path.split('/').filter((entry) => { return !Ember.isBlank(entry) });
  30. currentPath.unshift("/");
  31. var that = this;
  32. var shouldCollapse = function(scope, array, index) {
  33. return (((array.length - 1) >= scope.get('collapseAt')) && (index < array.length - 2));
  34. };
  35. var getCrumb = function(index, allCrumbs) {
  36. return {name: allCrumbs[index], path: "/" + allCrumbs.slice(1, index + 1).join('/'), last: false};
  37. };
  38. var collapsedCrumbs = currentPath.map(function(curr, i, array) {
  39. if(shouldCollapse(that, array, i)) {
  40. return getCrumb(i, array);
  41. } else {
  42. return {};
  43. }
  44. }).filterBy('name');
  45. var crumbs = currentPath.map(function(curr, i, array) {
  46. if(!shouldCollapse(that, array, i)) {
  47. return getCrumb(i, array);
  48. } else {
  49. return {};
  50. }
  51. }).filterBy('name');
  52. crumbs.set('lastObject.last', true);
  53. if (collapsedCrumbs.length > 0) {
  54. this.set('collapsingRequired', true);
  55. } else {
  56. this.set('collapsingRequired', false);
  57. }
  58. this.set('collapsedCrumbs', collapsedCrumbs.reverse());
  59. this.set('expandedCrumbs', crumbs);
  60. }))
  61. });