Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
/**
2
 * --------------------------------------------------------------------------
1441 ariadna 3
 * Bootstrap collapse.js
1 efrain 4
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
5
 * --------------------------------------------------------------------------
6
 */
7
 
1441 ariadna 8
import BaseComponent from './base-component'
9
import EventHandler from './dom/event-handler'
10
import SelectorEngine from './dom/selector-engine'
11
import {
12
  defineJQueryPlugin,
13
  getElement,
14
  reflow
15
} from './util/index'
1 efrain 16
 
17
/**
18
 * Constants
19
 */
20
 
21
const NAME = 'collapse'
22
const DATA_KEY = 'bs.collapse'
23
const EVENT_KEY = `.${DATA_KEY}`
24
const DATA_API_KEY = '.data-api'
25
 
1441 ariadna 26
const EVENT_SHOW = `show${EVENT_KEY}`
27
const EVENT_SHOWN = `shown${EVENT_KEY}`
28
const EVENT_HIDE = `hide${EVENT_KEY}`
29
const EVENT_HIDDEN = `hidden${EVENT_KEY}`
30
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
31
 
1 efrain 32
const CLASS_NAME_SHOW = 'show'
33
const CLASS_NAME_COLLAPSE = 'collapse'
34
const CLASS_NAME_COLLAPSING = 'collapsing'
35
const CLASS_NAME_COLLAPSED = 'collapsed'
1441 ariadna 36
const CLASS_NAME_DEEPER_CHILDREN = `:scope .${CLASS_NAME_COLLAPSE} .${CLASS_NAME_COLLAPSE}`
37
const CLASS_NAME_HORIZONTAL = 'collapse-horizontal'
1 efrain 38
 
1441 ariadna 39
const WIDTH = 'width'
40
const HEIGHT = 'height'
1 efrain 41
 
1441 ariadna 42
const SELECTOR_ACTIVES = '.collapse.show, .collapse.collapsing'
43
const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="collapse"]'
1 efrain 44
 
45
const Default = {
1441 ariadna 46
  parent: null,
47
  toggle: true
1 efrain 48
}
49
 
50
const DefaultType = {
1441 ariadna 51
  parent: '(null|element)',
52
  toggle: 'boolean'
1 efrain 53
}
54
 
55
/**
56
 * Class definition
57
 */
58
 
