This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathDefaultAPISidebar.ejs
149 lines (120 loc) · 4.23 KB
/
DefaultAPISidebar.ejs
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
<%
// API Sidebar for overview pages
//
// Parameters:
// $0 Name of the API group in GroupData
// Variables and data
var slug = env.slug;
var locale = env.locale;
var APIHref = '/' + locale + '/docs/Web/API';
var output = "";
var group = $0;
var htmlEscape = kuma.htmlEscape;
var escapeQuotes = mdn.escapeQuotes;
// slug is not available in preview mode.
if (slug && group) {
var webAPIGroups = string.deserialize(await template("GroupData"));
var commonl10n = string.deserialize(await template('L10n:Common'));
var text = {
'Methods': mdn.getLocalString(commonl10n, 'Methods'),
'Properties': mdn.getLocalString(commonl10n, 'Properties'),
'Events': mdn.getLocalString(commonl10n, 'Events'),
'Interfaces': mdn.getLocalString(commonl10n, 'Interfaces'),
'Guides': mdn.getLocalString(commonl10n, 'Guides'),
'translate': mdn.getLocalString(commonl10n, '[Translate]'),
'title': mdn.getLocalString(commonl10n, 'TranslationCTA'),
};
if (webAPIGroups[0][group]) {
var interfaces = webAPIGroups[0][group].interfaces || [];
var methods = webAPIGroups[0][group].methods || [];
var properties = webAPIGroups[0][group].properties || [];
var events = webAPIGroups[0][group].events || [];
var groupGuides = webAPIGroups[0][group].guides || [];
var guides = [];
for (let guideUrl of groupGuides) {
const page = await wiki.getPage(guideUrl);
if (page && page.url) {
guides.push(page);
}
}
function buildSublist(pages, title) {
var result = '<li class="toggle"><details open><summary>' + title + '</summary><ol>';
for (var i in pages) {
var aPage = pages[i];
var summary = escapeQuotes(aPage.summary || '');
var url = aPage.url.replace('en-US', locale);
var title = htmlEscape(aPage.title);
var translated = false;
if (locale != 'en-US' && aPage.translations) {
aPage.translations.forEach(function(translation){
if(translation.locale === locale) {
summary = escapeQuotes(translation.summary) || '';
url = translation.url;
title = htmlEscape(translation.title);
translated = true;
}
});
}
var cta = '';
if (!translated && locale != 'en-US' && aPage.translations) {
cta += ' <a href="'+ url +'$translate" style="opacity:0.5" title="'+ text['title'] + '">' + text['translate'] + '</a>';
}
result += '<li>';
result += '<a href="' + url + '" title="' + summary + '">' + title + '</a>' + cta;
result += '</li>';
}
result += '</ol></details></li>';
return result;
}
function buildIFList(interfaces, title) {
var result = '<li class="toggle"><details open><summary>' + title + '</summary><ol>';
for (let interface of interfaces) {
result += '<li><a href="' + APIHref + '/' + interface.target + '">' + interface.text + '</a></li>';
}
result += '</ol></details></li>';
return result;
}
function convert(itemName) {
return {
text: `<code>${itemName}</code>`,
target: itemName.replace('()', '').replace('.', '/')
}
}
function convertEvent(eventName) {
const eventComponents = eventName.split(': ');
if (eventComponents.length !== 2) {
return null;
}
return {
text: `<code>${eventComponents[0]}</code>: <code>${eventComponents[1]}</code>`,
target: `${eventComponents[0]}/${eventComponents[1]}_event`
}
}
// output
output = '<section class="Quick_links" id="Quick_Links"><ol>';
if (webAPIGroups[0][group].overview) {
output += '<li><strong><a href="' + APIHref + '/' + webAPIGroups[0][group].overview[0].replace(/ /g, '_') + '">' + webAPIGroups[0][group].overview[0] + '</a></strong></li>';
}
if (guides.length > 0) {
output += buildSublist(guides, text['Guides']);
}
interfaces = interfaces.map(convert);
if (interfaces.length > 0) {
output += buildIFList(interfaces, text['Interfaces']);
}
properties = properties.map(convert);
if (properties.length > 0) {
output += buildIFList(properties, text['Properties']);
}
methods = methods.map(convert);
if (methods.length > 0) {
output += buildIFList(methods, text['Methods']);
}
events = events.sort().map(convertEvent).filter( e => e !== null );
if (events.length > 0) {
output += buildIFList(events, text['Events']);
}
output += '</ol></section>';
}}
%>
<%-output%>