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 modal.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 Backdrop from './util/backdrop'
12
import { enableDismissTrigger } from './util/component-functions'
13
import FocusTrap from './util/focustrap'
14
import {
15
  defineJQueryPlugin, isRTL, isVisible, reflow
16
} from './util/index'
17
import ScrollBarHelper from './util/scrollbar'
1 efrain 18
 
19
/**
20
 * Constants
21
 */
22
 
23
const NAME = 'modal'
24
const DATA_KEY = 'bs.modal'
25
const EVENT_KEY = `.${DATA_KEY}`
26
const DATA_API_KEY = '.data-api'
1441 ariadna 27
const ESCAPE_KEY = 'Escape'
1 efrain 28
 
29
const EVENT_HIDE = `hide${EVENT_KEY}`
30
const EVENT_HIDE_PREVENTED = `hidePrevented${EVENT_KEY}`
31
const EVENT_HIDDEN = `hidden${EVENT_KEY}`
32
const EVENT_SHOW = `show${EVENT_KEY}`
33
const EVENT_SHOWN = `shown${EVENT_KEY}`
34
const EVENT_RESIZE = `resize${EVENT_KEY}`
35
const EVENT_CLICK_DISMISS = `click.dismiss${EVENT_KEY}`
1441 ariadna 36
const EVENT_MOUSEDOWN_DISMISS = `mousedown.dismiss${EVENT_KEY}`
1 efrain 37
const EVENT_KEYDOWN_DISMISS = `keydown.dismiss${EVENT_KEY}`
38
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
39
 
1441 ariadna 40
const CLASS_NAME_OPEN = 'modal-open'
41
const CLASS_NAME_FADE = 'fade'
42
const CLASS_NAME_SHOW = 'show'
43
const CLASS_NAME_STATIC = 'modal-static'
44
 
45
const OPEN_SELECTOR = '.modal.show'
1 efrain 46
const SELECTOR_DIALOG = '.modal-dialog'
47
const SELECTOR_MODAL_BODY = '.modal-body'
1441 ariadna 48
const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="modal"]'
1 efrain 49
 
50
const Default = {
51
  backdrop: true,
52
  focus: true,
1441 ariadna 53
  keyboard: true
1 efrain 54
}
55
 
56
const DefaultType = {
57
  backdrop: '(boolean|string)',
58
  focus: 'boolean',
1441 ariadna 59
  keyboard: 'boolean'
1 efrain 60
}
61
 
62
/**
63
 * Class definition
64
 */
65
 
