|
| 1 | +// NOTICE!!! Initially embedded in our docs this JavaScript |
| 2 | +// file contains elements that can help you create reproducible |
| 3 | +// use cases in StackBlitz for instance. |
| 4 | +// In a real project please adapt this content to your needs. |
| 5 | +// ++++++++++++++++++++++++++++++++++++++++++ |
| 6 | + |
| 7 | +/*! |
| 8 | + * JavaScript for CoreUI's docs (https://coreui.io/) |
| 9 | + * Copyright 2024 creativeLabs Lukasz Holeczek |
| 10 | + * Licensed under the Creative Commons Attribution 3.0 Unported License. |
| 11 | + * For details, see https://creativecommons.org/licenses/by/3.0/. |
| 12 | + */ |
| 13 | + |
| 14 | +/* global coreui: false */ |
| 15 | + |
| 16 | +export default () => { |
| 17 | + // -------- |
| 18 | + // Tooltips |
| 19 | + // -------- |
| 20 | + // Instantiate all tooltips in a docs or StackBlitz |
| 21 | + document.querySelectorAll('[data-coreui-toggle="tooltip"]') |
| 22 | + .forEach(tooltip => { |
| 23 | + new coreui.Tooltip(tooltip) |
| 24 | + }) |
| 25 | + |
| 26 | + // -------- |
| 27 | + // Popovers |
| 28 | + // -------- |
| 29 | + // Instantiate all popovers in docs or StackBlitz |
| 30 | + document.querySelectorAll('[data-coreui-toggle="popover"]') |
| 31 | + .forEach(popover => { |
| 32 | + new coreui.Popover(popover) |
| 33 | + }) |
| 34 | + |
| 35 | + // ------------------------------- |
| 36 | + // Toasts |
| 37 | + // ------------------------------- |
| 38 | + // Used by 'Placement' example in docs or StackBlitz |
| 39 | + const toastPlacement = document.getElementById('toastPlacement') |
| 40 | + if (toastPlacement) { |
| 41 | + document.getElementById('selectToastPlacement').addEventListener('change', function () { |
| 42 | + if (!toastPlacement.dataset.originalClass) { |
| 43 | + toastPlacement.dataset.originalClass = toastPlacement.className |
| 44 | + } |
| 45 | + |
| 46 | + toastPlacement.className = `${toastPlacement.dataset.originalClass} ${this.value}` |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + // Instantiate all toasts in docs pages only |
| 51 | + document.querySelectorAll('.docs-example .toast') |
| 52 | + .forEach(toastNode => { |
| 53 | + const toast = new coreui.Toast(toastNode, { |
| 54 | + autohide: false |
| 55 | + }) |
| 56 | + |
| 57 | + toast.show() |
| 58 | + }) |
| 59 | + |
| 60 | + // Instantiate all toasts in docs pages only |
| 61 | + // js-docs-start live-toast |
| 62 | + const toastTrigger = document.getElementById('liveToastBtn') |
| 63 | + const toastLiveExample = document.getElementById('liveToast') |
| 64 | + |
| 65 | + if (toastTrigger) { |
| 66 | + const toastCoreUI = coreui.Toast.getOrCreateInstance(toastLiveExample) |
| 67 | + toastTrigger.addEventListener('click', () => { |
| 68 | + toastCoreUI.show() |
| 69 | + }) |
| 70 | + } |
| 71 | + // js-docs-end live-toast |
| 72 | + |
| 73 | + // ------------------------------- |
| 74 | + // Alerts |
| 75 | + // ------------------------------- |
| 76 | + // Used in 'Show live alert' example in docs or StackBlitz |
| 77 | + |
| 78 | + // js-docs-start live-alert |
| 79 | + const alertPlaceholder = document.getElementById('liveAlertPlaceholder') |
| 80 | + const appendAlert = (message, type) => { |
| 81 | + const wrapper = document.createElement('div') |
| 82 | + wrapper.innerHTML = [ |
| 83 | + `<div class="alert alert-${type} alert-dismissible" role="alert">`, |
| 84 | + ` <div>${message}</div>`, |
| 85 | + ' <button type="button" class="btn-close" data-coreui-dismiss="alert" aria-label="Close"></button>', |
| 86 | + '</div>' |
| 87 | + ].join('') |
| 88 | + |
| 89 | + alertPlaceholder.append(wrapper) |
| 90 | + } |
| 91 | + |
| 92 | + const alertTrigger = document.getElementById('liveAlertBtn') |
| 93 | + if (alertTrigger) { |
| 94 | + alertTrigger.addEventListener('click', () => { |
| 95 | + appendAlert('Nice, you triggered this alert message!', 'success') |
| 96 | + }) |
| 97 | + } |
| 98 | + // js-docs-end live-alert |
| 99 | + |
| 100 | + // -------- |
| 101 | + // Carousels |
| 102 | + // -------- |
| 103 | + // Instantiate all non-autoplaying carousels in docs or StackBlitz |
| 104 | + document.querySelectorAll('.carousel:not([data-coreui-ride="carousel"])') |
| 105 | + .forEach(carousel => { |
| 106 | + coreui.Carousel.getOrCreateInstance(carousel) |
| 107 | + }) |
| 108 | + |
| 109 | + // ------------------------------- |
| 110 | + // Checks & Radios |
| 111 | + // ------------------------------- |
| 112 | + // Indeterminate checkbox example in docs and StackBlitz |
| 113 | + document.querySelectorAll('.docs-example-indeterminate [type="checkbox"]') |
| 114 | + .forEach(checkbox => { |
| 115 | + if (checkbox.id.includes('Indeterminate')) { |
| 116 | + checkbox.indeterminate = true |
| 117 | + } |
| 118 | + }) |
| 119 | + |
| 120 | + // ------------------------------- |
| 121 | + // Links |
| 122 | + // ------------------------------- |
| 123 | + // Disable empty links in docs examples only |
| 124 | + document.querySelectorAll('.docs-content [href="#"]') |
| 125 | + .forEach(link => { |
| 126 | + link.addEventListener('click', event => { |
| 127 | + event.preventDefault() |
| 128 | + }) |
| 129 | + }) |
| 130 | + |
| 131 | + // ------------------------------- |
| 132 | + // Modal |
| 133 | + // ------------------------------- |
| 134 | + // Modal 'Varying modal content' example in docs and StackBlitz |
| 135 | + // js-docs-start varying-modal-content |
| 136 | + const exampleModal = document.getElementById('exampleModal') |
| 137 | + if (exampleModal) { |
| 138 | + exampleModal.addEventListener('show.coreui.modal', event => { |
| 139 | + // Button that triggered the modal |
| 140 | + const button = event.relatedTarget |
| 141 | + // Extract info from data-coreui-* attributes |
| 142 | + const recipient = button.getAttribute('data-coreui-whatever') |
| 143 | + // If necessary, you could initiate an Ajax request here |
| 144 | + // and then do the updating in a callback. |
| 145 | + |
| 146 | + // Update the modal's content. |
| 147 | + const modalTitle = exampleModal.querySelector('.modal-title') |
| 148 | + const modalBodyInput = exampleModal.querySelector('.modal-body input') |
| 149 | + |
| 150 | + modalTitle.textContent = `New message to ${recipient}` |
| 151 | + modalBodyInput.value = recipient |
| 152 | + }) |
| 153 | + } |
| 154 | + // js-docs-end varying-modal-content |
| 155 | + |
| 156 | + // ------------------------------- |
| 157 | + // Offcanvas |
| 158 | + // ------------------------------- |
| 159 | + // 'Offcanvas components' example in docs only |
| 160 | + const myOffcanvas = document.querySelectorAll('.docs-example-offcanvas .offcanvas') |
| 161 | + if (myOffcanvas) { |
| 162 | + myOffcanvas.forEach(offcanvas => { |
| 163 | + offcanvas.addEventListener('show.coreui.offcanvas', event => { |
| 164 | + event.preventDefault() |
| 165 | + }, false) |
| 166 | + }) |
| 167 | + } |
| 168 | +} |
0 commit comments