2018-02-10 15:18:01 +00:00
|
|
|
import ngModule from '../../module';
|
2018-05-18 12:59:56 +00:00
|
|
|
import Input from '../../lib/input';
|
2018-02-10 15:18:01 +00:00
|
|
|
|
2017-06-28 11:12:01 +00:00
|
|
|
/**
|
|
|
|
* Draw checkbox with a drop-down and multi options
|
2018-05-08 07:55:45 +00:00
|
|
|
* @param {SmallInt} checkAll Primary input-check state: 0 -> uncheck, 1 -> checked
|
|
|
|
* @param {Array} data List of options shown in drop-down
|
2017-06-28 11:12:01 +00:00
|
|
|
* @param {Array} models Elements to check / unCheck
|
|
|
|
*/
|
2018-05-18 12:59:56 +00:00
|
|
|
export default class MultiCheck extends Input {
|
|
|
|
constructor($element, $scope) {
|
|
|
|
super($element, $scope);
|
2018-05-08 07:55:45 +00:00
|
|
|
this._checkAll = false;
|
|
|
|
this.checkField = 'checked';
|
2019-04-30 06:00:27 +00:00
|
|
|
this.isIntermediate = false;
|
2017-06-28 11:12:01 +00:00
|
|
|
}
|
2017-10-04 11:27:39 +00:00
|
|
|
|
2019-04-30 06:00:27 +00:00
|
|
|
/**
|
|
|
|
* Gets array model instance
|
|
|
|
*
|
|
|
|
* @return {ArrayModel} - Array model instance
|
|
|
|
*/
|
|
|
|
get model() {
|
|
|
|
return this._model;
|
2017-06-26 08:52:22 +00:00
|
|
|
}
|
2017-10-04 11:27:39 +00:00
|
|
|
|
2019-04-30 06:00:27 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2017-06-26 08:52:22 +00:00
|
|
|
}
|
2017-10-04 11:27:39 +00:00
|
|
|
|
2019-04-30 06:00:27 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
2018-05-08 07:55:45 +00:00
|
|
|
});
|
2017-10-19 12:13:28 +00:00
|
|
|
}
|
2017-06-26 08:52:22 +00:00
|
|
|
}
|
|
|
|
|
2018-02-10 15:18:01 +00:00
|
|
|
ngModule.component('vnMultiCheck', {
|
2017-06-26 08:52:22 +00:00
|
|
|
template: require('./multi-check.html'),
|
|
|
|
controller: MultiCheck,
|
|
|
|
bindings: {
|
2019-04-30 06:00:27 +00:00
|
|
|
model: '<',
|
2018-05-08 07:55:45 +00:00
|
|
|
checkField: '<?',
|
2018-05-18 12:59:56 +00:00
|
|
|
checkAll: '=?',
|
|
|
|
disabled: '<?'
|
2017-06-26 08:52:22 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|