add more comments

This commit is contained in:
vitam 2017-07-29 22:35:07 +02:00
parent 2f4cb14b09
commit 19ba83f273

View File

@ -34,7 +34,7 @@ app.directive("indeterminate", [
} }
}; };
}; };
var passIfNotIsLeafChild = function (callback) { // ensure to execute callback only when this input hasn't subinput var passIfNotIsLeafChild = function (callback) { // ensure to execute callback only when this input hasn't any subinput
return function () { return function () {
if (children.length === 0) { if (children.length === 0) {
callback.apply(this, arguments); callback.apply(this, arguments);
@ -48,44 +48,45 @@ app.directive("indeterminate", [
} }
}; };
}; };
var passIfIsIndeterminate = function (callback) { // pass through the event from the scope where they sent var passIfIsIndeterminate = function (callback) { // pass through the event when this input is indeterminate
return function () { return function () {
if (!elem.prop("indeterminate")) { if (!elem.prop("indeterminate")) {
return callback.apply(this, arguments); return callback.apply(this, arguments);
} }
}; };
}; };
var catchEventOnlyOnce = function (callback) { // only fire once, and stop event's propagation /* var catchEventOnlyOnce = function (callback) { // only fire once, and stop event's propagation
return function (event) { return function (event) {
callback.apply(this, arguments); callback.apply(this, arguments);
return event.stopPropagation(); return event.stopPropagation();
}; };
}; }; */
if (attr["indeterminate"] && parse(attr["indeterminate"]).constant) { if (attr["indeterminate"] && parse(attr["indeterminate"]).constant) {
setIndeterminateState(scope.$eval(attr["indeterminate"])); // set to default value (set in template) setIndeterminateState(scope.$eval(attr["indeterminate"])); // set to default value (set in template)
} }
if (attr["indeterminate"] && parse(attr["indeterminate"]).constant && !scope.$eval(attr["indeterminate"])) { if (attr["indeterminate"] && parse(attr["indeterminate"]).constant && !scope.$eval(attr["indeterminate"])) {
// when this input wont have subinput, they will only receive parent change and emit child change event // when this input wont have subinput, they will only receive parent change and emit child change event
ngModelCtrl.$viewChangeListeners.push(passIfNotIsLeafChild(function () { ngModelCtrl.$viewChangeListeners.push(passIfNotIsLeafChild(function () {
scope.$emit("childSelectedChange", get()); scope.$emit("childSelectedChange", get()); // notifies parents to change their state
})); }));
scope.$on("ParentSelectedChange", passThroughThisScope(passIfNotIsLeafChild( scope.$on("ParentSelectedChange", passThroughThisScope(passIfNotIsLeafChild(
function (event, newVal) { function (event, newVal) {
setModelValueWithSideEffect(newVal); // set value to parent's value; this will cause listener to emit childChange event; this won't be a infinite loop setModelValueWithSideEffect(newVal); // set value to parent's value; this will cause listener to emit childChange event; this won't be a infinite loop
}))); })));
// init first time and only once // init first time and only once
scope.$emit("i'm child input", get); scope.$emit("i'm child input", get); // traverses upwards toward the root scope notifying the listeners for keep reference to this input's value
scope.$emit("childSelectedChange", get()); // force emitted, and force the parent change their state base on children at first time scope.$emit("childSelectedChange", get()); // force emitted, and force the parent change their state base on children at first time
} else { } else {
// establish parent-child's relation // establish parent-child's relation
// listen for the child emitted token // listen for the child emitted token
scope.$on("i'm child input", passThroughThisScope( scope.$on("i'm child input", passThroughThisScope( // can't apply pass passIfIsLeafChild, at beginning all has not child input
function (event, child) { function (event, child) {
children.push(child); children.push(child);
}) })
); );
var updateBaseOnChildrenState = function (event, newChildValue) { var updateBaseOnChildrenState = function (event, newChildValue) {
if ((cacheSelectedSubInputNumber + cacheNoSelectedSubInputNumber) !== children.length) { if ((cacheSelectedSubInputNumber + cacheNoSelectedSubInputNumber) !== children.length) {
// cache childern state
cacheSelectedSubInputNumber = 0; cacheSelectedSubInputNumber = 0;
cacheNoSelectedSubInputNumber = 0; cacheNoSelectedSubInputNumber = 0;
for (var i = 0; i < children.length; i++) { for (var i = 0; i < children.length; i++) {
@ -106,7 +107,7 @@ app.directive("indeterminate", [
cacheNoSelectedSubInputNumber++; cacheNoSelectedSubInputNumber++;
} }
} }
var allSelected = (cacheNoSelectedSubInputNumber === 0); var allSelected = (cacheNoSelectedSubInputNumber === 0); // if doesn't has any no selected input
var anySeleted = (cacheSelectedSubInputNumber > 0); var anySeleted = (cacheSelectedSubInputNumber > 0);
setIndeterminateState(allSelected !== anySeleted); // if at least one is selected, but not all then set input property indeterminate to true setIndeterminateState(allSelected !== anySeleted); // if at least one is selected, but not all then set input property indeterminate to true
setModelValueWithSideEffect(allSelected); // change binding model value and trigger onchange event setModelValueWithSideEffect(allSelected); // change binding model value and trigger onchange event