Skip to content

Commit def9c96

Browse files
committed
working fieldset and table fields, fixes with list output
1 parent a7a2e03 commit def9c96

File tree

6 files changed

+171
-19
lines changed

6 files changed

+171
-19
lines changed

src/Model/template/_scripts.html

+110-7
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,116 @@
6262

6363
// Get the fieldset
6464
var fieldset = target.data('fieldset');
65-
// Get the container
66-
var container = target.parent().parent();
65+
66+
//collect parents
67+
var parents = [];
68+
//get the parents from source to top
69+
var getParents = function(element) {
70+
var parent = element.parent();
71+
var type = parent[0].tagName;
72+
var name = parent.data('reindex');
73+
74+
//fieldset? and has a reindex name?
75+
if (type === 'FIELDSET' && name && name) {
76+
var index = element.index() - 1;
77+
//get the current element index from parent
78+
parents.push({
79+
name: name,
80+
index: index < 0 ? 0 : index
81+
});
82+
}
83+
84+
//we should continue
85+
if (type !== 'FORM') {
86+
getParents(parent);
87+
}
88+
};
89+
90+
//start gathering the parents
91+
getParents(target);
92+
//reverse so we can start replacing from bottom to top
93+
var parent = parents.reverse().shift();
94+
var container = $('fieldset[data-reindex="' + parent.name + '"]');
6795

6896
// Remove the target
6997
target.parent().remove();
7098

7199
// Get all the inputs
72100
var inputs = container.find('[name]');
73101
// Re-index
102+
reindex(inputs, true);
103+
});
104+
105+
$(window).on('table-field-add-click', function(e, target) {
106+
target = $(target);
107+
108+
//get the name
109+
var name = target.parent().data('name');
110+
//get the columns
111+
var columns = target.parent().data('columns').split('|');
112+
//get the template
113+
var template = target.parent().find('#table-field-template').html();
114+
//determine the index
115+
var index = target.parent().find('tbody').children('tr').length;
116+
117+
//parse the template
118+
template = $(template);
119+
//clone the input
120+
var input = $('<div />')
121+
.append(
122+
template
123+
.find('.input-template')
124+
.removeAttr('class')
125+
.remove()
126+
.clone()
127+
);
128+
129+
//on each columns
130+
for(var i in columns) {
131+
//get the html
132+
var html = input.html();
133+
134+
//replace name
135+
var re = new RegExp('{NAME}', 'g');
136+
html = html.replace(re, name);
137+
//replace index
138+
re = new RegExp('{INDEX}', 'g');
139+
html = html.replace(re, index);
140+
//replace column
141+
re = new RegExp('{COLUMN}', 'g');
142+
html = html.replace(re, columns[i]);
143+
144+
template.append(html);
145+
}
146+
147+
//active scripts
148+
template = $(template).doon();
149+
//append to body
150+
target.parent().find('tbody').append(template);
151+
});
152+
153+
$(window).on('table-field-remove-click', function(e, target) {
154+
target = $(target);
155+
156+
//get the container
157+
var container = target
158+
.parent()
159+
.parent()
160+
.parent();
161+
162+
//remove
163+
target
164+
.parent()
165+
.parent()
166+
.remove();
167+
168+
//get all inputs from container
169+
var inputs = container.find('[name]');
170+
//reindex
74171
reindex(inputs);
75172
});
76173

