UNPKG

8.5 kBJavaScriptView Raw
1// Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license. See LICENSE file in the project root for full license information.
2var common = require('./common.js');
3
4exports.transform = function (model) {
5 var namespaceItems = {
6 "class": { inClass: true, typePropertyName: "inClass", id: "classes" },
7 "struct": { inStruct: true, typePropertyName: "inStruct", id: "structs" },
8 "interface": { inInterface: true, typePropertyName: "inInterface", id: "interfaces" },
9 "enum": { inEnum: true, typePropertyName: "inEnum", id: "enums" },
10 "delegate": { inDelegate: true, typePropertyName: "inDelegate", id: "delegates" }
11 };
12 var classItems = {
13 "constructor": { inConstructor: true, typePropertyName: "inConstructor", id: "constructors" },
14 "field": { inField: true, typePropertyName: "inField", id: "fields" },
15 "property": { inProperty: true, typePropertyName: "inProperty", id: "properties" },
16 "method": { inMethod: true, typePropertyName: "inMethod", id: "methods" },
17 "event": { inEvent: true, typePropertyName: "inEvent", id: "events" },
18 "operator": { inOperator: true, typePropertyName: "inOperator", id: "operators" },
19 "eii": { inEii: true, typePropertyName: "inEii", id: "eii" }
20 };
21
22 if (!model) return null;
23
24 langs = model.langs;
25 handleItem(model, model._gitContribute, model._gitUrlPattern);
26 if (model.children) {
27 model.children.forEach(function (item) { handleItem(item, model._gitContribute, model._gitUrlPattern); });
28 }
29
30 if (model.type) {
31 switch (model.type.toLowerCase()) {
32 case 'namespace':
33 model.isNamespace = true;
34 if (model.children) groupChildren(model, namespaceItems);
35 break;
36 case 'class':
37 case 'interface':
38 case 'struct':
39 case 'delegate':
40 case 'enum':
41 model.isClass = true;
42 if (model.children) groupChildren(model, classItems);
43 model[namespaceItems[model.type.toLowerCase()].typePropertyName] = true;
44 handleNamespace(model);
45 break;
46 default:
47 break;
48 }
49 }
50
51 return model;
52}
53
54exports.getBookmarks = function (model) {
55 if (!model || !model.type || model.type.toLowerCase() === "namespace") return null;
56
57 var bookmarks = {};
58 // Reference's first level bookmark should have no anchor
59 bookmarks[model.uid] = "";
60
61 if (model.children) {
62 model.children.forEach(function (item) {
63 bookmarks[item.uid] = common.getHtmlId(item.uid);
64 if (item.overload && item.overload.uid) {
65 bookmarks[item.overload.uid] = common.getHtmlId(item.overload.uid);
66 }
67 });
68 }
69
70 return bookmarks;
71}
72
73function groupChildren(model, typeChildrenItems) {
74 var grouped = {};
75
76 model.children.forEach(function (c) {
77 if (c.isEii) {
78 var type = "eii";
79 } else {
80 var type = c.type.toLowerCase();
81 }
82 if (!grouped.hasOwnProperty(type)) {
83 grouped[type] = [];
84 }
85 // special handle for field
86 if (type === "field" && c.syntax) {
87 c.syntax.fieldValue = c.syntax.return;
88 c.syntax.return = undefined;
89 }
90 // special handle for property
91 if (type === "property" && c.syntax) {
92 c.syntax.propertyValue = c.syntax.return;
93 c.syntax.return = undefined;
94 }
95 // special handle for event
96 if (type === "event" && c.syntax) {
97 c.syntax.eventType = c.syntax.return;
98 c.syntax.return = undefined;
99 }
100 grouped[type].push(c);
101 })
102 var children = [];
103 for (var key in typeChildrenItems) {
104 if (typeChildrenItems.hasOwnProperty(key) && grouped.hasOwnProperty(key)) {
105 var typeChildrenItem = typeChildrenItems[key];
106 var items = typeChildrenItem.children = grouped[key];
107 if (items && items.length > 0) {
108 children.push(typeChildrenItem);
109 }
110 }
111 }
112
113 model.children = children;
114}
115
116// reserve "namespace" of string for backward compatibility
117// will replace "namespace" with "namespaceExpanded" of object
118function handleNamespace(model) {
119 model.namespaceExpanded = model.namespace;
120 if (model.namespaceExpanded) {
121 model.namespace = model.namespaceExpanded.uid;
122 }
123}
124
125function handleItem(vm, gitContribute, gitUrlPattern) {
126 // get contribution information
127 vm.docurl = common.getImproveTheDocHref(vm, gitContribute, gitUrlPattern);
128 vm.sourceurl = common.getViewSourceHref(vm, null, gitUrlPattern);
129
130 // set to null incase mustache looks up
131 vm.summary = vm.summary || null;
132 vm.remarks = vm.remarks || null;
133 vm.conceptual = vm.conceptual || null;
134 vm.syntax = vm.syntax || null;
135 vm.implements = vm.implements || null;
136 common.processSeeAlso(vm);
137
138 // id is used as default template's bookmark
139 vm.id = common.getHtmlId(vm.uid);
140 if (vm.overload && vm.overload.uid) {
141 vm.overload.id = common.getHtmlId(vm.overload.uid);
142 }
143
144 // change type in syntax from array to string
145 if (vm.syntax) {
146 var syntax = vm.syntax;
147 if (syntax.parameters) {
148 syntax.parameters = syntax.parameters.map(function (p) {
149 return joinType(p);
150 })
151 syntax.parameters = groupParameters(syntax.parameters);
152 }
153 if (syntax.return) {
154 syntax.return = joinType(syntax.return);
155 }
156 }
157
158 if (vm.supported_platforms) {
159 vm.supported_platforms = transformDictionaryToArray(vm.supported_platforms);
160 }
161
162 if (vm.requirements) {
163 var type = vm.type.toLowerCase();
164 if (type == "method") {
165 vm.requirements_method = transformDictionaryToArray(vm.requirements);
166 } else {
167 vm.requirements = transformDictionaryToArray(vm.requirements);
168 }
169 }
170
171 if (vm && langs) {
172 if (shouldHideTitleType(vm)) {
173 vm.hideTitleType = true;
174 } else {
175 vm.hideTitleType = false;
176 }
177
178 if (shouldHideSubtitle(vm)) {
179 vm.hideSubtitle = true;
180 } else {
181 vm.hideSubtitle = false;
182 }
183 }
184
185 function shouldHideTitleType(vm) {
186 var type = vm.type.toLowerCase();
187 return ((type === 'namespace' && langs.length == 1 && (langs[0] === 'objectivec' || langs[0] === 'java' || langs[0] === 'c'))
188 || ((type === 'class' || type === 'enum') && langs.length == 1 && langs[0] === 'c'));
189 }
190
191 function shouldHideSubtitle(vm) {
192 var type = vm.type.toLowerCase();
193 return (type === 'class' || type === 'namespace') && langs.length == 1 && langs[0] === 'c';
194 }
195
196 function transformDictionaryToArray(dic) {
197 var array = [];
198 for (var key in dic) {
199 if (dic.hasOwnProperty(key)) {
200 array.push({ "name": key, "value": dic[key] })
201 }
202 }
203
204 return array;
205 }
206
207 function joinType(parameter) {
208 var joinTypeProperty = function (type, key) {
209 if (!type || !type[0] || !type[0][key]) return null;
210 var value = type.map(function (t) {
211 return t[key][0].value;
212 }).join(' | ');
213 return [{
214 lang: type[0][key][0].lang,
215 value: value
216 }];
217 };
218 if (parameter.type) {
219 parameter.type = {
220 name: joinTypeProperty(parameter.type, "name"),
221 nameWithType: joinTypeProperty(parameter.type, "nameWithType"),
222 fullName: joinTypeProperty(parameter.type, "fullName"),
223 specName: joinTypeProperty(parameter.type, "specName")
224 }
225 }
226 return parameter;
227 }
228
229 function groupParameters(parameters) {
230 if (!parameters || parameters.length == 0) return parameters;
231 var groupedParameters = [];
232 var stack = [];
233 for (var i = 0; i < parameters.length; i++) {
234 var parameter = parameters[i];
235 parameter.properties = null;
236 var prefixLength = 0;
237 while (stack.length > 0) {
238 var top = stack.pop();
239 var prefix = top.id + '.';
240 if (parameter.id.indexOf(prefix) == 0) {
241 prefixLength = prefix.length;
242 if (!top.parameter.properties) {
243 top.parameter.properties = [];
244 }
245 top.parameter.properties.push(parameter);
246 stack.push(top);
247 break;
248 }
249 if (stack.length == 0) {
250 groupedParameters.push(top.parameter);
251 }
252 }
253 stack.push({ id: parameter.id, parameter: parameter });
254 parameter.id = parameter.id.substring(prefixLength);
255 }
256 while (stack.length > 0) {
257 top = stack.pop();
258 }
259 groupedParameters.push(top.parameter);
260 return groupedParameters;
261 }
262}
\No newline at end of file