2018-02-10 15:18:01 +00:00
|
|
|
import ngModule from '../../module';
|
2017-06-26 08:52:22 +00:00
|
|
|
import './multi-check.scss';
|
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
|
2017-10-19 10:31:55 +00:00
|
|
|
* @param {SmallInt} checkAll Primary input-check state: 0 -> uncheck, 1 -> checked, 2 -> indeterminate checked
|
2017-06-28 11:12:01 +00:00
|
|
|
* @param {Array} options List of options shown in drop-down
|
|
|
|
* @param {Array} models Elements to check / unCheck
|
|
|
|
* @param {String} className Optional css class name
|
|
|
|
*/
|
2017-06-26 08:52:22 +00:00
|
|
|
export default class MultiCheck {
|
2017-10-19 12:13:28 +00:00
|
|
|
constructor($timeout) {
|
|
|
|
this.$timeout = $timeout;
|
2017-06-26 12:41:08 +00:00
|
|
|
this._checkAll = 0;
|
2017-06-28 11:12:01 +00:00
|
|
|
this._models = [];
|
2017-10-19 10:31:55 +00:00
|
|
|
this._type = {};
|
|
|
|
this.showDropDown = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
get type() {
|
|
|
|
return this._type;
|
|
|
|
}
|
|
|
|
|
|
|
|
set type(value) {
|
|
|
|
if (value && value.id) {
|
|
|
|
this._type = value;
|
|
|
|
switch (value.id) {
|
|
|
|
case 'all':
|
|
|
|
this.checkAll = 1;
|
|
|
|
break;
|
|
|
|
case 'any':
|
|
|
|
this.checkAll = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
this.checkAll = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._type = {};
|
2017-06-26 11:53:52 +00:00
|
|
|
this.showDropDown = false;
|
2017-06-26 08:52:22 +00:00
|
|
|
}
|
2017-10-04 11:27:39 +00:00
|
|
|
|
2017-06-28 11:12:01 +00:00
|
|
|
get models() {
|
|
|
|
return this._models;
|
|
|
|
}
|
2017-10-04 11:27:39 +00:00
|
|
|
|
2017-06-28 11:12:01 +00:00
|
|
|
set models(value) {
|
|
|
|
this._models = value;
|
|
|
|
}
|
2017-10-04 11:27:39 +00:00
|
|
|
|
2017-06-26 08:52:22 +00:00
|
|
|
get checkAll() {
|
|
|
|
return this._checkAll;
|
|
|
|
}
|
2017-10-04 11:27:39 +00:00
|
|
|
|
2017-06-26 08:52:22 +00:00
|
|
|
set checkAll(value) {
|
|
|
|
this._checkAll = value;
|
2017-06-26 11:53:52 +00:00
|
|
|
this.switchChecks();
|
2017-06-26 08:52:22 +00:00
|
|
|
}
|
2017-10-04 11:27:39 +00:00
|
|
|
|
2017-06-26 11:53:52 +00:00
|
|
|
switchChecks() {
|
2017-06-28 11:12:01 +00:00
|
|
|
if (this.models)
|
|
|
|
this.models.forEach(
|
|
|
|
el => {
|
|
|
|
let checked;
|
|
|
|
if (this.type.id && this.type.id !== 'all' && this.type.id !== 'any') {
|
|
|
|
if (this.type.id.length > 3 && this.type.id.substr(0, 3) === 'no-') {
|
2017-10-13 14:26:47 +00:00
|
|
|
let label = this.type.id.replace('no-', '');
|
2017-11-07 08:59:49 +00:00
|
|
|
checked = Boolean(el[label]) === false;
|
2017-06-28 11:12:01 +00:00
|
|
|
} else if (this.type.id.length > 6 && this.type.id.substr(0, 6) === 'equal-') {
|
|
|
|
let label = this.type.id.replace('equal-', '');
|
|
|
|
checked = (el[label] && el[label] === this.type.name);
|
|
|
|
} else {
|
2017-11-07 08:59:49 +00:00
|
|
|
checked = Boolean(el[this.type.id]) === true;
|
2017-06-28 11:12:01 +00:00
|
|
|
}
|
2017-06-26 11:53:52 +00:00
|
|
|
} else {
|
2017-06-28 11:12:01 +00:00
|
|
|
checked = this.checkAll === 1;
|
2017-06-26 11:53:52 +00:00
|
|
|
}
|
2017-06-28 11:12:01 +00:00
|
|
|
el.checked = checked;
|
2017-06-26 11:53:52 +00:00
|
|
|
}
|
2017-06-28 11:12:01 +00:00
|
|
|
);
|
2017-06-26 08:52:22 +00:00
|
|
|
}
|
2017-10-04 11:27:39 +00:00
|
|
|
|
2017-06-28 06:55:01 +00:00
|
|
|
$onChanges() {
|
|
|
|
this.type = {};
|
|
|
|
this.checkAll = 0;
|
|
|
|
}
|
2017-10-19 12:13:28 +00:00
|
|
|
|
|
|
|
onBlur() {
|
|
|
|
this.$timeout(() => {
|
|
|
|
this.showDropDown = false;
|
|
|
|
}, 200);
|
|
|
|
}
|
2017-06-26 08:52:22 +00:00
|
|
|
}
|
2017-10-19 12:13:28 +00:00
|
|
|
MultiCheck.$inject = ['$timeout'];
|
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: {
|
2017-07-06 12:21:55 +00:00
|
|
|
checkAll: '=',
|
2017-06-26 08:52:22 +00:00
|
|
|
options: '<',
|
2017-10-19 10:31:55 +00:00
|
|
|
models: '<',
|
2017-06-26 08:52:22 +00:00
|
|
|
className: '@?'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|