Project

General

Profile

Download (3.16 KB) Statistics
| Branch: | Revision:
1
/* =============================================================
2
 * flatui-checkbox v0.0.3
3
 * ============================================================ */
4
 
5
!function ($) {
6

    
7
 /* CHECKBOX PUBLIC CLASS DEFINITION
8
  * ============================== */
9

    
10
  var Checkbox = function (element, options) {
11
    this.init(element, options);
12
  }
13

    
14
  Checkbox.prototype = {
15
    
16
    constructor: Checkbox
17
    
18
  , init: function (element, options) {      
19
    var $el = this.$element = $(element)
20
    
21
    this.options = $.extend({}, $.fn.checkbox.defaults, options);      
22
    $el.before(this.options.template);    
23
    this.setState(); 
24
  }  
25
   
26
  , setState: function () {    
27
      var $el = this.$element
28
        , $parent = $el.closest('.checkbox');
29
        
30
        $el.prop('disabled') && $parent.addClass('disabled');   
31
        $el.prop('checked') && $parent.addClass('checked');
32
    }  
33
    
34
  , toggle: function () {    
35
      var ch = 'checked'
36
        , $el = this.$element
37
        , $parent = $el.closest('.checkbox')
38
        , checked = $el.prop(ch)
39
        , e = $.Event('toggle')
40
      
41
      if ($el.prop('disabled') == false) {
42
        $parent.toggleClass(ch) && checked ? $el.removeAttr(ch) : $el.prop(ch, ch);
43
        $el.trigger(e).trigger('change'); 
44
      }
45
    }  
46
    
47
  , setCheck: function (option) {    
48
      var d = 'disabled'
49
        , ch = 'checked'
50
        , $el = this.$element
51
        , $parent = $el.closest('.checkbox')
52
        , checkAction = option == 'check' ? true : false
53
        , e = $.Event(option)
54
      
55
      $parent[checkAction ? 'addClass' : 'removeClass' ](ch) && checkAction ? $el.prop(ch, ch) : $el.removeAttr(ch);
56
      $el.trigger(e).trigger('change');       
57
    }  
58
      
59
  }
60

    
61

    
62
 /* CHECKBOX PLUGIN DEFINITION
63
  * ======================== */
64

    
65
  var old = $.fn.checkbox
66

    
67
  $.fn.checkbox = function (option) {
68
    return this.each(function () {
69
      var $this = $(this)
70
        , data = $this.data('checkbox')
71
        , options = $.extend({}, $.fn.checkbox.defaults, $this.data(), typeof option == 'object' && option);
72
      if (!data) $this.data('checkbox', (data = new Checkbox(this, options)));
73
      if (option == 'toggle') data.toggle()
74
      if (option == 'check' || option == 'uncheck') data.setCheck(option)
75
      else if (option) data.setState(); 
76
    });
77
  }
78
  
79
  $.fn.checkbox.defaults = {
80
    template: '<span class="icons"><span class="first-icon fui-checkbox-unchecked"></span><span class="second-icon fui-checkbox-checked"></span></span>'
81
  }
82

    
83

    
84
 /* CHECKBOX NO CONFLICT
85
  * ================== */
86

    
87
  $.fn.checkbox.noConflict = function () {
88
    $.fn.checkbox = old;
89
    return this;
90
  }
91

    
92

    
93
 /* CHECKBOX DATA-API
94
  * =============== */
95

    
96
  $(document).on('click.checkbox.data-api', '[data-toggle^=checkbox], .checkbox', function (e) {
97
    var $checkbox = $(e.target);
98
    if (e.target.tagName != "A") {      
99
      e && e.preventDefault() && e.stopPropagation();
100
      if (!$checkbox.hasClass('checkbox')) $checkbox = $checkbox.closest('.checkbox');
101
      $checkbox.find(':checkbox').checkbox('toggle');
102
    }
103
  });
104
  
105
  $(function () {
106
    $('[data-toggle="checkbox"]').each(function () {
107
      var $checkbox = $(this);
108
      $checkbox.checkbox();
109
    });
110
  });
111

    
112
}(window.jQuery);
(4-4/6)