如何在Drupal 8上重置Bootstrap 4工具提示? [英] How to reset Bootstrap 4 Tooltips on Drupal 8?

查看:72
本文介绍了如何在Drupal 8上重置Bootstrap 4工具提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个网站:

  • 带有Bootstrap 5的静态网站.
  • 带有Bootstrap 4的Drupal 8站点.

我的问题与之前的问题有关:

My question relates to my previous question :

如何单击按钮时工具提示的文本会更改吗?

答案适用于Bootstrap 5,但在Drupal 8和Bootstrap 4的控制台中出现错误.

The answer works for Bootstrap 5 but I have an error in the console with Drupal 8 and Bootstrap 4.

我认为工具提示的初始化不正确.

I think the initialization of tooltip is not correct.

如何解决此问题?这是与Bootstap 5一起使用的代码,以及对Bootstrap 4的改编.

How can I correct this problem? Here is the code used with Bootstap 5 and an adaptation for Bootstrap 4.

我刚刚将 data-bs-original-title 更改为 data-original-title

我还修改了tooltip.js文件的代码

I also modified the code of the tooltip.js file

BOOTSTRAP 5

tooltip.js

tooltip.js

var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
  return new bootstrap.Tooltip(tooltipTriggerEl)
})

btn-clipboard.js

btn-clipboard.js

(function (doc, clip, boot) {

  var tooltipShareButton = doc.getElementById('btn-clipboard-share');
  var tooltipShare = new boot.Tooltip(tooltipShareButton);
  var clipboardShare = new clip('#btn-clipboard-share', {
    container: doc.getElementById('modal-share')
  });

  clipboardShare.on('success', function(e) {      
    function restoreTitle(e) {
      tooltipShare.hide();
      tooltipShareButton.setAttribute('data-bs-original-title', 'Copier le lien');
      tooltipShareButton.removeEventListener('mouseleave', restoreTitle);
    }
    tooltipShareButton.setAttribute('data-bs-original-title', 'Lien copié');
    tooltipShare.update();
    tooltipShare.show();
    tooltipShareButton.addEventListener('mouseleave', restoreTitle);
  });

  clipboardShare.on('error', function(e) {
    tooltipShareButton.setAttribute('data-bs-original-title', 'Erreur');
    function restoreTitle(e) {
      tooltipShareButton.setAttribute('data-bs-original-title', 'Copier le lien');
      tooltipShareButton.removeEventListener('mouseleave', restoreTitle);
    }
    tooltipShare.update();
    tooltipShare.show();
    tooltipShareButton.addEventListener('mouseleave', restoreTitle);
  });

  var tooltipDonationButton = doc.getElementById('btn-clipboard-donation');
  var tooltipDonation = new boot.Tooltip(tooltipDonationButton);
  var clipboardDonation = new clip('#btn-clipboard-donation', {
    container: doc.getElementById('modal-donation')
  });

  clipboardDonation.on('success', function() {
    function restoreTitle() {
      tooltipDonation.hide();
      tooltipDonationButton.setAttribute('data-bs-original-title', "Copier ladresse");
      tooltipDonationButton.removeEventListener('mouseleave', restoreTitle);
    }
    tooltipDonationButton.setAttribute('data-bs-original-title', 'Adresse copiée');
    tooltipDonation.hide();
    tooltipDonation.update();
    tooltipDonation.show();
    tooltipDonationButton.addEventListener('mouseleave', restoreTitle);
  });

  clipboardDonation.on('error', function(e) {
    function restoreTitle(e) {
      tooltipDonation.hide();
      tooltipDonationButton.setAttribute('data-bs-original-title', 'Copier ladresse');
      tooltipDonationButton.removeEventListener('mouseleave', restoreTitle);
    }
    tooltipDonationButton.setAttribute('data-bs-original-title', 'Erreur');
    tooltipDonation.hide();
    tooltipShare.update();
    tooltipShare.show();
    tooltipDonationButton.addEventListener('mouseleave', restoreTitle);
  });

}(document, ClipboardJS, bootstrap));

