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