button.js 5.32 KB
Newer Older
XhmikosR's avatar
Dist    
XhmikosR committed
1
/*!
XhmikosR's avatar
XhmikosR committed
2
  * Bootstrap button.js v5.0.0-alpha3 (https://getbootstrap.com/)
XhmikosR's avatar
XhmikosR committed
3
  * Copyright 2011-2020 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
XhmikosR's avatar
XhmikosR committed
4
  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
XhmikosR's avatar
Dist    
XhmikosR committed
5
  */
Mark Otto's avatar
dist    
Mark Otto committed
6
(function (global, factory) {
XhmikosR's avatar
XhmikosR committed
7
8
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('./dom/data.js'), require('./dom/event-handler.js')) :
  typeof define === 'function' && define.amd ? define(['./dom/data.js', './dom/event-handler.js'], factory) :
XhmikosR's avatar
XhmikosR committed
9
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Button = factory(global.Data, global.EventHandler));
XhmikosR's avatar
XhmikosR committed
10
}(this, (function (Data, EventHandler) { 'use strict';
Mark Otto's avatar
dist    
Mark Otto committed
11

XhmikosR's avatar
XhmikosR committed
12
13
14
15
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

  var Data__default = /*#__PURE__*/_interopDefaultLegacy(Data);
  var EventHandler__default = /*#__PURE__*/_interopDefaultLegacy(EventHandler);
Mark Otto's avatar
dist    
Mark Otto committed
16

XhmikosR's avatar
XhmikosR committed
17
18
  /**
   * --------------------------------------------------------------------------
XhmikosR's avatar
XhmikosR committed
19
   * Bootstrap (v5.0.0-alpha3): util/index.js
XhmikosR's avatar
XhmikosR committed
20
   * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
XhmikosR's avatar
XhmikosR committed
21
22
   * --------------------------------------------------------------------------
   */
XhmikosR's avatar
XhmikosR committed
23
24
25
26
27

  var getjQuery = function getjQuery() {
    var _window = window,
        jQuery = _window.jQuery;

XhmikosR's avatar
XhmikosR committed
28
    if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
XhmikosR's avatar
XhmikosR committed
29
30
31
32
33
      return jQuery;
    }

    return null;
  };
XhmikosR's avatar
XhmikosR committed
34

XhmikosR's avatar
XhmikosR committed
35
36
37
38
39
40
41
42
  var onDOMContentLoaded = function onDOMContentLoaded(callback) {
    if (document.readyState === 'loading') {
      document.addEventListener('DOMContentLoaded', callback);
    } else {
      callback();
    }
  };

Mark Otto's avatar
Mark Otto committed
43
44
45
  function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
XhmikosR's avatar
Dist    
XhmikosR committed
46
47
48
49
50
51
52
  /**
   * ------------------------------------------------------------------------
   * Constants
   * ------------------------------------------------------------------------
   */

  var NAME = 'button';
XhmikosR's avatar
XhmikosR committed
53
  var VERSION = '5.0.0-alpha3';
XhmikosR's avatar
Dist    
XhmikosR committed
54
55
56
  var DATA_KEY = 'bs.button';
  var EVENT_KEY = "." + DATA_KEY;
  var DATA_API_KEY = '.data-api';
XhmikosR's avatar
XhmikosR committed
57
  var CLASS_NAME_ACTIVE = 'active';
XhmikosR's avatar
XhmikosR committed
58
  var SELECTOR_DATA_TOGGLE = '[data-bs-toggle="button"]';
XhmikosR's avatar
XhmikosR committed
59
  var EVENT_CLICK_DATA_API = "click" + EVENT_KEY + DATA_API_KEY;
XhmikosR's avatar
XhmikosR committed
60
61
62
63
64
  /**
   * ------------------------------------------------------------------------
   * Class Definition
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
65

XhmikosR's avatar
XhmikosR committed
66
  var Button = /*#__PURE__*/function () {
XhmikosR's avatar
Dist    
XhmikosR committed
67
68
    function Button(element) {
      this._element = element;
XhmikosR's avatar
XhmikosR committed
69
      Data__default['default'].setData(element, DATA_KEY, this);
XhmikosR's avatar
Dist    
XhmikosR committed
70
    } // Getters
Jacob Thornton's avatar
Jacob Thornton committed
71

Mark Otto's avatar
dist    
Mark Otto committed
72

XhmikosR's avatar
Dist    
XhmikosR committed
73
    var _proto = Button.prototype;
Mark Otto's avatar
dist    
Mark Otto committed
74

XhmikosR's avatar
Dist    
XhmikosR committed
75
76
    // Public
    _proto.toggle = function toggle() {
XhmikosR's avatar
XhmikosR committed
77
78
      // Toggle class and sync the `aria-pressed` attribute with the return value of the `.toggle()` method
      this._element.setAttribute('aria-pressed', this._element.classList.toggle(CLASS_NAME_ACTIVE));
XhmikosR's avatar
Dist    
XhmikosR committed
79
    };
fat's avatar
fat committed
80

XhmikosR's avatar
Dist    
XhmikosR committed
81
    _proto.dispose = function dispose() {
XhmikosR's avatar
XhmikosR committed
82
      Data__default['default'].removeData(this._element, DATA_KEY);
XhmikosR's avatar
Dist    
XhmikosR committed
83
      this._element = null;
Mark Otto's avatar
Mark Otto committed
84
85
    } // Static
    ;
Mark Otto's avatar
grunt    
Mark Otto committed
86

XhmikosR's avatar
XhmikosR committed
87
    Button.jQueryInterface = function jQueryInterface(config) {
XhmikosR's avatar
Dist    
XhmikosR committed
88
      return this.each(function () {
XhmikosR's avatar
XhmikosR committed
89
        var data = Data__default['default'].getData(this, DATA_KEY);
Mark Otto's avatar
grunt    
Mark Otto committed
90

XhmikosR's avatar
Dist    
XhmikosR committed
91
92
93
        if (!data) {
          data = new Button(this);
        }
Mark Otto's avatar
dist    
Mark Otto committed
94

XhmikosR's avatar
Dist    
XhmikosR committed
95
96
        if (config === 'toggle') {
          data[config]();
Mark Otto's avatar
dist    
Mark Otto committed
97
        }
XhmikosR's avatar
Dist    
XhmikosR committed
98
99
      });
    };
Mark Otto's avatar
dist    
Mark Otto committed
100

XhmikosR's avatar
XhmikosR committed
101
    Button.getInstance = function getInstance(element) {
XhmikosR's avatar
XhmikosR committed
102
      return Data__default['default'].getData(element, DATA_KEY);
XhmikosR's avatar
XhmikosR committed
103
104
    };

XhmikosR's avatar
Dist    
XhmikosR committed
105
106
107
108
109
110
    _createClass(Button, null, [{
      key: "VERSION",
      get: function get() {
        return VERSION;
      }
    }]);
fat's avatar
fat committed
111

XhmikosR's avatar
Dist    
XhmikosR committed
112
113
114
115
116
117
118
    return Button;
  }();
  /**
   * ------------------------------------------------------------------------
   * Data Api implementation
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
119

Mark Otto's avatar
dist    
Mark Otto committed
120

XhmikosR's avatar
XhmikosR committed
121
  EventHandler__default['default'].on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
XhmikosR's avatar
Dist    
XhmikosR committed
122
    event.preventDefault();
XhmikosR's avatar
XhmikosR committed
123
    var button = event.target.closest(SELECTOR_DATA_TOGGLE);
XhmikosR's avatar
XhmikosR committed
124
    var data = Data__default['default'].getData(button, DATA_KEY);
XhmikosR's avatar
XhmikosR committed
125
126
127
128
129
130
131

    if (!data) {
      data = new Button(button);
    }

    data.toggle();
  });
XhmikosR's avatar
Dist    
XhmikosR committed
132
133
134
135
  /**
   * ------------------------------------------------------------------------
   * jQuery
   * ------------------------------------------------------------------------
XhmikosR's avatar
XhmikosR committed
136
   * add .Button to jQuery only if jQuery is present
XhmikosR's avatar
Dist    
XhmikosR committed
137
   */
fat's avatar
fat committed
138

XhmikosR's avatar
XhmikosR committed
139
140
141
  onDOMContentLoaded(function () {
    var $ = getjQuery();
    /* istanbul ignore if */
142

XhmikosR's avatar
XhmikosR committed
143
144
145
146
    if ($) {
      var JQUERY_NO_CONFLICT = $.fn[NAME];
      $.fn[NAME] = Button.jQueryInterface;
      $.fn[NAME].Constructor = Button;
Mark Otto's avatar
dist    
Mark Otto committed
147

XhmikosR's avatar
XhmikosR committed
148
149
150
151
152
153
      $.fn[NAME].noConflict = function () {
        $.fn[NAME] = JQUERY_NO_CONFLICT;
        return Button.jQueryInterface;
      };
    }
  });
fat's avatar
fat committed
154
155

  return Button;
Mark Otto's avatar
dist    
Mark Otto committed
156

XhmikosR's avatar
XhmikosR committed
157
})));
Mark Otto's avatar
dist    
Mark Otto committed
158
//# sourceMappingURL=button.js.map