|
| 1 | +import { Reflection, ContainerReflection } from '../../models/reflections/index'; |
| 2 | +import { ReflectionCategory } from '../../models/ReflectionCategory'; |
| 3 | +import { SourceDirectory } from '../../models/sources/directory'; |
| 4 | +import { Component, ConverterComponent } from '../components'; |
| 5 | +import { Converter } from '../converter'; |
| 6 | +import { Context } from '../context'; |
| 7 | +import { GroupPlugin } from './GroupPlugin'; |
| 8 | + |
| 9 | +/** |
| 10 | + * A handler that sorts and categorizes the found reflections in the resolving phase. |
| 11 | + * |
| 12 | + * The handler sets the ´category´ property of all reflections. |
| 13 | + */ |
| 14 | +@Component({name: 'category'}) |
| 15 | +export class CategoryPlugin extends ConverterComponent { |
| 16 | + /** |
| 17 | + * Define the sort order of categories. By default, sort alphabetically. |
| 18 | + */ |
| 19 | + static WEIGHTS = []; |
| 20 | + |
| 21 | + /** |
| 22 | + * Create a new CategoryPlugin instance. |
| 23 | + */ |
| 24 | + initialize() { |
| 25 | + this.listenTo(this.owner, { |
| 26 | + [Converter.EVENT_RESOLVE]: this.onResolve, |
| 27 | + [Converter.EVENT_RESOLVE_END]: this.onEndResolve |
| 28 | + }); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Triggered when the converter resolves a reflection. |
| 33 | + * |
| 34 | + * @param context The context object describing the current state the converter is in. |
| 35 | + * @param reflection The reflection that is currently resolved. |
| 36 | + */ |
| 37 | + private onResolve(context: Context, reflection: Reflection) { |
| 38 | + if (reflection instanceof ContainerReflection) { |
| 39 | + const container = <ContainerReflection> reflection; |
| 40 | + if (container.children && container.children.length > 0) { |
| 41 | + container.children.sort(GroupPlugin.sortCallback); |
| 42 | + container.categories = CategoryPlugin.getReflectionCategories(container.children); |
| 43 | + } |
| 44 | + if (container.categories && container.categories.length > 1) { |
| 45 | + container.categories.sort(CategoryPlugin.sortCatCallback); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Triggered when the converter has finished resolving a project. |
| 52 | + * |
| 53 | + * @param context The context object describing the current state the converter is in. |
| 54 | + */ |
| 55 | + private onEndResolve(context: Context) { |
| 56 | + function walkDirectory(directory: SourceDirectory) { |
| 57 | + directory.categories = CategoryPlugin.getReflectionCategories(directory.getAllReflections()); |
| 58 | + |
| 59 | + for (let key in directory.directories) { |
| 60 | + if (!directory.directories.hasOwnProperty(key)) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + walkDirectory(directory.directories[key]); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + const project = context.project; |
| 68 | + if (project.children && project.children.length > 0) { |
| 69 | + project.children.sort(GroupPlugin.sortCallback); |
| 70 | + project.categories = CategoryPlugin.getReflectionCategories(project.children); |
| 71 | + } |
| 72 | + if (project.categories && project.categories.length > 1) { |
| 73 | + project.categories.sort(CategoryPlugin.sortCatCallback); |
| 74 | + } |
| 75 | + |
| 76 | + walkDirectory(project.directory); |
| 77 | + project.files.forEach((file) => { |
| 78 | + file.categories = CategoryPlugin.getReflectionCategories(file.reflections); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Create a categorized representation of the given list of reflections. |
| 84 | + * |
| 85 | + * @param reflections The reflections that should be categorized. |
| 86 | + * @returns An array containing all children of the given reflection categorized |
| 87 | + */ |
| 88 | + static getReflectionCategories(reflections: Reflection[]): ReflectionCategory[] { |
| 89 | + const categories: ReflectionCategory[] = []; |
| 90 | + reflections.forEach((child) => { |
| 91 | + const childCat = CategoryPlugin.getCategory(child); |
| 92 | + if (childCat === '') { |
| 93 | + return; |
| 94 | + } |
| 95 | + for (let i = 0; i < categories.length; i++) { |
| 96 | + const category = categories[i]; |
| 97 | + |
| 98 | + if (category.title !== childCat) { |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + category.children.push(child); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + const category = new ReflectionCategory(childCat); |
| 107 | + category.children.push(child); |
| 108 | + categories.push(category); |
| 109 | + }); |
| 110 | + return categories; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Return the category of a given reflection. |
| 115 | + * |
| 116 | + * @param reflection The reflection. |
| 117 | + * @returns The category the reflection belongs to |
| 118 | + */ |
| 119 | + static getCategory(reflection: Reflection): string { |
| 120 | + if (reflection.comment) { |
| 121 | + const tags = reflection.comment.tags; |
| 122 | + if (tags) { |
| 123 | + for (let i = 0; i < tags.length; i++) { |
| 124 | + if (tags[i].tagName === 'category') { |
| 125 | + let tag = tags[i].text; |
| 126 | + return (tag.charAt(0).toUpperCase() + tag.slice(1).toLowerCase()).trim(); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + return ''; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Callback used to sort reflections by name. |
| 136 | + * |
| 137 | + * @param a The left reflection to sort. |
| 138 | + * @param b The right reflection to sort. |
| 139 | + * @returns The sorting weight. |
| 140 | + */ |
| 141 | + static sortCallback(a: Reflection, b: Reflection): number { |
| 142 | + return a.name > b.name ? 1 : -1; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Callback used to sort categories by name. |
| 147 | + * |
| 148 | + * @param a The left reflection to sort. |
| 149 | + * @param b The right reflection to sort. |
| 150 | + * @returns The sorting weight. |
| 151 | + */ |
| 152 | + static sortCatCallback(a: ReflectionCategory, b: ReflectionCategory): number { |
| 153 | + const aWeight = CategoryPlugin.WEIGHTS.indexOf(a.title); |
| 154 | + const bWeight = CategoryPlugin.WEIGHTS.indexOf(b.title); |
| 155 | + if (aWeight < 0 && bWeight < 0) { |
| 156 | + return a.title > b.title ? 1 : -1; |
| 157 | + } |
| 158 | + if (aWeight < 0) { |
| 159 | + return 1; |
| 160 | + } |
| 161 | + if (bWeight < 0) { |
| 162 | + return -1; |
| 163 | + } |
| 164 | + return aWeight - bWeight; |
| 165 | + } |
| 166 | +} |
0 commit comments