button.js 4.21 KB
Newer Older
fat's avatar
fat committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
 * --------------------------------------------------------------------------
 * Bootstrap (v4.0.0): button.js
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 * --------------------------------------------------------------------------
 */

const Button = (($) => {


  /**
   * ------------------------------------------------------------------------
   * Constants
   * ------------------------------------------------------------------------
   */

  const NAME                = 'button'
  const VERSION             = '4.0.0'
  const DATA_KEY            = 'bs.button'
fat's avatar
fat committed
20
21
  const EVENT_KEY           = `.${DATA_KEY}`
  const DATA_API_KEY        = '.data-api'
fat's avatar
fat committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  const JQUERY_NO_CONFLICT  = $.fn[NAME]

  const ClassName = {
    ACTIVE : 'active',
    BUTTON : 'btn',
    FOCUS  : 'focus'
  }

  const Selector = {
    DATA_TOGGLE_CARROT : '[data-toggle^="button"]',
    DATA_TOGGLE        : '[data-toggle="buttons"]',
    INPUT              : 'input',
    ACTIVE             : '.active',
    BUTTON             : '.btn'
  }

  const Event = {
fat's avatar
fat committed
39
40
41
    CLICK_DATA_API      : `click${EVENT_KEY}${DATA_API_KEY}`,
    FOCUS_BLUR_DATA_API : `focus${EVENT_KEY}${DATA_API_KEY} `
                        + `blur${EVENT_KEY}${DATA_API_KEY}`
fat's avatar
fat committed
42
43
44
45
46
47
48
49
50
51
52
53
  }


  /**
   * ------------------------------------------------------------------------
   * Class Definition
   * ------------------------------------------------------------------------
   */

  class Button {

    constructor(element) {
fat's avatar
fat committed
54
      this._element = element
fat's avatar
fat committed
55
56
    }

57
58
59
60
61
62
63
64

    // getters

    static get VERSION() {
      return VERSION
    }


fat's avatar
fat committed
65
66
67
68
    // public

    toggle() {
      let triggerChangeEvent = true
Jacob Thornton's avatar
Jacob Thornton committed
69
      let rootElement        = $(this._element).closest(
fat's avatar
fat committed
70
71
72
73
        Selector.DATA_TOGGLE
      )[0]

      if (rootElement) {
fat's avatar
fat committed
74
        let input = $(this._element).find(Selector.INPUT)[0]
fat's avatar
fat committed
75
76
77
78

        if (input) {
          if (input.type === 'radio') {
            if (input.checked &&
fat's avatar
fat committed
79
              $(this._element).hasClass(ClassName.ACTIVE)) {
fat's avatar
fat committed
80
81
82
83
84
85
86
87
88
89
90
91
              triggerChangeEvent = false

            } else {
              let activeElement = $(rootElement).find(Selector.ACTIVE)[0]

              if (activeElement) {
                $(activeElement).removeClass(ClassName.ACTIVE)
              }
            }
          }

          if (triggerChangeEvent) {
fat's avatar
fat committed
92
93
            input.checked = !$(this._element).hasClass(ClassName.ACTIVE)
            $(this._element).trigger('change')
fat's avatar
fat committed
94
95
96
          }
        }
      } else {
fat's avatar
fat committed
97
98
        this._element.setAttribute('aria-pressed',
          !$(this._element).hasClass(ClassName.ACTIVE))
fat's avatar
fat committed
99
100
101
      }

      if (triggerChangeEvent) {
fat's avatar
fat committed
102
        $(this._element).toggleClass(ClassName.ACTIVE)
fat's avatar
fat committed
103
104
105
      }
    }

fat's avatar
fat committed
106
107
108
109
110
    dispose() {
      $.removeData(this._element, DATA_KEY)
      this._element = null
    }

fat's avatar
fat committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

    // static

    static _jQueryInterface(config) {
      return this.each(function () {
        let data = $(this).data(DATA_KEY)

        if (!data) {
          data = new Button(this)
          $(this).data(DATA_KEY, data)
        }

        if (config === 'toggle') {
          data[config]()
        }
      })
    }

  }


  /**
   * ------------------------------------------------------------------------
   * Data Api implementation
   * ------------------------------------------------------------------------
   */

  $(document)
Jacob Thornton's avatar
Jacob Thornton committed
139
    .on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE_CARROT, (event) => {
fat's avatar
fat committed
140
141
142
143
144
145
146
147
148
149
      event.preventDefault()

      let button = event.target

      if (!$(button).hasClass(ClassName.BUTTON)) {
        button = $(button).closest(Selector.BUTTON)
      }

      Button._jQueryInterface.call($(button), 'toggle')
    })
Jacob Thornton's avatar
Jacob Thornton committed
150
151
    .on(Event.FOCUS_BLUR_DATA_API, Selector.DATA_TOGGLE_CARROT, (event) => {
      let button = $(event.target).closest(Selector.BUTTON)[0]
fat's avatar
fat committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
      $(button).toggleClass(ClassName.FOCUS, /^focus(in)?$/.test(event.type))
    })


  /**
   * ------------------------------------------------------------------------
   * jQuery
   * ------------------------------------------------------------------------
   */

  $.fn[NAME]             = Button._jQueryInterface
  $.fn[NAME].Constructor = Button
  $.fn[NAME].noConflict  = function () {
    $.fn[NAME] = JQUERY_NO_CONFLICT
    return Button._jQueryInterface
  }

  return Button

})(jQuery)

export default Button