Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored block table with structure field #3411

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions panel/src/components/Blocks/Types/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<th
v-for="(column, columnName) in columns"
:key="columnName"
:style="'width:' + width(column.width)"
:data-align="column.align"
>
{{ column.label }}
Expand All @@ -18,28 +19,46 @@
<td
v-for="(column, columnName) in columns"
:key="rowIndex + '-' + columnName"
:style="'width:' + width(column.width)"
:data-align="column.align"
>
{{ column.before }} {{ row[columnName] }} {{ column.after }}
<component
:is="'k-' + column.type + '-field-preview'"
v-if="previewExists(column.type)"
:value="row[columnName]"
:column="column"
:field="fields[columnName]"
/>
<template v-else>
<p class="k-structure-table-text">
{{ column.before }} {{ displayText(fields[columnName], row[columnName]) || "–" }} {{ column.after }}
</p>
</template>
</td>
</tr>
</table>
</template>

<script>
import structure from "@/mixins/forms/structure.js";

/**
* @displayName BlockTypeTable
* @internal
*/
export default {
mixins: [structure],
inheritAttrs: false,
computed: {
columns() {
return this.table.columns || this.table.fields || {};
return this.table.columns || this.fields;
},
columnsCount() {
return Object.keys(this.columns).length;
},
fields() {
return this.table.fields || {};
},
rows() {
return this.content.rows || [];
},
Expand Down
69 changes: 3 additions & 66 deletions panel/src/components/Forms/Field/StructureField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@
</template>

<script>
import Field from "../Field.vue";
import direction from "@/helpers/direction.js";
import structure from "@/mixins/forms/structure.js";

export default {
mixins: [structure],
inheritAttrs: false,
props: {
...Field.props,
columns: Object,
duplicate: {
type: Boolean,
Expand Down Expand Up @@ -414,51 +414,6 @@ export default {
}
});
},
displayText(field, value) {
switch (field.type) {
case "user": {
return value.email;
}
case "date": {
const date = this.$library.dayjs(value);
const format = field.time === true ? "YYYY-MM-DD HH:mm" : "YYYY-MM-DD";
return date.isValid() ? date.format(format) : "";
}
case "tags":
case "multiselect":
return value
.map(item => {
return item.text;
})
.join(", ");
case "checkboxes": {
return value
.map(item => {
let text = item;

field.options.forEach(option => {
if (option.value === item) {
text = option.text;
}
});

return text;
})
.join(", ");
}
case "radio":
case "select": {
const option = field.options.filter(item => item.value === value)[0];
return option ? option.text : null;
}
}

if (typeof value === "object" && value !== null) {
return "…";
}

return value.toString();
},
duplicateItem(index) {
this.addItem(this.items[index + this.pagination.offset]);
this.onInput();
Expand Down Expand Up @@ -511,7 +466,7 @@ export default {
this.$emit("input", this.items);
},
/**
* Edit the structure field entry at `index` position
* Edit the structure field entry at `index` position
* with field `field` focused.
* @public
* @param {number} index
Expand All @@ -528,9 +483,6 @@ export default {
paginateItems(pagination) {
this.page = pagination.page;
},
previewExists(type) {
return this.$helper.isComponent(`k-${type}-field-preview`);
},
remove() {
if (this.trash === null) {
return false;
Expand Down Expand Up @@ -599,21 +551,6 @@ export default {
}
});
},
width(fraction) {
if (!fraction) {
return "auto";
}
const parts = fraction.toString().split("/");

if (parts.length !== 2) {
return "auto";
}

const a = Number(parts[0]);
const b = Number(parts[1]);

return parseFloat(100 / b * a, 2).toFixed(2) + "%";
},
update(index, column, value) {
this.items[index][column] = value;
this.onInput();
Expand Down
73 changes: 73 additions & 0 deletions panel/src/mixins/forms/structure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

import Field from "@/components/Forms/Field.vue";

export default {
props: {
...Field.props,
},
methods: {
displayText(field, value) {
switch (field.type) {
case "user": {
return value.email;
}
case "date": {
const date = this.$library.dayjs(value);
const format = field.time === true ? "YYYY-MM-DD HH:mm" : "YYYY-MM-DD";
return date.isValid() ? date.format(format) : "";
}
case "tags":
case "multiselect":
return value
.map(item => {
return item.text;
})
.join(", ");
case "checkboxes": {
return value
.map(item => {
let text = item;

field.options.forEach(option => {
if (option.value === item) {
text = option.text;
}
});

return text;
})
.join(", ");
}
case "radio":
case "select": {
const option = field.options.filter(item => item.value === value)[0];
return option ? option.text : null;
}
}

if (typeof value === "object" && value !== null) {
return "…";
}

return value.toString();
},
previewExists(type) {
return this.$helper.isComponent(`k-${type}-field-preview`);
},
width(fraction) {
if (!fraction) {
return "auto";
}
const parts = fraction.toString().split("/");

if (parts.length !== 2) {
return "auto";
}

const a = Number(parts[0]);
const b = Number(parts[1]);

return parseFloat(String(100 / b * a)).toFixed(2) + "%";
}
}
}