1 | 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); } }
|
2 |
|
3 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | var Button = function ($) {
|
12 | |
13 |
|
14 |
|
15 |
|
16 |
|
17 | var NAME = 'button';
|
18 | var VERSION = '4.1.0';
|
19 | var DATA_KEY = 'bs.button';
|
20 | var EVENT_KEY = "." + DATA_KEY;
|
21 | var DATA_API_KEY = '.data-api';
|
22 | var JQUERY_NO_CONFLICT = $.fn[NAME];
|
23 | var ClassName = {
|
24 | ACTIVE: 'active',
|
25 | BUTTON: 'btn',
|
26 | FOCUS: 'focus'
|
27 | };
|
28 | var Selector = {
|
29 | DATA_TOGGLE_CARROT: '[data-toggle^="button"]',
|
30 | DATA_TOGGLE: '[data-toggle="buttons"]',
|
31 | INPUT: 'input',
|
32 | ACTIVE: '.active',
|
33 | BUTTON: '.btn'
|
34 | };
|
35 | var Event = {
|
36 | CLICK_DATA_API: "click" + EVENT_KEY + DATA_API_KEY,
|
37 | FOCUS_BLUR_DATA_API: "focus" + EVENT_KEY + DATA_API_KEY + " " + ("blur" + EVENT_KEY + DATA_API_KEY)
|
38 | |
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | };
|
45 |
|
46 | var Button =
|
47 |
|
48 | function () {
|
49 | function Button(element) {
|
50 | this._element = element;
|
51 | }
|
52 |
|
53 |
|
54 | var _proto = Button.prototype;
|
55 |
|
56 |
|
57 | _proto.toggle = function toggle() {
|
58 | var triggerChangeEvent = true;
|
59 | var addAriaPressed = true;
|
60 | var rootElement = $(this._element).closest(Selector.DATA_TOGGLE)[0];
|
61 |
|
62 | if (rootElement) {
|
63 | var input = $(this._element).find(Selector.INPUT)[0];
|
64 |
|
65 | if (input) {
|
66 | if (input.type === 'radio') {
|
67 | if (input.checked && $(this._element).hasClass(ClassName.ACTIVE)) {
|
68 | triggerChangeEvent = false;
|
69 | } else {
|
70 | var activeElement = $(rootElement).find(Selector.ACTIVE)[0];
|
71 |
|
72 | if (activeElement) {
|
73 | $(activeElement).removeClass(ClassName.ACTIVE);
|
74 | }
|
75 | }
|
76 | }
|
77 |
|
78 | if (triggerChangeEvent) {
|
79 | if (input.hasAttribute('disabled') || rootElement.hasAttribute('disabled') || input.classList.contains('disabled') || rootElement.classList.contains('disabled')) {
|
80 | return;
|
81 | }
|
82 |
|
83 | input.checked = !$(this._element).hasClass(ClassName.ACTIVE);
|
84 | $(input).trigger('change');
|
85 | }
|
86 |
|
87 | input.focus();
|
88 | addAriaPressed = false;
|
89 | }
|
90 | }
|
91 |
|
92 | if (addAriaPressed) {
|
93 | this._element.setAttribute('aria-pressed', !$(this._element).hasClass(ClassName.ACTIVE));
|
94 | }
|
95 |
|
96 | if (triggerChangeEvent) {
|
97 | $(this._element).toggleClass(ClassName.ACTIVE);
|
98 | }
|
99 | };
|
100 |
|
101 | _proto.dispose = function dispose() {
|
102 | $.removeData(this._element, DATA_KEY);
|
103 | this._element = null;
|
104 | };
|
105 |
|
106 |
|
107 | Button._jQueryInterface = function _jQueryInterface(config) {
|
108 | return this.each(function () {
|
109 | var data = $(this).data(DATA_KEY);
|
110 |
|
111 | if (!data) {
|
112 | data = new Button(this);
|
113 | $(this).data(DATA_KEY, data);
|
114 | }
|
115 |
|
116 | if (config === 'toggle') {
|
117 | data[config]();
|
118 | }
|
119 | });
|
120 | };
|
121 |
|
122 | _createClass(Button, null, [{
|
123 | key: "VERSION",
|
124 | get: function get() {
|
125 | return VERSION;
|
126 | }
|
127 | }]);
|
128 |
|
129 | return Button;
|
130 | }();
|
131 | |
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | $(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE_CARROT, function (event) {
|
139 | event.preventDefault();
|
140 | var button = event.target;
|
141 |
|
142 | if (!$(button).hasClass(ClassName.BUTTON)) {
|
143 | button = $(button).closest(Selector.BUTTON);
|
144 | }
|
145 |
|
146 | Button._jQueryInterface.call($(button), 'toggle');
|
147 | }).on(Event.FOCUS_BLUR_DATA_API, Selector.DATA_TOGGLE_CARROT, function (event) {
|
148 | var button = $(event.target).closest(Selector.BUTTON)[0];
|
149 | $(button).toggleClass(ClassName.FOCUS, /^focus(in)?$/.test(event.type));
|
150 | });
|
151 | |
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | $.fn[NAME] = Button._jQueryInterface;
|
158 | $.fn[NAME].Constructor = Button;
|
159 |
|
160 | $.fn[NAME].noConflict = function () {
|
161 | $.fn[NAME] = JQUERY_NO_CONFLICT;
|
162 | return Button._jQueryInterface;
|
163 | };
|
164 |
|
165 | return Button;
|
166 | }($);
|
167 |
|
\ | No newline at end of file |