BOOTSTRAP 4

在此处输入图片描述

tooltip.js

tooltip.js

(function ($) {

  $(document).ready(function(){
    $('[data-toggle="tooltip"]').tooltip();
  });

})(jQuery);

btn-clipboard.js

btn-clipboard.js

(function (doc, clip, boot) {

  var tooltipShareButton = doc.getElementById('btn-clipboard-share');
  var tooltipShare = new boot.Tooltip(tooltipShareButton);
  var clipboardShare = new clip('#btn-clipboard-share', {
    container: doc.getElementById('modal-share')
  });

  clipboardShare.on('success', function(e) {      
    function restoreTitle(e) {
      tooltipShare.hide();
      tooltipShareButton.setAttribute('data-original-title', 'Copier le lien');
      tooltipShareButton.removeEventListener('mouseleave', restoreTitle);
    }
    tooltipShareButton.setAttribute('data-original-title', 'Lien copié');
    tooltipShare.update();
    tooltipShare.show();
    tooltipShareButton.addEventListener('mouseleave', restoreTitle);
  });

  clipboardShare.on('error', function(e) {
    tooltipShareButton.setAttribute('data-original-title', 'Erreur');
    function restoreTitle(e) {
      tooltipShareButton.setAttribute('data-original-title', 'Copier le lien');
      tooltipShareButton.removeEventListener('mouseleave', restoreTitle);
    }
    tooltipShare.update();
    tooltipShare.show();
    tooltipShareButton.addEventListener('mouseleave', restoreTitle);
  });

  var tooltipDonationButton = doc.getElementById('btn-clipboard-donation');
  var tooltipDonation = new boot.Tooltip(tooltipDonationButton);
  var clipboardDonation = new clip('#btn-clipboard-donation', {
    container: doc.getElementById('modal-donation')
  });

  clipboardDonation.on('success', function() {
    function restoreTitle() {
      tooltipDonation.hide();
      tooltipDonationButton.setAttribute('data-original-title', "Copier ladresse");
      tooltipDonationButton.removeEventListener('mouseleave', restoreTitle);
    }
    tooltipDonationButton.setAttribute('data-original-title', 'Adresse copiée');
    tooltipDonation.hide();
    tooltipDonation.update();
    tooltipDonation.show();
    tooltipDonationButton.addEventListener('mouseleave', restoreTitle);
  });

  clipboardDonation.on('error', function(e) {
    function restoreTitle(e) {
      tooltipDonation.hide();
      tooltipDonationButton.setAttribute('data-original-title', 'Copier ladresse');
      tooltipDonationButton.removeEventListener('mouseleave', restoreTitle);
    }
    tooltipDonationButton.setAttribute('data-original-title', 'Erreur');
    tooltipDonation.hide();
    tooltipShare.update();
    tooltipShare.show();
    tooltipDonationButton.addEventListener('mouseleave', restoreTitle);
  });

}(document, ClipboardJS, bootstrap));

推荐答案

按照您使用ClipboardJS的方式,使用Bootstrap的工具提示在模式上复制按钮上的内容,以显示在剪贴板上成功复制到剪贴板您提供的Bootstrap 5示例,我已经复制了Bootstrap 4的功能.

Following the way you’re using ClipboardJS to copy the content of the buttons on your modals using Bootstrap’s tooltips to show the copy-to-clipboard was successful on the Bootstrap 5 example you provided, I have duplicated the function for Bootstrap 4.

虽然Bootstrap 5在Bootstrap的JavaScript中具有工具提示功能,但是Bootstrap 4扩展了jQuery以处理工具提示功能,因此我将jQuery函数的工具提示的Bootstrap 5实例替换了.

While Bootstrap 5 has the tooltip function as part of Bootstrap’s JavaScript, Bootstrap 4 extends jQuery to handle tooltip functions, so I replaced the Bootstrap 5 instances of the tooltip with jQuery functions.

