Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 1... Línea 1...
1
/**
1
/**
2
 * --------------------------------------------------------------------------
2
 * --------------------------------------------------------------------------
3
 * Bootstrap (v4.6.2): button.js
3
 * Bootstrap button.js
4
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
4
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
5
 * --------------------------------------------------------------------------
5
 * --------------------------------------------------------------------------
6
 */
6
 */
Línea 7... Línea 7...
7
 
7
 
-
 
8
import BaseComponent from './base-component'
-
 
9
import EventHandler from './dom/event-handler'
Línea 8... Línea 10...
8
import $ from 'jquery'
10
import { defineJQueryPlugin } from './util/index'
9
 
11
 
10
/**
12
/**
Línea 11... Línea 13...
11
 * Constants
13
 * Constants
12
 */
-
 
13
 
14
 */
14
const NAME = 'button'
15
 
15
const VERSION = '4.6.2'
16
const NAME = 'button'
16
const DATA_KEY = 'bs.button'
-
 
Línea 17... Línea 17...
17
const EVENT_KEY = `.${DATA_KEY}`
17
const DATA_KEY = 'bs.button'
18
const DATA_API_KEY = '.data-api'
-
 
19
const JQUERY_NO_CONFLICT = $.fn[NAME]
18
const EVENT_KEY = `.${DATA_KEY}`
20
 
-
 
21
const CLASS_NAME_ACTIVE = 'active'
19
const DATA_API_KEY = '.data-api'
22
const CLASS_NAME_BUTTON = 'btn'
-
 
23
const CLASS_NAME_FOCUS = 'focus'
-
 
24
 
-
 
25
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
-
 
26
const EVENT_FOCUS_BLUR_DATA_API = `focus${EVENT_KEY}${DATA_API_KEY} ` +
-
 
27
                          `blur${EVENT_KEY}${DATA_API_KEY}`
-
 
28
const EVENT_LOAD_DATA_API = `load${EVENT_KEY}${DATA_API_KEY}`
-
 
29
 
-
 
30
const SELECTOR_DATA_TOGGLE_CARROT = '[data-toggle^="button"]'
-
 
31
const SELECTOR_DATA_TOGGLES = '[data-toggle="buttons"]'
-
 
32
const SELECTOR_DATA_TOGGLE = '[data-toggle="button"]'
-
 
Línea 33... Línea 20...
33
const SELECTOR_DATA_TOGGLES_BUTTONS = '[data-toggle="buttons"] .btn'
20
 
34
const SELECTOR_INPUT = 'input:not([type="hidden"])'
21
const CLASS_NAME_ACTIVE = 'active'
35
const SELECTOR_ACTIVE = '.active'
22
const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="button"]'
Línea 36... Línea -...
36
const SELECTOR_BUTTON = '.btn'
-
 
37
 
-
 
38
/**
23
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
39
 * Class definition
-
 
40
 */
-
 
41
 
-
 
