2022-04-29 18:29:42 +08:00
|
|
|
<template>
|
2023-07-24 19:28:44 +08:00
|
|
|
<div class="content__checklist-container" >
|
|
|
|
<div class="checklist-header flex rounded pl-10 mb-1 items-center relative w-full group/checklist-header" :class="{ 'editing-name': editingName, 'locked': !element.attributes.orderable.urls.update_url }">
|
|
|
|
<div v-if="reorderElementUrl"
|
|
|
|
class="absolute items-center h-full justify-center left-0 p-2 tw-hidden text-sn-grey"
|
|
|
|
:class="{ 'group-hover/checklist-header:flex': !locked }"
|
|
|
|
@click="$emit('reorder')">
|
2023-06-15 21:12:51 +08:00
|
|
|
<i class="sn-icon sn-icon-sort"></i>
|
2022-05-05 18:56:31 +08:00
|
|
|
</div>
|
2023-07-24 19:28:44 +08:00
|
|
|
<div class="grow-1 text-ellipsis whitespace-nowrap grow my-1 font-bold">
|
2022-05-05 18:56:31 +08:00
|
|
|
<InlineEdit
|
|
|
|
: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>
|
2023-07-24 19:28:44 +08:00
|
|
|
<div class="tw-hidden group-hover/checklist-header:flex items-center gap-2 ml-auto">
|
2023-07-13 19:55:32 +08:00
|
|
|
<button v-if="element.attributes.orderable.urls.update_url" class="btn icon-btn btn-light btn-sm" @click="editingName = true" tabindex="0">
|
2023-06-15 21:12:51 +08:00
|
|
|
<i class="sn-icon sn-icon-edit"></i>
|
2022-05-05 18:56:31 +08:00
|
|
|
</button>
|
2023-07-13 19:55:32 +08:00
|
|
|
<button v-if="element.attributes.orderable.urls.duplicate_url" class="btn icon-btn btn-light btn-sm" tabindex="0" @click="duplicateElement">
|
2023-06-08 14:33:37 +08:00
|
|
|
<i class="sn-icon sn-icon-duplicate"></i>
|
2022-08-22 20:02:19 +08:00
|
|
|
</button>
|
2023-08-21 15:22:56 +08:00
|
|
|
<button v-if="element.attributes.orderable.urls.move_targets_url" class="btn btn-light btn-sm" tabindex="0" @click="showMoveModal">
|
|
|
|
Move
|
|
|
|
</button>
|
2023-07-13 19:55:32 +08:00
|
|
|
<button v-if="element.attributes.orderable.urls.delete_url" class="btn icon-btn btn-light btn-sm" @click="showDeleteModal" tabindex="0">
|
2023-06-08 14:33:37 +08:00
|
|
|
<i class="sn-icon sn-icon-delete"></i>
|
2022-05-05 18:56:31 +08:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-07-24 19:28:44 +08:00
|
|
|
<div v-if="element.attributes.orderable.urls.create_item_url || orderedChecklistItems.length > 0">
|
2022-05-26 20:02:02 +08:00
|
|
|
<Draggable
|
|
|
|
v-model="checklistItems"
|
2023-07-24 19:28:44 +08:00
|
|
|
:ghostClass="'checklist-item-ghost border-blue border-solid'"
|
|
|
|
:dragClass="'checklist-item-drag'"
|
|
|
|
:chosenClass="'checklist-item-chosen'"
|
2023-07-11 19:42:08 +08:00
|
|
|
:forceFallback="true"
|
2023-07-24 19:28:44 +08:00
|
|
|
:handle="'.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"
|
2023-07-24 19:28:44 +08:00
|
|
|
class="ml-7 btn btn-light btn-sm px-2"
|
2022-07-06 20:31:55 +08:00
|
|
|
tabindex="0"
|
|
|
|
@keyup.enter="addItem"
|
|
|
|
@click="addItem">
|
2023-07-24 19:28:44 +08:00
|
|
|
<i class="sn-icon sn-icon-new-task w-6 text-center"></i>
|
2022-05-11 21:51:26 +08:00
|
|
|
{{ i18n.t('protocols.steps.insert.checklist_item') }}
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-07-24 19:28:44 +08:00
|
|
|
<div v-else class="text-sn-grey ml-12">
|
2022-07-15 20:58:06 +08:00
|
|
|
{{ i18n.t("protocols.steps.checklist.empty_checklist") }}
|
|
|
|
</div>
|
2022-05-30 19:45:51 +08:00
|
|
|
<deleteElementModal v-if="confirmingDelete" @confirm="deleteElement" @cancel="closeDeleteModal"/>
|
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 ChecklistItem from './checklistItem.vue'
|
2022-05-26 20:02:02 +08:00
|
|
|
import Draggable from 'vuedraggable'
|
2023-08-21 15:22:56 +08:00
|
|
|
import moveElementModal from './modal/move.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: 'Checklist',
|
2023-08-21 15:22:56 +08:00
|
|
|
components: { deleteElementModal, InlineEdit, ChecklistItem, Draggable, moveElementModal },
|
|
|
|
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-12 16:41:58 +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-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) {
|
2023-08-09 16:33:58 +08:00
|
|
|
$.post(this.element.attributes.orderable.urls.create_item_url, item).done((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();
|
2023-08-09 22:43:15 +08:00
|
|
|
}).fail((xhr) => {
|
2023-08-02 21:37:01 +08:00
|
|
|
this.setFlashErrors(xhr.responseJSON.errors)
|
2022-05-11 21:51:26 +08:00
|
|
|
});
|
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)
|
|
|
|
},
|
2023-08-02 21:37:01 +08:00
|
|
|
error: (xhr) => setFlashErrors(xhr.responseJSON.errors)
|
2022-05-11 21:51:26 +08:00
|
|
|
});
|
|
|
|
} 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;
|
|
|
|
},
|
2023-08-02 21:37:01 +08:00
|
|
|
endReorder(event) {
|
2022-05-26 20:02:02 +08:00
|
|
|
this.reordering = false;
|
2023-08-02 21:37:01 +08:00
|
|
|
if(
|
|
|
|
Number.isInteger(event.newIndex)
|
|
|
|
&& Number.isInteger(event.newIndex)
|
|
|
|
&& event.newIndex !== event.oldIndex
|
|
|
|
){
|
|
|
|
const { id, position } = this.orderedChecklistItems[event.newIndex]?.attributes
|
|
|
|
this.saveItemOrder(id, position);
|
|
|
|
}
|
2022-05-26 20:02:02 +08:00
|
|
|
},
|
2023-08-02 21:37:01 +08:00
|
|
|
saveItemOrder(id, position) {
|
2022-05-26 20:02:02 +08:00
|
|
|
$.ajax({
|
|
|
|
type: "POST",
|
|
|
|
url: this.element.attributes.orderable.urls.reorder_url,
|
2023-08-02 21:37:01 +08:00
|
|
|
data: JSON.stringify({ attributes: { id, position } }),
|
2022-05-26 20:02:02 +08:00
|
|
|
contentType: "application/json",
|
|
|
|
dataType: "json",
|
2023-08-02 21:37:01 +08:00
|
|
|
error: (xhr) => this.setFlashErrors(xhr.responseJSON.errors),
|
|
|
|
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);
|
2023-08-02 21:37:01 +08:00
|
|
|
},
|
|
|
|
setFlashErrors(errors) {
|
|
|
|
for(const key in errors){
|
|
|
|
HelperModule.flashAlertMsg(
|
|
|
|
this.i18n.t(`activerecord.errors.models.checklist_item.attributes.${key}`),
|
|
|
|
'danger'
|
|
|
|
)
|
|
|
|
}
|
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>
|