Skip to content

Commit

Permalink
Adding checkToggleAll to CheckAllOrNone.js
Browse files Browse the repository at this point in the history
  • Loading branch information
davywentworth committed Oct 10, 2013
1 parent ce0c6d0 commit 4c6f246
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
48 changes: 45 additions & 3 deletions Docs/Delegators/Delegator.CheckAllOrNone.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
Delegator Triggers: checkAll, checkNone
Delegator Triggers: checkAll, checkNone, checkToggleAll
=======================

Provides click event delegators for selecting or deselecting a group of checkboxes.

### Example
### checkAll, checkNone

#### Example

<div class="chex">
<a data-trigger="checkAll" data-checkall-options ="'targets': '!div.chex input'">checkAll</a>
Expand All @@ -16,9 +18,49 @@ Provides click event delegators for selecting or deselecting a group of checkbox
<input type="checkbox"/>
</div>

### Options
#### Options

* targets - (*string*; **required**) a selector that will return the inputs to check/uncheck.

### checkToggle

#### Example
<div class="chex">
<label>
<input type="checkbox" data-trigger="checkToggleAll" data-checktoggleall-options= "
'targets': '!div.chex input',
'classTarget': '!label',
'class': 'red'
" />
Here's a label
</label>
<label>
<input type="checkbox"/>
Here's a label
</label>
<label>
<input type="checkbox"/>
Here's a label
</label>
<label>
<input type="checkbox"/>
Here's a label
</label>
<label>
<input type="checkbox"/>
Here's a label
</label>
<label>
<input type="checkbox"/>
Here's a label
</label>
</div>

#### Options

* targets - (*string*; **required**) a selector that will return the inputs to check/uncheck.
* class - (*string*; **optional**) a class to apply to the targets' classTarget.
* classTarget - (*string*; **optional**) a selector relative to the targets to apply *class* to.

### See Also

Expand Down
28 changes: 28 additions & 0 deletions Source/Delegators/Delegator.CheckAllOrNone.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,34 @@ Delegator.register('click', {
if (targets.length) targets.set('checked', false);
else api.warn('There were no inputs found to uncheck.');
}
},

'checkToggleAll': {
require: ['targets'],
handler: function(event, link, api){
var classTarget = api.get('classTarget');
var classForTarget = api.get('class');
var targets = link.getElements(api.get('targets'));
if (targets.length) {
if (link.get('data-state') == undefined) api.error('Must specify an initial state as data-state.');
if (link.get('data-state') == '1') {
targets.set('checked', false);
link.set('data-state', '0');
if (classTarget && classForTarget) {
if (!targets.getElement(classTarget)) api.fail('Could not find classTarget: ' + classTarget)
targets.getElement(classTarget).removeClass(classForTarget);
}
} else {
targets.set('checked', true);
link.set('data-state', '1');
if (classTarget && classForTarget) {
if (!targets.getElement(classTarget)) api.fail('Could not find classTarget: ' + classTarget)
targets.getElement(classTarget).addClass(classForTarget);
}
}
}
else api.warn('There were no inputs found to uncheck.');
}
}

});

0 comments on commit 4c6f246

Please sign in to comment.