source: imaps-frontend/node_modules/bootstrap/js/src/util/config.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.7 KB
Line 
1/**
2 * --------------------------------------------------------------------------
3 * Bootstrap util/config.js
4 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
5 * --------------------------------------------------------------------------
6 */
7
8import Manipulator from '../dom/manipulator.js'
9import { isElement, toType } from './index.js'
10
11/**
12 * Class definition
13 */
14
15class Config {
16 // Getters
17 static get Default() {
18 return {}
19 }
20
21 static get DefaultType() {
22 return {}
23 }
24
25 static get NAME() {
26 throw new Error('You have to implement the static method "NAME", for each component!')
27 }
28
29 _getConfig(config) {
30 config = this._mergeConfigObj(config)
31 config = this._configAfterMerge(config)
32 this._typeCheckConfig(config)
33 return config
34 }
35
36 _configAfterMerge(config) {
37 return config
38 }
39
40 _mergeConfigObj(config, element) {
41 const jsonConfig = isElement(element) ? Manipulator.getDataAttribute(element, 'config') : {} // try to parse
42
43 return {
44 ...this.constructor.Default,
45 ...(typeof jsonConfig === 'object' ? jsonConfig : {}),
46 ...(isElement(element) ? Manipulator.getDataAttributes(element) : {}),
47 ...(typeof config === 'object' ? config : {})
48 }
49 }
50
51 _typeCheckConfig(config, configTypes = this.constructor.DefaultType) {
52 for (const [property, expectedTypes] of Object.entries(configTypes)) {
53 const value = config[property]
54 const valueType = isElement(value) ? 'element' : toType(value)
55
56 if (!new RegExp(expectedTypes).test(valueType)) {
57 throw new TypeError(
58 `${this.constructor.NAME.toUpperCase()}: Option "${property}" provided type "${valueType}" but expected type "${expectedTypes}".`
59 )
60 }
61 }
62 }
63}
64
65export default Config
Note: See TracBrowser for help on using the repository browser.