2022-04-29 18:29:42 +08:00
|
|
|
<template>
|
2023-07-24 19:28:44 +08:00
|
|
|
<div class="content__checklist-container" >
|
2023-09-14 18:12:15 +08:00
|
|
|
<div class="border-0 border-b border-dashed border-sn-light-grey my-6" v-if="!inRepository"></div>
|
|
|
|
<div class="checklist-header flex rounded mb-1 items-center relative w-full group/checklist-header" :class="{ 'editing-name': editingName, 'locked': !element.attributes.orderable.urls.update_url }">
|
2023-10-04 19:15:41 +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"
|
2023-09-01 23:18:13 +08:00
|
|
|
:placeholder="i18n.t('protocols.steps.checklist.placeholder')"
|
2022-05-05 18:56:31 +08:00
|
|
|
: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-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="editingName = true"
|
|
|
|
@duplicate="duplicateElement"
|
|
|
|
@move="showMoveModal"
|
|
|
|
@delete="showDeleteModal"
|
|
|
|
></MenuDropdown>
|
2022-05-05 18:56:31 +08:00
|
|
|
</div>
|
2023-10-04 19:15:41 +08:00
|
|
|
<div v-if="element.attributes.orderable.urls.create_item_url || orderedChecklistItems.length > 0" :class="{ 'pointer-events-none': locked }">
|
2022-05-26 20:02:02 +08:00
|
|
|
<Draggable
|
|
|
|
v-model="checklistItems"
|
2023-09-13 20:13:34 +08:00
|
|
|
:ghostClass="'checklist-item-ghost'"
|
2023-07-24 19:28:44 +08:00
|
|
|
: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"
|
2023-09-22 01:24:58 +08:00
|
|
|
:key="checklistItem.id"
|
2022-05-26 20:02:02 +08:00
|
|
|
:checklistItem="checklistItem"
|
|
|
|
:locked="locked"
|
2023-10-06 20:32:39 +08:00
|
|
|
:reordering="reordering"
|
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"
|
|
|
|
/>
|
|
|
|
</Draggable>
|
2023-09-22 01:24:58 +08:00
|
|
|
<div v-if="element.attributes.orderable.urls.create_item_url && !addingNewItem"
|
|
|
|
class="flex items-center gap-1 text-sn-blue cursor-pointer mb-2 mt-1 "
|
2022-07-06 20:31:55 +08:00
|
|
|
tabindex="0"
|
2023-09-22 01:24:58 +08:00
|
|
|
@keyup.enter="addItem(orderedChecklistItems.length + 1)"
|
|
|
|
@click="addItem(orderedChecklistItems.length + 1)">
|
2023-09-14 18:12:15 +08:00
|
|
|
<i class="sn-icon sn-icon-new-task w-6 text-center inline-block"></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'
|
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 {
|
2022-05-03 21:15:49 +08:00
|
|
|
name: 'Checklist',
|
2023-09-13 18:12:36 +08:00
|
|
|
components: { deleteElementModal, InlineEdit, ChecklistItem, Draggable, 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-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: [],
|
2022-05-11 21:51:26 +08:00
|
|
|
editingName: false,
|
2022-05-26 20:02:02 +08:00
|
|
|
reordering: false,
|
2023-09-22 01:24:58 +08:00
|
|
|
editingItem: false,
|
2022-05-11 21:51:26 +08:00
|
|
|
}
|
|
|
|
},
|
2022-05-26 20:02:02 +08:00
|
|
|
created() {
|
2022-07-12 16:41:58 +08:00
|
|
|
if (this.isNew) {
|
2023-09-25 20:15:08 +08:00
|
|
|
this.addItem(1);
|
|
|
|
} else {
|
|
|
|
this.loadChecklistItems();
|
2022-07-12 16:41:58 +08:00
|
|
|
}
|
2022-05-26 20:02:02 +08:00
|
|
|
},
|
2022-08-10 16:37:27 +08:00
|
|
|
watch: {
|
|
|
|
element() {
|
2023-09-22 01:24:58 +08:00
|
|
|
this.loadChecklistItems();
|
2022-08-10 16:37:27 +08:00
|
|
|
}
|
|
|
|
},
|
2022-05-11 21:51:26 +08:00
|
|
|
computed: {
|
2022-05-26 20:02:02 +08:00
|
|
|
orderedChecklistItems() {
|
2023-09-22 01:24:58 +08:00
|
|
|
return this.checklistItems.sort((a, b) => a.attributes.position - b.attributes.position || b.id - a.id)
|
|
|
|
.map((item, index) => {
|
|
|
|
item.attributes.position = index + 1;
|
|
|
|
return item;
|
|
|
|
});
|
2022-05-26 20:02:02 +08:00
|
|
|
},
|
|
|
|
locked() {
|
2023-10-06 20:32:39 +08:00
|
|
|
return this.editingName || !this.element.attributes.orderable.urls.update_url
|
2023-09-13 18:12:36 +08:00
|
|
|
},
|
2023-09-22 01:24:58 +08:00
|
|
|
addingNewItem() {
|
|
|
|
return this.checklistItems.find((item) => item.attributes.isNew);
|
|
|
|
},
|
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-05-05 18:56:31 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2023-09-25 21:03:35 +08:00
|
|
|
update: function() {
|
|
|
|
this.$emit('update', this.element, false)
|
|
|
|
},
|
2023-09-22 01:24:58 +08:00
|
|
|
loadChecklistItems(insertAfter) {
|
|
|
|
$.get(this.element.attributes.orderable.urls.checklist_items_url, (result) => {
|
|
|
|
this.checklistItems = result.data;
|
2023-10-06 20:32:39 +08:00
|
|
|
if (insertAfter) {
|
2023-09-22 01:24:58 +08:00
|
|
|
this.addItem(insertAfter);
|
|
|
|
}
|
2022-08-10 16:37:27 +08:00
|
|
|
});
|
|
|
|
},
|
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;
|
2023-09-25 21:03:35 +08:00
|
|
|
this.update();
|
2022-05-05 18:56:31 +08:00
|
|
|
},
|
2023-09-22 01:24:58 +08:00
|
|
|
postItem(item) {
|
|
|
|
item.attributes.position = item.attributes.position - 1;
|
2023-08-09 16:33:58 +08:00
|
|
|
$.post(this.element.attributes.orderable.urls.create_item_url, item).done((result) => {
|
2023-09-22 01:24:58 +08:00
|
|
|
this.loadChecklistItems(result.data[result.data.length - 1].attributes.position)
|
2023-09-01 23:18:13 +08:00
|
|
|
}).fail((e) => {
|
|
|
|
HelperModule.flashAlertMsg(this.i18n.t('errors.general'), 'danger');
|
2022-05-11 21:51:26 +08:00
|
|
|
});
|
2022-06-02 17:53:24 +08:00
|
|
|
|
2023-09-22 01:24:58 +08:00
|
|
|
// Fake element during loading
|
|
|
|
item.id = 'new' + Math.floor(Math.random() * 1000000000);
|
|
|
|
this.checklistItems.push(item);
|
|
|
|
|
2022-05-11 21:51:26 +08:00
|
|
|
},
|
2023-09-22 01:24:58 +08:00
|
|
|
saveItem(item, key) {
|
|
|
|
if (item.id > 0) {
|
|
|
|
let insertAfter = key === 'Enter' ? item.attributes.position : null;
|
2022-05-11 21:51:26 +08:00
|
|
|
$.ajax({
|
|
|
|
url: item.attributes.urls.update_url,
|
|
|
|
type: 'PATCH',
|
|
|
|
data: item,
|
2023-09-22 01:24:58 +08:00
|
|
|
success: () => {
|
|
|
|
this.loadChecklistItems(insertAfter)
|
2022-07-08 19:49:22 +08:00
|
|
|
},
|
2023-08-02 21:37:01 +08:00
|
|
|
error: (xhr) => setFlashErrors(xhr.responseJSON.errors)
|
2022-05-11 21:51:26 +08:00
|
|
|
});
|
|
|
|
} else {
|
2023-09-22 01:24:58 +08:00
|
|
|
this.postItem(item, key);
|
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) => {
|
2023-09-22 01:24:58 +08:00
|
|
|
this.checklistItems.find(
|
|
|
|
(i) => i.id === item.id
|
|
|
|
).attributes.checked = result.data.attributes.checked;
|
2022-07-14 21:09:28 +08:00
|
|
|
},
|
|
|
|
error: () => HelperModule.flashAlertMsg(this.i18n.t('errors.general'), 'danger')
|
|
|
|
});
|
|
|
|
},
|
2023-09-22 01:24:58 +08:00
|
|
|
addItem(insertAfter) {
|
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,
|
2023-09-22 01:24:58 +08:00
|
|
|
position: insertAfter,
|
2022-07-12 16:41:58 +08:00
|
|
|
isNew: true
|
2022-05-26 20:02:02 +08:00
|
|
|
}
|
2022-05-11 21:51:26 +08:00
|
|
|
}
|
|
|
|
);
|
2023-09-22 01:24:58 +08:00
|
|
|
this.checklistItems = this.orderedChecklistItems;
|
2022-05-11 21:51:26 +08:00
|
|
|
},
|
2022-06-02 17:15:32 +08:00
|
|
|
removeItem(position) {
|
2023-09-22 01:24:58 +08:00
|
|
|
this.checklistItems = this.orderedChecklistItems.filter((item) => item.attributes.position !== position);
|
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)
|
2023-09-22 01:24:58 +08:00
|
|
|
&& Number.isInteger(event.oldIndex)
|
2023-08-02 21:37:01 +08:00
|
|
|
&& event.newIndex !== event.oldIndex
|
|
|
|
){
|
2023-09-22 01:24:58 +08:00
|
|
|
let position = this.orderedChecklistItems[event.newIndex]?.attributes.position;
|
|
|
|
let id = this.checklistItems[event.oldIndex]?.id;
|
|
|
|
this.checklistItems[event.oldIndex].attributes.position = position + (event.newIndex > event.oldIndex ? 1 : -1);
|
2023-08-02 21:37:01 +08:00
|
|
|
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),
|
2023-09-22 01:24:58 +08:00
|
|
|
success: () => this.loadChecklistItems()
|
2022-05-26 20:02:02 +08:00
|
|
|
});
|
2022-05-11 21:51:26 +08:00
|
|
|
},
|
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>
|