-
Notifications
You must be signed in to change notification settings - Fork 101
/
CheckMatrix.js
178 lines (157 loc) · 4.49 KB
/
CheckMatrix.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* This file shows how to create a custom component and register that within an Angular application.
*
* Get the base component class by referencing Formio.Components.components map.
*/
import { Formio } from '@formio/angular';
const Base = (Formio as any).Components.components.component;
const editForm = (Formio as any).Components.components.table.editForm;
const Components = (Formio as any).Components;
/**
* Create a new CheckMatrixComponent "class" using ES5 class inheritance notation.
* https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Inheritance
*
* Here we will derive from the base component which all Form.io form components derive from.
*
* @param component
* @param options
* @param data
* @constructor
*/
export default class CheckMatrix extends Base {
constructor(component, options, data) {
super(component, options, data);
}
static schema() {
return Base.schema({
type: 'checkmatrix',
numRows: 3,
numCols: 3
});
}
static builderInfo = {
title: 'Check Matrix',
group: 'basic',
icon: 'fa fa-table',
weight: 70,
documentation: 'http://help.form.io/userguide/#table',
schema: CheckMatrix.schema()
}
static editForm = editForm
/**
* Render returns an html string of the fully rendered component.
*
* @param children - If this class is extendended, the sub string is passed as children.
* @returns {string}
*/
render(children) {
// To make this dynamic, we could call this.renderTemplate('templatename', {}).
let tableClass = 'table ';
['striped', 'bordered', 'hover', 'condensed'].forEach((prop) => {
if (this.component[prop]) {
tableClass += `table-${prop} `;
}
});
let content = '';
for (let i = 0; i < this.component.numRows; i++) {
let row = '<tr>';
for (let j = 0; j < this.component.numCols; j++) {
let cell = '<td>';
cell += this.renderTemplate('input', {
input: {
type: 'input',
ref: `${this.component.key}-${i}`,
attr: {
id: `${this.component.key}-${i}-${j}`,
class: 'form-control',
type: 'checkbox',
}
}
});
cell += '</td>';
row += cell;
}
row += '</tr>';
content += row;
}
// Calling super.render will wrap it html as a component.
return super.render(`
<table class=${tableClass}>
<tbody>
${content}
</tbody>
</table>
`);
}
/**
* After the html string has been mounted into the dom, the dom element is returned here. Use refs to find specific
* elements to attach functionality to.
*
* @param element
* @returns {Promise}
*/
attach(element) {
const refs = {};
for (let i = 0; i < this.component.numRows; i++) {
refs[`${this.component.key}-${i}`] = 'multiple';
}
this.loadRefs(element, refs);
this.checks = [];
for (let i = 0; i < this.component.numRows; i++) {
this.checks[i] = Array.prototype.slice.call(this.refs[`${this.component.key}-${i}`], 0);
// Attach click events to each input in the row
this.checks[i].forEach(input => {
this.addEventListener(input, 'click', () => this.updateValue())
});
}
// Allow basic component functionality to attach like field logic and tooltips.
return super.attach(element);
}
/**
* Get the value of the component from the dom elements.
*
* @returns {Array}
*/
getValue() {
var value = [];
for (var rowIndex in this.checks) {
var row = this.checks[rowIndex];
value[rowIndex] = [];
for (var colIndex in row) {
var col = row[colIndex];
value[rowIndex][colIndex] = !!col.checked;
}
}
return value;
}
/**
* Set the value of the component into the dom elements.
*
* @param value
* @returns {boolean}
*/
setValue(value) {
if (!value) {
return;
}
for (var rowIndex in this.checks) {
var row = this.checks[rowIndex];
if (!value[rowIndex]) {
break;
}
for (var colIndex in row) {
var col = row[colIndex];
if (!value[rowIndex][colIndex]) {
return false;
}
let checked = value[rowIndex][colIndex] ? 1 : 0;
col.value = checked;
col.checked = checked;
}
}
}
}
// Use the table component edit form.
CheckMatrix.editForm = editForm;
// Register the component to the Formio.Components registry.
Components.addComponent('checkmatrix', CheckMatrix);