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