angular2-schema-form
Version:
Angular2 Schema Form (DISCLAIMER: it is not related to angular-schema-form)
58 lines (57 loc) • 3.32 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { Component } from '@angular/core';
import { ControlWidget } from '../../widget';
var CheckboxWidget = (function (_super) {
__extends(CheckboxWidget, _super);
function CheckboxWidget() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.checked = {};
return _this;
}
CheckboxWidget.prototype.ngAfterViewInit = function () {
var _this = this;
var control = this.control;
this.formProperty.valueChanges.subscribe(function (newValue) {
if (control.value !== newValue) {
control.setValue(newValue, { emitEvent: false });
if (newValue && Array.isArray(newValue)) {
newValue.map(function (v) { return _this.checked[v] = true; });
}
}
});
this.formProperty.errorsChanges.subscribe(function (errors) {
control.setErrors(errors, { emitEvent: true });
});
control.valueChanges.subscribe(function (newValue) {
_this.formProperty.setValue(newValue, false);
});
};
CheckboxWidget.prototype.onCheck = function (el) {
if (el.checked) {
this.checked[el.value] = true;
}
else {
delete this.checked[el.value];
}
this.formProperty.setValue(Object.keys(this.checked), false);
};
return CheckboxWidget;
}(ControlWidget));
export { CheckboxWidget };
CheckboxWidget.decorators = [
{ type: Component, args: [{
selector: 'sf-checkbox-widget',
template: "<div class=\"widget form-group\">\n <label [attr.for]=\"id\" class=\"horizontal control-label\">\n {{ schema.title }}\n </label>\n\t<div *ngIf=\"schema.type!='array'\" class=\"checkbox\">\n\t\t<label class=\"horizontal control-label\">\n\t\t\t<input [formControl]=\"control\" [attr.name]=\"name\" [indeterminate]=\"control.value !== false && control.value !== true ? true :null\" type=\"checkbox\" [attr.disabled]=\"schema.readOnly\">\n\t\t\t<input *ngIf=\"schema.readOnly\" [attr.name]=\"name\" type=\"hidden\" [formControl]=\"control\">\n\t\t\t{{schema.description}}\n\t\t</label>\n\t</div>\n\t<ng-container *ngIf=\"schema.type==='array'\">\n\t\t<div *ngFor=\"let option of schema.items.oneOf\" class=\"checkbox\">\n\t\t\t<label class=\"horizontal control-label\">\n\t\t\t\t<input [attr.name]=\"name\"\n\t\t\t\t\tvalue=\"{{option.enum[0]}}\" type=\"checkbox\" \n\t\t\t\t\t[attr.disabled]=\"schema.readOnly\"\n\t\t\t\t\t(change)=\"onCheck($event.target)\"\n\t\t\t\t\t[attr.checked]=\"checked[option.enum[0]] ? true : null\">\n\t\t\t\t{{option.description}}\n\t\t\t</label>\n\t\t</div>\n\t</ng-container>\n</div>"
},] },
];
/** @nocollapse */
CheckboxWidget.ctorParameters = function () { return []; };