|
| 1 | +// Copyright 2013 PSF. Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2 |
| 2 | +// File originates from the cpython source found in Doc/tools/sphinxext/static/version_switch.js |
| 3 | + |
| 4 | +(function() { |
| 5 | + 'use strict'; |
| 6 | + |
| 7 | + var doc_url = "www.pygmt.org"; |
| 8 | + //var doc_url = "0.0.0.0:8000"; // for local testing only |
| 9 | + var url_re = new RegExp(doc_url + "\\/(dev|latest|(v\\d+\\.\\d+\\.\\d+))\\/"); |
| 10 | + // List all versions. |
| 11 | + // Add one entry "version: title" for any minor releases |
| 12 | + var all_versions = { |
| 13 | + 'latest': 'latest', |
| 14 | + 'dev': 'dev', |
| 15 | + 'v0.2.0': 'v0.2.0', |
| 16 | + 'v0.1.2': 'v0.1.2', |
| 17 | + 'v0.1.1': 'v0.1.1', |
| 18 | + 'v0.1.0': 'v0.1.0', |
| 19 | + '0.0.1a0': 'v0.0.1a0', |
| 20 | + }; |
| 21 | + |
| 22 | + function build_select(current_version, current_release) { |
| 23 | + var buf = ['<select>']; |
| 24 | + |
| 25 | + $.each(all_versions, function(version, title) { |
| 26 | + buf.push('<option value="' + version + '"'); |
| 27 | + if (version == current_version) { |
| 28 | + buf.push(' selected="selected">'); |
| 29 | + if (version == "latest" || version == "dev") { |
| 30 | + buf.push(title + ' (' + current_release + ')'); |
| 31 | + } else { |
| 32 | + buf.push(current_version); |
| 33 | + } |
| 34 | + } else { |
| 35 | + buf.push('>' + title); |
| 36 | + } |
| 37 | + buf.push('</option>'); |
| 38 | + }); |
| 39 | + |
| 40 | + buf.push('</select>'); |
| 41 | + return buf.join(''); |
| 42 | + } |
| 43 | + |
| 44 | + function patch_url(url, new_version) { |
| 45 | + return url.replace(url_re, doc_url + '/' + new_version + '/'); |
| 46 | + } |
| 47 | + |
| 48 | + function on_switch() { |
| 49 | + var selected = $(this).children('option:selected').attr('value'); |
| 50 | + |
| 51 | + var url = window.location.href, |
| 52 | + new_url = patch_url(url, selected); |
| 53 | + |
| 54 | + if (new_url != url) { |
| 55 | + // check beforehand if url exists, else redirect to version's start page |
| 56 | + $.ajax({ |
| 57 | + url: new_url, |
| 58 | + success: function() { |
| 59 | + window.location.href = new_url; |
| 60 | + }, |
| 61 | + error: function() { |
| 62 | + window.location.href = 'http://' + doc_url + '/' + selected; |
| 63 | + } |
| 64 | + }); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + $(document).ready(function() { |
| 69 | + var match = url_re.exec(window.location.href); |
| 70 | + if (match) { |
| 71 | + var release = DOCUMENTATION_OPTIONS.VERSION; |
| 72 | + var version = match[1]; |
| 73 | + var select = build_select(version, release); |
| 74 | + $('.version_switch_note').html('Or, select a version from the drop-down menu above.'); |
| 75 | + $('.version').html(select); |
| 76 | + $('.version select').bind('change', on_switch); |
| 77 | + } |
| 78 | + }); |
| 79 | +})(); |
0 commit comments