|
| 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 = "docs.generic-mapping-tools.org"; |
| 8 | + // var doc_url = "0.0.0.0:8000"; // for local testing only |
| 9 | + var url_re = new RegExp(doc_url + "\\/(dev|latest|(\\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 | + '6.1': '6.1', |
| 16 | + '6.0': '6.0', |
| 17 | + '5.4': '5.4', |
| 18 | + '4.5': '4', |
| 19 | + }; |
| 20 | + |
| 21 | + function build_select(current_version, current_release) { |
| 22 | + var buf = ['<select>']; |
| 23 | + |
| 24 | + $.each(all_versions, function(version, title) { |
| 25 | + buf.push('<option value="' + version + '"'); |
| 26 | + if (version == current_version) { |
| 27 | + buf.push(' selected="selected">'); |
| 28 | + if (version == "latest" || version == "dev") { |
| 29 | + buf.push(title + ' (' + current_release + ')'); |
| 30 | + } else { |
| 31 | + buf.push(current_version); |
| 32 | + } |
| 33 | + } else { |
| 34 | + buf.push('>' + title); |
| 35 | + } |
| 36 | + buf.push('</option>'); |
| 37 | + }); |
| 38 | + |
| 39 | + buf.push('</select>'); |
| 40 | + return buf.join(''); |
| 41 | + } |
| 42 | + |
| 43 | + function patch_url(url, new_version) { |
| 44 | + return url.replace(url_re, doc_url + '/' + new_version + '/'); |
| 45 | + } |
| 46 | + |
| 47 | + function on_switch() { |
| 48 | + var selected = $(this).children('option:selected').attr('value'); |
| 49 | + |
| 50 | + var url = window.location.href, |
| 51 | + new_url = patch_url(url, selected); |
| 52 | + console.log("OK"); |
| 53 | + console.log(new_url); |
| 54 | + console.log(url); |
| 55 | + |
| 56 | + if (new_url != url) { |
| 57 | + // check beforehand if url exists, else redirect to version's start page |
| 58 | + $.ajax({ |
| 59 | + url: new_url, |
| 60 | + success: function() { |
| 61 | + window.location.href = new_url; |
| 62 | + }, |
| 63 | + error: function() { |
| 64 | + window.location.href = 'http://' + doc_url + '/' + selected; |
| 65 | + } |
| 66 | + }); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + $(document).ready(function() { |
| 71 | + var match = url_re.exec(window.location.href); |
| 72 | + if (match) { |
| 73 | + var release = DOCUMENTATION_OPTIONS.VERSION; |
| 74 | + var version = match[1]; |
| 75 | + var select = build_select(version, release); |
| 76 | + $('.version_switch_note').html('Or, select a version from the drop-down menu above.'); |
| 77 | + $('.version').html(select); |
| 78 | + $('.version select').bind('change', on_switch); |
| 79 | + } |
| 80 | + }); |
| 81 | +})(); |
0 commit comments