refactor: Use pure JS equivalent of jQuery's $.ready()

https://stackoverflow.com/a/9899701
This commit is contained in:
curben 2019-05-22 10:52:58 +09:30
parent 821ac056b7
commit 0e686c620b
1 changed files with 48 additions and 41 deletions

View File

@ -23,44 +23,6 @@
$('.fancybox').fancybox() $('.fancybox').fancybox()
} }
// Add "Copy" button to code snippet
const code = document.getElementsByClassName('code')
for (let i = 0; i < code.length; i++) {
const button = document.createElement('button')
button.className = 'copy-button'
button.textContent = 'Copy'
code[i].appendChild(button)
}
$(document).ready(() => {
// Add copy to clipboard button for code snippet
const copyCode = new ClipboardJS('.copy-button', {
target: (trigger) => {
return trigger.previousElementSibling
}
})
copyCode.on('success', (event) => {
event.clearSelection()
event.trigger.textContent = 'Copied'
window.setTimeout(() => {
event.trigger.textContent = 'Copy'
}, 2000)
})
copyCode.on('error', (event) => {
event.trigger.textContent = 'Press "Ctrl + C" to copy'
window.setTimeout(() => {
event.trigger.textContent = 'Copy'
}, 2000)
})
// Initialize Cloudinary responsive function
const cl = cloudinary.Cloudinary.new({ cloud_name: 'curben' })
cl.responsive()
})
})(jQuery) })(jQuery)
// Search button function // Search button function
@ -70,3 +32,48 @@ searchClick.onclick = function() {
document.getElementById('searchTxt').value + document.getElementById('searchTxt').value +
'&group_id=&project_id=8306723&search_code=true&repository_ref=master') '&group_id=&project_id=8306723&search_code=true&repository_ref=master')
} }
// Add "Copy" button to code snippet
const code = document.getElementsByClassName('code')
for (let i = 0; i < code.length; i++) {
const button = document.createElement('button')
button.className = 'copy-button'
button.textContent = 'Copy'
code[i].appendChild(button)
}
/*
* Copy button and Cloudinary functions.
* Following functions only execute after
* the 'document' is ready or
* <script src> is executed.
*/
document.addEventListener('DOMContentLoaded', () => {
// Copy button
const copyCode = new ClipboardJS('.copy-button', {
target: (trigger) => {
return trigger.previousElementSibling
}
})
copyCode.on('success', (event) => {
event.clearSelection()
event.trigger.textContent = 'Copied'
window.setTimeout(() => {
event.trigger.textContent = 'Copy'
}, 2000)
})
copyCode.on('error', (event) => {
event.trigger.textContent = 'Press "Ctrl + C" to copy'
window.setTimeout(() => {
event.trigger.textContent = 'Copy'
}, 2000)
})
// Initialize Cloudinary responsive function
const cl = cloudinary.Cloudinary.new({ cloud_name: 'curben' })
cl.responsive()
}, false);