-
Notifications
You must be signed in to change notification settings - Fork 642
/
Copy patheditableTable.twig
297 lines (287 loc) · 14.3 KB
/
editableTable.twig
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
{%- set static = static ?? false %}
{%- set fullWidth = fullWidth ?? true %}
{%- set cols = cols ?? [] %}
{%- set rows = rows ?? [] %}
{%- set initJs = not static and (initJs ?? true) -%}
{%- set minRows = minRows ?? null %}
{%- set maxRows = maxRows ?? null %}
{%- set describedBy = describedBy ?? null %}
{%- set totalRows = rows|length %}
{%- set staticRows = static or (staticRows ?? false) or (minRows == 1 and maxRows == 1 and totalRows == 1) %}
{%- set allowAdd = (allowAdd ?? false) and not staticRows %}
{%- set allowReorder = (allowReorder ?? false) and not staticRows %}
{%- set allowDelete = (allowDelete ?? false) and not staticRows %}
{% set actionMenuItems = [
{
icon: 'arrow-up',
label: 'Move up'|t('app'),
attributes: {
data: {action: 'moveUp'},
},
},
{
icon: 'arrow-down',
label: 'Move down'|t('app'),
attributes: {
data: {action: 'moveDown'},
},
},
] %}
{% if not static %}
{{ hiddenInput(name, '') }}
{% endif %}
{% macro cellClass(fullWidth, col, class) %}
{{- (class is iterable ? class : [class])|merge([
"#{col.type}-cell",
col.type in [
'autosuggest',
'color',
'date',
'email',
'multiline',
'number',
'singleline',
'template',
'time',
'url',
] ? 'textual' : null,
fullWidth and (col.thin ?? false) ? 'thin' : null,
col.info is defined ? 'has-info' : null,
]|filter)|join(' ') -}}
{% endmacro %}
{% set tableAttributes = {
id: id,
class: [
'editable',
fullWidth ? 'fullwidth',
static ? 'static',
totalRows == 0 ? 'hidden',
]|filter,
} %}
{%- if block('attr') is defined %}
{%- set tableAttributes = tableAttributes|merge(('<div ' ~ block('attr') ~ '>')|parseAttr, recursive=true) %}
{% endif %}
{% for col in cols %}
{%- switch col.type %}
{%- case 'time' %}
{%- do view.registerAssetBundle('craft\\web\\assets\\timepicker\\TimepickerAsset') %}
{%- case 'template' %}
{%- do view.registerAssetBundle("craft\\web\\assets\\vue\\VueAsset") %}
{%- endswitch %}
{% endfor %}
<span role="status" class="visually-hidden" data-status-message></span>
{% tag 'table' with tableAttributes %}
{% for col in cols %}
<col>
{% endfor %}
{% if (allowDelete and allowReorder) %}
<colgroup span="2"></colgroup>
{% else %}
{% if allowDelete %}<col>{% endif %}
{% if allowReorder %}<col>{% endif %}
{% endif %}
{% if cols|filter(c => (c.headingHtml ?? c.heading ?? c.info ?? '') is not same as(''))|length %}
<thead>
<tr>
{% for col in cols %}
{% set columnHeadingId = "#{id}-heading-#{loop.index}" %}
<th id="{{ columnHeadingId }}" scope="col" class="{{ _self.cellClass(fullWidth, col, col.class ?? []) }}">
{%- if col.headingHtml is defined %}
{{- col.headingHtml|raw }}
{%- elseif col.heading ?? false %}
{{- col.heading }}
{%- else %}
{%- endif %}
{%- if col.info is defined -%}
<span class="info{% if static %} disabled{% endif %}"{% if static %} disabled=""{% endif %}>{{ col.info|md|raw }}</span>
{%- endif -%}
</th>
{% endfor %}
{% if (allowDelete or allowReorder) %}
<th colspan="{{ not allowDelete or not allowReorder ? 1 : 2 }}" scope="colgroup"><span class="visually-hidden">{{ 'Row actions'|t('app') }}</span></th>
{% endif %}
</tr>
</thead>
{% endif %}
<tbody>
{% for rowId, row in rows %}
{% set rowNumber = loop.index %}
{% set rowName = 'Row {index}'|t('app', {index: rowNumber}) %}
{% set actionBtnLabel = "#{rowName} #{'Actions'|t('app')}" %}
<tr data-id="{{ rowId }}">
{% for colId, col in cols %}
{% set cell = row[colId] is defined ? row[colId] : (defaultValues[colId] ?? null) %}
{% set value = cell.value is defined ? cell.value : cell %}
{% if col.type == 'heading' %}
<th scope="row" class="{{ _self.cellClass(fullWidth, col, cell.class ?? col.class ?? []) }}"{% if col.width ?? false %} width="{{ col.width }}"{% endif %}>{{ value|raw }}</th>
{% elseif col.type == 'html' %}
<td class="{{ _self.cellClass(fullWidth, col, cell.class ?? col.class ?? []) }}"{% if col.width ?? false %} width="{{ col.width }}"{% endif %}>{{ value|raw }}</td>
{% else %}
{% set headingId = "#{id}-heading-#{loop.index}" %}
{% set hasErrors = cell.hasErrors ?? false %}
{% set cellName = name~'['~rowId~']['~colId~']' %}
{% set isCode = (col.code ?? false) or col.type == 'color' %}
<td class="{{ _self.cellClass(fullWidth, col, col.class ?? []) }} {% if isCode %}code{% endif %} {% if hasErrors %}error{% endif %}"{% if col.width ?? false %} width="{{ col.width }}"{% endif %}>
{% block tablecell %}
{%- switch col.type -%}
{%- case 'checkbox' -%}
<div class="checkbox-wrapper">
{% include "_includes/forms/checkbox" with {
name: cellName,
value: col.value ?? 1,
checked: value is not empty,
disabled: static,
labelledBy: headingId,
describedBy: describedBy,
} only %}
</div>
{%- case 'color' -%}
{% include "_includes/forms/color" with {
name: cellName,
value: value,
small: true,
disabled: static,
labelledBy: headingId,
describedBy: describedBy,
} only %}
{%- case 'date' -%}
{% include "_includes/forms/date" with {
name: cellName,
value: value,
disabled: static,
labelledBy: headingId,
describedBy: describedBy,
} only %}
{%- case 'lightswitch' -%}
{% include "_includes/forms/lightswitch" with {
name: cellName,
on: value,
value: col.value ?? 1,
small: true,
disabled: static,
labelledBy: headingId,
describedBy: describedBy,
} only %}
{% case 'select' -%}
{% include "_includes/forms/select" with {
class: 'small',
name: cellName,
options: cell.options ?? col.options,
value: value,
disabled: static,
labelledBy: headingId,
describedBy: describedBy,
} only %}
{%- case 'time' -%}
{% include "_includes/forms/time" with {
name: cellName,
value: value,
disabled: static,
labelledBy: headingId,
describedBy: describedBy,
} only %}
{%- case 'email' or 'url' -%}
{% include "_includes/forms/text" with {
type: col.type,
name: cellName,
placeholder: col.placeholder ?? null,
value: value,
disabled: static,
labelledBy: headingId,
describedBy: describedBy,
} only %}
{%- case 'autosuggest' or 'template' -%}
{% include "_includes/forms/autosuggest" with {
name: cellName,
suggestTemplates: col.type == 'template',
suggestEnvVars: col.suggestEnvVars ?? false,
suggestAliases: col.suggestAliases ?? false,
value: value,
disabled: static,
labelledBy: headingId,
describedBy: describedBy,
} only %}
{%- default -%}
{% if static %}
<pre class="noteditable">{{ value }}</pre>
{% else %}
{% if value %}
<div class="editable-table-preview" aria-hidden="true">{{ value }}</div>
{% endif %}
{{ tag('textarea', {
name: cellName,
rows: col.rows ?? 1,
placeholder: col.placeholder ?? false,
aria: {
labelledby: headingId,
describedby: describedBy,
},
html: value,
}) }}
{% endif %}
{%- endswitch -%}
{% endblock %}
</td>
{% endif %}
{% endfor %}
{% if allowReorder -%}
<td class="thin action">
<div class="flex flex-nowrap">
<a class="move icon" title="{{ 'Reorder'|t('app') }}" aria-label="{{ 'Reorder'|t('app') }}" type="button" role="button"></a>
{{~ disclosureMenu(actionMenuItems, {
buttonAttributes: {
'aria-label': actionBtnLabel,
class: ['action-btn'],
title: 'Actions'|t('app'),
data: {
'disclosure-trigger': true,
},
},
}) }}
</div>
</td>
{%- endif -%}
{%- if allowDelete -%}
<td class="thin action">
{{ tag('button', {
class: [
'delete',
'icon',
minRows and totalRows <= minRows ? 'disabled' : null,
]|filter,
type: 'button',
disabled: minRows and totalRows <= minRows,
title: 'Delete'|t('app'),
aria: {
label: 'Delete row {index}'|t('app', {
index: rowNumber,
}),
}
}) }}
</td>
{%- endif -%}
</tr>
{% endfor %}
</tbody>
{% endtag %}
{% if allowAdd %}
{% set buttonText = addRowLabel ?? "Add a row"|t('app') %}
<button type="button" class="btn dashed add icon" aria-label="{{ buttonText }}">{{ buttonText }}</button>
{% endif %}
{% if initJs %}
{% set jsId = id|namespaceInputId|e('js') %}
{% set jsName = name|namespaceInputName|e('js') %}
{% set jsCols = cols|json_encode %}
{% set defaultValues = defaultValues ?? null %}
{% js %}
new Craft.EditableTable("{{ jsId }}", "{{ jsName }}", {{ jsCols|raw }}, {
defaultValues: {{ defaultValues ? defaultValues|json_encode|raw : '{}' }},
allowAdd: {{ allowAdd ? 'true' : 'false' }},
allowDelete: {{ allowDelete ? 'true' : 'false' }},
allowReorder: {{ allowReorder ? 'true' : 'false' }},
minRows: {{ minRows ? minRows : 'null' }},
maxRows: {{ maxRows ? maxRows : 'null' }}
});
{% endjs %}
{% endif %}