You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.6 KiB

8 months ago
  1. /*!
  2. * Bootstrap template-factory.js v5.3.3 (https://getbootstrap.com/)
  3. * Copyright 2011-2024 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
  4. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  5. */
  6. (function (global, factory) {
  7. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('../dom/selector-engine.js'), require('./config.js'), require('./sanitizer.js'), require('./index.js')) :
  8. typeof define === 'function' && define.amd ? define(['../dom/selector-engine', './config', './sanitizer', './index'], factory) :
  9. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.TemplateFactory = factory(global.SelectorEngine, global.Config, global.Sanitizer, global.Index));
  10. })(this, (function (SelectorEngine, Config, sanitizer_js, index_js) { 'use strict';
  11. /**
  12. * --------------------------------------------------------------------------
  13. * Bootstrap util/template-factory.js
  14. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  15. * --------------------------------------------------------------------------
  16. */
  17. /**
  18. * Constants
  19. */
  20. const NAME = 'TemplateFactory';
  21. const Default = {
  22. allowList: sanitizer_js.DefaultAllowlist,
  23. content: {},
  24. // { selector : text , selector2 : text2 , }
  25. extraClass: '',
  26. html: false,
  27. sanitize: true,
  28. sanitizeFn: null,
  29. template: '<div></div>'
  30. };
  31. const DefaultType = {
  32. allowList: 'object',
  33. content: 'object',
  34. extraClass: '(string|function)',
  35. html: 'boolean',
  36. sanitize: 'boolean',
  37. sanitizeFn: '(null|function)',
  38. template: 'string'
  39. };
  40. const DefaultContentType = {
  41. entry: '(string|element|function|null)',
  42. selector: '(string|element)'
  43. };
  44. /**
  45. * Class definition
  46. */
  47. class TemplateFactory extends Config {
  48. constructor(config) {
  49. super();
  50. this._config = this._getConfig(config);
  51. }
  52. // Getters
  53. static get Default() {
  54. return Default;
  55. }
  56. static get DefaultType() {
  57. return DefaultType;
  58. }
  59. static get NAME() {
  60. return NAME;
  61. }
  62. // Public
  63. getContent() {
  64. return Object.values(this._config.content).map(config => this._resolvePossibleFunction(config)).filter(Boolean);
  65. }
  66. hasContent() {
  67. return this.getContent().length > 0;
  68. }
  69. changeContent(content) {
  70. this._checkContent(content);
  71. this._config.content = {
  72. ...this._config.content,
  73. ...content
  74. };
  75. return this;
  76. }
  77. toHtml() {
  78. const templateWrapper = document.createElement('div');
  79. templateWrapper.innerHTML = this._maybeSanitize(this._config.template);
  80. for (const [selector, text] of Object.entries(this._config.content)) {
  81. this._setContent(templateWrapper, text, selector);
  82. }
  83. const template = templateWrapper.children[0];
  84. const extraClass = this._resolvePossibleFunction(this._config.extraClass);
  85. if (extraClass) {
  86. template.classList.add(...extraClass.split(' '));
  87. }
  88. return template;
  89. }
  90. // Private
  91. _typeCheckConfig(config) {
  92. super._typeCheckConfig(config);
  93. this._checkContent(config.content);
  94. }
  95. _checkContent(arg) {
  96. for (const [selector, content] of Object.entries(arg)) {
  97. super._typeCheckConfig({
  98. selector,
  99. entry: content
  100. }, DefaultContentType);
  101. }
  102. }
  103. _setContent(template, content, selector) {
  104. const templateElement = SelectorEngine.findOne(selector, template);
  105. if (!templateElement) {
  106. return;
  107. }
  108. content = this._resolvePossibleFunction(content);
  109. if (!content) {
  110. templateElement.remove();
  111. return;
  112. }
  113. if (index_js.isElement(content)) {
  114. this._putElementInTemplate(index_js.getElement(content), templateElement);
  115. return;
  116. }
  117. if (this._config.html) {
  118. templateElement.innerHTML = this._maybeSanitize(content);
  119. return;
  120. }
  121. templateElement.textContent = content;
  122. }
  123. _maybeSanitize(arg) {
  124. return this._config.sanitize ? sanitizer_js.sanitizeHtml(arg, this._config.allowList, this._config.sanitizeFn) : arg;
  125. }
  126. _resolvePossibleFunction(arg) {
  127. return index_js.execute(arg, [this]);
  128. }
  129. _putElementInTemplate(element, templateElement) {
  130. if (this._config.html) {
  131. templateElement.innerHTML = '';
  132. templateElement.append(element);
  133. return;
  134. }
  135. templateElement.textContent = element.textContent;
  136. }
  137. }
  138. return TemplateFactory;
  139. }));
  140. //# sourceMappingURL=template-factory.js.map