theme/boost/amd/src/loader.js

  1. // This file is part of Moodle - http://moodle.org/
  2. //
  3. // Moodle is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // Moodle is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * Template renderer for Moodle. Load and render Moodle templates with Mustache.
  17. *
  18. * @module theme_boost/loader
  19. * @copyright 2015 Damyon Wiese <damyon@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @since 2.9
  22. */
  23. import $ from 'jquery';
  24. import * as Aria from './aria';
  25. import Bootstrap from './index';
  26. import Pending from 'core/pending';
  27. import {DefaultWhitelist} from './bootstrap/tools/sanitizer';
  28. import setupBootstrapPendingChecks from './pending';
  29. /**
  30. * Rember the last visited tabs.
  31. */
  32. const rememberTabs = () => {
  33. $('a[data-toggle="tab"]').on('shown.bs.tab', function(e) {
  34. var hash = $(e.target).attr('href');
  35. if (history.replaceState) {
  36. history.replaceState(null, null, hash);
  37. } else {
  38. location.hash = hash;
  39. }
  40. });
  41. const hash = window.location.hash;
  42. if (hash) {
  43. const tab = document.querySelector('[role="tablist"] [href="' + hash + '"]');
  44. if (tab) {
  45. tab.click();
  46. }
  47. }
  48. };
  49. /**
  50. * Enable all popovers
  51. *
  52. */
  53. const enablePopovers = () => {
  54. $('body').popover({
  55. container: 'body',
  56. selector: '[data-toggle="popover"]',
  57. trigger: 'focus',
  58. whitelist: Object.assign(DefaultWhitelist, {
  59. table: [],
  60. thead: [],
  61. tbody: [],
  62. tr: [],
  63. th: [],
  64. td: [],
  65. }),
  66. });
  67. document.addEventListener('keydown', e => {
  68. if (e.key === 'Escape' && e.target.closest('[data-toggle="popover"]')) {
  69. $(e.target).popover('hide');
  70. }
  71. });
  72. };
  73. /**
  74. * Enable tooltips
  75. *
  76. */
  77. const enableTooltips = () => {
  78. $('body').tooltip({
  79. container: 'body',
  80. selector: '[data-toggle="tooltip"]',
  81. });
  82. };
  83. const pendingPromise = new Pending('theme_boost/loader:init');
  84. // Add pending promise event listeners to relevant Bootstrap custom events.
  85. setupBootstrapPendingChecks();
  86. // Setup Aria helpers for Bootstrap features.
  87. Aria.init();
  88. // Remember the last visited tabs.
  89. rememberTabs();
  90. // Enable all popovers.
  91. enablePopovers();
  92. // Enable all tooltips.
  93. enableTooltips();
  94. // Disables flipping the dropdowns up or dynamically repositioning them along the Y-axis (based on the viewport)
  95. // to prevent the dropdowns getting hidden behind the navbar or them covering the trigger element.
  96. $.fn.dropdown.Constructor.Default.popperConfig = {
  97. modifiers: {
  98. flip: {
  99. enabled: false,
  100. },
  101. storeTopPosition: {
  102. enabled: true,
  103. // eslint-disable-next-line no-unused-vars
  104. fn(data, options) {
  105. data.storedTop = data.offsets.popper.top;
  106. return data;
  107. },
  108. order: 299
  109. },
  110. restoreTopPosition: {
  111. enabled: true,
  112. // eslint-disable-next-line no-unused-vars
  113. fn(data, options) {
  114. data.offsets.popper.top = data.storedTop;
  115. return data;
  116. },
  117. order: 301
  118. }
  119. },
  120. };
  121. pendingPromise.resolve();
  122. export {
  123. Bootstrap,
  124. };