~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-2.7.11-docs-html/_static/version_switch.js

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

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