1441 ariadna 59
class Collapse extends BaseComponent {
1 efrain 60
  constructor(element, config) {
1441 ariadna 61
    super(element, config)
62
 
1 efrain 63
    this._isTransitioning = false
1441 ariadna 64
    this._triggerArray = []
1 efrain 65
 
1441 ariadna 66
    const toggleList = SelectorEngine.find(SELECTOR_DATA_TOGGLE)
1 efrain 67
 
1441 ariadna 68
    for (const elem of toggleList) {
69
      const selector = SelectorEngine.getSelectorFromElement(elem)
70
      const filterElement = SelectorEngine.find(selector)
71
        .filter(foundElement => foundElement === this._element)
72
 
73
      if (selector !== null && filterElement.length) {
1 efrain 74
        this._triggerArray.push(elem)
75
      }
76
    }
77
 
1441 ariadna 78
    this._initializeChildren()
1 efrain 79
 
80
    if (!this._config.parent) {
1441 ariadna 81
      this._addAriaAndCollapsedClass(this._triggerArray, this._isShown())
1 efrain 82
    }
83
 
84
    if (this._config.toggle) {
85
      this.toggle()
86
    }
87
  }
88
 
89
  // Getters
90
  static get Default() {
91
    return Default
92
  }
93
 
1441 ariadna 94
  static get DefaultType() {
95
    return DefaultType
96
  }
97
 
98
  static get NAME() {
99
    return NAME
100
  }
101
 
1 efrain 102
  // Public
103
  toggle() {
1441 ariadna 104
    if (this._isShown()) {
1 efrain 105
      this.hide()
106
    } else {
107
      this.show()
108
    }
109
  }
110
 
111
  show() {
1441 ariadna 112
    if (this._isTransitioning || this._isShown()) {
1 efrain 113
      return
114
    }
115
 
1441 ariadna 116
    let activeChildren = []
1 efrain 117
 
1441 ariadna 118
    // find active children
119
    if (this._config.parent) {
120
      activeChildren = this._getFirstLevelChildren(SELECTOR_ACTIVES)
121
        .filter(element => element !== this._element)
122
        .map(element => Collapse.getOrCreateInstance(element, { toggle: false }))
1 efrain 123
    }
124
 
1441 ariadna 125
    if (activeChildren.length && activeChildren[0]._isTransitioning) {
126
      return
1 efrain 127
    }
128
 
1441 ariadna 129
    const startEvent = EventHandler.trigger(this._element, EVENT_SHOW)
130
    if (startEvent.defaultPrevented) {
1 efrain 131
      return
132
    }
133
 
1441 ariadna 134
    for (const activeInstance of activeChildren) {
135
      activeInstance.hide()
1 efrain 136
    }
137
 
138
    const dimension = this._getDimension()
139
 
1441 ariadna 140
    this._element.classList.remove(CLASS_NAME_COLLAPSE)
141
    this._element.classList.add(CLASS_NAME_COLLAPSING)
1 efrain 142
 
143
    this._element.style[dimension] = 0
144
 
1441 ariadna 145
    this._addAriaAndCollapsedClass(this._triggerArray, true)
146
    this._isTransitioning = true
1 efrain 147
 
148
    const complete = () => {
1441 ariadna 149
      this._isTransitioning = false
1 efrain 150
 
1441 ariadna 151
      this._element.classList.remove(CLASS_NAME_COLLAPSING)
152
      this._element.classList.add(CLASS_NAME_COLLAPSE, CLASS_NAME_SHOW)
153
 
1 efrain 154
      this._element.style[dimension] = ''
155
 
1441 ariadna 156
      EventHandler.trigger(this._element, EVENT_SHOWN)
1 efrain 157
    }
158
 
159
    const capitalizedDimension = dimension[0].toUpperCase() + dimension.slice(1)
160
    const scrollSize = `scroll${capitalizedDimension}`
161
 
1441 ariadna 162
    this._queueCallback(complete, this._element, true)
1 efrain 163
    this._element.style[dimension] = `${this._element[scrollSize]}px`
164
  }
165
 
166
  hide() {
1441 ariadna 167
    if (this._isTransitioning || !this._isShown()) {
1 efrain 168
      return
169
    }
170
 
1441 ariadna 171
    const startEvent = EventHandler.trigger(this._element, EVENT_HIDE)
172
    if (startEvent.defaultPrevented) {
1 efrain 173
      return
174
    }
175
 
176
    const dimension = this._getDimension()
177
 
178
    this._element.style[dimension] = `${this._element.getBoundingClientRect()[dimension]}px`
179
 
1441 ariadna 180
    reflow(this._element)
1 efrain 181
 
1441 ariadna 182
    this._element.classList.add(CLASS_NAME_COLLAPSING)
183
    this._element.classList.remove(CLASS_NAME_COLLAPSE, CLASS_NAME_SHOW)
1 efrain 184
 
1441 ariadna 185
    for (const trigger of this._triggerArray) {
186
      const element = SelectorEngine.getElementFromSelector(trigger)
1 efrain 187
 
1441 ariadna 188
      if (element && !this._isShown(element)) {
189
        this._addAriaAndCollapsedClass([trigger], false)
1 efrain 190
      }
191
    }
192
 
1441 ariadna 193
    this._isTransitioning = true
1 efrain 194
 
195
    const complete = () => {
1441 ariadna 196
      this._isTransitioning = false
197
      this._element.classList.remove(CLASS_NAME_COLLAPSING)
198
      this._element.classList.add(CLASS_NAME_COLLAPSE)
199
      EventHandler.trigger(this._element, EVENT_HIDDEN)
1 efrain 200
    }
201
 
202
    this._element.style[dimension] = ''
203
 
1441 ariadna 204
    this._queueCallback(complete, this._element, true)
1 efrain 205
  }
206
 
1441 ariadna 207
  _isShown(element = this._element) {
208
    return element.classList.contains(CLASS_NAME_SHOW)
1 efrain 209
  }
210
 
211
  // Private
1441 ariadna 212
  _configAfterMerge(config) {
1 efrain 213
    config.toggle = Boolean(config.toggle) // Coerce string values
1441 ariadna 214
    config.parent = getElement(config.parent)
1 efrain 215
    return config
216
  }
217
 
218
  _getDimension() {
1441 ariadna 219
    return this._element.classList.contains(CLASS_NAME_HORIZONTAL) ? WIDTH : HEIGHT
1 efrain 220
  }
221
 
1441 ariadna 222
  _initializeChildren() {
223
    if (!this._config.parent) {
224
      return
225
    }
1 efrain 226
 
1441 ariadna 227
    const children = this._getFirstLevelChildren(SELECTOR_DATA_TOGGLE)
1 efrain 228
 
1441 ariadna 229
    for (const element of children) {
230
      const selected = SelectorEngine.getElementFromSelector(element)
231
 
232
      if (selected) {
233
        this._addAriaAndCollapsedClass([element], this._isShown(selected))
1 efrain 234
      }
235
    }
1441 ariadna 236
  }
1 efrain 237
 
1441 ariadna 238
  _getFirstLevelChildren(selector) {
239
    const children = SelectorEngine.find(CLASS_NAME_DEEPER_CHILDREN, this._config.parent)
240
    // remove children if greater depth
241
    return SelectorEngine.find(selector, this._config.parent).filter(element => !children.includes(element))
1 efrain 242
  }
243
 
1441 ariadna 244
  _addAriaAndCollapsedClass(triggerArray, isOpen) {
245
    if (!triggerArray.length) {
246
      return
247
    }
1 efrain 248
 
1441 ariadna 249
    for (const element of triggerArray) {
250
      element.classList.toggle(CLASS_NAME_COLLAPSED, !isOpen)
251
      element.setAttribute('aria-expanded', isOpen)
1 efrain 252
    }
253
  }
254
 
255
  // Static
1441 ariadna 256
  static jQueryInterface(config) {
257
    const _config = {}
258
    if (typeof config === 'string' && /show|hide/.test(config)) {
259
      _config.toggle = false
260
    }
1 efrain 261
 
262
    return this.each(function () {
1441 ariadna 263
      const data = Collapse.getOrCreateInstance(this, _config)
1 efrain 264
 
265
      if (typeof config === 'string') {
266
        if (typeof data[config] === 'undefined') {
267
          throw new TypeError(`No method named "${config}"`)
268
        }
269
 
270
        data[config]()
271
      }
272
    })
273
  }
274
}
275
 
276
/**
277
 * Data API implementation
278
 */
279
 
1441 ariadna 280
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
1 efrain 281
  // preventDefault only for <a> elements (which change the URL) not inside the collapsible element
1441 ariadna 282
  if (event.target.tagName === 'A' || (event.delegateTarget && event.delegateTarget.tagName === 'A')) {
1 efrain 283
    event.preventDefault()
284
  }
285
 
1441 ariadna 286
  for (const element of SelectorEngine.getMultipleElementsFromSelector(this)) {
287
    Collapse.getOrCreateInstance(element, { toggle: false }).toggle()
288
  }
1 efrain 289
})
290
 
291
/**
292
 * jQuery
293
 */
294
 
1441 ariadna 295
defineJQueryPlugin(Collapse)
1 efrain 296
 
297
export default Collapse