forked from cheton/jquery-checkboxset
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.checkboxset-1.0.js
More file actions
140 lines (124 loc) · 4.32 KB
/
jquery.checkboxset-1.0.js
File metadata and controls
140 lines (124 loc) · 4.32 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* jQuery CheckboxSet plugin
*
* This document is licensed as free software under the terms of the
* MIT License: http://www.opensource.org/licenses/mit-license.php
*
* @author Cheton Wu
* @version $Id: jquery.checkboxset.js 2010-12-03 cheton@gmail.com $
*/
(function($) {
$.fn.checkboxset = function(options) {
var settings = $.extend({
tristate : true,
data : {},
change : function(name) { }
}, options);
var config = {
selector : $(this),
tristate : settings.tristate,
data : settings.data,
change : settings.change
};
/**
* Public Methods
*/
/**
* Returns the checked status with the given name
*/
this.checked = function(name) {
return config.selector.find("input[name='" + name + "']").attr("checked");
}
/**
* Returns the indeterminate status with the given name
*/
this.indeterminate = function(name) {
return config.selector.find("input[name='" + name + "']").attr("indeterminate");
}
__init__(config, config.data, "");
return this;
}
/**
* Private Methods
*/
__init__ = function(config, obj, prefix) {
for (var key in obj) {
var checked = false;
var val = obj[key];
if (isset(val.checked)) {
config.selector.find("input[name='" + key + "']").attr("checked", val.checked);
delete val.checked;
}
if ( ! empty(val)) {
__init__(config, val, empty(prefix) ? key : prefix + "." + key);
}
config.selector.find("input[name='" + key + "']").bind(
"click", { prefix : prefix, name : key, descendants : val },
function(e) {
var checked = $(this).attr("checked");
traverse_descendants(config, e.data.descendants, checked);
traverse_ancestors(config, e.data.prefix);
config.change(e.data.name); // change callback
}
);
}
traverse_ancestors(config, prefix);
}
traverse_descendants = function(config, objs, checked) {
for (var key in objs) {
var val = objs[key];
if ( ! empty(val)) {
traverse_descendants(config, val, checked);
}
config.selector.find("input[name='" + key + "']").attr("checked", checked);
if (config.tristate) {
config.selector.find("input[name='" + key + "']").attr("indeterminate", false);
}
}
}
traverse_ancestors = function(config, prefix) {
if (empty(prefix))
return;
var arr = prefix.split(".");
var obj = config.data;
for (var i = 0; i < arr.length; i++) {
obj = obj[arr[i]];
if ( ! isset(obj))
break; // skip undefined or null object
}
var count = 0, checked = 0, indeterminate = 0;
for (var name in obj) {
if (config.selector.find("input[name='" + name + "']").attr("checked")) {
++checked;
}
if (config.selector.find("input[name='" + name + "']").attr("indeterminate")) {
++indeterminate;
}
++count;
}
var name = arr.pop();
config.selector.find("input[name='" + name + "']").attr("checked", checked > 0);
if (config.tristate) {
config.selector.find("input[name='" + name + "']").attr("indeterminate", (checked > 0 && count != checked) || (indeterminate > 0));
}
traverse_ancestors(config, arr.join("."));
}
function isset(mixed_var) {
return (mixed_var != null) && (mixed_var != undefined);
}
function empty(mixed_var) {
if (mixed_var === "" || mixed_var === 0 || mixed_var === "0" || mixed_var === null || mixed_var === false || typeof mixed_var === 'undefined') {
return true;
}
if ( ! mixed_var) {
return true;
}
if (typeof mixed_var == 'object') {
for (var key in mixed_var) {
return false;
}
return true;
}
return false;
}
})(jQuery);