ManagedReference.common.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. model[getTypePropertyName(model.type)] = true;
  20. break;
  21. case 'class':
  22. case 'interface':
  23. case 'struct':
  24. case 'delegate':
  25. case 'enum':
  26. model.isClass = true;
  27. if (model.children) groupChildren(model, classCategory);
  28. model[getTypePropertyName(model.type)] = true;
  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" || type === "attachedproperty") && c.syntax) {
  78. c.syntax.propertyValue = c.syntax.return;
  79. c.syntax.return = undefined;
  80. }
  81. // special handle for event
  82. if ((type === "event" || type === "attachedevent") && 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. "namespace": { inNamespace: true, typePropertyName: "inNamespace", id: "namespaces" },
  143. "class": { inClass: true, typePropertyName: "inClass", id: "classes" },
  144. "struct": { inStruct: true, typePropertyName: "inStruct", id: "structs" },
  145. "interface": { inInterface: true, typePropertyName: "inInterface", id: "interfaces" },
  146. "enum": { inEnum: true, typePropertyName: "inEnum", id: "enums" },
  147. "delegate": { inDelegate: true, typePropertyName: "inDelegate", id: "delegates" }
  148. };
  149. var classItems = {
  150. "constructor": { inConstructor: true, typePropertyName: "inConstructor", id: "constructors" },
  151. "field": { inField: true, typePropertyName: "inField", id: "fields" },
  152. "property": { inProperty: true, typePropertyName: "inProperty", id: "properties" },
  153. "attachedproperty": { inAttachedProperty: true, typePropertyName: "inAttachedProperty", id: "attachedProperties" },
  154. "method": { inMethod: true, typePropertyName: "inMethod", id: "methods" },
  155. "event": { inEvent: true, typePropertyName: "inEvent", id: "events" },
  156. "attachedevent": { inAttachedEvent: true, typePropertyName: "inAttachedEvent", id: "attachedEvents" },
  157. "operator": { inOperator: true, typePropertyName: "inOperator", id: "operators" },
  158. "eii": { inEii: true, typePropertyName: "inEii", id: "eii" }
  159. };
  160. if (category === 'class') {
  161. return classItems;
  162. }
  163. if (category === 'ns') {
  164. return namespaceItems;
  165. }
  166. console.err("category '" + category + "' is not valid.");
  167. return undefined;
  168. }
  169. function handleItem(vm, gitContribute, gitUrlPattern) {
  170. // get contribution information
  171. vm.docurl = common.getImproveTheDocHref(vm, gitContribute, gitUrlPattern);
  172. vm.sourceurl = common.getViewSourceHref(vm, null, gitUrlPattern);
  173. // set to null incase mustache looks up
  174. vm.summary = vm.summary || null;
  175. vm.remarks = vm.remarks || null;
  176. vm.conceptual = vm.conceptual || null;
  177. vm.syntax = vm.syntax || null;
  178. vm.implements = vm.implements || null;
  179. vm.example = vm.example || null;
  180. common.processSeeAlso(vm);
  181. // id is used as default template's bookmark
  182. vm.id = common.getHtmlId(vm.uid);
  183. if (vm.overload && vm.overload.uid) {
  184. vm.overload.id = common.getHtmlId(vm.overload.uid);
  185. }
  186. if (vm.supported_platforms) {
  187. vm.supported_platforms = transformDictionaryToArray(vm.supported_platforms);
  188. }
  189. if (vm.requirements) {
  190. var type = vm.type.toLowerCase();
  191. if (type == "method") {
  192. vm.requirements_method = transformDictionaryToArray(vm.requirements);
  193. } else {
  194. vm.requirements = transformDictionaryToArray(vm.requirements);
  195. }
  196. }
  197. if (vm && langs) {
  198. if (shouldHideTitleType(vm)) {
  199. vm.hideTitleType = true;
  200. } else {
  201. vm.hideTitleType = false;
  202. }
  203. if (shouldHideSubtitle(vm)) {
  204. vm.hideSubtitle = true;
  205. } else {
  206. vm.hideSubtitle = false;
  207. }
  208. }
  209. function shouldHideTitleType(vm) {
  210. var type = vm.type.toLowerCase();
  211. return ((type === 'namespace' && langs.length == 1 && (langs[0] === 'objectivec' || langs[0] === 'java' || langs[0] === 'c'))
  212. || ((type === 'class' || type === 'enum') && langs.length == 1 && langs[0] === 'c'));
  213. }
  214. function shouldHideSubtitle(vm) {
  215. var type = vm.type.toLowerCase();
  216. return (type === 'class' || type === 'namespace') && langs.length == 1 && langs[0] === 'c';
  217. }
  218. function transformDictionaryToArray(dic) {
  219. var array = [];
  220. for (var key in dic) {
  221. if (dic.hasOwnProperty(key)) {
  222. array.push({ "name": key, "value": dic[key] })
  223. }
  224. }
  225. return array;
  226. }
  227. }