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