Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
/**
2
 * --------------------------------------------------------------------------
3
 * Bootstrap util/config.js
4
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
5
 * --------------------------------------------------------------------------
6
 */
7
 
8
import Manipulator from '../dom/manipulator'
9
import { isElement, toType } from './index'
10
 
11
/**
12
 * Class definition
13
 */
14
 
15
class 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
 
65
export default Config