[ChButton] Prevents 'forceMaxWidth' to be executed after every click.
[profile/ivi/cowhide.git] / src / cowhide-button.js
1 (function($, undefined) {
2     'use strict';
3
4     var ChButton = function(element, options) {
5         $.fn.ch_widget.Constructor(element, options);
6         this.$element = $(element);
7         this.options = $.extend({}, $.fn.ch_widget.defaults);
8     };
9
10     ChButton.prototype = $.extend(
11         {},
12         $.fn.ch_widget.Constructor.prototype,
13         {
14             constructor: ChButton
15         }
16     );
17
18     $.fn.ch_button = function(option) {
19         return this.each(function() {
20             var $this = $(this),
21                 data = $this.data('ch_button'),
22                 options = typeof option == 'object' && option;
23
24             if (!data) {
25                 $this.data('ch_button', (data = new ChButton(this, options)));
26                 data.forceMaxWidth();
27             }
28
29             $this.button(option);
30         });
31     };
32
33     $.fn.ch_button.Constructor = ChButton;
34
35     /* CHBUTTON DATA-API
36      * ================= */
37     $(function() {
38         $('body').on('click.ch_button.data-api', '[data-toggle^=ch-button]', function(e) {
39             var $btn = $(e.target);
40             if (!$btn.hasClass('btn'))
41                 $btn = $btn.closest('.btn');
42             if ($btn) {
43                 $btn.ch_button('toggle');
44             }
45         });
46
47         $('.btn, button, input[type=button]').ch_button();
48     })
49 })(window.jQuery);