propagate check value to multicheck component for intermediate value #1305
gitea/salix/dev There was a failure building this commit Details

This commit is contained in:
Joan Sanchez 2019-04-30 08:00:27 +02:00
parent 6b6faff383
commit a572130b6c
7 changed files with 73 additions and 15 deletions

View File

@ -1 +1,5 @@
<vn-check field="$ctrl.checkAll"></vn-check>
<vn-check field="$ctrl.checked"
intermediate="$ctrl.isIntermediate"
vn-tooltip="Check all"
tooltip-position="up">
</vn-check>

View File

@ -12,21 +12,74 @@ export default class MultiCheck extends Input {
super($element, $scope);
this._checkAll = false;
this.checkField = 'checked';
this.isIntermediate = false;
}
get checkAll() {
return this._checkAll;
/**
* Gets array model instance
*
* @return {ArrayModel} - Array model instance
*/
get model() {
return this._model;
}
set checkAll(value) {
this._checkAll = value;
this.switchChecks();
/**
* Sets the array model instance
* Changes intermediate property for
* the check component
*
* @param {ArrayModel} value - Array model instance
*/
set model(value) {
this._model = value;
if (value) {
value.on('rowChange', () => {
this.isIntermediate = this.allChecked && !this.areAllChecked();
if (this.isIntermediate)
this._checked = undefined;
else if (this.areAllChecked())
this._checked = true;
});
}
}
switchChecks() {
if (!this.data) return;
this.data.forEach(el => {
el[this.checkField] = this._checkAll;
/**
* Returns a bolean result for
* checked instances
*
* @return {Boolean} - True if all instances are checked
*/
areAllChecked() {
if (!this.model || !this.model.data) return;
const data = this.model.data;
return data.every(item => {
return item[this.checkField] === true;
});
}
/**
* Gets current check state
*/
get checked() {
return this._checked;
}
/**
* Sets current check state
*
* @param {Boolean} value - Checkbox state [undefined, true, false]
*/
set checked(value) {
this._checked = value;
this.allChecked = value;
const data = this.model.data;
if (!data) return;
data.forEach(el => {
el[this.checkField] = value;
});
}
}
@ -35,7 +88,7 @@ ngModule.component('vnMultiCheck', {
template: require('./multi-check.html'),
controller: MultiCheck,
bindings: {
data: '=',
model: '<',
checkField: '<?',
checkAll: '=?',
disabled: '<?'

View File

@ -41,4 +41,5 @@ Has delivery: Hay reparto
Loading: Cargando
Fields to show: Campos a mostrar
Create new one: Crear nuevo
Toggle: Desplegar/Plegar
Toggle: Desplegar/Plegar
Check all: Seleccionar todo

View File

@ -30,7 +30,7 @@
<vn-tr>
<vn-th shrink>
<vn-multi-check
data="$ctrl.tickets">
model="model">
</vn-multi-check>
</vn-th>
<vn-th>Order</vn-th>

View File

@ -36,7 +36,7 @@
<vn-tr>
<vn-th shrink>
<vn-multi-check
data="tickets">
model="model">
</vn-multi-check>
</vn-th>
<vn-th></vn-th>

View File

@ -56,7 +56,7 @@
<vn-tr>
<vn-th shrink>
<vn-multi-check
data="$ctrl.sales">
model="model">
</vn-multi-check>
</vn-th>
<vn-th shrink></vn-th>