1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-02-22 15:39:59 +00:00

Use Babel and ES6 in docs JS files (#31607)

* Pass docs js through Babel

* Use ES6 in docs js

* Only run babel on src files

* Allow babel in Hugo

* Update scripts.html

* Inherit from the root .eslintrc.json

* Use `Array.from`

* Drop Babel from docs

* Prefer template

* replace IIFE with arrow functions

Co-authored-by: XhmikosR <xhmikosr@gmail.com>
Co-authored-by: GeoSot <geo.sotis@gmail.com>
This commit is contained in:
Tiger Oakes 2022-04-12 08:07:25 -07:00 committed by GitHub
parent f6cb4b64b5
commit fe257823ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 122 additions and 167 deletions

View File

@ -1,61 +1,17 @@
{ {
"root": true, "env": {
"browser": true,
"node": false
},
"parserOptions": { "parserOptions": {
"ecmaVersion": 5,
"sourceType": "script" "sourceType": "script"
}, },
"extends": [ "extends": "../.eslintrc.json",
"plugin:unicorn/recommended",
"xo",
"xo/browser"
],
"rules": { "rules": {
"arrow-body-style": "off",
"capitalized-comments": "off",
"comma-dangle": [
"error",
"never"
],
"indent": [
"error",
2,
{
"MemberExpression": "off",
"SwitchCase": 1
}
],
"multiline-ternary": [
"error",
"always-multiline"
],
"no-negated-condition": "off",
"no-new": "off", "no-new": "off",
"no-var": "off", "prefer-template": "error",
"object-curly-spacing": [
"error",
"always"
],
"object-shorthand": "off",
"operator-linebreak": [
"error",
"after"
],
"prefer-arrow-callback": "off",
"prefer-destructuring": "off",
"semi": [
"error",
"never"
],
"strict": "error", "strict": "error",
"unicorn/no-array-for-each": "off", "unicorn/no-array-for-each": "off",
"unicorn/no-array-method-this-argument": "off", "unicorn/numeric-separators-style": "off"
"unicorn/no-for-loop": "off",
"unicorn/no-null": "off",
"unicorn/numeric-separators-style": "off",
"unicorn/prefer-array-flat": "off",
"unicorn/prefer-dom-node-dataset": "off",
"unicorn/prefer-module": "off",
"unicorn/prefer-query-selector": "off",
"unicorn/prevent-abbreviations": "off"
} }
} }

View File

@ -12,117 +12,117 @@
/* global ClipboardJS: false, bootstrap: false */ /* global ClipboardJS: false, bootstrap: false */
(function () { (() => {
'use strict' 'use strict'
// Tooltip and popover demos // Tooltip and popover demos
document.querySelectorAll('.tooltip-demo') document.querySelectorAll('.tooltip-demo')
.forEach(function (tooltip) { .forEach(tooltip => {
new bootstrap.Tooltip(tooltip, { new bootstrap.Tooltip(tooltip, {
selector: '[data-bs-toggle="tooltip"]' selector: '[data-bs-toggle="tooltip"]'
}) })
}) })
document.querySelectorAll('[data-bs-toggle="popover"]') document.querySelectorAll('[data-bs-toggle="popover"]')
.forEach(function (popover) { .forEach(popover => {
new bootstrap.Popover(popover) new bootstrap.Popover(popover)
}) })
var toastPlacement = document.getElementById('toastPlacement') const toastPlacement = document.getElementById('toastPlacement')
if (toastPlacement) { if (toastPlacement) {
document.getElementById('selectToastPlacement').addEventListener('change', function () { document.getElementById('selectToastPlacement').addEventListener('change', function () {
if (!toastPlacement.dataset.originalClass) { if (!toastPlacement.dataset.originalClass) {
toastPlacement.dataset.originalClass = toastPlacement.className toastPlacement.dataset.originalClass = toastPlacement.className
} }
toastPlacement.className = toastPlacement.dataset.originalClass + ' ' + this.value toastPlacement.className = `${toastPlacement.dataset.originalClass} ${this.value}`
}) })
} }
document.querySelectorAll('.bd-example .toast') document.querySelectorAll('.bd-example .toast')
.forEach(function (toastNode) { .forEach(toastNode => {
var toast = new bootstrap.Toast(toastNode, { const toast = new bootstrap.Toast(toastNode, {
autohide: false autohide: false
}) })
toast.show() toast.show()
}) })
var toastTrigger = document.getElementById('liveToastBtn') const toastTrigger = document.getElementById('liveToastBtn')
var toastLiveExample = document.getElementById('liveToast') const toastLiveExample = document.getElementById('liveToast')
if (toastTrigger) { if (toastTrigger) {
toastTrigger.addEventListener('click', function () { toastTrigger.addEventListener('click', () => {
var toast = new bootstrap.Toast(toastLiveExample) const toast = new bootstrap.Toast(toastLiveExample)
toast.show() toast.show()
}) })
} }
var alertPlaceholder = document.getElementById('liveAlertPlaceholder') const alertPlaceholder = document.getElementById('liveAlertPlaceholder')
var alertTrigger = document.getElementById('liveAlertBtn') const alertTrigger = document.getElementById('liveAlertBtn')
function alert(message, type) { function alert(message, type) {
var wrapper = document.createElement('div') const wrapper = document.createElement('div')
wrapper.innerHTML = '<div class="alert alert-' + type + ' alert-dismissible" role="alert">' + message + '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>' wrapper.innerHTML = `<div class="alert alert-${type} alert-dismissible" role="alert">${message}<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>`
alertPlaceholder.append(wrapper) alertPlaceholder.append(wrapper)
} }
if (alertTrigger) { if (alertTrigger) {
alertTrigger.addEventListener('click', function () { alertTrigger.addEventListener('click', () => {
alert('Nice, you triggered this alert message!', 'success') alert('Nice, you triggered this alert message!', 'success')
}) })
} }
// Demos within modals // Demos within modals
document.querySelectorAll('.tooltip-test') document.querySelectorAll('.tooltip-test')
.forEach(function (tooltip) { .forEach(tooltip => {
new bootstrap.Tooltip(tooltip) new bootstrap.Tooltip(tooltip)
}) })
document.querySelectorAll('.popover-test') document.querySelectorAll('.popover-test')
.forEach(function (popover) { .forEach(popover => {
new bootstrap.Popover(popover) new bootstrap.Popover(popover)
}) })
// Indeterminate checkbox example // Indeterminate checkbox example
document.querySelectorAll('.bd-example-indeterminate [type="checkbox"]') document.querySelectorAll('.bd-example-indeterminate [type="checkbox"]')
.forEach(function (checkbox) { .forEach(checkbox => {
checkbox.indeterminate = true checkbox.indeterminate = true
}) })
// Disable empty links in docs examples // Disable empty links in docs examples
document.querySelectorAll('.bd-content [href="#"]') document.querySelectorAll('.bd-content [href="#"]')
.forEach(function (link) { .forEach(link => {
link.addEventListener('click', function (event) { link.addEventListener('click', event => {
event.preventDefault() event.preventDefault()
}) })
}) })
// Modal relatedTarget demo // Modal relatedTarget demo
var exampleModal = document.getElementById('exampleModal') const exampleModal = document.getElementById('exampleModal')
if (exampleModal) { if (exampleModal) {
exampleModal.addEventListener('show.bs.modal', function (event) { exampleModal.addEventListener('show.bs.modal', event => {
// Button that triggered the modal // Button that triggered the modal
var button = event.relatedTarget const button = event.relatedTarget
// Extract info from data-bs-* attributes // Extract info from data-bs-* attributes
var recipient = button.getAttribute('data-bs-whatever') const recipient = button.getAttribute('data-bs-whatever')
// Update the modal's content. // Update the modal's content.
var modalTitle = exampleModal.querySelector('.modal-title') const modalTitle = exampleModal.querySelector('.modal-title')
var modalBodyInput = exampleModal.querySelector('.modal-body input') const modalBodyInput = exampleModal.querySelector('.modal-body input')
modalTitle.textContent = 'New message to ' + recipient modalTitle.textContent = `New message to ${recipient}`
modalBodyInput.value = recipient modalBodyInput.value = recipient
}) })
} }
// Insert copy to clipboard button before .highlight // Insert copy to clipboard button before .highlight
var btnTitle = 'Copy to clipboard' const btnTitle = 'Copy to clipboard'
var btnEdit = 'Edit on StackBlitz' const btnEdit = 'Edit on StackBlitz'
var btnHtml = '<div class="bd-clipboard"><button type="button" class="btn-clipboard">Copy</button></div>' const btnHtml = '<div class="bd-clipboard"><button type="button" class="btn-clipboard">Copy</button></div>'
document.querySelectorAll('div.highlight') document.querySelectorAll('div.highlight')
.forEach(function (element) { .forEach(element => {
element.insertAdjacentHTML('beforebegin', btnHtml) element.insertAdjacentHTML('beforebegin', btnHtml)
}) })
@ -132,10 +132,10 @@
* @param {string} title * @param {string} title
*/ */
function snippetButtonTooltip(selector, title) { function snippetButtonTooltip(selector, title) {
document.querySelectorAll(selector).forEach(function (btn) { document.querySelectorAll(selector).forEach(btn => {
var tooltipBtn = new bootstrap.Tooltip(btn, { title: title }) const tooltipBtn = new bootstrap.Tooltip(btn, { title })
btn.addEventListener('mouseleave', function () { btn.addEventListener('mouseleave', () => {
// Explicitly hide tooltip, since after clicking it remains // Explicitly hide tooltip, since after clicking it remains
// focused (as it's a button), so tooltip would otherwise // focused (as it's a button), so tooltip would otherwise
// remain visible until focus is moved away // remain visible until focus is moved away
@ -147,29 +147,29 @@
snippetButtonTooltip('.btn-clipboard', btnTitle) snippetButtonTooltip('.btn-clipboard', btnTitle)
snippetButtonTooltip('.btn-edit', btnEdit) snippetButtonTooltip('.btn-edit', btnEdit)
var clipboard = new ClipboardJS('.btn-clipboard', { const clipboard = new ClipboardJS('.btn-clipboard', {
target: function (trigger) { target(trigger) {
return trigger.parentNode.nextElementSibling return trigger.parentNode.nextElementSibling
} }
}) })
clipboard.on('success', function (event) { clipboard.on('success', event => {
var tooltipBtn = bootstrap.Tooltip.getInstance(event.trigger) const tooltipBtn = bootstrap.Tooltip.getInstance(event.trigger)
tooltipBtn.setContent({ '.tooltip-inner': 'Copied!' }) tooltipBtn.setContent({ '.tooltip-inner': 'Copied!' })
event.trigger.addEventListener('hidden.bs.tooltip', function () { event.trigger.addEventListener('hidden.bs.tooltip', () => {
tooltipBtn.setContent({ '.tooltip-inner': btnTitle }) tooltipBtn.setContent({ '.tooltip-inner': btnTitle })
}, { once: true }) }, { once: true })
event.clearSelection() event.clearSelection()
}) })
clipboard.on('error', function (event) { clipboard.on('error', event => {
var modifierKey = /mac/i.test(navigator.userAgent) ? '\u2318' : 'Ctrl-' const modifierKey = /mac/i.test(navigator.userAgent) ? '\u2318' : 'Ctrl-'
var fallbackMsg = 'Press ' + modifierKey + 'C to copy' const fallbackMsg = `Press ${modifierKey}C to copy`
var tooltipBtn = bootstrap.Tooltip.getInstance(event.trigger) const tooltipBtn = bootstrap.Tooltip.getInstance(event.trigger)
tooltipBtn.setContent({ '.tooltip-inner': fallbackMsg }) tooltipBtn.setContent({ '.tooltip-inner': fallbackMsg })
event.trigger.addEventListener('hidden.bs.tooltip', function () { event.trigger.addEventListener('hidden.bs.tooltip', () => {
tooltipBtn.setContent({ '.tooltip-inner': btnTitle }) tooltipBtn.setContent({ '.tooltip-inner': btnTitle })
}, { once: true }) }, { once: true })
}) })

View File

@ -2,18 +2,18 @@
// IT'S ALL JUST JUNK FOR OUR DOCS! // IT'S ALL JUST JUNK FOR OUR DOCS!
// ++++++++++++++++++++++++++++++++++++++++++ // ++++++++++++++++++++++++++++++++++++++++++
(function () { (() => {
'use strict' 'use strict'
var inputElement = document.getElementById('search-input') const inputElement = document.getElementById('search-input')
if (!window.docsearch || !inputElement) { if (!window.docsearch || !inputElement) {
return return
} }
var siteDocsVersion = inputElement.getAttribute('data-bd-docs-version') const siteDocsVersion = inputElement.getAttribute('data-bd-docs-version')
document.addEventListener('keydown', function (event) { document.addEventListener('keydown', event => {
if (event.ctrlKey && event.key === '/') { if (event.ctrlKey && event.key === '/') {
event.preventDefault() event.preventDefault()
inputElement.focus() inputElement.focus()
@ -25,11 +25,11 @@
indexName: 'bootstrap', indexName: 'bootstrap',
inputSelector: '#search-input', inputSelector: '#search-input',
algoliaOptions: { algoliaOptions: {
facetFilters: ['version:' + siteDocsVersion] facetFilters: [`version:${siteDocsVersion}`]
}, },
transformData: function (hits) { transformData(hits) {
return hits.map(function (hit) { return hits.map(hit => {
var liveUrl = 'https://getbootstrap.com/' const liveUrl = 'https://getbootstrap.com/'
hit.url = window.location.origin.startsWith(liveUrl) ? hit.url = window.location.origin.startsWith(liveUrl) ?
// On production, return the result as is // On production, return the result as is

View File

@ -1,24 +1,24 @@
/* global bootstrap: false */ /* global bootstrap: false */
(function () { (() => {
'use strict' 'use strict'
// Tooltip and popover demos // Tooltip and popover demos
document.querySelectorAll('.tooltip-demo') document.querySelectorAll('.tooltip-demo')
.forEach(function (tooltip) { .forEach(tooltip => {
new bootstrap.Tooltip(tooltip, { new bootstrap.Tooltip(tooltip, {
selector: '[data-bs-toggle="tooltip"]' selector: '[data-bs-toggle="tooltip"]'
}) })
}) })
document.querySelectorAll('[data-bs-toggle="popover"]') document.querySelectorAll('[data-bs-toggle="popover"]')
.forEach(function (popover) { .forEach(popover => {
new bootstrap.Popover(popover) new bootstrap.Popover(popover)
}) })
document.querySelectorAll('.toast') document.querySelectorAll('.toast')
.forEach(function (toastNode) { .forEach(toastNode => {
var toast = new bootstrap.Toast(toastNode, { const toast = new bootstrap.Toast(toastNode, {
autohide: false autohide: false
}) })
@ -27,27 +27,27 @@
// Disable empty links and submit buttons // Disable empty links and submit buttons
document.querySelectorAll('[href="#"], [type="submit"]') document.querySelectorAll('[href="#"], [type="submit"]')
.forEach(function (link) { .forEach(link => {
link.addEventListener('click', function (event) { link.addEventListener('click', event => {
event.preventDefault() event.preventDefault()
}) })
}) })
function setActiveItem() { function setActiveItem() {
var hash = window.location.hash const { hash } = window.location
if (hash === '') { if (hash === '') {
return return
} }
var link = document.querySelector('.bd-aside a[href="' + hash + '"]') const link = document.querySelector(`.bd-aside a[href="${hash}"]`)
if (!link) { if (!link) {
return return
} }
var active = document.querySelector('.bd-aside .active') const active = document.querySelector('.bd-aside .active')
var parent = link.parentNode.parentNode.previousElementSibling const parent = link.parentNode.parentNode.previousElementSibling
link.classList.add('active') link.classList.add('active')
@ -59,7 +59,7 @@
return return
} }
var expanded = active.parentNode.parentNode.previousElementSibling const expanded = active.parentNode.parentNode.previousElementSibling
active.classList.remove('active') active.classList.remove('active')

View File

@ -1,20 +1,19 @@
// Example starter JavaScript for disabling form submissions if there are invalid fields // Example starter JavaScript for disabling form submissions if there are invalid fields
(function () { (() => {
'use strict' 'use strict'
// Fetch all the forms we want to apply custom Bootstrap validation styles to // Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.querySelectorAll('.needs-validation') const forms = document.querySelectorAll('.needs-validation')
// Loop over them and prevent submission // Loop over them and prevent submission
Array.prototype.slice.call(forms) Array.from(forms).forEach(form => {
.forEach(function (form) { form.addEventListener('submit', event => {
form.addEventListener('submit', function (event) { if (!form.checkValidity()) {
if (!form.checkValidity()) { event.preventDefault()
event.preventDefault() event.stopPropagation()
event.stopPropagation() }
}
form.classList.add('was-validated') form.classList.add('was-validated')
}, false) }, false)
}) })
})() })()

View File

@ -1,14 +1,14 @@
/* globals Chart:false, feather:false */ /* globals Chart:false, feather:false */
(function () { (() => {
'use strict' 'use strict'
feather.replace({ 'aria-hidden': 'true' }) feather.replace({ 'aria-hidden': 'true' })
// Graphs // Graphs
var ctx = document.getElementById('myChart') const ctx = document.getElementById('myChart')
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
var myChart = new Chart(ctx, { const myChart = new Chart(ctx, {
type: 'line', type: 'line',
data: { data: {
labels: [ labels: [

View File

@ -1,14 +1,14 @@
/* globals Chart:false, feather:false */ /* globals Chart:false, feather:false */
(function () { (() => {
'use strict' 'use strict'
feather.replace({ 'aria-hidden': 'true' }) feather.replace({ 'aria-hidden': 'true' })
// Graphs // Graphs
var ctx = document.getElementById('myChart') const ctx = document.getElementById('myChart')
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
var myChart = new Chart(ctx, { const myChart = new Chart(ctx, {
type: 'line', type: 'line',
data: { data: {
labels: [ labels: [

View File

@ -1,7 +1,7 @@
(function () { (() => {
'use strict' 'use strict'
document.querySelector('#navbarSideCollapse').addEventListener('click', function () { document.querySelector('#navbarSideCollapse').addEventListener('click', () => {
document.querySelector('.offcanvas-collapse').classList.toggle('open') document.querySelector('.offcanvas-collapse').classList.toggle('open')
}) })
})() })()

View File

@ -1,8 +1,8 @@
/* global bootstrap: false */ /* global bootstrap: false */
(function () { (() => {
'use strict' 'use strict'
var tooltipTriggerList = Array.prototype.slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]')) const tooltipTriggerList = Array.from(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
tooltipTriggerList.forEach(function (tooltipTriggerEl) { tooltipTriggerList.forEach(tooltipTriggerEl => {
new bootstrap.Tooltip(tooltipTriggerEl) new bootstrap.Tooltip(tooltipTriggerEl)
}) })
})() })()

View File

@ -23,19 +23,16 @@
{{ if eq .Page.Layout "docs" -}} {{ if eq .Page.Layout "docs" -}}
<script> <script>
// Open in StackBlitz logic // Open in StackBlitz logic
document.querySelectorAll('.btn-edit') document.querySelectorAll('.btn-edit').forEach(btn => {
.forEach(function (btn) { btn.addEventListener('click', event => {
btn.addEventListener('click', function (event) { const htmlSnippet = event.target.closest('.bd-edit').previousSibling.innerHTML
var htmlSnippet = event.target.closest('.bd-edit').previousSibling.innerHTML
StackBlitzSDK.openBootstrapSnippet(htmlSnippet) StackBlitzSDK.openBootstrapSnippet(htmlSnippet)
})
}) })
})
StackBlitzSDK.openBootstrapSnippet = function(snippet) { StackBlitzSDK.openBootstrapSnippet = snippet => {
var project = { const markup = `<!doctype html>
files: {
'index.html': `<!doctype html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
@ -52,12 +49,16 @@ ${snippet.replace(/^/gm, ' ')}
<${'script'} src="{{ .Site.Params.cdn.js_bundle }}"></${'script'}> <${'script'} src="{{ .Site.Params.cdn.js_bundle }}"></${'script'}>
</body> </body>
</html>` </html>`
},
title: 'Bootstrap Example', const project = {
description: 'Official example from ' + window.location.href, files: {
template: 'html', 'index.html': markup
tags: ['bootstrap'] },
} title: 'Bootstrap Example',
description: `Official example from ${window.location.href}`,
template: 'html',
tags: ['bootstrap']
}
StackBlitzSDK.openProject(project, { openFile: 'index.html' }) StackBlitzSDK.openProject(project, { openFile: 'index.html' })
} }

View File

@ -1,20 +1,19 @@
// Example starter JavaScript for disabling form submissions if there are invalid fields // Example starter JavaScript for disabling form submissions if there are invalid fields
(function () { (() => {
'use strict' 'use strict'
// Fetch all the forms we want to apply custom Bootstrap validation styles to // Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.querySelectorAll('.needs-validation') const forms = document.querySelectorAll('.needs-validation')
// Loop over them and prevent submission // Loop over them and prevent submission
Array.prototype.slice.call(forms) Array.from(forms).forEach(form => {
.forEach(function (form) { form.addEventListener('submit', event => {
form.addEventListener('submit', function (event) { if (!form.checkValidity()) {
if (!form.checkValidity()) { event.preventDefault()
event.preventDefault() event.stopPropagation()
event.stopPropagation() }
}
form.classList.add('was-validated') form.classList.add('was-validated')
}, false) }, false)
}) })
})() })()