1(function() { 2 'use strict'; 3 4 // Parses versions in URL segments like: 5 // "3", "dev", "release/2.7" or "3.6rc2" 6 var version_regexs = [ 7 '(?:\\d)', 8 '(?:\\d\\.\\d[\\w\\d\\.]*)', 9 '(?:dev)', 10 '(?:release/\\d.\\d[\\x\\d\\.]*)']; 11 12 var all_versions = { 13 '3.8': 'dev (3.8)', 14 '3.7': '3.7', 15 '3.6': '3.6', 16 '3.5': '3.5', 17 '2.7': '2.7', 18 }; 19 20 var all_languages = { 21 'en': 'English', 22 'fr': 'French', 23 'ja': 'Japanese', 24 }; 25 26 function build_version_select(current_version, current_release) { 27 var buf = ['<select>']; 28 29 $.each(all_versions, function(version, title) { 30 buf.push('<option value="' + version + '"'); 31 if (version == current_version) 32 buf.push(' selected="selected">' + current_release + '</option>'); 33 else 34 buf.push('>' + title + '</option>'); 35 }); 36 37 buf.push('</select>'); 38 return buf.join(''); 39 } 40 41 function build_language_select(current_language) { 42 var buf = ['<select>']; 43 44 $.each(all_languages, function(language, title) { 45 if (language == current_language) 46 buf.push('<option value="' + language + '" selected="selected">' + 47 all_languages[current_language] + '</option>'); 48 else 49 buf.push('<option value="' + language + '">' + title + '</option>'); 50 }); 51 buf.push('</select>'); 52 return buf.join(''); 53 } 54 55 function navigate_to_first_existing(urls) { 56 // Navigate to the first existing URL in urls. 57 var url = urls.shift(); 58 if (urls.length == 0) { 59 window.location.href = url; 60 return; 61 } 62 $.ajax({ 63 url: url, 64 success: function() { 65 window.location.href = url; 66 }, 67 error: function() { 68 navigate_to_first_existing(urls); 69 } 70 }); 71 } 72 73 function on_version_switch() { 74 var selected_version = $(this).children('option:selected').attr('value') + '/'; 75 var url = window.location.href; 76 var current_language = language_segment_from_url(url); 77 var current_version = version_segment_in_url(url); 78 var new_url = url.replace('.org/' + current_language + current_version, 79 '.org/' + current_language + selected_version); 80 if (new_url != url) { 81 navigate_to_first_existing([ 82 new_url, 83 url.replace('.org/' + current_language + current_version, 84 '.org/' + selected_version), 85 'https://docs.python.org/' + current_language + selected_version, 86 'https://docs.python.org/' + selected_version, 87 'https://docs.python.org/' 88 ]); 89 } 90 } 91 92 function on_language_switch() { 93 var selected_language = $(this).children('option:selected').attr('value') + '/'; 94 var url = window.location.href; 95 var current_language = language_segment_from_url(url); 96 var current_version = version_segment_in_url(url); 97 if (selected_language == 'en/') // Special 'default' case for english. 98 selected_language = ''; 99 var new_url = url.replace('.org/' + current_language + current_version, 100 '.org/' + selected_language + current_version); 101 if (new_url != url) { 102 navigate_to_first_existing([ 103 new_url, 104 'https://docs.python.org/' 105 ]); 106 } 107 } 108 109 // Returns the path segment of the language as a string, like 'fr/' 110 // or '' if not found. 111 function language_segment_from_url(url) { 112 var language_regexp = '\.org/([a-z]{2}(?:-[a-z]{2})?/)'; 113 var match = url.match(language_regexp); 114 if (match !== null) 115 return match[1]; 116 return ''; 117 } 118 119 // Returns the path segment of the version as a string, like '3.6/' 120 // or '' if not found. 121 function version_segment_in_url(url) { 122 var language_segment = '(?:[a-z]{2}(?:-[a-z]{2})?/)'; 123 var version_segment = '(?:(?:' + version_regexs.join('|') + ')/)'; 124 var version_regexp = '\\.org/' + language_segment + '?(' + version_segment + ')'; 125 var match = url.match(version_regexp); 126 if (match !== null) 127 return match[1]; 128 return '' 129 } 130 131 $(document).ready(function() { 132 var release = DOCUMENTATION_OPTIONS.VERSION; 133 var language_segment = language_segment_from_url(window.location.href); 134 var current_language = language_segment.replace(/\/+$/g, '') || 'en'; 135 var version = release.substr(0, 3); 136 var version_select = build_version_select(version, release); 137 138 $('.version_switcher_placeholder').html(version_select); 139 $('.version_switcher_placeholder select').bind('change', on_version_switch); 140 141 var language_select = build_language_select(current_language); 142 143 $('.language_switcher_placeholder').html(language_select); 144 $('.language_switcher_placeholder select').bind('change', on_language_switch); 145 }); 146})(); 147