Only load copy-and-paste helper on pages that use it

This change helps improve load times for key non-documentation pages
such as https://kubernetes.io/
pull/24229/head
Tim Bannister 2020-09-29 18:27:45 +01:00
parent e2233f570a
commit 735410f17b
5 changed files with 44 additions and 72 deletions

View File

@ -8,8 +8,6 @@
{{ partial "deprecation-warning.html" . }}
{{ block "main" . }}{{ end }}
{{ partialCached "footer.html" . }}
<!-- Disabling this as elements queries do not appear to exist on case studies -->
<!-- {{ partialCached "footer-scripts.html" . }} -->
{{ partialCached "scripts.html" . }}
</body>

View File

@ -1,35 +0,0 @@
<button class="flyout-button" onclick="kub.toggleToc()" aria-label="Toggle table of contents visibility"></button>
<script>
// hide docs nav area if no nav is present, or if nav only contains a link to the current page
(function () {
window.addEventListener('DOMContentLoaded', init)
// play nice with our neighbors
function init() {
window.removeEventListener('DOMContentLoaded', init)
hideNav()
}
function hideNav(toc){
if (!toc) toc = document.querySelector('#docsToc')
if (!toc) return
var container = toc.querySelector('.container')
// container is built dynamically, so it may not be present on the first runloop
if (container) {
if (container.childElementCount === 0 || toc.querySelectorAll('a.item').length === 1) {
toc.style.display = 'none'
document.getElementById('docsContent').style.width = '100%'
}
} else {
requestAnimationFrame(function () {
hideNav(toc)
})
}
}
})();
</script>
<!-- Commenting out AnswerDash for now; we need to work on our list of questions/answers/design first
<!-- Start of AnswerDash script <script>var AnswerDash;!function(e,t,n,s,a){if(!t.getElementById(s)){var i,r=t.createElement(n),c=t.getElementsByTagName(n)[0];e[a]||(i=e[a]=function(){i.__oninit.push(arguments)},i.__oninit=[]),r.type="text/javascript",r.async=!0,r.src="https://p1.answerdash.com/answerdash.min.js?siteid=756",r.setAttribute("id",s),c.parentNode.insertBefore(r,c)}}(window,document,"script","answerdash-script","AnswerDash");</script> <!-- End of AnswerDash script -->

View File

@ -85,7 +85,6 @@
<meta property="og:image" content="{{ "/images/kubernetes-horizontal-color.png" | relURL }}">
{{ end }}
<meta property="og:type" content="{{ $ogType }}">
<script src="{{ "js/sweetalert-2.1.2.min.js" | relURL }}"></script>
{{ if .HasShortcode "mermaid" }}
<!-- Copied from https://unpkg.com/mermaid@8.5.0/dist/mermaid.min.js -->

View File

@ -0,0 +1,44 @@
{{ with .Site.Params.algolia_docsearch }}
<!-- scripts for algolia docsearch -->
{{ end }}
{{/* copy-and-paste helper for codenew shortcode */}}
{{- if .HasShortcode "codenew" -}}
<script async src="{{ "js/sweetalert-2.1.2.min.js" | relURL }}"></script>
<script type="text/javascript">
function copyCode(elem){
if (document.getElementById(elem)) {
// create hidden text element, if it doesn't already exist
var targetId = "_hiddenCopyText_";
// must use a temporary form element for the selection and copy
target = document.getElementById(targetId);
if (!target) {
var target = document.createElement("textarea");
target.style.position = "absolute";
target.style.left = "-9999px";
target.style.top = "0";
target.id = targetId;
document.body.appendChild(target);
}
target.value = document.getElementById(elem).innerText;
// select the content
target.select();
// copy the selection
var succeed;
try {
succeed = document.execCommand("copy");
} catch(e) {
swal("Oh, no…","Sorry, your browser doesn't support copying this example to your clipboard.");
succeed = false;
}
if (succeed) {
swal("Copied to clipboard: ",elem);
return succeed;
} else {
swal("Oops!", elem + " not found when trying to copy code");
return false;
}
}
}
</script>
{{- end -}}

View File

@ -9,40 +9,6 @@
var body;
//helper functions
function copyCode(elem){
if (document.getElementById(elem)) {
// create hidden text element, if it doesn't already exist
var targetId = "_hiddenCopyText_";
// must use a temporary form element for the selection and copy
target = document.getElementById(targetId);
if (!target) {
var target = document.createElement("textarea");
target.style.position = "absolute";
target.style.left = "-9999px";
target.style.top = "0";
target.id = targetId;
document.body.appendChild(target);
}
target.value = document.getElementById(elem).innerText;
// select the content
target.select();
// copy the selection
var succeed;
try {
succeed = document.execCommand("copy");
} catch(e) {
swal("Oh, no...","Sorry, your browser doesn't support document.execCommand('copy'), so we can't copy this code to your clipboard.");
succeed = false;
}
if (succeed) swal("Copied to clipboard: ",elem);
return succeed;
} else {
swal("Oops!",elem + " not found when trying to copy code");
return false;
}
}
function booleanAttributeValue(element, attribute, defaultValue){
// returns true if an attribute is present with no value
// e.g. booleanAttributeValue(element, 'data-modal', false);