|
1 | 1 | /**
|
2 | 2 | * @fileoverview This file executes on load and is meant to check if the help
|
3 | 3 | * page is the latest version and inject warnings into the page if not.
|
4 |
| - * TODO: Need to use GitHub API to retrieve latest version published |
| 4 | + * |
| 5 | + * Changes: |
| 6 | + * - 2017/04/22: Use the GitHub Releases API to check if this is the lastest documentation |
5 | 7 | */
|
6 | 8 |
|
7 | 9 | 'use strict';
|
|
33 | 35 | return null;
|
34 | 36 | };
|
35 | 37 |
|
36 |
| - var getLatestReleaseVersion = function() { |
37 |
| - // TODO: This is a hard-coded version string, need to use GH API instead |
38 |
| - var version = "0.9.13"; |
39 |
| - return semverRegex().test(version) ? semverRegex().exec(version)[0] : null; |
| 38 | + var getLatestReleaseVersion = function(cb) { |
| 39 | + // On the basis Mu doesn't run on XP, IE6 support is irrelavent |
| 40 | + var xhr = new XMLHttpRequest(); |
| 41 | + // We want the lastest release (doesn't include draft or preview releases) |
| 42 | + xhr.open('GET', 'https://api.github.com/repos/mu-editor/mu/releases/latest'); |
| 43 | + xhr.onload = function() { |
| 44 | + // We got the data |
| 45 | + if (xhr.status === 200) { |
| 46 | + // IE6 & IE7 support is superfluous |
| 47 | + try { |
| 48 | + var lastest = JSON.parse(xhr.responseText); |
| 49 | + // Run the callback |
| 50 | + cb(semverRegex().test(lastest.tag_name) ? semverRegex().exec(lastest.tag_name)[0] : null); |
| 51 | + } catch (e) { |
| 52 | + if (console && console.error) console.error('Unable to parse release information', e); |
| 53 | + } |
| 54 | + } else { |
| 55 | + // Something went wrong. Fail quietly |
| 56 | + if (console && console.error) console.error('Unable to fetch release information'); |
| 57 | + } |
| 58 | + }; |
| 59 | + // Send the request |
| 60 | + xhr.send(); |
40 | 61 | };
|
41 | 62 |
|
42 | 63 | var isVersionLowerThan = function(baseVersion, compareVersion) {
|
|
52 | 73 | var helpInjectDiv = document.getElementById('mu-help-header-inject');
|
53 | 74 | if (helpInjectDiv) {
|
54 | 75 | var urlVersion = getUrlVersion();
|
55 |
| - var latestVersion = getLatestReleaseVersion(); |
56 |
| - if (urlVersion && latestVersion && isVersionLowerThan(urlVersion, latestVersion)) { |
57 |
| - helpInjectDiv.innerHTML = [ |
58 |
| - '<div class="alert alert-danger" role="alert">', |
59 |
| - ' <h2><strong>Update Mu!</strong></h2>', |
60 |
| - ' <p>You appear to be using an old version of Mu. Please <a href="https://codewith.mu" target="_blank">download the latest version</a>.</p>', |
61 |
| - '</div>', |
62 |
| - '<div role="alert" class="alert alert-warning">', |
63 |
| - ' <h2><strong>This is not the latest Documentation!</strong></h2>', |
64 |
| - ' <p>This documentation corresponds to the Mu version you are using, however documentation for newer versions of Mu might contain more content and troubleshooting information.</p>', |
65 |
| - ' <p><a href="/help">View documentation for newest versions of Mu.</a></p>', |
66 |
| - '</div>'].join('\n'); |
67 |
| - } |
| 76 | + getLatestReleaseVersion(function (latestVersion) { |
| 77 | + if (urlVersion && latestVersion && isVersionLowerThan(urlVersion, latestVersion)) { |
| 78 | + helpInjectDiv.innerHTML = [ |
| 79 | + '<div class="alert alert-danger" role="alert">', |
| 80 | + ' <h2><strong>Update Mu!</strong></h2>', |
| 81 | + ' <p>You appear to be using an old version of Mu. Please <a href="https://codewith.mu" target="_blank">download the latest version (' + latestVersion + ')</a>.</p>', |
| 82 | + '</div>', |
| 83 | + '<div role="alert" class="alert alert-warning">', |
| 84 | + ' <h2><strong>This is not the latest Documentation!</strong></h2>', |
| 85 | + ' <p>This documentation corresponds to Mu version (' + urlVersion + ') which seems to be what you are using, however documentation for newer versions of Mu might contain more content and troubleshooting information.</p>', |
| 86 | + ' <p><a href="/help">View documentation for latest version of Mu.</a></p>', |
| 87 | + '</div>'].join('\n'); |
| 88 | + } |
| 89 | + }); |
68 | 90 | }
|
69 | 91 | });
|
70 | 92 | })();
|
0 commit comments