1441 ariadna 66
class Modal extends BaseComponent {
1 efrain 67
  constructor(element, config) {
1441 ariadna 68
    super(element, config)
69
 
70
    this._dialog = SelectorEngine.findOne(SELECTOR_DIALOG, this._element)
71
    this._backdrop = this._initializeBackDrop()
72
    this._focustrap = this._initializeFocusTrap()
1 efrain 73
    this._isShown = false
74
    this._isTransitioning = false
1441 ariadna 75
    this._scrollBar = new ScrollBarHelper()
76
 
77
    this._addEventListeners()
1 efrain 78
  }
79
 
80
  // Getters
81
  static get Default() {
82
    return Default
83
  }
84
 
1441 ariadna 85
  static get DefaultType() {
86
    return DefaultType
87
  }
88
 
89
  static get NAME() {
90
    return NAME
91
  }
92
 
1 efrain 93
  // Public
94
  toggle(relatedTarget) {
95
    return this._isShown ? this.hide() : this.show(relatedTarget)
96
  }
97
 
98
  show(relatedTarget) {
99
    if (this._isShown || this._isTransitioning) {
100
      return
101
    }
102
 
1441 ariadna 103
    const showEvent = EventHandler.trigger(this._element, EVENT_SHOW, {
1 efrain 104
      relatedTarget
105
    })
106
 
1441 ariadna 107
    if (showEvent.defaultPrevented) {
1 efrain 108
      return
109
    }
110
 
111
    this._isShown = true
1441 ariadna 112
    this._isTransitioning = true
1 efrain 113
 
1441 ariadna 114
    this._scrollBar.hide()
1 efrain 115
 
1441 ariadna 116
    document.body.classList.add(CLASS_NAME_OPEN)
1 efrain 117
 
118
    this._adjustDialog()
119
 
1441 ariadna 120
    this._backdrop.show(() => this._showElement(relatedTarget))
1 efrain 121
  }
122
 
1441 ariadna 123
  hide() {
1 efrain 124
    if (!this._isShown || this._isTransitioning) {
125
      return
126
    }
127
 
1441 ariadna 128
    const hideEvent = EventHandler.trigger(this._element, EVENT_HIDE)
1 efrain 129
 
1441 ariadna 130
    if (hideEvent.defaultPrevented) {
1 efrain 131
      return
132
    }
133
 
134
    this._isShown = false
1441 ariadna 135
    this._isTransitioning = true
136
    this._focustrap.deactivate()
1 efrain 137
 
1441 ariadna 138
    this._element.classList.remove(CLASS_NAME_SHOW)
1 efrain 139
 
1441 ariadna 140
    this._queueCallback(() => this._hideModal(), this._element, this._isAnimated())
1 efrain 141
  }
142
 
143
  dispose() {
1441 ariadna 144
    EventHandler.off(window, EVENT_KEY)
145
    EventHandler.off(this._dialog, EVENT_KEY)
1 efrain 146
 
1441 ariadna 147
    this._backdrop.dispose()
148
    this._focustrap.deactivate()
1 efrain 149
 
1441 ariadna 150
    super.dispose()
1 efrain 151
  }
152
 
153
  handleUpdate() {
154
    this._adjustDialog()
155
  }
156
 
157
  // Private
1441 ariadna 158
  _initializeBackDrop() {
159
    return new Backdrop({
160
      isVisible: Boolean(this._config.backdrop), // 'static' option will be translated to true, and booleans will keep their value,
161
      isAnimated: this._isAnimated()
162
    })
1 efrain 163
  }
164
 
1441 ariadna 165
  _initializeFocusTrap() {
166
    return new FocusTrap({
167
      trapElement: this._element
1 efrain 168
    })
169
  }
170
 
171
  _showElement(relatedTarget) {
1441 ariadna 172
    // try to append dynamic modal
173
    if (!document.body.contains(this._element)) {
174
      document.body.append(this._element)
1 efrain 175
    }
176
 
177
    this._element.style.display = 'block'
178
    this._element.removeAttribute('aria-hidden')
179
    this._element.setAttribute('aria-modal', true)
180
    this._element.setAttribute('role', 'dialog')
1441 ariadna 181
    this._element.scrollTop = 0
1 efrain 182
 
1441 ariadna 183
    const modalBody = SelectorEngine.findOne(SELECTOR_MODAL_BODY, this._dialog)
184
    if (modalBody) {
1 efrain 185
      modalBody.scrollTop = 0
186
    }
187
 
1441 ariadna 188
    reflow(this._element)
1 efrain 189
 
1441 ariadna 190
    this._element.classList.add(CLASS_NAME_SHOW)
1 efrain 191
 
192
    const transitionComplete = () => {
193
      if (this._config.focus) {
1441 ariadna 194
        this._focustrap.activate()
1 efrain 195
      }
196
 
197
      this._isTransitioning = false
1441 ariadna 198
      EventHandler.trigger(this._element, EVENT_SHOWN, {
199
        relatedTarget
200
      })
1 efrain 201
    }
202
 
1441 ariadna 203
    this._queueCallback(transitionComplete, this._dialog, this._isAnimated())
1 efrain 204
  }
205
 
1441 ariadna 206
  _addEventListeners() {
207
    EventHandler.on(this._element, EVENT_KEYDOWN_DISMISS, event => {
208
      if (event.key !== ESCAPE_KEY) {
209
        return
210
      }
211
 
212
      if (this._config.keyboard) {
213
        this.hide()
214
        return
215
      }
216
 
217
      this._triggerBackdropTransition()
218
    })
219
 
220
    EventHandler.on(window, EVENT_RESIZE, () => {
221
      if (this._isShown && !this._isTransitioning) {
222
        this._adjustDialog()
223
      }
224
    })
225
 
226
    EventHandler.on(this._element, EVENT_MOUSEDOWN_DISMISS, event => {
227
      // a bad trick to segregate clicks that may start inside dialog but end outside, and avoid listen to scrollbar clicks
228
      EventHandler.one(this._element, EVENT_CLICK_DISMISS, event2 => {
229
        if (this._element !== event.target || this._element !== event2.target) {
230
          return
1 efrain 231
        }
232
 
1441 ariadna 233
        if (this._config.backdrop === 'static') {
1 efrain 234
          this._triggerBackdropTransition()
1441 ariadna 235
          return
1 efrain 236
        }
1441 ariadna 237
 
238
        if (this._config.backdrop) {
239
          this.hide()
240
        }
1 efrain 241
      })
1441 ariadna 242
    })
1 efrain 243
  }
244
 
245
  _hideModal() {
246
    this._element.style.display = 'none'
247
    this._element.setAttribute('aria-hidden', true)
248
    this._element.removeAttribute('aria-modal')
249
    this._element.removeAttribute('role')
250
    this._isTransitioning = false
1441 ariadna 251
 
252
    this._backdrop.hide(() => {
253
      document.body.classList.remove(CLASS_NAME_OPEN)
1 efrain 254
      this._resetAdjustments()
1441 ariadna 255
      this._scrollBar.reset()
256
      EventHandler.trigger(this._element, EVENT_HIDDEN)
1 efrain 257
    })
258
  }
259
 
1441 ariadna 260
  _isAnimated() {
261
    return this._element.classList.contains(CLASS_NAME_FADE)
1 efrain 262
  }
263
 
1441 ariadna 264
  _triggerBackdropTransition() {
265
    const hideEvent = EventHandler.trigger(this._element, EVENT_HIDE_PREVENTED)
266
    if (hideEvent.defaultPrevented) {
267
      return
268
    }
1 efrain 269
 
1441 ariadna 270
    const isModalOverflowing = this._element.scrollHeight > document.documentElement.clientHeight
271
    const initialOverflowY = this._element.style.overflowY
272
    // return if the following background transition hasn't yet completed
273
    if (initialOverflowY === 'hidden' || this._element.classList.contains(CLASS_NAME_STATIC)) {
274
      return
275
    }
1 efrain 276
 
1441 ariadna 277
    if (!isModalOverflowing) {
278
      this._element.style.overflowY = 'hidden'
279
    }
1 efrain 280
 
1441 ariadna 281
    this._element.classList.add(CLASS_NAME_STATIC)
282
    this._queueCallback(() => {
283
      this._element.classList.remove(CLASS_NAME_STATIC)
284
      this._queueCallback(() => {
285
        this._element.style.overflowY = initialOverflowY
286
      }, this._dialog)
287
    }, this._dialog)
1 efrain 288
 
1441 ariadna 289
    this._element.focus()
1 efrain 290
  }
291
 
1441 ariadna 292
  /**
293
   * The following methods are used to handle overflowing modals
294
   */
1 efrain 295
 
296
  _adjustDialog() {
297
    const isModalOverflowing = this._element.scrollHeight > document.documentElement.clientHeight
1441 ariadna 298
    const scrollbarWidth = this._scrollBar.getWidth()
299
    const isBodyOverflowing = scrollbarWidth > 0
1 efrain 300
 
1441 ariadna 301
    if (isBodyOverflowing && !isModalOverflowing) {
302
      const property = isRTL() ? 'paddingLeft' : 'paddingRight'
303
      this._element.style[property] = `${scrollbarWidth}px`
1 efrain 304
    }
305
 
1441 ariadna 306
    if (!isBodyOverflowing && isModalOverflowing) {
307
      const property = isRTL() ? 'paddingRight' : 'paddingLeft'
308
      this._element.style[property] = `${scrollbarWidth}px`
1 efrain 309
    }
310
  }
311
 
312
  _resetAdjustments() {
313
    this._element.style.paddingLeft = ''
314
    this._element.style.paddingRight = ''
315
  }
316
 
317
  // Static
1441 ariadna 318
  static jQueryInterface(config, relatedTarget) {
1 efrain 319
    return this.each(function () {
1441 ariadna 320
      const data = Modal.getOrCreateInstance(this, config)
321
 
322
      if (typeof config !== 'string') {
323
        return
1 efrain 324
      }
325
 
1441 ariadna 326
      if (typeof data[config] === 'undefined') {
327
        throw new TypeError(`No method named "${config}"`)
1 efrain 328
      }
329
 
1441 ariadna 330
      data[config](relatedTarget)
1 efrain 331
    })
332
  }
333
}
334
 
