-
Notifications
You must be signed in to change notification settings - Fork 3
/
version-select.js
73 lines (61 loc) · 1.92 KB
/
version-select.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
(function() {
'use strict';
var url_re = /docs\.bsframework\.io\/(nightly|latest|(v\d\.\d+))\//;
var all_versions = {
'nightly': 'git-master',
'latest': 'Latest',
'v1.0': 'v1.0',
'v1.1': 'v1.1',
};
var current_version = 'v1.1';
function build_select(current_version, current_release) {
var buf = ['<select class="version-select">'];
$.each(all_versions, function(version, title) {
buf.push('<option value="' + version + '"');
if (version == current_version) {
buf.push(' selected="selected">');
if(version == 'latest') {
buf.push(title + ' (' + current_release + ')')
} else {
buf.push(title);
}
} else {
buf.push('>' + title);
}
buf.push('</option>');
});
buf.push('</select>');
return buf.join('');
}
function patch_url(url, new_version) {
return url.replace(url_re, 'docs.bsframework.io/' + new_version + '/');
}
function on_switch() {
var selected = $(this).children('option:selected').attr('value');
var url = window.location.href,
new_url = patch_url(url, selected);
if (new_url != url) {
$.ajax({
url: new_url,
success: function() {
window.location.href = new_url;
},
error: function() {
window.location.href = 'http://docs.bsframework.io/' + selected;
}
});
}
}
$(document).ready(function() {
var match = url_re.exec(window.location.href);
if (match) {
var release = current_version;
var version = match[1];
var select = build_select(version, release);
if(version[0] != 'v' || version == release)
$('.outdated').hide();
$('.version-select').html(select);
$('.version-select select').bind('change', on_switch);
}
});
})();