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