1 | import { IHtmlEngineHelper } from './html-engine-helper.interface';
|
2 | import * as Handlebars from 'handlebars';
|
3 |
|
4 | import DependenciesEngine from '../dependencies.engine';
|
5 | import AngularVersionUtil from '../../../utils/angular-version.util';
|
6 | import BasicTypeUtil from '../../../utils/basic-type.util';
|
7 | import Configuration from '../../configuration';
|
8 |
|
9 | export class FunctionSignatureHelper implements IHtmlEngineHelper {
|
10 | constructor() {}
|
11 |
|
12 | private handleFunction(arg): string {
|
13 | if (arg.function.length === 0) {
|
14 | return `${arg.name}${this.getOptionalString(arg)}: () => void`;
|
15 | }
|
16 |
|
17 | let argums = arg.function.map(argu => {
|
18 | let _result = DependenciesEngine.find(argu.type);
|
19 | if (_result) {
|
20 | if (_result.source === 'internal') {
|
21 | let path = _result.data.type;
|
22 | if (_result.data.type === 'class') {
|
23 | path = 'classe';
|
24 | }
|
25 | return `${argu.name}${this.getOptionalString(arg)}: <a href="../${path}s/${
|
26 | _result.data.name
|
27 | }.html">${argu.type}</a>`;
|
28 | } else {
|
29 | let path = AngularVersionUtil.getApiLink(
|
30 | _result.data,
|
31 | Configuration.mainData.angularVersion
|
32 | );
|
33 | return `${argu.name}${this.getOptionalString(
|
34 | arg
|
35 | )}: <a href="${path}" target="_blank">${argu.type}</a>`;
|
36 | }
|
37 | } else if (BasicTypeUtil.isKnownType(argu.type)) {
|
38 | let path = BasicTypeUtil.getTypeUrl(argu.type);
|
39 | return `${argu.name}${this.getOptionalString(
|
40 | arg
|
41 | )}: <a href="${path}" target="_blank">${argu.type}</a>`;
|
42 | } else {
|
43 | if (argu.name && argu.type) {
|
44 | return `${argu.name}${this.getOptionalString(arg)}: ${argu.type}`;
|
45 | } else {
|
46 | if (argu.name) {
|
47 | return `${argu.name.text}`;
|
48 | } else {
|
49 | return '';
|
50 | }
|
51 | }
|
52 | }
|
53 | });
|
54 | return `${arg.name}${this.getOptionalString(arg)}: (${argums}) => void`;
|
55 | }
|
56 |
|
57 | private getOptionalString(arg): string {
|
58 | return arg.optional ? '?' : '';
|
59 | }
|
60 |
|
61 | public helperFunc(context: any, method) {
|
62 | let args = '';
|
63 |
|
64 | let argDestructuredCounterInitial = 0;
|
65 | let argDestructuredCounterReal = 0;
|
66 |
|
67 | if (method.args) {
|
68 | method.args.forEach(arg => {
|
69 | if (arg.destructuredParameter) {
|
70 | argDestructuredCounterInitial += 1;
|
71 | }
|
72 | });
|
73 |
|
74 | method.args.forEach((arg, index) => {
|
75 | const _result = DependenciesEngine.find(arg.type);
|
76 | if (arg.destructuredParameter) {
|
77 | if (argDestructuredCounterReal === 0) {
|
78 | args += '__namedParameters: {';
|
79 | }
|
80 | argDestructuredCounterReal += 1;
|
81 | }
|
82 | if (_result) {
|
83 | if (_result.source === 'internal') {
|
84 | let path = _result.data.type;
|
85 | if (_result.data.type === 'class') {
|
86 | path = 'classe';
|
87 | }
|
88 | args += `${arg.name}${this.getOptionalString(arg)}: <a href="../${path}s/${
|
89 | _result.data.name
|
90 | }.html" target="_self">${Handlebars.escapeExpression(arg.type)}</a>`;
|
91 | } else {
|
92 | let path = AngularVersionUtil.getApiLink(
|
93 | _result.data,
|
94 | Configuration.mainData.angularVersion
|
95 | );
|
96 | args += `${arg.name}${this.getOptionalString(
|
97 | arg
|
98 | )}: <a href="${path}" target="_blank">${Handlebars.escapeExpression(
|
99 | arg.type
|
100 | )}</a>`;
|
101 | }
|
102 | } else if (arg.dotDotDotToken) {
|
103 | args += `...${arg.name}: ${arg.type}`;
|
104 | } else if (arg.function) {
|
105 | args += this.handleFunction(arg);
|
106 | } else if (BasicTypeUtil.isKnownType(arg.type)) {
|
107 | const path = BasicTypeUtil.getTypeUrl(arg.type);
|
108 | args += `${arg.name}${this.getOptionalString(
|
109 | arg
|
110 | )}: <a href="${path}" target="_blank">${Handlebars.escapeExpression(
|
111 | arg.type
|
112 | )}</a>`;
|
113 | } else {
|
114 | if (arg.type) {
|
115 | args += `${arg.name}${this.getOptionalString(arg)}: ${arg.type}`;
|
116 | } else {
|
117 | args += `${arg.name}${this.getOptionalString(arg)}`;
|
118 | }
|
119 | }
|
120 | if (arg.destructuredParameter) {
|
121 | if (argDestructuredCounterReal === argDestructuredCounterInitial) {
|
122 | args += '}';
|
123 | }
|
124 | }
|
125 | if (index < method.args.length - 1) {
|
126 | args += ', ';
|
127 | }
|
128 | });
|
129 | }
|
130 |
|
131 | if (method.name) {
|
132 | return `${method.name}(${args})`;
|
133 | } else {
|
134 | return `(${args})`;
|
135 | }
|
136 | }
|
137 | }
|