-
Notifications
You must be signed in to change notification settings - Fork 3
/
switchers.js
226 lines (203 loc) · 6.54 KB
/
switchers.js
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
(function () {
"use strict";
if (!String.prototype.startsWith) {
Object.defineProperty(String.prototype, "startsWith", {
value: function (search, rawPos) {
var pos = rawPos > 0 ? rawPos | 0 : 0;
return this.substring(pos, pos + search.length) === search;
},
});
}
// Parses versions in URL segments like:
// "3", "dev", "release/2.7" or "3.6rc2"
var version_regexs = [
"(?:\\d)",
"(?:\\d\\.\\d[\\w\\d\\.]*)",
"(?:dev)",
"(?:release/\\d.\\d[\\x\\d\\.]*)",
];
var all_versions = {
3.11: "dev (3.11)",
"3.10": "pre (3.10)",
3.9: "3.9",
3.8: "3.8",
3.7: "3.7",
3.6: "3.6",
3.5: "3.5",
2.7: "2.7",
};
var all_languages = {
en: "English",
es: "Spanish",
fr: "French",
ja: "Japanese",
ko: "Korean",
"pt-br": "Brazilian Portuguese",
"zh-cn": "Simplified Chinese",
"zh-tw": "Traditional Chinese",
};
function quote_attr(str) {
return '"' + str.replace('"', '\\"') + '"';
}
function build_version_select(release) {
var buf = ['<select id="version_select">'];
var major_minor = release.split(".").slice(0, 2).join(".");
$.each(all_versions, function (version, title) {
if (version == major_minor)
buf.push(
"<option value=" +
quote_attr(version) +
' selected="selected">' +
release +
"</option>"
);
else
buf.push(
"<option value=" + quote_attr(version) + ">" + title + "</option>"
);
});
buf.push("</select>");
return buf.join("");
}
function build_language_select(current_language) {
var buf = ['<select id="language_select">'];
$.each(all_languages, function (language, title) {
if (language == current_language)
buf.push(
'<option value="' +
language +
'" selected="selected">' +
all_languages[current_language] +
"</option>"
);
else buf.push('<option value="' + language + '">' + title + "</option>");
});
if (!(current_language in all_languages)) {
// In case we're browsing a language that is not yet in all_languages.
buf.push(
'<option value="' +
current_language +
'" selected="selected">' +
current_language +
"</option>"
);
all_languages[current_language] = current_language;
}
buf.push("</select>");
return buf.join("");
}
function navigate_to_first_existing(urls) {
// Navigate to the first existing URL in urls.
var url = urls.shift();
if (urls.length == 0 || url.startsWith("file:///")) {
window.location.href = url;
return;
}
$.ajax({
url: url,
success: function () {
window.location.href = url;
},
error: function () {
navigate_to_first_existing(urls);
},
});
}
function on_version_switch() {
var selected_version =
$(this).children("option:selected").attr("value") + "/";
var url = window.location.href;
var current_language = language_segment_from_url();
var current_version = version_segment_from_url();
var new_url = url.replace(
"/" + current_language + current_version,
"/" + current_language + selected_version
);
if (new_url != url) {
navigate_to_first_existing([
new_url,
url.replace(
"/" + current_language + current_version,
"/" + selected_version
),
"/" + current_language + selected_version,
"/" + selected_version,
"/",
]);
}
}
function on_language_switch() {
var selected_language =
$(this).children("option:selected").attr("value") + "/";
var url = window.location.href;
var current_language = language_segment_from_url();
var current_version = version_segment_from_url();
if (selected_language == "en/")
// Special 'default' case for english.
selected_language = "";
var new_url = url.replace(
"/" + current_language + current_version,
"/" + selected_language + current_version
);
if (new_url != url) {
navigate_to_first_existing([new_url, "/"]);
}
}
// Returns the path segment of the language as a string, like 'fr/'
// or '' if not found.
function language_segment_from_url() {
var path = window.location.pathname;
var language_regexp =
"/((?:" + Object.keys(all_languages).join("|") + ")/)";
var match = path.match(language_regexp);
if (match !== null) return match[1];
return "";
}
// Returns the path segment of the version as a string, like '3.6/'
// or '' if not found.
function version_segment_from_url() {
var path = window.location.pathname;
var language_segment = language_segment_from_url();
var version_segment = "(?:(?:" + version_regexs.join("|") + ")/)";
var version_regexp = language_segment + "(" + version_segment + ")";
var match = path.match(version_regexp);
if (match !== null) return match[1];
return "";
}
function create_placeholders_if_missing() {
var version_segment = version_segment_from_url();
var language_segment = language_segment_from_url();
var index = "/" + language_segment + version_segment;
if ($(".version_switcher_placeholder").length) return;
var html =
'<span class="language_switcher_placeholder"></span> \
<span class="version_switcher_placeholder"></span> \
<a href="/" id="indexlink">Documentation</a> »';
var probable_places = [
"body>div.related>ul>li:not(.right):contains('Documentation'):first",
"body>div.related>ul>li:not(.right):contains('documentation'):first",
];
for (var i = 0; i < probable_places.length; i++) {
var probable_place = $(probable_places[i]);
if (probable_place.length == 1) {
probable_place.html(html);
document.getElementById("indexlink").href = index;
return;
}
}
}
$(document).ready(function () {
var language_segment = language_segment_from_url();
var current_language = language_segment.replace(/\/+$/g, "") || "en";
var version_select = build_version_select(DOCUMENTATION_OPTIONS.VERSION);
create_placeholders_if_missing();
$(".version_switcher_placeholder").html(version_select);
$(".version_switcher_placeholder select").bind("change", on_version_switch);
var language_select = build_language_select(current_language);
$(".language_switcher_placeholder").html(language_select);
$(".language_switcher_placeholder select").bind(
"change",
on_language_switch
);
});
})();