Skip to content

Commit d4ad1ad

Browse files
committed
Added #9; reverse option.
1 parent c2ee4f1 commit d4ad1ad

File tree

6 files changed

+84
-42
lines changed

6 files changed

+84
-42
lines changed

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
jQuery Tristate
22
===============
3-
Version 1.2.0
3+
Version 1.2.1
44

5-
Copyright © 2013-2016 Martijn van der Lee.
5+
Copyright © 2013-2017 Martijn van der Lee.
66
MIT Open Source license applies.
77

88
Based on work by Chris Coyier (http://css-tricks.com/indeterminate-checkboxes/)
@@ -23,9 +23,15 @@ Features
2323
`indeterminatevalue` attributes or options.
2424
- HTML attributes modified by the plugin.
2525
- Should support every major browser, including IE6.
26+
- Configurable order of states.
2627

2728
Changelog
2829
-----------
30+
Version 1.2.1
31+
32+
- Added `reverse` option.
33+
- Fixed QUnit testcases.
34+
2935
Version 1.2
3036

3137
- Dropped jQueryUI requirement.
@@ -48,12 +54,12 @@ Sourcecode on Github: https://github.com/vanderlee/tristate
4854

4955
Browser support
5056
---------------
51-
Tested with v1.0.5
57+
Tested with v1.2.1
5258

53-
- Chrome 31
54-
- FireFox 25
55-
- Opera 17
56-
- Internet Explorer 10
59+
- Chrome 56
60+
- FireFox 51
61+
- Opera 43
62+
- Internet Explorer 11
5763

5864
Quick start
5965
-----------

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tristate",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"homepage": "https://github.com/vanderlee/tristate",
55
"authors": [
66
"Martijn van der Lee <martijn@vanderlee.com>"

jqtristate.jquery.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jqtristate",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"title": "Vanderlee Tristate",
55
"description": "jQuery standalone tristate (indeterminate) checkbox with pseudo selectors and optional value modification and .val() overwrite. Standalone, so usable for purposes other than list/tree marking.",
66
"author": {

jquery.tristate.js

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
/*globals jQuery */
33

44
/*!
5-
* Tristate v1.2.0
5+
* Tristate v1.2.1
66
*
7-
* Copyright (c) 2013-2016 Martijn W. van der Lee
7+
* Copyright (c) 2013-2017 Martijn W. van der Lee
88
* Licensed under the MIT.
99
*/
1010
/* Based on work by:
@@ -20,13 +20,14 @@
2020

2121
var pluginName = 'tristate',
2222
defaults = {
23-
change: undefined,
24-
checked: undefined,
25-
indeterminate: undefined,
26-
init: undefined,
27-
state: undefined,
28-
unchecked: undefined,
29-
value: undefined // one-way only!
23+
'change': undefined,
24+
'checked': undefined,
25+
'indeterminate': undefined,
26+
'init': undefined,
27+
'reverse': false,
28+
'state': undefined,
29+
'unchecked': undefined,
30+
'value': undefined // one-way only!
3031
},
3132
valFunction = $.fn.val;
3233

@@ -38,34 +39,23 @@
3839
}
3940
}
4041

41-
$.extend(Plugin.prototype, {
42+
$.extend(Plugin.prototype, {
4243
_create: function() {
4344
var that = this,
4445
state;
4546

4647
// Fix for #1
4748
if (window.navigator.userAgent.indexOf('Trident') >= 0) {
4849
this.element.click(function(e) {
49-
if (!this.indeterminate && $(this).attr('indeterminate')) {
50-
$(this).trigger('change');
51-
}
50+
that._change.call(that, e);
51+
that.element.closest('form').change();
52+
});
53+
} else {
54+
this.element.change(function(e) {
55+
that._change.call(that, e);
5256
});
5357
}
5458

55-
this.element.change(function(e) {
56-
if (e.isTrigger || !e.hasOwnProperty('which')) {
57-
e.preventDefault();
58-
}
59-
60-
switch (that.settings.state) {
61-
case true: that.settings.state = null; break;
62-
case false: that.settings.state = true; break;
63-
default: that.settings.state = false; break;
64-
}
65-
66-
that._refresh(that.settings.change);
67-
});
68-
6959
this.settings.checked = this.element.attr('checkedvalue') || this.settings.checked;
7060
this.settings.unchecked = this.element.attr('uncheckedvalue') || this.settings.unchecked;
7161
this.settings.indeterminate = this.element.attr('indeterminatevalue') || this.settings.indeterminate;
@@ -88,6 +78,20 @@
8878
return this;
8979
},
9080

81+
_change: function(e) {
82+
if (e.isTrigger || !e.hasOwnProperty('which')) {
83+
e.preventDefault();
84+
}
85+
86+
switch (this.settings.state) {
87+
case true: this.settings.state = (this.settings.reverse ? false : null); break;
88+
case false: this.settings.state = (this.settings.reverse ? null : true); break;
89+
default: this.settings.state = (this.settings.reverse ? true : false); break;
90+
}
91+
92+
this._refresh(this.settings.change);
93+
},
94+
9195
_refresh: function(callback) {
9296
var value = this.value();
9397

@@ -97,7 +101,7 @@
97101
this.element.prop('indeterminate', this.settings.state === null);
98102
this.element.get(0).indeterminate = this.settings.state === null;
99103

100-
this.element[this.settings.state ? 'attr' : 'removeAttr']('checked', true);
104+
this.element[this.settings.state === true ? 'attr' : 'removeAttr']('checked', true);
101105
this.element.prop('checked', this.settings.state === true);
102106

103107
if ($.isFunction(callback)) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tristate",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"license": "MIT",
55
"description": "Turn any checkbox into a tristate checkbox. Including .val() extension and pseudo selectors.",
66
"homepage": "https://github.com/vanderlee/tristate",

test/tests.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,54 @@ QUnit.jqui.tests({
1818
equal(cb.is(':determinate'), true, "Initial, Determinate");
1919
equal(cb.is(':indeterminate'), false, "Initial, Not indeterminate");
2020

21-
cb.get(0).click();
21+
cb.click(); // false -> true
2222
equal(cb.is(':checked'), true, "1st click, Checked");
2323
equal(cb.is(':determinate'), true, "1st click, Determinate");
2424
equal(cb.is(':indeterminate'), false, "1st click, Not indeterminate");
2525

26-
cb.click();
26+
cb.click(); // true -> indeterminate
2727
equal(cb.is(':checked'), false, "2nd click, Not checked");
2828
equal(cb.is(':determinate'), false, "2nd click, Not determinate");
2929
equal(cb.is(':indeterminate'), true, "2nd click, Indeterminate");
3030

31-
cb.click();
31+
cb.click(); // indeterminate -> true
3232
equal(cb.is(':checked'), false, "3rd click, Not checked");
3333
equal(cb.is(':determinate'), true, "3rd click, Determinate");
3434
equal(cb.is(':indeterminate'), false, "3rd click, Not indeterminate");
3535
},
36-
36+
37+
"Reverse order": function() {
38+
'use strict';
39+
40+
var cb = $('<input type="checkbox"/>').appendTo('body');
41+
equal(cb.is(':tristate'), false, "It's not a tristate");
42+
43+
cb.tristate({
44+
'reverse': true
45+
});
46+
47+
equal(cb.is(':tristate'), true, "It's a tristate");
48+
49+
equal(cb.is(':checked'), false, "Initial, Not checked");
50+
equal(cb.is(':determinate'), true, "Initial, Determinate");
51+
equal(cb.is(':indeterminate'), false, "Initial, Not indeterminate");
52+
53+
cb.click(); // false -> indeterminate
54+
equal(cb.is(':checked'), false, "1st click, Not checked");
55+
equal(cb.is(':determinate'), false, "1st click, Not determinate");
56+
equal(cb.is(':indeterminate'), true, "1st click, Indeterminate");
57+
58+
cb.click(); // indeterminate -> true
59+
equal(cb.is(':checked'), true, "2nd click, Checked");
60+
equal(cb.is(':determinate'), true, "2nd click, Determinate");
61+
equal(cb.is(':indeterminate'), false, "2nd click, Not indeterminate");
62+
63+
cb.click(); // true -> false
64+
equal(cb.is(':checked'), false, "3rd click, Not checked");
65+
equal(cb.is(':determinate'), true, "3rd click, Determinate");
66+
equal(cb.is(':indeterminate'), false, "3rd click, Not indeterminate");
67+
},
68+
3769
"Expando values": function() {
3870
'use strict';
3971

0 commit comments

Comments
 (0)