]>
Commit | Line | Data |
---|---|---|
91e44d91 S |
1 | var _createClass = function () { 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); |
2 | ||
3 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | |
4 | ||
5 | /** | |
6 | * -------------------------------------------------------------------------- | |
7 | * Bootstrap (v4.0.0-alpha.6): button.js | |
8 | * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) | |
9 | * -------------------------------------------------------------------------- | |
10 | */ | |
11 | ||
12 | var Button = function ($) { | |
13 | ||
14 | /** | |
15 | * ------------------------------------------------------------------------ | |
16 | * Constants | |
17 | * ------------------------------------------------------------------------ | |
18 | */ | |
19 | ||
20 | var NAME = 'button'; | |
21 | var VERSION = '4.0.0-alpha.6'; | |
22 | var DATA_KEY = 'bs.button'; | |
23 | var EVENT_KEY = '.' + DATA_KEY; | |
24 | var DATA_API_KEY = '.data-api'; | |
25 | var JQUERY_NO_CONFLICT = $.fn[NAME]; | |
26 | ||
27 | var ClassName = { | |
28 | ACTIVE: 'active', | |
29 | BUTTON: 'btn', | |
30 | FOCUS: 'focus' | |
31 | }; | |
32 | ||
33 | var Selector = { | |
34 | DATA_TOGGLE_CARROT: '[data-toggle^="button"]', | |
35 | DATA_TOGGLE: '[data-toggle="buttons"]', | |
36 | INPUT: 'input', | |
37 | ACTIVE: '.active', | |
38 | BUTTON: '.btn' | |
39 | }; | |
40 | ||
41 | var Event = { | |
42 | CLICK_DATA_API: 'click' + EVENT_KEY + DATA_API_KEY, | |
43 | FOCUS_BLUR_DATA_API: 'focus' + EVENT_KEY + DATA_API_KEY + ' ' + ('blur' + EVENT_KEY + DATA_API_KEY) | |
44 | }; | |
45 | ||
46 | /** | |
47 | * ------------------------------------------------------------------------ | |
48 | * Class Definition | |
49 | * ------------------------------------------------------------------------ | |
50 | */ | |
51 | ||
52 | var Button = function () { | |
53 | function Button(element) { | |
54 | _classCallCheck(this, Button); | |
55 | ||
56 | this._element = element; | |
57 | } | |
58 | ||
59 | // getters | |
60 | ||
61 | // public | |
62 | ||
63 | Button.prototype.toggle = function toggle() { | |
64 | var triggerChangeEvent = true; | |
65 | var rootElement = $(this._element).closest(Selector.DATA_TOGGLE)[0]; | |
66 | ||
67 | if (rootElement) { | |
68 | var input = $(this._element).find(Selector.INPUT)[0]; | |
69 | ||
70 | if (input) { | |
71 | if (input.type === 'radio') { | |
72 | if (input.checked && $(this._element).hasClass(ClassName.ACTIVE)) { | |
73 | triggerChangeEvent = false; | |
74 | } else { | |
75 | var activeElement = $(rootElement).find(Selector.ACTIVE)[0]; | |
76 | ||
77 | if (activeElement) { | |
78 | $(activeElement).removeClass(ClassName.ACTIVE); | |
79 | } | |
80 | } | |
81 | } | |
82 | ||
83 | if (triggerChangeEvent) { | |
84 | input.checked = !$(this._element).hasClass(ClassName.ACTIVE); | |
85 | $(input).trigger('change'); | |
86 | } | |
87 | ||
88 | input.focus(); | |
89 | } | |
90 | } | |
91 | ||
92 | this._element.setAttribute('aria-pressed', !$(this._element).hasClass(ClassName.ACTIVE)); | |
93 | ||
94 | if (triggerChangeEvent) { | |
95 | $(this._element).toggleClass(ClassName.ACTIVE); | |
96 | } | |
97 | }; | |
98 | ||
99 | Button.prototype.dispose = function dispose() { | |
100 | $.removeData(this._element, DATA_KEY); | |
101 | this._element = null; | |
102 | }; | |
103 | ||
104 | // static | |
105 | ||
106 | Button._jQueryInterface = function _jQueryInterface(config) { | |
107 | return this.each(function () { | |
108 | var data = $(this).data(DATA_KEY); | |
109 | ||
110 | if (!data) { | |
111 | data = new Button(this); | |
112 | $(this).data(DATA_KEY, data); | |
113 | } | |
114 | ||
115 | if (config === 'toggle') { | |
116 | data[config](); | |
117 | } | |
118 | }); | |
119 | }; | |
120 | ||
121 | _createClass(Button, null, [{ | |
122 | key: 'VERSION', | |
123 | get: function get() { | |
124 | return VERSION; | |
125 | } | |
126 | }]); | |
127 | ||
128 | return Button; | |
129 | }(); | |
130 | ||
131 | /** | |
132 | * ------------------------------------------------------------------------ | |
133 | * Data Api implementation | |
134 | * ------------------------------------------------------------------------ | |
135 | */ | |
136 | ||
137 | $(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE_CARROT, function (event) { | |
138 | event.preventDefault(); | |
139 | ||
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 | * jQuery | |
155 | * ------------------------------------------------------------------------ | |
156 | */ | |
157 | ||
158 | $.fn[NAME] = Button._jQueryInterface; | |
159 | $.fn[NAME].Constructor = Button; | |
160 | $.fn[NAME].noConflict = function () { | |
161 | $.fn[NAME] = JQUERY_NO_CONFLICT; | |
162 | return Button._jQueryInterface; | |
163 | }; | |
164 | ||
165 | return Button; | |
166 | }(jQuery); | |
167 | //# sourceMappingURL=button.js.map |