42
class Button {
24
 
43
  constructor(element) {
25
/**
44
    this._element = element
26
 * Class definition
45
    this.shouldAvoidTriggerChange = false
27
 */
Línea 46... Línea 28...
46
  }
28
 
47
 
29
class Button extends BaseComponent {
48
  // Getters
-
 
49
  static get VERSION() {
-
 
50
    return VERSION
-
 
51
  }
-
 
52
 
-
 
53
  // Public
-
 
54
  toggle() {
-
 
55
    let triggerChangeEvent = true
-
 
56
    let addAriaPressed = true
-
 
57
    const rootElement = $(this._element).closest(SELECTOR_DATA_TOGGLES)[0]
-
 
58
 
-
 
59
    if (rootElement) {
-
 
60
      const input = this._element.querySelector(SELECTOR_INPUT)
-
 
61
 
-
 
62
      if (input) {
-
 
63
        if (input.type === 'radio') {
-
 
64
          if (input.checked && this._element.classList.contains(CLASS_NAME_ACTIVE)) {
-
 
65
            triggerChangeEvent = false
-
 
66
          } else {
-
 
67
            const activeElement = rootElement.querySelector(SELECTOR_ACTIVE)
-
 
68
 
-
 
69
            if (activeElement) {
30
  // Getters
70
              $(activeElement).removeClass(CLASS_NAME_ACTIVE)
-
 
71
            }
-
 
72
          }
-
 
73
        }
-
 
74
 
-
 
75
        if (triggerChangeEvent) {
-
 
76
          // if it's not a radio button or checkbox don't add a pointless/invalid checked property to the input
-
 
77
          if (input.type === 'checkbox' || input.type === 'radio') {
-
 
78
            input.checked = !this._element.classList.contains(CLASS_NAME_ACTIVE)
-
 
79
          }
-
 
80
 
-
 
81
          if (!this.shouldAvoidTriggerChange) {
-
 
82
            $(input).trigger('change')
-
 
83
          }
-
 
84
        }
-
 
85
 
-
 
86
        input.focus()
31
  static get NAME() {
87
        addAriaPressed = false
-
 
88
      }
-
 
89
    }
-
 
90
 
-
 
91
    if (!(this._element.hasAttribute('disabled') || this._element.classList.contains('disabled'))) {
-
 
92
      if (addAriaPressed) {
-
 
93
        this._element.setAttribute('aria-pressed', !this._element.classList.contains(CLASS_NAME_ACTIVE))
-
 
94
      }
-
 
95
 
-
 
96
      if (triggerChangeEvent) {
-
 
97
        $(this._element).toggleClass(CLASS_NAME_ACTIVE)
-
 
98
      }
32
    return NAME
Línea 99... Línea 33...
99
    }
33
  }
100
  }
34
 
101
 
35
  // Public
102
  dispose() {
-
 
103
    $.removeData(this._element, DATA_KEY)
-
 
104
    this._element = null
-
 
105
  }
-
 
106
 
36
  toggle() {
107
  // Static
-
 
108
  static _jQueryInterface(config, avoidTriggerChange) {
-
 
109
    return this.each(function () {
-
 
110
      const $element = $(this)
-
 
Línea 111... Línea 37...
111
      let data = $element.data(DATA_KEY)
37
    // Toggle class and sync the `aria-pressed` attribute with the return value of the `.toggle()` method
112
 
38
    this._element.setAttribute('aria-pressed', this._element.classList.toggle(CLASS_NAME_ACTIVE))
113
      if (!data) {
39
  }
114
        data = new Button(this)
40
 
Línea 126... Línea 52...
126
 
52
 
127
/**
53
/**
128
 * Data API implementation
54
 * Data API implementation
Línea 129... Línea -...
129
 */
-
 
130
 
55
 */
131
$(document)
56
 
132
  .on(EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE_CARROT, event => {
-
 
133
    let button = event.target
-
 
134
    const initialButton = button
-
 
135
 
-
 
136
    if (!$(button).hasClass(CLASS_NAME_BUTTON)) {
-
 
137
      button = $(button).closest(SELECTOR_BUTTON)[0]
-
 
138
    }
-
 
139
 
-
 
140
    if (!button || button.hasAttribute('disabled') || button.classList.contains('disabled')) {
-
 
141
      event.preventDefault() // work around Firefox bug #1540995
-
 
142
    } else {
-
 
143
      const inputBtn = button.querySelector(SELECTOR_INPUT)
-
 
144
 
-
 
145
      if (inputBtn && (inputBtn.hasAttribute('disabled') || inputBtn.classList.contains('disabled'))) {
-
 
146
        event.preventDefault() // work around Firefox bug #1540995
-
 
Línea 147... Línea -...
147
        return
-
 
148
      }
-
 
149
 
-
 
150
      if (initialButton.tagName === 'INPUT' || button.tagName !== 'LABEL') {
-
 
151
        Button._jQueryInterface.call($(button), 'toggle', initialButton.tagName === 'INPUT')
-
 
152
      }
-
 
153
    }
57
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, event => {
154
  })
-
 
155
  .on(EVENT_FOCUS_BLUR_DATA_API, SELECTOR_DATA_TOGGLE_CARROT, event => {
-
 
156
    const button = $(event.target).closest(SELECTOR_BUTTON)[0]
-
 
157
    $(button).toggleClass(CLASS_NAME_FOCUS, /^focus(in)?$/.test(event.type))
-
 
158
  })
-
 
159
 
-
 
160
$(window).on(EVENT_LOAD_DATA_API, () => {
-
 
161
  // ensure correct active class is set to match the controls' actual values/states
-
 
162
 
-
 
163
  // find all checkboxes/readio buttons inside data-toggle groups
-
 
164
  let buttons = [].slice.call(document.querySelectorAll(SELECTOR_DATA_TOGGLES_BUTTONS))
58
  event.preventDefault()
165
  for (let i = 0, len = buttons.length; i < len; i++) {
-
 
166
    const button = buttons[i]
-
 
167
    const input = button.querySelector(SELECTOR_INPUT)
-
 
168
    if (input.checked || input.hasAttribute('checked')) {
-
 
169
      button.classList.add(CLASS_NAME_ACTIVE)
-
 
170
    } else {
-
 
Línea 171... Línea 59...
171
      button.classList.remove(CLASS_NAME_ACTIVE)
59
 
172
    }
-
 
173
  }
-
 
174
 
-
 
175
  // find all button toggles
-
 
176
  buttons = [].slice.call(document.querySelectorAll(SELECTOR_DATA_TOGGLE))
-
 
177
  for (let i = 0, len = buttons.length; i < len; i++) {
-
 
178
    const button = buttons[i]
-
 
179
    if (button.getAttribute('aria-pressed') === 'true') {
-
 
180
      button.classList.add(CLASS_NAME_ACTIVE)
-
 
181
    } else {
60
  const button = event.target.closest(SELECTOR_DATA_TOGGLE)
Línea 182... Línea 61...
182
      button.classList.remove(CLASS_NAME_ACTIVE)
61
  const data = Button.getOrCreateInstance(button)
183
    }
62
 
184
  }
63
  data.toggle()
Línea 185... Línea -...
185
})
-
 
186
 
64
})
187
/**
-
 
188
 * jQuery
-
 
189
 */
-
 
190
 
-
 
Línea 191... Línea 65...
191
$.fn[NAME] = Button._jQueryInterface
65