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.

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