(function ($, doc, clip) {
    var $shareBtn = $("#btn-clipboard-share");
    var $donateBtn = $("#btn-clipboard-donation");
    var clipboardShare;
    var clipboardDonation;

    // Initialize tooltips
    $shareBtn.tooltip();
    $donateBtn.tooltip();

    clipboardShare = new clip("#btn-clipboard-share", {
        container: doc.getElementById("modal-share")
    });

    clipboardShare.on("success", function () {

        function restoreTitle() {
            $shareBtn.tooltip("hide");
            $shareBtn.attr("data-original-title", "Copier l'adresse");
            $shareBtn.off("mouseleave.clipboard", restoreTitle);
        }

        $shareBtn.attr("data-original-title", "Lien copié");
        $shareBtn.tooltip("update");
        $shareBtn.tooltip("show");
        $shareBtn.on("mouseleave.clipboard", restoreTitle);
    });

    clipboardShare.on("error", function () {

        function restoreTitle() {
            $shareBtn.tooltip("hide");
            $shareBtn.attr("data-original-title", "Copier l'adresse");
            $shareBtn.off("mouseleave.clipboard", restoreTitle);
        }

        $shareBtn.attr("data-original-title", "Erreur");
        $shareBtn.tooltip("update");
        $shareBtn.tooltip("show");
        $shareBtn.on("mouseleave.clipboard", restoreTitle);
    });

    clipboardDonation = new clip("#btn-clipboard-donation", {
        container: doc.getElementById("modal-donation")
    });

    clipboardDonation.on("success", function () {

        function restoreTitle() {
            $donateBtn.tooltip("hide");
            $donateBtn.attr("data-original-title", "Copier l'adresse");
            $donateBtn.off("mouseleave.clipboard", restoreTitle);
        }

        $donateBtn.attr("data-original-title", "Adresse copiée");
        $donateBtn.tooltip("update");
        $donateBtn.tooltip("show");
        $donateBtn.on("mouseleave.clipboard", restoreTitle);
    });

    clipboardDonation.on("error", function () {

        function restoreTitle() {
            $donateBtn.tooltip("hide");
            $donateBtn.attr("data-original-title", "Copier l'adresse");
            $donateBtn.off("mouseleave.clipboard", restoreTitle);
        }

        $donateBtn.attr("data-original-title", "Erreur");
        $donateBtn.tooltip("update");
        $donateBtn.tooltip("show");
        $donateBtn.on("mouseleave.clipboard", restoreTitle);
    });
}(jQuery, document, ClipboardJS));

<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/clipboard@2.0.8/dist/clipboard.min.js"></script>

