forked from ExactTarget/fuelux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckbox.js
109 lines (79 loc) · 2.26 KB
/
checkbox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Fuel UX Checkbox
* https://github.com/ExactTarget/fuelux
*
* Copyright (c) 2012 ExactTarget
* Licensed under the MIT license.
*/
define(['require','jquery'],function (require) {
var $ = require('jquery');
// CHECKBOX CONSTRUCTOR AND PROTOTYPE
var Checkbox = function (element, options) {
this.$element = $(element);
this.options = $.extend({}, $.fn.checkbox.defaults, options);
// cache elements
this.$label = this.$element.parent();
this.$icon = this.$label.find('i');
this.$chk = this.$label.find('input[type=checkbox]');
// set default state
this.setState(this.$chk);
// handle events
this.$chk.on('change', $.proxy(this.itemchecked, this));
};
Checkbox.prototype = {
constructor: Checkbox,
setState: function ($chk) {
var checked = $chk.is(':checked');
var disabled = $chk.is(':disabled');
// reset classes
this.$icon.removeClass('checked').removeClass('disabled');
// set state of checkbox
if (checked === true) {
this.$icon.addClass('checked');
}
if (disabled === true) {
this.$icon.addClass('disabled');
}
},
enable: function () {
this.$chk.attr('disabled', false);
this.$icon.removeClass('disabled');
},
disable: function () {
this.$chk.attr('disabled', true);
this.$icon.addClass('disabled');
},
toggle: function () {
this.$chk.click();
},
itemchecked: function (e) {
var chk = $(e.target);
this.setState(chk);
}
};
// CHECKBOX PLUGIN DEFINITION
$.fn.checkbox = function (option, value) {
var methodReturn;
var $set = this.each(function () {
var $this = $(this);
var data = $this.data('checkbox');
var options = typeof option === 'object' && option;
if (!data) $this.data('checkbox', (data = new Checkbox(this, options)));
if (typeof option === 'string') methodReturn = data[option](value);
});
return (methodReturn === undefined) ? $set : methodReturn;
};
$.fn.checkbox.defaults = {};
$.fn.checkbox.Constructor = Checkbox;
// CHECKBOX DATA-API
$(function () {
$(window).on('load', function () {
//$('i.checkbox').each(function () {
$('.checkbox-custom > input[type=checkbox]').each(function () {
var $this = $(this);
if ($this.data('checkbox')) return;
$this.checkbox($this.data());
});
});
});
});