updated check component

This commit is contained in:
Joan Sanchez 2019-02-15 13:31:14 +01:00
parent 8bea3afbfb
commit 08c252fb9b
2 changed files with 28 additions and 38 deletions

View File

@ -2,8 +2,8 @@
aria-label="Checkbox 1" aria-label="Checkbox 1"
md-indeterminate="$ctrl.isIntermediate" md-indeterminate="$ctrl.isIntermediate"
ng-disabled="$ctrl.disabled" ng-disabled="$ctrl.disabled"
ng-checked="$ctrl.checkValue" ng-checked="$ctrl.isChecked"
ng-model="$ctrl.checkValue"> ng-model="$ctrl.model">
<span translate>{{::$ctrl.label}}</span> <span translate>{{::$ctrl.label}}</span>
</md-checkbox> </md-checkbox>
<i class="material-icons" <i class="material-icons"

View File

@ -9,61 +9,51 @@ export default class Controller extends Component {
this.info = $attrs.info || null; this.info = $attrs.info || null;
} }
set checkValue(value) { get model() {
this.checkIntermediate(); return this._model;
if (this.isIntermediate)
value = false;
this._field = value;
if (typeof this._checkValue === 'boolean')
this.emit('change', {value: this.field});
this._checkValue = value;
} }
get checkValue() { set model(value) {
return this._checkValue; if (value === null) return;
if (this.model === true)
value = false;
else if (this.model === false && this.tripleState)
value = null;
this.emit('change', {value});
this._model = value;
} }
set field(value) { set field(value) {
this.checkValue = value; if (typeof value === 'number')
value = Boolean(value);
this._model = value;
} }
get field() { get field() {
if (!this._field && this.isIntermediate) return this._model;
return null;
return this._field;
} }
set tripleState(value) { get isIntermediate() {
this._tripleState = value; return this.intermediate
this.checkIntermediate(); || this.model === null
|| this.model === undefined;
} }
get tripleState() { get isChecked() {
return this._tripleState; return this.checked || this.model === true;
}
} }
checkIntermediate() {
if ((this.intermediate || (!this._field && this.tripleState)) && !this.isIntermediate) {
this.isIntermediate = true;
return;
}
if (!this.intermediate)
this.isIntermediate = false;
}
}
Controller.$inject = ['$element', '$scope', '$attrs']; Controller.$inject = ['$element', '$scope', '$attrs'];
ngModule.component('vnCheck', { ngModule.component('vnCheck', {
template: require('./check.html'), template: require('./check.html'),
controller: Controller, controller: Controller,
require: {
model: '?ngModel'
},
bindings: { bindings: {
field: '=?', field: '=?',
label: '@?', label: '@?',