diff options
| author | Ezio Melotti <ezio.melotti@gmail.com> | 2012-10-27 19:09:16 (GMT) |
|---|---|---|
| committer | Ezio Melotti <ezio.melotti@gmail.com> | 2012-10-27 19:09:16 (GMT) |
| commit | 380ce651e940c977058cace141c6ed8a45b7d82a (patch) | |
| tree | 04b16e259ed1b78ab0a92ea200f61c9534bc6cbd /Doc/tools/sphinxext/static/version_switch.js | |
| parent | 252cd0e4e0b11c7d13e24146617ad48e80feca43 (diff) | |
| download | cpython-380ce651e940c977058cace141c6ed8a45b7d82a.zip cpython-380ce651e940c977058cace141c6ed8a45b7d82a.tar.gz cpython-380ce651e940c977058cace141c6ed8a45b7d82a.tar.bz2 | |
#8040: add a version switcher to the documentation. Patch by Yury Selivanov.
Diffstat (limited to 'Doc/tools/sphinxext/static/version_switch.js')
| -rw-r--r-- | Doc/tools/sphinxext/static/version_switch.js | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/Doc/tools/sphinxext/static/version_switch.js b/Doc/tools/sphinxext/static/version_switch.js new file mode 100644 index 0000000..cc99153 --- /dev/null +++ b/Doc/tools/sphinxext/static/version_switch.js @@ -0,0 +1,60 @@ +(function() { + 'use strict'; + + var all_versions = { + '3.4': 'dev (3.4)', + '3.3': '3.3', + '3.2': '3.2', + '2.7': '2.7', + '2.6': '2.6' + }; + + function build_select(current_version, current_release) { + var buf = ['<select>']; + + $.each(all_versions, function(version, title) { + buf.push('<option value="' + version + '"'); + if (version == current_version) + buf.push(' selected="selected">' + current_release + '</option>'); + else + buf.push('>' + title + '</option>'); + }); + + buf.push('</select>'); + return buf.join(''); + } + + function patch_url(url, new_version) { + var url_re = /\.org\/(\d|py3k|dev|((release\/)?\d\.\d[\w\d\.]*))\//, + new_url = url.replace(url_re, '.org/' + new_version + '/'); + + if (new_url == url && !new_url.match(url_re)) { + // python 2 url without version? + new_url = url.replace(/\.org\//, '.org/' + new_version + '/'); + } + return new_url; + } + + 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) { + // check beforehand if url exists, else redirect to version's start page + $.get(new_url, function() { + window.location.href = new_url; + }).error(function() { + window.location.href = 'http://docs.python.org/' + selected; + }); + } + } + + $(document).ready(function() { + var select = build_select(DOCUMENTATION_OPTIONS.VERSION, + DOCUMENTATION_OPTIONS.RELEASE); + $('.version_switcher_placeholder').html(select); + $('.version_switcher_placeholder select').bind('change', on_switch); + }); +})(); |
