-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Convert to ES6, module syntax * modify precommit hook * modify export * finalize 2.0.0
- Loading branch information
Showing
9 changed files
with
405 additions
and
443 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
angular.module('ngIndeterminate', []) | ||
.directive('indeterminate', ($parse) => { | ||
return { | ||
restrict: 'AE', | ||
link: (scope, elem, attrs) => { | ||
let propKey = attrs.indeterminateKey || 'enabled'; | ||
let disabledKey = attrs.indeterminateDisabled || 'adminDisabled'; | ||
let trueValue = true; | ||
let falseValue = false; | ||
if (attrs.ngTrueValue && attrs.ngFalseValue) { | ||
trueValue = $parse(attrs.ngTrueValue)(scope); | ||
falseValue = $parse(attrs.ngFalseValue)(scope); | ||
} | ||
else if (attrs.ngTrueValue || attrs.ngFalseValue) { | ||
throw new Error('Must have both ngTrueValue and ngFalseValue set'); | ||
} | ||
|
||
// Watch for changes to the list that comprises the indeterminate checkbox | ||
scope.$watch(() => { | ||
const values = scope.$eval(attrs.indeterminate); | ||
return values.filter(v => !v[disabledKey]).filter(v => v[propKey] === trueValue).length + values.length; | ||
}, () => { | ||
const values = scope.$eval(attrs.indeterminate).filter(v => !v[disabledKey]); | ||
const enabled = values.filter(v => v[propKey] === trueValue); | ||
elem[0].indeterminate = 0 < enabled.length && enabled.length < values.length; | ||
if (enabled.length === values.length) { | ||
elem[0].checked = true; | ||
} else { | ||
elem[0].checked = false; | ||
} | ||
}); | ||
|
||
// Update the list when the indeterminate is clicked | ||
elem.on('click', (e) => { | ||
scope.$apply(() => { | ||
const values = scope.$eval(attrs.indeterminate).filter(v => !v[disabledKey]); | ||
const enabled = values.filter(v => v[propKey] === trueValue); | ||
let setValue; | ||
if (enabled.length < values.length) { | ||
setValue = trueValue; | ||
} else { | ||
setValue = falseValue; | ||
} | ||
for (let i = 0; i < values.length; i++) { | ||
if (!values[i][disabledKey]) values[i][propKey] = setValue; | ||
} | ||
}); | ||
}); | ||
}, | ||
}; | ||
}); | ||
|
||
if (typeof exports !== 'undefined' && typeof module !== 'undefined') module.exports = exports = 'ngIndeterminate'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.