|
| 1 | +import Component from '@ember/component'; |
| 2 | +import { inject as service } from '@ember/service'; |
| 3 | +import { computed } from '@ember/object'; |
| 4 | +import layout from './template'; |
| 5 | +import { readOnly } from '@ember/object/computed'; |
| 6 | +import { set as _set } from 'lodash'; |
| 7 | + |
| 8 | +export default Component.extend({ |
| 9 | + layout, |
| 10 | + tagName: '', |
| 11 | + store: service(), |
| 12 | + |
| 13 | + sections: readOnly('project.navigationIndex'), |
| 14 | + |
| 15 | + /* |
| 16 | + Autogenerated sections include "resolved types", by which we mean things like |
| 17 | + Components and Helpers, as well as generic "modules", which is any other |
| 18 | + public JavaScript export from this library. |
| 19 | +
|
| 20 | + These are the sections for the resolved types. |
| 21 | + */ |
| 22 | + resolvedTypeSections: computed(function() { |
| 23 | + return this.get('sections').filter(section => section.type !== 'modules'); |
| 24 | + }), |
| 25 | + |
| 26 | + /* |
| 27 | + Autogenerated sections include "resolved types", by which we mean things like |
| 28 | + Components and Helpers, as well as generic "modules", which is any other |
| 29 | + public JavaScript export from this library. |
| 30 | +
|
| 31 | + This is the index of nodes for generic modules. We transform the raw array |
| 32 | + of modules that look like this |
| 33 | +
|
| 34 | + ``` |
| 35 | + [ |
| 36 | + {id: "ember-cli-addon-docs/keyboard-config", path: "modules/ember-cli-addon-docs/keyboard-config", name: "ember-cli-addon-docs/keyboard-config"} |
| 37 | + {id: "ember-cli-addon-docs/router", path: "modules/ember-cli-addon-docs/router", name: "ember-cli-addon-docs/router"} |
| 38 | + {id: "ember-cli-addon-docs/utils/compile-markdown", path: "modules/ember-cli-addon-docs/utils/compile-markdown", name: "ember-cli-addon-docs/utils/compile-markdown"} |
| 39 | + ] |
| 40 | + ``` |
| 41 | +
|
| 42 | + into a nested data structure resembling the filesystem: |
| 43 | +
|
| 44 | + ``` |
| 45 | + { |
| 46 | + name: '@ember-cli-addon-docs', |
| 47 | + children: [ |
| 48 | + { name: 'keyboard-config', path: "modules/ember-cli-addon-docs/keyboard-config" }, |
| 49 | + { name: 'router', children: [], path: "modules/ember-cli-addon-docs/router" }, |
| 50 | + { |
| 51 | + name: 'utils', |
| 52 | + children: [ |
| 53 | + { name: 'compile-markdown', children: [], path: "modules/ember-cli-addon-docs/utils/compile-markdown" }, |
| 54 | + ] |
| 55 | + }, |
| 56 | + ] |
| 57 | + }; |
| 58 | + ``` |
| 59 | + */ |
| 60 | + moduleIndex: computed(function() { |
| 61 | + let modules = this.get('sections').filter(section => section.type === 'modules')[0].items; |
| 62 | + |
| 63 | + /* |
| 64 | + Intermediate data structure: |
| 65 | +
|
| 66 | + ``` |
| 67 | + { |
| 68 | + '@ember-cli-addon-docs': { |
| 69 | + 'keyboard-config': {}, |
| 70 | + 'router': {}, |
| 71 | + 'utils': { |
| 72 | + 'compile-markdown': {} |
| 73 | + } |
| 74 | + } |
| 75 | + }; |
| 76 | + ``` |
| 77 | + */ |
| 78 | + let index = {}; |
| 79 | + modules.forEach(module => { |
| 80 | + let parts = module.id.split('/'); |
| 81 | + _set(index, parts, {}); |
| 82 | + }); |
| 83 | + |
| 84 | + let transform = (obj, id) => Object.keys(obj) |
| 85 | + .map(key => { |
| 86 | + let node = { |
| 87 | + name: key |
| 88 | + }; |
| 89 | + let children = transform(obj[key], id ? `${id}/${key}` : key); |
| 90 | + if (children.length) { |
| 91 | + node.children = children; |
| 92 | + } else { |
| 93 | + node.id = `${id}/${key}`; |
| 94 | + } |
| 95 | + |
| 96 | + return node; |
| 97 | + }); |
| 98 | + |
| 99 | + return transform(index)[0]; |
| 100 | + }) |
| 101 | + |
| 102 | +}); |
0 commit comments