|
| 1 | +(function() { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + var all_versions = { |
| 5 | + '3.4': 'dev (3.4)', |
| 6 | + '3.3': '3.3', |
| 7 | + '3.2': '3.2', |
| 8 | + '2.7': '2.7', |
| 9 | + '2.6': '2.6' |
| 10 | + }; |
| 11 | + |
| 12 | + function build_select(current_version, current_release) { |
| 13 | + var buf = ['<select>']; |
| 14 | + |
| 15 | + $.each(all_versions, function(version, title) { |
| 16 | + buf.push('<option value="' + version + '"'); |
| 17 | + if (version == current_version) |
| 18 | + buf.push(' selected="selected">' + current_release + '</option>'); |
| 19 | + else |
| 20 | + buf.push('>' + title + '</option>'); |
| 21 | + }); |
| 22 | + |
| 23 | + buf.push('</select>'); |
| 24 | + return buf.join(''); |
| 25 | + } |
| 26 | + |
| 27 | + function patch_url(url, new_version) { |
| 28 | + var url_re = /\.org\/(\d|py3k|dev|((release\/)?\d\.\d[\w\d\.]*))\//, |
| 29 | + new_url = url.replace(url_re, '.org/' + new_version + '/'); |
| 30 | + |
| 31 | + if (new_url == url && !new_url.match(url_re)) { |
| 32 | + // python 2 url without version? |
| 33 | + new_url = url.replace(/\.org\//, '.org/' + new_version + '/'); |
| 34 | + } |
| 35 | + return new_url; |
| 36 | + } |
| 37 | + |
| 38 | + function on_switch() { |
| 39 | + var selected = $(this).children('option:selected').attr('value'); |
| 40 | + |
| 41 | + var url = window.location.href, |
| 42 | + new_url = patch_url(url, selected); |
| 43 | + |
| 44 | + if (new_url != url) { |
| 45 | + // check beforehand if url exists, else redirect to version's start page |
| 46 | + $.get(new_url, function() { |
| 47 | + window.location.href = new_url; |
| 48 | + }).error(function() { |
| 49 | + window.location.href = 'http://docs.python.org/' + selected; |
| 50 | + }); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + $(document).ready(function() { |
| 55 | + var select = build_select(DOCUMENTATION_OPTIONS.VERSION, |
| 56 | + DOCUMENTATION_OPTIONS.RELEASE); |
| 57 | + $('.version_switcher_placeholder').html(select); |
| 58 | + $('.version_switcher_placeholder select').bind('change', on_switch); |
| 59 | + }); |
| 60 | +})(); |
0 commit comments