2022-04-29 18:29:42 +08:00
|
|
|
<template>
|
2022-07-26 19:33:43 +08:00
|
|
|
<div class="step-checklist-container" >
|
2022-08-02 17:00:13 +08:00
|
|
|
<div class="step-element-header" :class="{ 'editing-name': editingName, 'no-hover': !element.attributes.orderable.urls.update_url }">
|
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-05 18:56:31 +08:00
|
|
|
</div>
|
2022-07-11 18:31:54 +08:00
|
|
|
<div v-else class="step-element-grip-placeholder"></div>
|
2022-05-05 18:56:31 +08:00
|
|
|
<div class="step-element-name">
|
|
|
|
<InlineEdit
|
2022-07-26 19:33:43 +08:00
|
|
|
:class="{ 'step-element--locked': !element.attributes.orderable.urls.update_url }"
|
2022-05-05 18:56:31 +08:00
|
|
|
:value="element.attributes.orderable.name"
|
2022-07-08 18:51:43 +08:00
|
|
|
:sa_value="element.attributes.orderable.sa_name"
|
2022-07-11 16:44:27 +08:00
|
|
|
:characterLimit="10000"
|
2022-05-05 18:56:31 +08:00
|
|
|
:placeholder="''"
|
|
|
|
:allowBlank="false"
|
|
|
|
:autofocus="editingName"
|
2022-07-08 18:51:43 +08:00
|
|
|
:smartAnnotation="true"
|
2022-05-05 18:56:31 +08:00
|
|
|
:attributeName="`${i18n.t('Checklist')} ${i18n.t('name')}`"
|
2022-05-26 20:02:02 +08:00
|
|
|
@editingEnabled="editingName = true"
|
2022-06-02 18:02:34 +08:00
|
|
|
@editingDisabled="editingName = false"
|
2022-05-05 18:56:31 +08:00
|
|
|
@update="updateName"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div class="step-element-controls">
|
2022-07-06 20:31:55 +08:00
|
|
|
<button v-if="element.attributes.orderable.urls.update_url" class="btn icon-btn btn-light" @click="editingName = true" tabindex="-1">
|
2022-05-05 18:56:31 +08:00
|
|
|
<i class="fas fa-pen"></i>
|
|
|
|
</button>
|
2022-08-22 20:02:19 +08:00
|
|
|
<button v-if="element.attributes.orderable.urls.duplicate_url" class="btn icon-btn btn-light" tabindex="-1" @click="duplicateElement">
|
|
|
|
<i class="fas fa-clone"></i>
|
|
|
|
</button>
|
2022-07-06 20:31:55 +08:00
|
|
|
<button v-if="element.attributes.orderable.urls.delete_url" class="btn icon-btn btn-light" @click="showDeleteModal" tabindex="-1">
|
2022-05-05 18:56:31 +08:00
|
|
|
<i class="fas fa-trash"></i>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-07-15 20:58:06 +08:00
|
|
|
<div v-if="element.attributes.orderable.urls.create_item_url || orderedChecklistItems.length > 0" class="step-checklist-items">
|
2022-05-26 20:02:02 +08:00
|
|
|
<Draggable
|
|
|
|
v-model="checklistItems"
|
|
|
|
:ghostClass="'step-checklist-item-ghost'"
|
|
|
|
:dragClass="'step-checklist-item-drag'"
|
|
|
|
:chosenClass="'step-checklist-item-chosen'"
|
|
|
|
:handle="'.step-element-grip'"
|
2022-07-21 15:57:14 +08:00
|
|
|
:disabled="editingItem || checklistItems.length < 2 || !element.attributes.orderable.urls.reorder_url"
|
2022-05-26 20:02:02 +08:00
|
|
|
@start="startReorder"
|
|
|
|
@end="endReorder"
|
|
|
|
>
|
|
|
|
<ChecklistItem
|
|
|
|
v-for="checklistItem in orderedChecklistItems"
|
|
|
|
:key="checklistItem.attributes.id"
|
|
|
|
:checklistItem="checklistItem"
|
|
|
|
:locked="locked"
|
2022-06-03 17:52:10 +08:00
|
|
|
:reorderChecklistItemUrl="element.attributes.orderable.urls.reorder_url"
|
2022-06-07 18:10:10 +08:00
|
|
|
:inRepository="inRepository"
|
2022-07-21 15:57:14 +08:00
|
|
|
:draggable="checklistItems.length > 1"
|
2022-05-26 20:02:02 +08:00
|
|
|
@editStart="editingItem = true"
|
|
|
|
@editEnd="editingItem = false"
|
|
|
|
@update="saveItem"
|
2022-07-14 21:09:28 +08:00
|
|
|
@toggle="saveItemChecked"
|
2022-05-26 20:02:02 +08:00
|
|
|
@removeItem="removeItem"
|
|
|
|
@component:delete="removeItem"
|
|
|
|
@multilinePaste="handleMultilinePaste"
|
|
|
|
/>
|
|
|
|
</Draggable>
|
2022-07-06 20:31:55 +08:00
|
|
|
<div v-if="element.attributes.orderable.urls.create_item_url"
|
|
|
|
class="btn btn-light step-checklist-add-item"
|
|
|
|
tabindex="0"
|
|
|
|
@keyup.enter="addItem"
|
|
|
|
@click="addItem">
|
2022-05-11 21:51:26 +08:00
|
|
|
<i class="fas fa-plus"></i>
|
|
|
|
{{ i18n.t('protocols.steps.insert.checklist_item') }}
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-07-15 20:58:06 +08:00
|
|
|
<div v-else class="empty-checklist-element">
|
|
|
|
{{ i18n.t("protocols.steps.checklist.empty_checklist") }}
|
|
|
|
</div>
|
2022-05-30 19:45:51 +08:00
|
|
|
<deleteElementModal v-if="confirmingDelete" @confirm="deleteElement" @cancel="closeDeleteModal"/>
|
2022-04-29 18:29:42 +08:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2022-05-03 21:15:49 +08:00
|
|
|
import DeleteMixin from 'vue/protocol/mixins/components/delete.js'
|
2022-08-30 21:26:52 +08:00
|
|
|
import DuplicateMixin from 'vue/protocol/mixins/components/duplicate.js'
|
2022-05-30 19:45:51 +08:00
|
|
|
import deleteElementModal from 'vue/protocol/modals/delete_element.vue'
|
2022-05-05 18:56:31 +08:00
|
|
|
import InlineEdit from 'vue/shared/inline_edit.vue'
|
2022-05-30 19:45:51 +08:00
|
|
|
import ChecklistItem from 'vue/protocol/step_elements/checklistItem.vue'
|
2022-05-26 20:02:02 +08:00
|
|
|
import Draggable from 'vuedraggable'
|
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: 'Checklist',
|
2022-05-30 19:45:51 +08:00
|
|
|
components: { deleteElementModal, InlineEdit, ChecklistItem, Draggable },
|
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-12 16:41:58 +08:00
|
|
|
},
|
|
|
|
isNew: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2022-05-03 21:15:49 +08:00
|
|
|
}
|
2022-05-05 18:56:31 +08:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
2022-05-26 20:02:02 +08:00
|
|
|
checklistItems: [],
|
|
|
|
linesToPaste: 0,
|
2022-05-11 21:51:26 +08:00
|
|
|
editingName: false,
|
2022-05-26 20:02:02 +08:00
|
|
|
reordering: false,
|
|
|
|
editingItem: false
|
2022-05-11 21:51:26 +08:00
|
|
|
}
|
|
|
|
},
|
2022-05-26 20:02:02 +08:00
|
|
|
created() {
|
2022-08-10 16:37:27 +08:00
|
|
|
this.initChecklistItems();
|
2022-07-12 16:41:58 +08:00
|
|
|
|
|
|
|
if (this.isNew) {
|
|
|
|
this.addItem();
|
|
|
|
}
|
2022-05-26 20:02:02 +08:00
|
|
|
},
|
2022-08-10 16:37:27 +08:00
|
|
|
watch: {
|
|
|
|
element() {
|
|
|
|
this.initChecklistItems();
|
|
|
|
}
|
|
|
|
},
|
2022-05-11 21:51:26 +08:00
|
|
|
computed: {
|
2022-05-26 20:02:02 +08:00
|
|
|
orderedChecklistItems() {
|
|
|
|
return this.checklistItems.map((item, index) => {
|
2022-06-02 17:15:32 +08:00
|
|
|
return { attributes: {...item.attributes, position: index } }
|
2022-05-11 21:51:26 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
pastingMultiline() {
|
|
|
|
return this.linesToPaste > 0;
|
2022-05-26 20:02:02 +08:00
|
|
|
},
|
|
|
|
locked() {
|
2022-07-15 20:58:06 +08:00
|
|
|
return this.reordering || this.editingName || !this.element.attributes.orderable.urls.update_url
|
2022-05-05 18:56:31 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2022-08-10 16:37:27 +08:00
|
|
|
initChecklistItems() {
|
|
|
|
this.checklistItems = this.element.attributes.orderable.checklist_items.map((item, index) => {
|
|
|
|
return { attributes: {...item, position: index } }
|
|
|
|
});
|
|
|
|
},
|
2022-05-05 18:56:31 +08:00
|
|
|
updateName(name) {
|
|
|
|
this.element.attributes.orderable.name = name;
|
2022-06-02 17:53:24 +08:00
|
|
|
this.editingName = false;
|
2022-08-10 16:37:27 +08:00
|
|
|
this.update(false);
|
2022-05-05 18:56:31 +08:00
|
|
|
},
|
2022-08-10 16:37:27 +08:00
|
|
|
update(skipRequest = true) {
|
2022-06-02 17:53:24 +08:00
|
|
|
this.element.attributes.orderable.checklist_items =
|
|
|
|
this.checklistItems.map((i) => i.attributes);
|
|
|
|
|
2022-08-10 16:37:27 +08:00
|
|
|
this.$emit('update', this.element, skipRequest);
|
2022-05-11 21:51:26 +08:00
|
|
|
},
|
|
|
|
postItem(item, callback) {
|
|
|
|
$.post(this.element.attributes.orderable.urls.create_item_url, item).success((result) => {
|
2022-05-26 20:02:02 +08:00
|
|
|
this.checklistItems.splice(
|
2022-06-02 17:15:32 +08:00
|
|
|
result.data.attributes.position,
|
2022-05-11 21:51:26 +08:00
|
|
|
1,
|
2022-05-26 20:02:02 +08:00
|
|
|
{ attributes: { ...result.data.attributes, id: result.data.id } }
|
2022-05-11 21:51:26 +08:00
|
|
|
);
|
|
|
|
|
2022-05-26 20:02:02 +08:00
|
|
|
if(callback) callback();
|
2022-05-11 21:51:26 +08:00
|
|
|
}).error(() => {
|
|
|
|
HelperModule.flashAlertMsg(this.i18n.t('errors.general'), 'danger');
|
|
|
|
});
|
2022-06-02 17:53:24 +08:00
|
|
|
|
|
|
|
this.update();
|
2022-05-11 21:51:26 +08:00
|
|
|
},
|
|
|
|
saveItem(item) {
|
|
|
|
if (item.attributes.id) {
|
|
|
|
$.ajax({
|
|
|
|
url: item.attributes.urls.update_url,
|
|
|
|
type: 'PATCH',
|
|
|
|
data: item,
|
2022-07-08 19:49:22 +08:00
|
|
|
success: (result) => {
|
|
|
|
let updatedItem = this.checklistItems[item.attributes.position]
|
|
|
|
updatedItem.attributes = result.data.attributes
|
|
|
|
updatedItem.attributes.id = item.attributes.id
|
|
|
|
this.$set(this.checklistItems, item.attributes.position, updatedItem)
|
|
|
|
},
|
2022-05-11 21:51:26 +08:00
|
|
|
error: () => HelperModule.flashAlertMsg(this.i18n.t('errors.general'), 'danger')
|
|
|
|
});
|
|
|
|
} else {
|
2022-05-26 20:02:02 +08:00
|
|
|
// create item, then append next one
|
2022-07-13 23:58:58 +08:00
|
|
|
this.postItem(item, this.addItem);
|
2022-05-11 21:51:26 +08:00
|
|
|
}
|
2022-08-10 16:37:27 +08:00
|
|
|
this.update(true);
|
2022-05-11 21:51:26 +08:00
|
|
|
},
|
2022-07-14 21:09:28 +08:00
|
|
|
saveItemChecked(item) {
|
|
|
|
$.ajax({
|
|
|
|
url: item.attributes.urls.toggle_url,
|
|
|
|
type: 'PATCH',
|
|
|
|
data: { attributes: { checked: item.attributes.checked } },
|
|
|
|
success: (result) => {
|
|
|
|
let updatedItem = this.checklistItems[item.attributes.position]
|
|
|
|
updatedItem.attributes = result.data.attributes
|
|
|
|
updatedItem.attributes.id = item.attributes.id
|
|
|
|
this.$set(this.checklistItems, item.attributes.position, updatedItem)
|
|
|
|
},
|
|
|
|
error: () => HelperModule.flashAlertMsg(this.i18n.t('errors.general'), 'danger')
|
|
|
|
});
|
|
|
|
},
|
2022-05-11 21:51:26 +08:00
|
|
|
addItem() {
|
2022-05-26 20:02:02 +08:00
|
|
|
this.checklistItems.push(
|
2022-05-11 21:51:26 +08:00
|
|
|
{
|
2022-05-26 20:02:02 +08:00
|
|
|
attributes: {
|
|
|
|
text: '',
|
|
|
|
checked: false,
|
2022-07-12 16:41:58 +08:00
|
|
|
position: this.checklistItems.length,
|
|
|
|
isNew: true
|
2022-05-26 20:02:02 +08:00
|
|
|
}
|
2022-05-11 21:51:26 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
2022-06-02 17:15:32 +08:00
|
|
|
removeItem(position) {
|
|
|
|
this.checklistItems.splice(position, 1);
|
2022-06-02 17:53:24 +08:00
|
|
|
this.update();
|
2022-05-26 20:02:02 +08:00
|
|
|
},
|
|
|
|
startReorder() {
|
|
|
|
this.reordering = true;
|
|
|
|
},
|
|
|
|
endReorder() {
|
|
|
|
this.reordering = false;
|
|
|
|
this.saveItemOrder();
|
|
|
|
},
|
|
|
|
saveItemOrder() {
|
|
|
|
let checklistItemPositions =
|
|
|
|
{
|
|
|
|
checklist_item_positions: this.orderedChecklistItems.map(
|
|
|
|
(i) => [i.attributes.id, i.attributes.position]
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
type: "POST",
|
|
|
|
url: this.element.attributes.orderable.urls.reorder_url,
|
|
|
|
data: JSON.stringify(checklistItemPositions),
|
|
|
|
contentType: "application/json",
|
|
|
|
dataType: "json",
|
2022-08-09 20:15:49 +08:00
|
|
|
error: (() => HelperModule.flashAlertMsg(this.i18n.t('errors.general'), 'danger')),
|
|
|
|
success: (() => this.update())
|
2022-05-26 20:02:02 +08:00
|
|
|
});
|
2022-05-11 21:51:26 +08:00
|
|
|
},
|
|
|
|
handleMultilinePaste(data) {
|
|
|
|
this.linesToPaste = data.length;
|
2022-07-12 16:41:58 +08:00
|
|
|
let nextPosition = this.checklistItems.length - 1;
|
2022-05-11 21:51:26 +08:00
|
|
|
|
|
|
|
// we need to post items to API in the right order, to avoid positions breaking
|
|
|
|
let synchronousPost = (index) => {
|
|
|
|
if(index === data.length) return;
|
|
|
|
|
|
|
|
let item = {
|
|
|
|
attributes: {
|
|
|
|
text: data[index],
|
|
|
|
checked: false,
|
|
|
|
position: nextPosition + index
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.linesToPaste -= 1;
|
|
|
|
this.postItem(item, () => synchronousPost(index + 1));
|
|
|
|
};
|
|
|
|
|
|
|
|
synchronousPost(0);
|
2022-05-05 18:56:31 +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>
|