|
| 1 | +import Component from '@glimmer/component'; |
| 2 | +import sortBy from 'lodash.sortby'; |
| 3 | + |
| 4 | +/** |
| 5 | + * @typedef Args |
| 6 | + * @property {object} model |
| 7 | + * @property {object} filterData |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * @typedef Blocks |
| 12 | + * @property {[ApiIndexFilter['filteredData']]} default |
| 13 | + */ |
| 14 | + |
| 15 | +/** |
| 16 | + * @extends Component<{ Args: Args, Blocks: Blocks }> |
| 17 | + */ |
| 18 | +export default class ApiIndexFilter extends Component { |
| 19 | + get filteredMethods() { |
| 20 | + return this.filterItems('methods'); |
| 21 | + } |
| 22 | + |
| 23 | + get filteredEvents() { |
| 24 | + return this.filterItems('events'); |
| 25 | + } |
| 26 | + |
| 27 | + get filteredProperties() { |
| 28 | + return this.filterItems('properties'); |
| 29 | + } |
| 30 | + |
| 31 | + filterItems(itemType) { |
| 32 | + let items = |
| 33 | + this.args.model[itemType] === undefined |
| 34 | + ? [] |
| 35 | + : this.args.model[`${itemType}`]; |
| 36 | + if (!this.args.filterData.showInherited) { |
| 37 | + items = items.filter((item) => item.inherited !== true); |
| 38 | + } |
| 39 | + if (!this.args.filterData.showProtected) { |
| 40 | + items = items.filter((item) => item.access !== 'protected'); |
| 41 | + } |
| 42 | + if (!this.args.filterData.showPrivate) { |
| 43 | + items = items.filter((item) => item.access !== 'private'); |
| 44 | + } |
| 45 | + if (!this.args.filterData.showDeprecated) { |
| 46 | + items = items.filter((item) => item.deprecated !== true); |
| 47 | + } |
| 48 | + |
| 49 | + let sortedItems = sortBy(items, (item) => item.name); |
| 50 | + return this.filterMultipleInheritance(sortedItems); |
| 51 | + } |
| 52 | + |
| 53 | + get filteredData() { |
| 54 | + return { |
| 55 | + methods: this.filteredMethods, |
| 56 | + properties: this.filteredProperties, |
| 57 | + events: this.filteredEvents, |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Returns an array where duplicate methods (by name) are removed. |
| 63 | + * The docs for the nearest inheritance are typically more helpful to users, |
| 64 | + * so in cases of duplicates, "more local" is preferred. |
| 65 | + * Without this, multiple entries for some methods will show up. |
| 66 | + * @method filterMultipleInheritance |
| 67 | + */ |
| 68 | + filterMultipleInheritance(items) { |
| 69 | + let dedupedArray = []; |
| 70 | + for (let i = 0; i < items.length; i++) { |
| 71 | + let currentItem = items[i]; |
| 72 | + if (i === items.length - 1) { |
| 73 | + // if it's the last item, keep it |
| 74 | + dedupedArray.push(currentItem); |
| 75 | + } else { |
| 76 | + let nextItem = items[i + 1]; |
| 77 | + if (currentItem.name === nextItem.name) { |
| 78 | + // if the method would be listed twice, find the more local documentation |
| 79 | + let mostLocal = this.findMostLocal(currentItem, nextItem); |
| 80 | + dedupedArray.push(mostLocal); |
| 81 | + i += 1; // skip the next item with duplicate name |
| 82 | + } else { |
| 83 | + dedupedArray.push(currentItem); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + return dedupedArray; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Returns whichever item is most local. |
| 92 | + * What is "most local" is determined by looking at the file path for the |
| 93 | + * method, the file path for the class being viewed, and the parent if needed. |
| 94 | + * @method findMostLocal |
| 95 | + */ |
| 96 | + findMostLocal(currentItem, nextItem) { |
| 97 | + let currentScope = this.args.model.file; |
| 98 | + let parentClassScope = this.args.model.parentClass.get('file'); |
| 99 | + if (currentScope === currentItem.file) { |
| 100 | + // if the item belongs to the class, keep it |
| 101 | + return currentItem; |
| 102 | + } else if (parentClassScope === currentItem.file) { |
| 103 | + // or if the item belongs to the parent class, keep it |
| 104 | + return currentItem; |
| 105 | + } else { |
| 106 | + // otherwise, the next item must be "more local" |
| 107 | + return nextItem; |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
1 | 112 | <div class="api-index-filter"> |
2 | 113 | {{yield this.filteredData}} |
3 | 114 | </div> |
0 commit comments