2017-07-27 12:31:18 +02:00
|
|
|
// watches changes in the file upload control (input[file]) and
|
|
|
|
// puts the files selected in an attribute
|
2017-07-29 14:42:06 +02:00
|
|
|
var app = angular.module("webui.directives.fileselect", ["webui.services.deps"]);
|
|
|
|
app.directive("indeterminate", [
|
|
|
|
"$parse", function syncInput (parse) {
|
|
|
|
return {
|
|
|
|
require : "ngModel",
|
|
|
|
// Restrict the directive so it can only be used as an attribute
|
|
|
|
restrict : "A",
|
2017-07-27 12:31:18 +02:00
|
|
|
|
2017-07-29 14:42:06 +02:00
|
|
|
link : function link (scope, elem, attr, ngModelCtrl) {
|
|
|
|
// Whenever the bound value of the attribute changes we update
|
|
|
|
// use upward emit notification for change to prevent the performance penalty bring by $scope.$watch
|
|
|
|
var getter = parse(attr["ngModel"]);
|
2017-07-29 20:15:01 +02:00
|
|
|
// var setter = getter.assign;
|
2017-07-29 14:42:06 +02:00
|
|
|
var children = []; // cache children input
|
2017-07-29 20:15:01 +02:00
|
|
|
var cacheSelectedSubInputNumber = 0;
|
|
|
|
var cacheNoSelectedSubInputNumber = 0;
|
2017-07-29 14:42:06 +02:00
|
|
|
var get = function () {
|
|
|
|
return getter(scope);
|
|
|
|
};
|
|
|
|
// the internal 'indeterminate' flag on the attached dom element
|
|
|
|
var setIndeterminateState = function (newValue) {
|
|
|
|
elem.prop("indeterminate", newValue);
|
|
|
|
};
|
2017-07-29 20:15:01 +02:00
|
|
|
var setModelValueWithSideEffect = function (newVal) { // will cause to emit corresponding events
|
2017-07-29 14:42:06 +02:00
|
|
|
ngModelCtrl.$setViewValue(newVal);
|
|
|
|
ngModelCtrl.$render();
|
|
|
|
};
|
2017-07-29 20:15:01 +02:00
|
|
|
var passIfIsLeafChild = function (callback) { // ensure to execute callback only when this input has one or more subinputs
|
2017-07-29 14:42:06 +02:00
|
|
|
return function () {
|
|
|
|
if (children.length > 0) {
|
|
|
|
callback.apply(this, arguments);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2017-07-29 20:15:01 +02:00
|
|
|
var passIfNotIsLeafChild = function (callback) { // ensure to execute callback only when this input hasn't subinput
|
2017-07-29 14:42:06 +02:00
|
|
|
return function () {
|
|
|
|
if (children.length === 0) {
|
|
|
|
callback.apply(this, arguments);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2017-07-29 17:35:03 +02:00
|
|
|
var passThroughThisScope = function (callback) { // pass through the event from the scope where they sent
|
2017-07-29 14:42:06 +02:00
|
|
|
return function (event) {
|
|
|
|
if (event.targetScope !== event.currentScope) {
|
|
|
|
return callback.apply(this, arguments);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2017-07-29 21:59:14 +02:00
|
|
|
var passIfIsIndeterminate = function (callback) { // pass through the event from the scope where they sent
|
|
|
|
return function () {
|
|
|
|
if (!elem.prop("indeterminate")) {
|
|
|
|
return callback.apply(this, arguments);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2017-07-29 20:15:01 +02:00
|
|
|
var catchEventOnlyOnce = function (callback) { // only fire once, and stop event's propagation
|
|
|
|
return function (event) {
|
|
|
|
callback.apply(this, arguments);
|
|
|
|
return event.stopPropagation();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
if (attr["indeterminate"] && parse(attr["indeterminate"]).constant) {
|
|
|
|
setIndeterminateState(scope.$eval(attr["indeterminate"])); // set to default value (set in template)
|
|
|
|
}
|
2017-07-29 14:42:06 +02:00
|
|
|
if (attr["indeterminate"] && parse(attr["indeterminate"]).constant && !scope.$eval(attr["indeterminate"])) {
|
2017-07-29 20:15:01 +02:00
|
|
|
// when this input wont have subinput, they will only receive parent change and emit child change event
|
2017-07-29 14:42:06 +02:00
|
|
|
ngModelCtrl.$viewChangeListeners.push(passIfNotIsLeafChild(function () {
|
2017-07-29 20:15:01 +02:00
|
|
|
scope.$emit("childSelectedChange", get());
|
2017-07-29 14:42:06 +02:00
|
|
|
}));
|
2017-07-29 20:15:01 +02:00
|
|
|
scope.$on("ParentSelectedChange", passThroughThisScope(passIfNotIsLeafChild(
|
2017-07-29 14:42:06 +02:00
|
|
|
function (event, newVal) {
|
2017-07-29 20:15:01 +02:00
|
|
|
setModelValueWithSideEffect(newVal); // set value to parent's value; this will cause listener to emit childChange event; this won't be a infinite loop
|
|
|
|
})));
|
2017-07-29 17:35:03 +02:00
|
|
|
// init first time and only once
|
2017-07-29 14:42:06 +02:00
|
|
|
scope.$emit("i'm child input", get);
|
2017-07-29 20:15:01 +02:00
|
|
|
scope.$emit("childSelectedChange", get()); // force emitted, and force the parent change their state base on children at first time
|
2017-07-29 14:42:06 +02:00
|
|
|
} else {
|
|
|
|
// establish parent-child's relation
|
2017-07-29 17:35:03 +02:00
|
|
|
// listen for the child emitted token
|
2017-07-29 14:42:06 +02:00
|
|
|
scope.$on("i'm child input", passThroughThisScope(
|
|
|
|
function (event, child) {
|
|
|
|
children.push(child);
|
|
|
|
})
|
|
|
|
);
|
2017-07-29 20:15:01 +02:00
|
|
|
var updateBaseOnChildrenState = function (event, newChildValue) {
|
|
|
|
if ((cacheSelectedSubInputNumber + cacheNoSelectedSubInputNumber) !== children.length) {
|
|
|
|
cacheSelectedSubInputNumber = 0;
|
|
|
|
cacheNoSelectedSubInputNumber = 0;
|
|
|
|
for (var i = 0; i < children.length; i++) {
|
|
|
|
if (children[i]()) {
|
|
|
|
cacheSelectedSubInputNumber += 1;
|
|
|
|
} else {
|
|
|
|
cacheNoSelectedSubInputNumber += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// no need for recalculated children state
|
|
|
|
// just make a few change to cache value
|
|
|
|
if (newChildValue) {
|
|
|
|
cacheSelectedSubInputNumber++;
|
|
|
|
cacheNoSelectedSubInputNumber--;
|
|
|
|
} else {
|
|
|
|
cacheSelectedSubInputNumber--;
|
|
|
|
cacheNoSelectedSubInputNumber++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var allSelected = (cacheNoSelectedSubInputNumber === 0);
|
|
|
|
var anySeleted = (cacheSelectedSubInputNumber > 0);
|
2017-07-29 17:35:03 +02:00
|
|
|
setIndeterminateState(allSelected !== anySeleted); // if at least one is selected, but not all then set input property indeterminate to true
|
2017-07-29 21:59:14 +02:00
|
|
|
setModelValueWithSideEffect(allSelected); // change binding model value and trigger onchange event
|
2017-07-29 14:42:06 +02:00
|
|
|
};
|
|
|
|
// is not leaf input, Only receive child change and parent change event
|
2017-07-29 21:59:14 +02:00
|
|
|
ngModelCtrl.$viewChangeListeners.push(passIfIsLeafChild(passIfIsIndeterminate(function () {
|
|
|
|
// emit when input property indeterminate is false, prevent recursively emitting event from parent to children, children to parent
|
|
|
|
scope.$broadcast("ParentSelectedChange", get());
|
|
|
|
})));
|
2017-07-29 14:42:06 +02:00
|
|
|
// reset input state base on children inputs
|
2017-07-29 20:15:01 +02:00
|
|
|
scope.$on("childSelectedChange", passThroughThisScope(passIfIsLeafChild(updateBaseOnChildrenState)));
|
2017-07-29 14:42:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
]);
|