1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/guides/assets/javascripts/guides.js
Petrik 4c258caba2 Add copy button for copying guide code examples [skip ci]
Instead of carefully selecting the code in examples, use a copy button
for copying the code. This uses https://clipboardjs.com/ for copying.

For the bash code examples only line starting with $ prompt are copied.
The $ prompt itself is not copied.
2020-10-01 16:32:43 +02:00

82 lines
2.5 KiB
JavaScript

(function() {
"use strict";
this.wrap = function(elem, wrapper) {
elem.parentNode.insertBefore(wrapper, elem);
wrapper.appendChild(elem);
}
this.unwrap = function(elem) {
var wrapper = elem.parentNode;
wrapper.parentNode.replaceChild(elem, wrapper);
}
this.createElement = function(tagName, className) {
var elem = document.createElement(tagName);
elem.classList.add(className);
return elem;
}
// 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() {
var guidesMenu = document.getElementById("guidesMenu");
var guides = document.getElementById("guides");
guidesMenu.addEventListener("click", function(e) {
e.preventDefault();
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) {
if (Turbolinks.supported) {
Turbolinks.visit(e.target.value);
} else {
window.location = e.target.value;
}
});
var moreInfoButton = document.querySelector(".more-info-button");
var moreInfoLinks = document.querySelector(".more-info-links");
moreInfoButton.addEventListener("click", function(e) {
e.preventDefault();
if (moreInfoLinks.classList.contains("s-hidden")) {
wrap(moreInfoLinks, createElement("div", "more-info-container"));
moreInfoLinks.classList.remove("s-hidden");
} else {
moreInfoLinks.classList.add("s-hidden");
unwrap(moreInfoLinks);
}
});
var clipboard = new ClipboardJS('.clipboard-button');
clipboard.on('success', function(e) {
var trigger = e.trigger;
var triggerLabel = trigger.innerHTML;
trigger.innerHTML = 'Copied!';
setTimeout(function(){
trigger.innerHTML = triggerLabel;
}, 3000);
e.clearSelection();
});
});
}).call(this);