You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

264 rivejä
7.9 KiB

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