-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathjquery.tristate.js
213 lines (181 loc) · 5.95 KB
/
jquery.tristate.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*jslint devel: true, bitwise: true, regexp: true, browser: true, confusion: true, unparam: true, eqeq: true, white: true, nomen: true, plusplus: true, maxerr: 50, indent: 4 */
/*globals jQuery */
/*!
* Tristate v1.2.1
*
* Copyright (c) 2013-2017 Martijn W. van der Lee
* Licensed under the MIT.
*/
/* Based on work by:
* Chris Coyier (http://css-tricks.com/indeterminate-checkboxes/)
*
* Tristate checkbox with support features
* pseudo selectors
* val() overwrite
*/
;(function($, undefined) {
'use strict';
var pluginName = 'tristate',
defaults = {
'change': undefined,
'checked': undefined,
'indeterminate': undefined,
'init': undefined,
'reverse': false,
'state': undefined,
'unchecked': undefined,
'value': undefined // one-way only!
},
valFunction = $.fn.val;
function Plugin(element, options) {
if($(element).is(':checkbox')) {
this.element = $(element);
this.settings = $.extend( {}, defaults, options );
this._create();
}
}
$.extend(Plugin.prototype, {
_create: function() {
var that = this,
state;
// Fix for #1
if (window.navigator.userAgent.indexOf('Trident') >= 0) {
this.element.click(function(e) {
that._change.call(that, e);
that.element.closest('form').change();
});
} else {
this.element.change(function(e) {
that._change.call(that, e);
});
}
this.settings.checked = this.element.attr('checkedvalue') || this.settings.checked;
this.settings.unchecked = this.element.attr('uncheckedvalue') || this.settings.unchecked;
this.settings.indeterminate = this.element.attr('indeterminatevalue') || this.settings.indeterminate;
// Initially, set state based on option state or attributes
if (typeof this.settings.state === 'undefined') {
this.settings.state = typeof this.element.attr('indeterminate') !== 'undefined'? null : this.element.is(':checked');
}
// If value specified, overwrite with value
if (typeof this.settings.value !== 'undefined') {
state = this._parseValue(this.settings.value);
if (typeof state !== 'undefined') {
this.settings.state = state;
}
}
this._refresh(this.settings.init);
return this;
},
_change: function(e) {
if (e.isTrigger || !e.hasOwnProperty('which')) {
e.preventDefault();
}
switch (this.settings.state) {
case true: this.settings.state = (this.settings.reverse ? false : null); break;
case false: this.settings.state = (this.settings.reverse ? null : true); break;
default: this.settings.state = (this.settings.reverse ? true : false); break;
}
this._refresh(this.settings.change);
},
_refresh: function(callback) {
var value = this.value();
this.element.data("vanderlee." + pluginName, value);
this.element[this.settings.state === null ? 'attr' : 'removeAttr']('indeterminate', 'indeterminate');
this.element.prop('indeterminate', this.settings.state === null);
this.element.get(0).indeterminate = this.settings.state === null;
this.element[this.settings.state === true ? 'attr' : 'removeAttr']('checked', true);
this.element.prop('checked', this.settings.state === true);
if ($.isFunction(callback)) {
callback.call(this.element, this.settings.state, this.value());
}
},
state: function(value) {
if (typeof value === 'undefined') {
return this.settings.state;
} else if (value === true || value === false || value === null) {
this.settings.state = value;
this._refresh(this.settings.change);
}
return this;
},
_parseValue: function(value) {
if (value === this.settings.checked) {
return true;
} else if (value === this.settings.unchecked) {
return false;
} else if (value === this.settings.indeterminate) {
return null;
}
},
value: function(value) {
if (typeof value === 'undefined') {
var value;
switch (this.settings.state) {
case true:
value = this.settings.checked;
break;
case false:
value = this.settings.unchecked;
break;
case null:
value = this.settings.indeterminate;
break;
}
return typeof value === 'undefined'? this.element.attr('value') : value;
} else {
var state = this._parseValue(value);
if (typeof state !== 'undefined') {
this.settings.state = state;
this._refresh(this.settings.change);
}
}
}
});
$.fn[pluginName] = function (options, value) {
var result = this;
this.each(function() {
if (!$.data(this, "plugin.vanderlee." + pluginName)) {
$.data(this, "plugin.vanderlee." + pluginName, new Plugin(this, options));
} else if (typeof options === 'string') {
if (typeof value === 'undefined') {
result = $(this).data("plugin.vanderlee." + pluginName)[options]();
return false;
} else {
$(this).data("plugin.vanderlee." + pluginName)[options](value);
}
}
});
return result;
};
// Overwrite fn.val
$.fn.val = function(value) {
var data = this.data("vanderlee." + pluginName);
if (typeof data === 'undefined') {
if (typeof value === 'undefined') {
return valFunction.call(this);
} else {
return valFunction.call(this, value);
}
} else {
if (typeof value === 'undefined') {
return data;
} else {
this.data("vanderlee." + pluginName, value);
return this;
}
}
};
// :indeterminate pseudo selector
$.expr.filters.indeterminate = function(element) {
var $element = $(element);
return typeof $element.data("vanderlee." + pluginName) !== 'undefined' && $element.prop('indeterminate');
};
// :determinate pseudo selector
$.expr.filters.determinate = function(element) {
return !($.expr.filters.indeterminate(element));
};
// :tristate selector
$.expr.filters.tristate = function(element) {
return typeof $(element).data("vanderlee." + pluginName) !== 'undefined';
};
})(jQuery);