add more comment

This commit is contained in:
vitam 2017-07-29 17:35:03 +02:00
parent 4fa683b4ee
commit ace00309d3

View File

@ -21,31 +21,25 @@ app.directive("indeterminate", [
var setIndeterminateState = function (newValue) { var setIndeterminateState = function (newValue) {
elem.prop("indeterminate", newValue); elem.prop("indeterminate", newValue);
}; };
var setModelValueWithoutSideEffect = function (newVal) {
setter(scope, newVal);
ngModelCtrl.$modelValue = newVal;
ngModelCtrl.$viewValue = newVal;
ngModelCtrl.$render();
};
var setWithSideEffect = function (newVal) { var setWithSideEffect = function (newVal) {
ngModelCtrl.$setViewValue(newVal); ngModelCtrl.$setViewValue(newVal);
ngModelCtrl.$render(); ngModelCtrl.$render();
}; };
var passIfLeafChild = function (callback) { var passIfLeafChild = function (callback) { // ensure to execute callback when this input have one or more subinputs
return function () { return function () {
if (children.length > 0) { if (children.length > 0) {
callback.apply(this, arguments); callback.apply(this, arguments);
} }
}; };
}; };
var passIfNotIsLeafChild = function (callback) { var passIfNotIsLeafChild = function (callback) { // ensure to execute callback when this input havent subinput
return function () { return function () {
if (children.length === 0) { if (children.length === 0) {
callback.apply(this, arguments); callback.apply(this, arguments);
} }
}; };
}; };
var passThroughThisScope = function (callback) { var passThroughThisScope = function (callback) { // pass through the event from the scope where they sent
return function (event) { return function (event) {
if (event.targetScope !== event.currentScope) { if (event.targetScope !== event.currentScope) {
return callback.apply(this, arguments); return callback.apply(this, arguments);
@ -60,14 +54,14 @@ app.directive("indeterminate", [
})); }));
scope.$on("ParentSelectedChange", passThroughThisScope( scope.$on("ParentSelectedChange", passThroughThisScope(
function (event, newVal) { function (event, newVal) {
setWithSideEffect(newVal); setWithSideEffect(newVal); // set value to parent's value; this will cause listener to emit childChange event; this won't be a infinite loop
})); }));
scope.$emit("i'm child input", get);
setWithSideEffect(get());
scope.$emit("childSelectedChange");
} else {
// init first time and only once // init first time and only once
scope.$emit("i'm child input", get);
scope.$emit("childSelectedChange"); // force emitted, and force the parent change their state base on children at first time
} else {
// establish parent-child's relation // establish parent-child's relation
// listen for the child emitted token
scope.$on("i'm child input", passThroughThisScope( scope.$on("i'm child input", passThroughThisScope(
function (event, child) { function (event, child) {
children.push(child); children.push(child);
@ -80,12 +74,13 @@ app.directive("indeterminate", [
var anySeleted = children.some(function (child) { var anySeleted = children.some(function (child) {
return child(); return child();
}); });
setIndeterminateState(allSelected !== anySeleted); setIndeterminateState(allSelected !== anySeleted); // if at least one is selected, but not all then set input property indeterminate to true
setWithSideEffect(allSelected); setWithSideEffect(allSelected);
}; };
// is not leaf input, Only receive child change and parent change event // is not leaf input, Only receive child change and parent change event
ngModelCtrl.$viewChangeListeners.push(passIfLeafChild(function () { ngModelCtrl.$viewChangeListeners.push(passIfLeafChild(function () {
if (!elem.prop("indeterminate")) { // emit when prop indeterminate is set to false // emit when property indeterminate is set to false, prevent recursively emitting event from parent to children, children to parent
if (!elem.prop("indeterminate")) {
scope.$broadcast("ParentSelectedChange", get()); scope.$broadcast("ParentSelectedChange", get());
} }
})); }));