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. if (e.key === 'Enter' && e.target.closest('[data-toggle="popover"]')) {
  72. $(e.target).popover('show');
  73. }
  74. });
  75. document.addEventListener('click', e => {
  76. $(e.target).closest('[data-toggle="popover"]').popover('show');
  77. });
  78. };
  79. /**
  80. * Enable tooltips
  81. *
  82. */
  83. const enableTooltips = () => {
  84. $('body').tooltip({
  85. container: 'body',
  86. selector: '[data-toggle="tooltip"]',
  87. });
  88. document.addEventListener('keydown', e => {
  89. if (e.key === 'Escape') {
  90. // Hide tooltips on escape key press.
  91. $('.tooltip').tooltip('hide');
  92. }
  93. });
  94. };
  95. const pendingPromise = new Pending('theme_boost/loader:init');
  96. // Add pending promise event listeners to relevant Bootstrap custom events.
  97. setupBootstrapPendingChecks();
  98. // Setup Aria helpers for Bootstrap features.
  99. Aria.init();
  100. // Remember the last visited tabs.
  101. rememberTabs();
  102. // Enable all popovers.
  103. enablePopovers();
  104. // Enable all tooltips.
  105. enableTooltips();
  106. // Disables flipping the dropdowns up or dynamically repositioning them along the Y-axis (based on the viewport)
  107. // to prevent the dropdowns getting hidden behind the navbar or them covering the trigger element.
  108. $.fn.dropdown.Constructor.Default.popperConfig = {
  109. modifiers: {
  110. flip: {
  111. enabled: false,
  112. },
  113. storeTopPosition: {
  114. enabled: true,
  115. // eslint-disable-next-line no-unused-vars
  116. fn(data, options) {
  117. data.storedTop = data.offsets.popper.top;
  118. return data;
  119. },
  120. order: 299
  121. },
  122. restoreTopPosition: {
  123. enabled: true,
  124. // eslint-disable-next-line no-unused-vars
  125. fn(data, options) {
  126. data.offsets.popper.top = data.storedTop;
  127. return data;
  128. },
  129. order: 301
  130. }
  131. },
  132. };
  133. pendingPromise.resolve();
  134. export {
  135. Bootstrap,
  136. };