forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocApi.vue
More file actions
358 lines (284 loc) · 8.42 KB
/
DocApi.vue
File metadata and controls
358 lines (284 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<template lang="pug">
q-card.doc-api.q-my-lg(flat bordered)
q-toolbar.text-grey-8
card-title(:title="nameBanner" prefix="api--")
q-btn.q-mr-sm(v-if="pageLink" size="sm" padding="xs sm" color="brand-primary" no-caps unelevated :to="docPath")
q-icon(name="launch")
.q-ml-xs Docs
q-input.col(
ref="inputRef"
v-model="filter"
dense
input-class="text-right"
borderless
placeholder="Filter..."
style="min-width: 6em"
)
template(v-slot:append)
q-icon.cursor-pointer(
:name="inputIcon"
@click="onFilterClick"
)
q-linear-progress(v-if="loading", color="brand-primary", indeterminate)
template(v-else-if="nothingToShow")
q-separator
.doc-api__nothing-to-show Nothing to display
template(v-else)
q-separator
q-tabs.bg-grey-2.text-grey-7(v-model="currentTab", active-color="brand-primary", indicator-color="brand-primary", align="left", :breakpoint="0", dense)
q-tab(
v-for="tab in tabsList"
:key="`api-tab-${tab}`"
:name="tab"
)
.row.no-wrap.items-center
span.q-mr-xs.text-capitalize.text-weight-medium {{ tab }}
q-badge(v-if="filteredApiCount[tab].overall" :label="filteredApiCount[tab].overall")
q-separator
q-tab-panels(v-model="currentTab", animated)
q-tab-panel.q-pa-none(v-for="tab in tabsList", :name="tab", :key="tab")
.row.no-wrap.api-container(v-if="innerTabsList[tab].length !== 1")
.col-auto.row.no-wrap.text-grey-7.q-py-sm
q-tabs(
v-model="currentInnerTab"
active-color="brand-primary"
indicator-color="brand-primary"
:breakpoint="0"
vertical
dense
shrink
)
q-tab(
v-for="innerTab in innerTabsList[tab]"
:key="`api-inner-tab-${innerTab}`"
class="inner-tab"
:name="innerTab"
)
.row.no-wrap.items-center.self-stretch
span.q-mr-xs.text-capitalize.text-weight-medium {{ innerTab }}
.col
q-badge(v-if="filteredApiCount[tab].category[innerTab]" :label="filteredApiCount[tab].category[innerTab]")
q-separator(vertical)
q-tab-panels.col(
v-model="currentInnerTab"
animated
transition-prev="slide-down"
transition-next="slide-up"
)
q-tab-panel.q-pa-none(v-for="innerTab in innerTabsList[tab]" :name="innerTab" :key="innerTab")
DocApiEntry(:type="tab" :definition="filteredApi[tab][innerTab]")
.api-container(v-else)
DocApiEntry(:type="tab" :definition="filteredApi[tab][defaultInnerTabName]")
</template>
<script>
import { ref, computed, watch, onMounted } from 'vue'
import { mdiClose, mdiMagnify } from '@quasar/extras/mdi-v6'
import CardTitle from './CardTitle.vue'
import DocApiEntry from './DocApiEntry.js'
const defaultInnerTabName = '__default'
function getPropsCategories (props) {
const acc = new Set()
for (const key in props) {
if (props[ key ] !== void 0) {
const value = props[ key ]
value.category.split('|').forEach(groupKey => {
acc.add(groupKey)
})
}
}
return acc.size === 1
? [defaultInnerTabName]
: Array.from(acc).sort()
}
function getInnerTabs (api, tabs, apiType) {
const acc = {}
tabs.forEach(tab => {
acc[ tab ] = apiType === 'component' && tab === 'props'
? getPropsCategories(api[ tab ])
: [defaultInnerTabName]
})
return acc
}
function parseApi (api, tabs, innerTabs) {
const acc = {}
tabs.forEach(tab => {
const apiValue = api[ tab ]
if (innerTabs[ tab ].length > 1) {
const inner = {}
innerTabs[ tab ].forEach(subTab => {
inner[ subTab ] = {}
})
for (const key in apiValue) {
if (apiValue[ key ] !== void 0) {
const value = apiValue[ key ]
value.category.split('|').forEach(groupKey => {
inner[ groupKey ][ key ] = value
})
}
}
acc[ tab ] = inner
}
else {
acc[ tab ] = {}
acc[ tab ][ defaultInnerTabName ] = apiValue
}
})
return acc
}
function passesFilter (filter, name, desc) {
return (name.toLowerCase().indexOf(filter) > -1) ||
(desc !== void 0 && desc.toLowerCase().indexOf(filter) > -1)
}
function getFilteredApi (parsedApi, filter, tabs, innerTabs) {
if (filter === '') {
return parsedApi
}
const acc = {}
tabs.forEach(tab => {
const tabApi = parsedApi[ tab ]
const tabCategories = innerTabs[ tab ]
acc[ tab ] = {}
tabCategories.forEach(categ => {
const subTabs = {}
const categoryEntries = tabApi[ categ ]
for (const name in categoryEntries) {
const entry = categoryEntries[ name ]
if (passesFilter(filter, name, entry.desc) === true) {
subTabs[ name ] = entry
}
}
acc[ tab ][ categ ] = subTabs
})
})
return acc
}
function getApiCount (parsedApi, tabs, innerTabs) {
const acc = {}
tabs.forEach(tab => {
const tabApi = parsedApi[ tab ]
const tabCategories = innerTabs[ tab ]
if ([ 'value', 'arg', 'quasarConfOptions', 'injection' ].includes(tab)) {
acc[ tab ] = {
overall: Object.keys(tabApi[ tabCategories[ 0 ] ]).length === 0
? 0
: 1
}
return
}
acc[ tab ] = { overall: 0 }
if (tabCategories.length === 1) {
const categ = tabCategories[ 0 ]
const count = Object.keys(tabApi[ categ ]).length
acc[ tab ] = {
overall: count,
category: { [ categ ]: count }
}
}
else {
acc[ tab ].category = {}
tabCategories.forEach(categ => {
const count = Object.keys(tabApi[ categ ]).length
acc[ tab ].category[ categ ] = count
acc[ tab ].overall += count
})
}
})
return acc
}
export default {
name: 'DocApi',
components: {
CardTitle,
DocApiEntry
},
props: {
file: {
type: String,
required: true
},
pageLink: Boolean
},
setup (props) {
const inputRef = ref(null)
const loading = ref(true)
const nameBanner = ref('Loading API...')
const nothingToShow = ref(false)
const docPath = ref('')
const filter = ref('')
const apiDef = ref({})
const tabsList = ref([])
const innerTabsList = ref({})
const currentTab = ref(null)
const currentInnerTab = ref(null)
watch(currentTab, val => {
currentInnerTab.value = innerTabsList.value[ val ][ 0 ]
})
const inputIcon = computed(() => filter.value !== '' ? mdiClose : mdiMagnify)
const filteredApi = computed(() => getFilteredApi(apiDef.value, filter.value.toLowerCase(), tabsList.value, innerTabsList.value))
const filteredApiCount = computed(() => getApiCount(filteredApi.value, tabsList.value, innerTabsList.value))
function parseApiFile (name, { type, behavior, meta, addedIn, ...api }) {
nameBanner.value = name
docPath.value = meta.docsUrl.replace(/^https:\/\/v[\d]+\.quasar\.dev/, '')
const tabs = Object.keys(api)
if (tabs.length === 0) {
nothingToShow.value = true
return
}
tabsList.value = tabs
currentTab.value = tabs[ 0 ]
const subTabs = getInnerTabs(api, tabs, type)
innerTabsList.value = subTabs
apiDef.value = parseApi(api, tabs, subTabs)
}
function onFilterClick () {
if (filter.value !== '') {
filter.value = ''
}
inputRef.value.focus()
}
onMounted(() => {
import(
/* webpackChunkName: "quasar-api" */
/* webpackMode: "lazy-once" */
'quasar/dist/api/' + props.file + '.json'
).then(json => {
parseApiFile(props.file, json.default)
loading.value = false
})
})
return {
loading,
nameBanner,
nothingToShow,
docPath,
filteredApi,
filteredApiCount,
tabsList,
innerTabsList,
defaultInnerTabName,
currentTab,
currentInnerTab,
inputRef,
filter,
inputIcon,
onFilterClick
}
}
}
</script>
<style lang="sass">
.doc-api
.q-tab
height: 40px
.inner-tab
justify-content: left
.q-tab__content
width: 100%
.api-container
max-height: 600px
&__nothing-to-show
padding: 16px
color: $grey
font-size: .8em
font-style: italic
</style>