summaryrefslogtreecommitdiffstats
path: root/Doc/tools/static/version_switch.js
blob: 7289a4d01ea2f6a0de6fe8760f9fea255e481e0e (plain)
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
(function() {
  'use strict';

  var all_versions = {
    '3.6': 'dev (3.6)',
    '3.5': '3.5',
    '3.4': '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
      $.ajax({
        url: new_url,
        success: function() {
           window.location.href = new_url;
        },
        error: function() {
           window.location.href = 'https://docs.python.org/' + selected;
        }
      });
    }
  }

  $(document).ready(function() {
    var release = DOCUMENTATION_OPTIONS.VERSION;
    var version = release.substr(0, 3);
    var select = build_select(version, release);

    $('.version_switcher_placeholder').html(select);
    $('.version_switcher_placeholder select').bind('change', on_switch);
  });
})();