335
/**
336
 * Data API implementation
337
 */
338
 
1441 ariadna 339
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
340
  const target = SelectorEngine.getElementFromSelector(this)
1 efrain 341
 
1441 ariadna 342
  if (['A', 'AREA'].includes(this.tagName)) {
1 efrain 343
    event.preventDefault()
344
  }
345
 
1441 ariadna 346
  EventHandler.one(target, EVENT_SHOW, showEvent => {
347
    if (showEvent.defaultPrevented) {
348
      // only register focus restorer if modal will actually get shown
1 efrain 349
      return
350
    }
351
 
1441 ariadna 352
    EventHandler.one(target, EVENT_HIDDEN, () => {
353
      if (isVisible(this)) {
1 efrain 354
        this.focus()
355
      }
356
    })
357
  })
358
 
1441 ariadna 359
  // avoid conflict when clicking modal toggler while another one is open
360
  const alreadyOpen = SelectorEngine.findOne(OPEN_SELECTOR)
361
  if (alreadyOpen) {
362
    Modal.getInstance(alreadyOpen).hide()
363
  }
364
 
365
  const data = Modal.getOrCreateInstance(target)
366
 
367
  data.toggle(this)
1 efrain 368
})
369
 
1441 ariadna 370
enableDismissTrigger(Modal)
371
 
1 efrain 372
/**
373
 * jQuery
374
 */
375
 
1441 ariadna 376
defineJQueryPlugin(Modal)
1 efrain 377
 
378
export default Modal