diff options
author | Yoshiyuki Hirano <yhirano@me.com> | 2018-04-23 09:28:30 +0900 |
---|---|---|
committer | Yoshiyuki Hirano <yhirano@me.com> | 2018-04-23 20:07:05 +0900 |
commit | e5d38a24dc03c765c0b6a1fb39cae0b0758a1c4a (patch) | |
tree | f948b67112ceaacb5f571f80d94e2e87550812cf /guides/assets/javascripts/guides.js | |
parent | c991ebbc91ffaabf7452bd6364604dd524fc569d (diff) | |
download | rails-e5d38a24dc03c765c0b6a1fb39cae0b0758a1c4a.tar.gz rails-e5d38a24dc03c765c0b6a1fb39cae0b0758a1c4a.tar.bz2 rails-e5d38a24dc03c765c0b6a1fb39cae0b0758a1c4a.zip |
Use Turbolinks in Rails guides
Diffstat (limited to 'guides/assets/javascripts/guides.js')
-rw-r--r-- | guides/assets/javascripts/guides.js | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/guides/assets/javascripts/guides.js b/guides/assets/javascripts/guides.js index e39ac239cd..a37f5d1927 100644 --- a/guides/assets/javascripts/guides.js +++ b/guides/assets/javascripts/guides.js @@ -19,7 +19,18 @@ return elem; } - document.addEventListener("DOMContentLoaded", function() { + // For old browsers + this.each = function(node, callback) { + var array = Array.prototype.slice.call(node); + for(var i = 0; i < array.length; i++) callback(array[i]); + } + + // Viewable on local + if (window.location.protocol === "file:") Turbolinks.supported = false; + + document.addEventListener("turbolinks:load", function() { + window.SyntaxHighlighter.highlight({ "auto-links": false }); + var guidesMenu = document.getElementById("guidesMenu"); var guides = document.getElementById("guides"); @@ -28,12 +39,22 @@ guides.classList.toggle("visible"); }); + each(document.querySelectorAll("#guides a"), function(element) { + element.addEventListener("click", function(e) { + guides.classList.toggle("visible"); + }); + }); + var guidesIndexItem = document.querySelector("select.guides-index-item"); var currentGuidePath = window.location.pathname; guidesIndexItem.value = currentGuidePath.substring(currentGuidePath.lastIndexOf("/") + 1); guidesIndexItem.addEventListener("change", function(e) { - window.location = e.target.value; + if (Turbolinks.supported) { + Turbolinks.visit(e.target.value); + } else { + window.location = e.target.value; + } }); var moreInfoButton = document.querySelector(".more-info-button"); |