|
1 |
| -// Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
| 1 | +// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT |
2 | 2 | // file at the top-level directory of this distribution and at
|
3 | 3 | // http://rust-lang.org/COPYRIGHT.
|
4 | 4 | //
|
|
11 | 11 | /*jslint browser: true, es5: true */
|
12 | 12 | /*globals $: true, rootPath: true */
|
13 | 13 |
|
14 |
| -(function() { |
15 |
| - if (window.playgroundUrl) { |
16 |
| - $('pre.rust').hover(function() { |
17 |
| - var a = $('<a>').text('⇱').attr('class', 'test-arrow'); |
18 |
| - var code = $(this).prev(".rusttest").text(); |
19 |
| - a.attr('href', window.playgroundUrl + '?code=' + |
20 |
| - encodeURIComponent(code)); |
21 |
| - a.attr('target', '_blank'); |
22 |
| - $(this).append(a); |
23 |
| - }, function() { |
24 |
| - $(this).find('a.test-arrow').remove(); |
25 |
| - }); |
| 14 | +document.addEventListener('DOMContentLoaded', function() { |
| 15 | + 'use strict'; |
| 16 | + |
| 17 | + if (!window.playgroundUrl) { |
| 18 | + return; |
26 | 19 | }
|
27 |
| -}()); |
| 20 | + |
| 21 | + var featureRegexp = new RegExp('^\s*#!\\[feature\\(\.*?\\)\\]'); |
| 22 | + var elements = document.querySelectorAll('pre.rust-example-rendered'); |
| 23 | + |
| 24 | + Array.prototype.forEach.call(elements, function(el) { |
| 25 | + el.onmouseover = function(e) { |
| 26 | + if (el.contains(e.relatedTarget)) { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + var a = document.createElement('a'); |
| 31 | + a.setAttribute('class', 'test-arrow'); |
| 32 | + a.textContent = 'Run'; |
| 33 | + |
| 34 | + var code = el.previousElementSibling.textContent; |
| 35 | + |
| 36 | + var channel = ''; |
| 37 | + if (featureRegexp.test(code)) { |
| 38 | + channel = '&version=nightly'; |
| 39 | + } |
| 40 | + |
| 41 | + a.setAttribute('href', window.playgroundUrl + '?code=' + |
| 42 | + encodeURIComponent(code) + channel); |
| 43 | + a.setAttribute('target', '_blank'); |
| 44 | + |
| 45 | + el.appendChild(a); |
| 46 | + }; |
| 47 | + |
| 48 | + el.onmouseout = function(e) { |
| 49 | + if (el.contains(e.relatedTarget)) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + el.removeChild(el.querySelectorAll('a.test-arrow')[0]); |
| 54 | + }; |
| 55 | + }); |
| 56 | +}); |
0 commit comments