2022-04-29 18:29:42 +08:00
|
|
|
<template>
|
2023-10-19 17:45:01 +08:00
|
|
|
<div class="content__table-container pr-8">
|
2023-10-09 17:28:18 +08:00
|
|
|
<div class="sci-divider my-6" v-if="!inRepository"></div>
|
2023-10-05 17:01:34 +08:00
|
|
|
<div class="table-header h-9 flex rounded mb-3 items-center relative w-full group/table-header" :class="{ 'editing-name': editingName, 'locked': locked }">
|
2023-07-24 19:28:44 +08:00
|
|
|
<div v-if="!locked || element.attributes.orderable.name" :key="reloadHeader"
|
2023-09-06 20:58:59 +08:00
|
|
|
class="grow-1 text-ellipsis whitespace-nowrap grow my-1 font-bold"
|
|
|
|
:class="{'pointer-events-none': locked}">
|
2022-05-06 17:59:22 +08:00
|
|
|
<InlineEdit
|
|
|
|
:value="element.attributes.orderable.name"
|
|
|
|
:characterLimit="255"
|
2023-08-28 16:36:40 +08:00
|
|
|
:placeholder="i18n.t('protocols.steps.table.table_name')"
|
2022-05-06 17:59:22 +08:00
|
|
|
:allowBlank="false"
|
|
|
|
:autofocus="editingName"
|
|
|
|
:attributeName="`${i18n.t('Table')} ${i18n.t('name')}`"
|
|
|
|
@editingEnabled="enableNameEdit"
|
|
|
|
@editingDisabled="disableNameEdit"
|
|
|
|
@update="updateName"
|
|
|
|
/>
|
|
|
|
</div>
|
2023-09-13 18:12:36 +08:00
|
|
|
<MenuDropdown
|
|
|
|
class="ml-auto"
|
|
|
|
:listItems="this.actionMenu"
|
2023-09-18 21:32:53 +08:00
|
|
|
:btnClasses="'btn btn-light icon-btn btn-sm'"
|
2023-09-13 18:12:36 +08:00
|
|
|
:position="'right'"
|
|
|
|
:btnIcon="'sn-icon sn-icon-more-hori'"
|
|
|
|
@edit="enableNameEdit"
|
|
|
|
@duplicate="duplicateElement"
|
|
|
|
@move="showMoveModal"
|
|
|
|
@delete="showDeleteModal"
|
|
|
|
></MenuDropdown>
|
2022-05-06 17:59:22 +08:00
|
|
|
</div>
|
2023-09-14 18:12:15 +08:00
|
|
|
<div class="table-body group/table-body relative border-solid border-transparent"
|
2023-07-24 19:28:44 +08:00
|
|
|
:class="{'edit border-sn-light-grey': editingTable, 'view': !editingTable, 'locked': !element.attributes.orderable.urls.update_url}"
|
2022-07-15 20:58:06 +08:00
|
|
|
tabindex="0"
|
|
|
|
@keyup.enter="!editingTable && enableTableEdit()">
|
2022-07-26 19:42:00 +08:00
|
|
|
<div ref="hotTable" class="hot-table-container" @click="!editingTable && enableTableEdit()">
|
2022-05-06 17:59:22 +08:00
|
|
|
</div>
|
2023-07-24 19:28:44 +08:00
|
|
|
<div v-if="editingTable" class="text-xs pt-3 pb-2 text-sn-grey">
|
2022-05-06 17:59:22 +08:00
|
|
|
{{ i18n.t('protocols.steps.table.edit_message') }}
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-05-30 19:45:51 +08:00
|
|
|
<deleteElementModal v-if="confirmingDelete" @confirm="deleteElement" @cancel="closeDeleteModal"/>
|
2022-08-12 16:17:45 +08:00
|
|
|
<tableNameModal v-if="nameModalOpen" :element="element" @update="updateEmptyName" @cancel="nameModalOpen = false" />
|
2023-08-21 15:22:56 +08:00
|
|
|
<moveElementModal v-if="movingElement"
|
|
|
|
:parent_type="element.attributes.orderable.parent_type"
|
|
|
|
:targets_url="element.attributes.orderable.urls.move_targets_url"
|
|
|
|
@confirm="moveElement($event)" @cancel="closeMoveModal"/>
|
2022-04-29 18:29:42 +08:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-07-21 16:35:14 +08:00
|
|
|
import DeleteMixin from './mixins/delete.js'
|
2023-08-21 15:22:56 +08:00
|
|
|
import MoveMixin from './mixins/move.js'
|
2023-07-21 16:35:14 +08:00
|
|
|
import DuplicateMixin from './mixins/duplicate.js'
|
|
|
|
import deleteElementModal from './modal/delete.vue'
|
|
|
|
import InlineEdit from '../inline_edit.vue'
|
|
|
|
import TableNameModal from './modal/table_name.vue'
|
2023-08-21 15:22:56 +08:00
|
|
|
import moveElementModal from './modal/move.vue'
|
2023-09-13 18:12:36 +08:00
|
|
|
import MenuDropdown from '../menu_dropdown.vue'
|
2022-05-04 16:25:53 +08:00
|
|
|
|
2022-04-29 18:29:42 +08:00
|
|
|
export default {
|
2023-07-24 19:28:44 +08:00
|
|
|
name: 'ContentTable',
|
2023-09-13 18:12:36 +08:00
|
|
|
components: { deleteElementModal, InlineEdit, TableNameModal, moveElementModal, MenuDropdown },
|
2023-08-21 15:22:56 +08:00
|
|
|
mixins: [DeleteMixin, DuplicateMixin, MoveMixin],
|
2022-05-03 21:15:49 +08:00
|
|
|
props: {
|
|
|
|
element: {
|
|
|
|
type: Object,
|
|
|
|
required: true
|
2022-06-03 17:52:10 +08:00
|
|
|
},
|
2022-06-07 18:10:10 +08:00
|
|
|
inRepository: {
|
|
|
|
type: Boolean,
|
|
|
|
required: true
|
|
|
|
},
|
2022-06-03 17:52:10 +08:00
|
|
|
reorderElementUrl: {
|
|
|
|
type: String
|
2022-07-06 20:18:11 +08:00
|
|
|
},
|
|
|
|
isNew: {
|
|
|
|
type: Boolean, default: false
|
2023-04-25 21:12:19 +08:00
|
|
|
},
|
|
|
|
assignableMyModuleId: {
|
|
|
|
type: Number,
|
|
|
|
required: false
|
2022-05-03 21:15:49 +08:00
|
|
|
}
|
2022-05-06 17:59:22 +08:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
editingName: false,
|
|
|
|
editingTable: false,
|
2023-11-08 19:26:11 +08:00
|
|
|
editingCell: false,
|
2022-08-12 16:17:45 +08:00
|
|
|
tableObject: null,
|
|
|
|
nameModalOpen: false,
|
2023-06-14 16:20:40 +08:00
|
|
|
reloadHeader: 0,
|
|
|
|
updatingTableData: false
|
2022-05-06 17:59:22 +08:00
|
|
|
}
|
|
|
|
},
|
2022-08-10 16:38:10 +08:00
|
|
|
computed: {
|
|
|
|
locked() {
|
|
|
|
return !this.element.attributes.orderable.urls.update_url
|
2023-09-13 18:12:36 +08:00
|
|
|
},
|
|
|
|
actionMenu() {
|
|
|
|
let menu = [];
|
|
|
|
if (this.element.attributes.orderable.urls.update_url) {
|
|
|
|
menu.push({
|
|
|
|
text: I18n.t('general.edit'),
|
|
|
|
emit: 'edit'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.element.attributes.orderable.urls.duplicate_url) {
|
|
|
|
menu.push({
|
|
|
|
text: I18n.t('general.duplicate'),
|
|
|
|
emit: 'duplicate'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.element.attributes.orderable.urls.move_targets_url) {
|
|
|
|
menu.push({
|
|
|
|
text: I18n.t('general.move'),
|
|
|
|
emit: 'move'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.element.attributes.orderable.urls.delete_url) {
|
|
|
|
menu.push({
|
|
|
|
text: I18n.t('general.delete'),
|
|
|
|
emit: 'delete'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return menu;
|
2022-08-10 16:38:10 +08:00
|
|
|
}
|
|
|
|
},
|
2023-11-03 17:35:15 +08:00
|
|
|
created() {
|
2023-11-08 19:26:11 +08:00
|
|
|
window.addEventListener('beforeunload', this.showSaveWarning);
|
|
|
|
},
|
|
|
|
beforeDestroy() {
|
|
|
|
window.removeEventListener('beforeunload', this.showSaveWarning);
|
2023-11-03 17:35:15 +08:00
|
|
|
},
|
2022-05-06 17:59:22 +08:00
|
|
|
updated() {
|
2023-06-14 16:20:40 +08:00
|
|
|
if(!this.updatingTableData) this.loadTableData();
|
2022-05-06 17:59:22 +08:00
|
|
|
},
|
|
|
|
beforeUpdate() {
|
2023-06-14 16:20:40 +08:00
|
|
|
if(!this.updatingTableData) this.tableObject.destroy();
|
2022-05-06 17:59:22 +08:00
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
this.loadTableData();
|
2022-07-06 20:18:11 +08:00
|
|
|
|
2023-10-10 22:09:47 +08:00
|
|
|
if (this.isNew) {
|
|
|
|
// needs to first update to save metadata at table creation
|
|
|
|
this.update(() => { this.enableTableEdit() });
|
|
|
|
}
|
2022-05-06 17:59:22 +08:00
|
|
|
},
|
|
|
|
methods: {
|
2023-11-08 19:26:11 +08:00
|
|
|
showSaveWarning(e) {
|
|
|
|
if (this.editingCell) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.returnValue = '';
|
|
|
|
}
|
|
|
|
},
|
2022-05-06 17:59:22 +08:00
|
|
|
enableTableEdit() {
|
2022-08-23 20:23:21 +08:00
|
|
|
if(this.locked) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-12 16:17:45 +08:00
|
|
|
if (!this.element.attributes.orderable.name) {
|
|
|
|
this.openNameModal();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-06 17:59:22 +08:00
|
|
|
this.editingTable = true;
|
2022-07-06 20:18:11 +08:00
|
|
|
this.$nextTick(() => this.tableObject.selectCell(0,0));
|
2022-05-06 17:59:22 +08:00
|
|
|
},
|
|
|
|
disableTableEdit() {
|
|
|
|
this.editingTable = false;
|
2023-06-14 16:20:40 +08:00
|
|
|
this.updatingTableData = false;
|
2022-05-06 17:59:22 +08:00
|
|
|
},
|
|
|
|
enableNameEdit() {
|
|
|
|
this.editingName = true;
|
|
|
|
},
|
|
|
|
disableNameEdit() {
|
|
|
|
this.editingName = false;
|
|
|
|
},
|
|
|
|
updateName(name) {
|
|
|
|
this.element.attributes.orderable.name = name;
|
2023-09-15 20:52:36 +08:00
|
|
|
// Prevents the table from being updated when the name is updated
|
|
|
|
this.updatingTableData = true;
|
|
|
|
this.update(() => {
|
|
|
|
this.updatingTableData = false;
|
|
|
|
});
|
2022-05-06 17:59:22 +08:00
|
|
|
},
|
2022-08-12 16:17:45 +08:00
|
|
|
openNameModal() {
|
|
|
|
this.tableObject.deselectCell();
|
|
|
|
this.nameModalOpen = true;
|
|
|
|
},
|
|
|
|
updateEmptyName(name) {
|
|
|
|
this.disableNameEdit();
|
|
|
|
|
|
|
|
// force reload header to properly reset name inline edit
|
|
|
|
this.reloadHeader = this.reloadHeader + 1;
|
|
|
|
|
|
|
|
this.element.attributes.orderable.name = name;
|
|
|
|
this.$emit('update', this.element, false, () => {
|
|
|
|
this.nameModalOpen = false;
|
|
|
|
this.enableTableEdit();
|
|
|
|
});
|
|
|
|
},
|
2022-05-06 17:59:22 +08:00
|
|
|
updateTable() {
|
2022-07-15 20:58:06 +08:00
|
|
|
if (this.editingTable == false) return;
|
|
|
|
|
2023-09-15 20:52:36 +08:00
|
|
|
this.update(() => {
|
|
|
|
this.editingTable = false;
|
|
|
|
this.updatingTableData = false;
|
|
|
|
});
|
2022-05-06 17:59:22 +08:00
|
|
|
},
|
2023-09-15 20:52:36 +08:00
|
|
|
update(callback = () => {}) {
|
2023-03-28 22:03:45 +08:00
|
|
|
this.element.attributes.orderable.contents = JSON.stringify({ data: this.tableObject.getData() });
|
2023-06-02 22:08:42 +08:00
|
|
|
let metadata = this.element.attributes.orderable.metadata || {};
|
|
|
|
if (metadata.plateTemplate) {
|
|
|
|
this.element.attributes.orderable.metadata = JSON.stringify({
|
|
|
|
cells: this.tableObject
|
|
|
|
.getCellsMeta()
|
|
|
|
.filter(e => !!e)
|
|
|
|
.map((x) => {
|
|
|
|
const {row, col} = x;
|
|
|
|
return {
|
|
|
|
row: row,
|
|
|
|
col: col,
|
|
|
|
className: x.className || '',
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.element.attributes.orderable.metadata = JSON.stringify({
|
|
|
|
cells: this.tableObject
|
|
|
|
.getCellsMeta()
|
|
|
|
.filter(e => !!e)
|
|
|
|
.map((x) => {
|
|
|
|
const {row, col} = x;
|
|
|
|
const plugins = this.tableObject.plugin;
|
|
|
|
const cellId = plugins.utils.translateCellCoords({row, col});
|
|
|
|
const calculated = plugins.matrix.getItem(cellId)?.value ||
|
|
|
|
this.tableObject.getDataAtCell(row, col) ||
|
|
|
|
null;
|
|
|
|
return {
|
|
|
|
row: row,
|
|
|
|
col: col,
|
|
|
|
className: x.className || '',
|
|
|
|
calculated: calculated
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
2023-09-15 20:52:36 +08:00
|
|
|
|
|
|
|
this.$emit('update', this.element, false, callback)
|
2022-05-06 17:59:22 +08:00
|
|
|
},
|
|
|
|
loadTableData() {
|
|
|
|
let container = this.$refs.hotTable;
|
|
|
|
let data = JSON.parse(this.element.attributes.orderable.contents);
|
2023-02-06 19:55:02 +08:00
|
|
|
let metadata = this.element.attributes.orderable.metadata || {};
|
2023-06-02 22:08:42 +08:00
|
|
|
let formulasEnabled = metadata.plateTemplate ? false : true;
|
2023-04-19 17:45:32 +08:00
|
|
|
|
2022-05-06 17:59:22 +08:00
|
|
|
this.tableObject = new Handsontable(container, {
|
|
|
|
data: data.data,
|
|
|
|
width: '100%',
|
|
|
|
startRows: 5,
|
|
|
|
startCols: 5,
|
2023-05-08 16:23:44 +08:00
|
|
|
rowHeaders: tableColRowName.tableRowHeaders(metadata.plateTemplate),
|
|
|
|
colHeaders: tableColRowName.tableColHeaders(metadata.plateTemplate),
|
2023-02-06 19:55:02 +08:00
|
|
|
cell: metadata.cells || [],
|
2022-05-06 17:59:22 +08:00
|
|
|
contextMenu: this.editingTable,
|
2023-06-02 22:08:42 +08:00
|
|
|
formulas: formulasEnabled,
|
2022-09-16 17:16:39 +08:00
|
|
|
preventOverflow: 'horizontal',
|
2022-07-06 20:18:11 +08:00
|
|
|
readOnly: !this.editingTable,
|
2023-06-14 16:20:40 +08:00
|
|
|
afterUnlisten: () => {
|
|
|
|
this.updatingTableData = true;
|
2023-11-08 19:26:11 +08:00
|
|
|
this.updateTable();
|
2023-11-03 17:35:15 +08:00
|
|
|
},
|
2023-11-08 19:26:11 +08:00
|
|
|
afterChange: () => {
|
2023-11-03 17:35:15 +08:00
|
|
|
if (this.editingTable == false) return;
|
|
|
|
this.updatingTableData = true;
|
|
|
|
|
|
|
|
this.$nextTick(() => {
|
2023-11-08 19:26:11 +08:00
|
|
|
this.update(() => this.editingCell = false);
|
2023-11-03 17:35:15 +08:00
|
|
|
});
|
2023-11-06 23:56:13 +08:00
|
|
|
},
|
|
|
|
beforeKeyDown: (e) => {
|
|
|
|
if (e.keyCode === 27) { // esc
|
|
|
|
this.editingCell = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
2023-11-08 19:26:11 +08:00
|
|
|
afterBeginEditing: (e) => {
|
|
|
|
this.editingCell = true;
|
|
|
|
}
|
2022-05-06 17:59:22 +08:00
|
|
|
});
|
2023-09-14 16:52:08 +08:00
|
|
|
this.$nextTick(this.tableObject.render);
|
2022-05-06 17:59:22 +08:00
|
|
|
}
|
2022-05-03 21:15:49 +08:00
|
|
|
}
|
2022-04-29 18:29:42 +08:00
|
|
|
}
|
2022-05-03 19:42:43 +08:00
|
|
|
</script>
|