77-
var reindex = function(inputs) {
174+
var reindex = function(inputs, filter) {
78175
// Get the input names
79176
var names = {};
80177
$.each(inputs, function(index, element) {
@@ -94,9 +191,13 @@
94191

95192
// Get the re-index filters
96193
var filters = [];
97-
$('[data-reindex]').map(function(index, element) {
98-
filters.push($(element).data('reindex'));
99-
});
194+
195+
// Filter?
196+
if (filter) {
197+
$('[data-reindex]').map(function(index, element) {
198+
filters.push($(element).data('reindex'));
199+
});
200+
}
100201

101202
// Re-index names
102203
var reindexed = arrange(names, filters);
@@ -105,6 +206,8 @@
105206
// Split by pairs
106207
serialized = serialized.split('&');
107208

209+
console.log(names);
210+
108211
// On each serialized pairs
109212
for(var i in serialized) {
110213
// Get the parts key & value
@@ -144,7 +247,7 @@
144247
// If it's a string but it's an object
145248
if (isNaN(parseInt(key)) && type === '[object Object]' && key !== '{INDEX}') {
146249
// If it's not fieldset
147-
if (filters.indexOf(key) === -1) {
250+
if (filters.length && filters.indexOf(key) === -1) {
148251
continue;
149252
}
150253

src/Model/template/form/_set-template.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<div class="fieldset border mb-2 p-3">
1919
<a
2020
href="javscript:void(0)"
21-
class="text-danger float-right"
21+
class="btn btn-sm btn-danger float-right"
2222
data-schema="{{../config.parameters}}"
2323
data-do="fieldset-remove"
2424
data-on="click"
@@ -51,7 +51,7 @@
5151
</script>
5252
{{/if}}
5353
{{else}}
54-
<div class="form-group">
54+
<div class="form-group mt-2">
5555
<label class="control-label">{{label}}</label>
5656
<div class="form-{{config.type}}">
5757
{{{value}}}

src/Model/template/form/_set.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
<label>{{label}}</label>
1010

1111
{{#each formats}}
12-
<div class="fieldset border mb-2 p-3" data-parent="{{../@key}}" data-index="{{@index}}">
12+
<div class="fieldset border mb-2 p-3">
1313
<a
1414
href="javscript:void(0)"
15-
class="text-danger float-right"
15+
class="btn btn-sm btn-danger float-right"
1616
data-schema="{{../config.parameters}}"
1717
data-do="fieldset-remove"
1818
data-on="click"
@@ -38,7 +38,7 @@
3838
</div>
3939
{{else}}
4040
<div class="
41-
form-group
41+
form-group mt-2
4242
{{#has ../../errors @path}}
4343
has-error
4444
{{else}}

src/Model/template/format/field.html

+30-1
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,8 @@
909909
<td>
910910
<a
911911
class="btn btn-danger remove"
912+
data-do="table-field-remove"
913+
data-on="click"
912914
href="javascript:void(0)"
913915
>
914916
<i class="fas fa-times"></i>
@@ -929,10 +931,37 @@
929931
{{/each}}
930932
</tbody>
931933
</table>
932-
<a class="field-add btn btn-success" href="javascript:void(0)">
934+
<a
935+
class="field-add btn btn-success"
936+
href="javascript:void(0)"
937+
data-do="table-field-add"
938+
data-on="click"
939+
>
933940
<i class="fas fa-plus"></i>
934941
<span>{{_ 'Add Row'}}</span>
935942
</a>
943+
944+
<script type="text/x-template" id="table-field-template">
945+
<tr>
946+
<td>
947+
<a
948+
class="btn btn-danger remove"
949+
data-do="table-field-remove"
950+
data-on="click"
951+
href="javascript:void(0)"
952+
>
953+
<i class="fas fa-times"></i>
954+
</a>
955+
</td>
956+
<td class="input-template">
957+
<input
958+
class="input-column form-control"
959+
type="text"
960+
name="{NAME}[{INDEX}][{COLUMN}]"
961+
/>
962+
</td>
963+
</tr>
964+
</script>
936965
</div>
937966
{{/when}}
938967
{{#when config.type '===' 'multirange'}}

src/Schema/template/search.html

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ <h3 class="main-title">
3333
</div>
3434

3535
<div class="float-right">
36+
<a class="btn btn-pink" href="javascript:void(0)">
37+
<i class="fas fa-upload"></i> Import
38+
</a>
39+
<a class="btn btn-purple" href="javascript:void(0)">
40+
<i class="fas fa-download"></i> Export
41+
</a>
3642
<a class="btn btn-success" href="/admin/system/schema/create?redirect_uri={{redirecturl}}">
3743
<i class="fas fa-plus"></i> {{_ 'Create Schema'}}
3844
</a>

src/bootstrap/helpers.php

+20-6
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@
289289
//need to do this for table format
290290
foreach($columns as $key => $column) {
291291
//on each row
292-
foreach($rows as $index => $row) {
292+
foreach($rows as $index => $inner) {
293293
//if it's set
294-
if (isset($row[$key])) {
294+
if (isset($inner[$key])) {
295295
continue;
296296
}
297297

@@ -307,18 +307,18 @@
307307
}
308308

309309
//on each row
310-
foreach($rows as $index => $row) {
310+
foreach($rows as $index => $inner) {
311311
//we should sort the rows based on column sorting
312312
$rows[$index] = array_merge(
313313
array_flip(array_keys($columns)),
314314
$rows[$index]
315315
);
316316

317317
//get the formats
318-
$results = $getFormats($row, $type, $fieldset, false);
318+
$results = $getFormats($inner, $type, $fieldset, false);
319319

320320
//on each value
321-
foreach($row as $key => $value) {
321+
foreach($inner as $key => $value) {
322322
//get the formatted value
323323
$rows[$index][$key] = $results[$key]['value'];
324324
}
@@ -327,6 +327,20 @@
327327
//update the value
328328
$value = $rows;
329329
}
330+
331+
//FIX: for table columns need to sort out
332+
//the values based on column order
333+
if ($field['field']['type'] === 'table') {
334+
$columns = $field['field']['columns'];
335+
336+
foreach($value as $index => $inner) {
337+
//we should sort the rows based on column sorting
338+
$value[$index] = array_merge(
339+
array_flip(array_values($columns)),
340+
$value[$index]
341+
);
342+
}
343+
}
330344

331345
//prepare the template
332346
$template = cradle('global')
@@ -341,7 +355,7 @@
341355

342356
//get the default value in case it's empty
343357
if (is_null($value) || empty($value)) {
344-
// $value = $fields[$name]['default'];
358+
$value = $fields[$name]['default'];
345359
}
346360

347361
//and prepare the results

0 commit comments

Comments
 (0)