2022-04-29 18:29:42 +08:00
|
|
|
<template>
|
2022-08-23 20:23:21 +08:00
|
|
|
<div class="step-table-container">
|
|
|
|
<div class="step-element-header" :class="{ 'editing-name': editingName, 'step-element--locked': locked }">
|
2022-06-03 17:52:10 +08:00
|
|
|
<div v-if="reorderElementUrl" class="step-element-grip" @click="$emit('reorder')">
|
2022-07-11 16:54:18 +08:00
|
|
|
<i class="fas fas-rotated-90 fa-exchange-alt"></i>
|
2022-05-06 17:59:22 +08:00
|
|
|
</div>
|
2022-07-11 18:31:54 +08:00
|
|
|
<div v-else class="step-element-grip-placeholder"></div>
|
2022-08-12 16:17:45 +08:00
|
|
|
<div v-if="!locked || element.attributes.orderable.name" :key="reloadHeader" class="step-element-name">
|
2022-05-06 17:59:22 +08:00
|
|
|
<InlineEdit
|
|
|
|
:value="element.attributes.orderable.name"
|
|
|
|
:characterLimit="255"
|
|
|
|
:placeholder="''"
|
|
|
|
:allowBlank="false"
|
|
|
|
:autofocus="editingName"
|
|
|
|
:attributeName="`${i18n.t('Table')} ${i18n.t('name')}`"
|
|
|
|
@editingEnabled="enableNameEdit"
|
|
|
|
@editingDisabled="disableNameEdit"
|
|
|
|
@update="updateName"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div class="step-element-controls">
|
2023-01-03 19:25:22 +08:00
|
|
|
<button v-if="element.attributes.orderable.urls.update_url" class="btn icon-btn btn-light" @click="enableNameEdit" tabindex="0">
|
2022-05-06 17:59:22 +08:00
|
|
|
<i class="fas fa-pen"></i>
|
|
|
|
</button>
|
2023-01-03 19:25:22 +08:00
|
|
|
<button v-if="element.attributes.orderable.urls.duplicate_url" class="btn icon-btn btn-light" tabindex="0" @click="duplicateElement">
|
2022-08-22 20:02:19 +08:00
|
|
|
<i class="fas fa-clone"></i>
|
|
|
|
</button>
|
2023-01-03 19:25:22 +08:00
|
|
|
<button v-if="element.attributes.orderable.urls.delete_url" class="btn icon-btn btn-light" @click="showDeleteModal" tabindex="0">
|
2022-05-06 17:59:22 +08:00
|
|
|
<i class="fas fa-trash"></i>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-07-15 20:58:06 +08:00
|
|
|
<div class="step-table"
|
|
|
|
:class="{'edit': editingTable, 'view': !editingTable, 'locked': !element.attributes.orderable.urls.update_url}"
|
|
|
|
tabindex="0"
|
|
|
|
@keyup.enter="!editingTable && enableTableEdit()">
|
2022-06-03 17:52:10 +08:00
|
|
|
<div class="enable-edit-mode" v-if="!editingTable && element.attributes.orderable.urls.update_url" @click="enableTableEdit">
|
2023-01-03 19:25:22 +08:00
|
|
|
<div class="enable-edit-mode__icon" tabindex="0">
|
2022-07-20 18:07:25 +08:00
|
|
|
<i class="fas fa-pen"></i>
|
|
|
|
</div>
|
2022-05-06 17:59:22 +08:00
|
|
|
</div>
|
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>
|
|
|
|
<div v-if="editingTable" class="edit-message">
|
|
|
|
{{ i18n.t('protocols.steps.table.edit_message') }}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="edit-buttons" v-if="editingTable">
|
|
|
|
<button class="btn icon-btn btn-primary" @click="updateTable">
|
|
|
|
<i class="fas fa-check"></i>
|
|
|
|
</button>
|
|
|
|
<button class="btn icon-btn btn-light" @click="disableTableEdit">
|
|
|
|
<i class="fas fa-times"></i>
|
|
|
|
</button>
|
|
|
|
</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" />
|
2022-04-29 18:29:42 +08:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-03-30 19:27:10 +08:00
|
|
|
import DeleteMixin from '../mixins/components/delete.js'
|
|
|
|
import DuplicateMixin from '../mixins/components/duplicate.js'
|
|
|
|
import deleteElementModal from '../modals/delete_element.vue'
|
|
|
|
import InlineEdit from '../../shared/inline_edit.vue'
|
|
|
|
import TableNameModal from '../modals/table_name_modal.vue'
|
2022-05-04 16:25:53 +08:00
|
|
|
|
2022-04-29 18:29:42 +08:00
|
|
|
export default {
|
2022-05-03 21:15:49 +08:00
|
|
|
name: 'StepTable',
|
2022-08-12 16:17:45 +08:00
|
|
|
components: { deleteElementModal, InlineEdit, TableNameModal },
|
2022-08-30 21:26:52 +08:00
|
|
|
mixins: [DeleteMixin, DuplicateMixin],
|
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
|
2022-05-03 21:15:49 +08:00
|
|
|
}
|
2022-05-06 17:59:22 +08:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
editingName: false,
|
|
|
|
editingTable: false,
|
2022-08-12 16:17:45 +08:00
|
|
|
tableObject: null,
|
|
|
|
nameModalOpen: false,
|
|
|
|
reloadHeader: 0
|
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
|
|
|
|
}
|
|
|
|
},
|
2022-05-06 17:59:22 +08:00
|
|
|
updated() {
|
|
|
|
this.loadTableData();
|
|
|
|
},
|
|
|
|
beforeUpdate() {
|
|
|
|
this.tableObject.destroy();
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
this.loadTableData();
|
2022-07-06 20:18:11 +08:00
|
|
|
|
|
|
|
if (this.isNew) this.enableTableEdit();
|
2022-05-06 17:59:22 +08:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
enableNameEdit() {
|
|
|
|
this.editingName = true;
|
|
|
|
},
|
|
|
|
disableNameEdit() {
|
|
|
|
this.editingName = false;
|
|
|
|
},
|
|
|
|
updateName(name) {
|
|
|
|
this.element.attributes.orderable.name = name;
|
|
|
|
this.update();
|
|
|
|
},
|
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;
|
|
|
|
|
2022-05-06 17:59:22 +08:00
|
|
|
this.update();
|
|
|
|
this.editingTable = false;
|
|
|
|
},
|
|
|
|
update() {
|
2023-03-28 22:03:45 +08:00
|
|
|
this.element.attributes.orderable.contents = JSON.stringify({ data: this.tableObject.getData() });
|
|
|
|
this.element.attributes.orderable.metadata = JSON.stringify({
|
|
|
|
cells: this.tableObject.getCellsMeta().map(
|
|
|
|
(x) => {
|
|
|
|
if (x) {
|
|
|
|
return {
|
|
|
|
col: x.col,
|
|
|
|
row: x.row,
|
|
|
|
className: x.className || ''
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}).filter(e => { return e !== null })
|
|
|
|
});
|
2022-05-06 17:59:22 +08:00
|
|
|
this.$emit('update', this.element)
|
2023-04-25 20:33:32 +08:00
|
|
|
this.ajax_update_url()
|
|
|
|
},
|
|
|
|
ajax_update_url() {
|
|
|
|
$.ajax({
|
|
|
|
url: this.element.attributes.orderable.urls.update_url,
|
|
|
|
method: 'PUT',
|
|
|
|
data: this.element.attributes.orderable,
|
|
|
|
})
|
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 || {};
|
2022-05-06 17:59:22 +08:00
|
|
|
this.tableObject = new Handsontable(container, {
|
|
|
|
data: data.data,
|
|
|
|
width: '100%',
|
|
|
|
startRows: 5,
|
|
|
|
startCols: 5,
|
|
|
|
rowHeaders: true,
|
|
|
|
colHeaders: true,
|
2023-02-06 19:55:02 +08:00
|
|
|
cell: metadata.cells || [],
|
2022-05-06 17:59:22 +08:00
|
|
|
contextMenu: this.editingTable,
|
|
|
|
formulas: true,
|
2022-09-16 17:16:39 +08:00
|
|
|
preventOverflow: 'horizontal',
|
2022-07-06 20:18:11 +08:00
|
|
|
readOnly: !this.editingTable,
|
2022-08-08 22:18:59 +08:00
|
|
|
afterUnlisten: () => setTimeout(this.updateTable, 100) // delay makes cancel button work
|
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>
|