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