Source: plot/core/utils/PlotlyNotifier.js

  1. export { PlotlyNotifier }
  2. /**
  3. * Class to simulate plotly's notification function and write own notification messages.
  4. * Required because it is not accessible from outside.
  5. */
  6. class PlotlyNotifier {
  7. /**
  8. * Creates the default plotly notification container if it does not already exist and then returns it.
  9. * @returns {HTMLDivElement} - Notification container.
  10. */
  11. static getPlotlyNotificationContainer() {
  12. let targetNode = document.body.querySelector('.plotly-notifier')
  13. if (!targetNode) {
  14. const defaultPlotlyNotifierElement = document.createElement('div');
  15. defaultPlotlyNotifierElement.classList.add('plotly-notifier');
  16. document.body.append(defaultPlotlyNotifierElement);
  17. targetNode = defaultPlotlyNotifierElement;
  18. }
  19. return targetNode
  20. }
  21. /**
  22. * Create the default plotly notification element containing the notification message.
  23. * @param {String} message - Message text.
  24. * @returns {HTMLDivElement} - Notification element.
  25. */
  26. static _createNotification(message) {
  27. const notifierNote = document.createElement('div');
  28. notifierNote.classList.add('notifier-note', 'notifier-note-custom', 'open');
  29. notifierNote.innerHTML = `
  30. <button class="notifier-close">×</button>
  31. <p>
  32. <span>${message}</span>
  33. </p>
  34. `;
  35. return notifierNote
  36. }
  37. /**
  38. * Displays a custom message in Plotly's default DOM node.
  39. * @param {String} message - Message text.
  40. * @param {Number} duration - How long the message should be displayed.
  41. */
  42. static displayMessage(message, duration) {
  43. const notifierContainer = PlotlyNotifier.getPlotlyNotificationContainer();
  44. const notifierNote = PlotlyNotifier._createNotification(message);
  45. notifierContainer.appendChild(notifierNote);
  46. const removeNotification = () => {
  47. notifierNote.classList.replace('open', 'closed');
  48. setTimeout(() => {
  49. notifierNote.remove();
  50. if (notifierContainer.childElementCount == 0 && !notifierContainer.classList.contains('observed')) {
  51. notifierContainer.remove()
  52. }
  53. }, 700);
  54. }
  55. notifierNote.querySelector(".notifier-close").addEventListener("click", removeNotification);
  56. setTimeout(removeNotification, duration);
  57. }
  58. }