<div class="container d-flex justify-content-center mt-5">
    <!-- Buttons triggering modals -->
    <button type="button" class="btn btn-outline-success mr-3" data-toggle="modal" data-target="#modal-share">
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-share" viewBox="0 0 16 16">
            <path d="M13.5 1a1.5 1.5 0 1 0 0 3 1.5 1.5 0 0 0 0-3zM11 2.5a2.5 2.5 0 1 1 .603 1.628l-6.718 3.12a2.499 2.499 0 0 1 0 1.504l6.718 3.12a2.5 2.5 0 1 1-.488.876l-6.718-3.12a2.5 2.5 0 1 1 0-3.256l6.718-3.12A2.5 2.5 0 0 1 11 2.5zm-8.5 4a1.5 1.5 0 1 0 0 3 1.5 1.5 0 0 0 0-3zm11 5.5a1.5 1.5 0 1 0 0 3 1.5 1.5 0 0 0 0-3z"></path>
        </svg> Partager
    </button>

    <button type="button" class="btn btn-outline-success ml-3" data-toggle="modal" data-target="#modal-donation">
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-suit-heart-fill" viewBox="0 0 16 16">
            <path d="M4 1c2.21 0 4 1.755 4 3.92C8 2.755 9.79 1 12 1s4 1.755 4 3.92c0 3.263-3.234 4.414-7.608 9.608a.513.513 0 0 1-.784 0C3.234 9.334 0 8.183 0 4.92 0 2.755 1.79 1 4 1z"></path>
        </svg> Faire un don
    </button>

    <!-- Modals -->
    <div class="modal fade show" id="modal-share" tabindex="-1" aria-labelledby="partager" aria-hidden="true" aria-modal="true" role="dialog">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" id="partager">Partager cette page</h4>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <p class="text-justify">Lorem ipsum dolor sit amet consectetur adipisicing elit. Numquam expedita consectetur laudantium iusto necessitatibus minima.</p>
                    <button id="btn-clipboard-share" class="btn btn-outline-dark btn-sm w-100" data-toggle="tooltip" data-placement="top" title="" data-clipboard-text="https://www.example.com/fr/groupe/le-blog" data-original-title="Copier l'adresse">
                        https://www.example.com/fr/groupe/le-blog
                        <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" fill="currentColor" class="bi bi-clipboard" viewBox="0 0 16 16">
                            <path d="M4 1.5H3a2 2 0 0 0-2 2V14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V3.5a2 2 0 0 0-2-2h-1v1h1a1 1 0 0 1 1 1V14a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3.5a1 1 0 0 1 1-1h1v-1z"></path>
                            <path d="M9.5 1a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5h-3a.5.5 0 0 1-.5-.5v-1a.5.5 0 0 1 .5-.5h3zm-3-1A1.5 1.5 0 0 0 5 1.5v1A1.5 1.5 0 0 0 6.5 4h3A1.5 1.5 0 0 0 11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3z"></path>
                        </svg>
                    </button>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade show" id="modal-donation" tabindex="-1" aria-labelledby="donation" aria-hidden="true" aria-modal="true" role="dialog">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" id="donation">Faire un don</h4>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <div class="message-donation">
                        <p class="text-justify">Si le contenu de ce groupe vous plaît, vous pouvez faire un don à son l'auteur en Bitcoin (BTC).</p>
                        <p class="mb-3"><a href="https://www.example.com/btc/address/1A5GqrNbpo7xwurpt1VQVvcA5yzoEcgaFvff" rel="nofollow" target="_blank">Voir les transactions sur la Blockchain</a></p>
                        <h5>Adresse BTC :</h5>
                        <p><img class="img-fluid" src="https://via.placeholder.com/300x300.png?text=QR%20Code" alt="Sample QR Code">
                        </p>
                        <button id="btn-clipboard-donation" class="btn btn-outline-dark btn-sm text-break" data-toggle="tooltip" data-placement="top" title="" data-clipboard-text="1A5GqrNbpo7xwurpt1VQVvcA5yzoEcgaFvff" data-original-title="Copier l'adresse">
                            1A5GqrNbpo7xwurpt1VQVvcA5yzoEcgaFvff
                            <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" fill="currentColor" class="bi bi-clipboard" viewBox="0 0 16 16">
                                <path d="M4 1.5H3a2 2 0 0 0-2 2V14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V3.5a2 2 0 0 0-2-2h-1v1h1a1 1 0 0 1 1 1V14a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3.5a1 1 0 0 1 1-1h1v-1z"></path>
                                <path d="M9.5 1a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5h-3a.5.5 0 0 1-.5-.5v-1a.5.5 0 0 1 .5-.5h3zm-3-1A1.5 1.5 0 0 0 5 1.5v1A1.5 1.5 0 0 0 6.5 4h3A1.5 1.5 0 0 0 11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3z"></path>
                            </svg>
                        </button>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
</div>

我确实使用example.com代替了您的URL,对代码进行了匿名处理,将QR码替换为灰色图像,并替换了地址哈希.

I did anonomize the code, using example.com in place of your URL, replaced the QR code with a gray image, and replaced the address hash.

这篇关于如何在Drupal 8上重置Bootstrap 4工具提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