2024-05-23 09:42:14 +00:00
|
|
|
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2024-07-11 15:53:50 +00:00
|
|
|
import { api } from '@converse/headless'
|
2024-05-11 15:37:20 +00:00
|
|
|
import tplMucTaskLists from '../templates/muc-task-lists'
|
2024-05-02 15:53:08 +00:00
|
|
|
import { __ } from 'i18n'
|
2024-07-30 10:54:05 +00:00
|
|
|
import { DraggablesCustomElement } from '../../../shared/components/draggables/index.js'
|
2024-04-30 07:21:18 +00:00
|
|
|
|
2024-05-11 15:37:20 +00:00
|
|
|
import '../styles/muc-task-lists.scss'
|
2024-05-07 11:12:57 +00:00
|
|
|
|
2024-07-30 10:54:05 +00:00
|
|
|
export default class MUCTaskListsView extends DraggablesCustomElement {
|
2024-04-30 07:21:18 +00:00
|
|
|
static get properties () {
|
|
|
|
return {
|
2024-05-02 15:53:08 +00:00
|
|
|
model: { type: Object, attribute: true },
|
|
|
|
create_tasklist_error_message: { type: String, attribute: false }
|
2024-04-30 07:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async initialize () {
|
2024-05-03 13:03:24 +00:00
|
|
|
this.create_tasklist_error_message = ''
|
|
|
|
|
2024-04-30 15:11:10 +00:00
|
|
|
if (!this.model) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-30 10:54:05 +00:00
|
|
|
this.draggableTagName = 'livechat-converse-muc-task'
|
|
|
|
this.droppableTagNames = ['livechat-converse-muc-task', 'livechat-converse-muc-task-list']
|
|
|
|
this.droppableAlwaysBottomTagNames = ['livechat-converse-muc-task-list']
|
|
|
|
|
2024-04-30 16:30:44 +00:00
|
|
|
// Adding or removing a new task list: we must update.
|
2024-04-30 15:11:10 +00:00
|
|
|
this.listenTo(this.model, 'add', () => this.requestUpdate())
|
2024-04-30 16:30:44 +00:00
|
|
|
this.listenTo(this.model, 'remove', () => this.requestUpdate())
|
2024-05-07 11:12:57 +00:00
|
|
|
this.listenTo(this.model, 'sort', () => this.requestUpdate())
|
|
|
|
|
2024-07-30 10:54:05 +00:00
|
|
|
return super.initialize()
|
2024-04-30 07:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2024-05-02 15:53:08 +00:00
|
|
|
return tplMucTaskLists(this, this.model)
|
|
|
|
}
|
|
|
|
|
|
|
|
async submitCreateTaskList (ev) {
|
|
|
|
ev.preventDefault()
|
2024-05-03 13:03:24 +00:00
|
|
|
|
2024-05-02 15:53:08 +00:00
|
|
|
const name = ev.target.name.value.trim()
|
|
|
|
if (this.create_tasklist_error_message) {
|
|
|
|
this.create_tasklist_error_message = ''
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((name ?? '') === '') { return }
|
|
|
|
|
|
|
|
try {
|
2024-05-03 13:03:24 +00:00
|
|
|
this.querySelectorAll('input[type=submit]').forEach(el => {
|
|
|
|
el.setAttribute('disabled', true)
|
|
|
|
el.classList.add('disabled')
|
|
|
|
})
|
|
|
|
|
2024-05-02 15:53:08 +00:00
|
|
|
await this.model.createTaskList({
|
|
|
|
name
|
|
|
|
})
|
|
|
|
|
2024-05-03 13:03:24 +00:00
|
|
|
this.querySelector('input[name=name]').value = ''
|
2024-05-02 15:53:08 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
this.create_tasklist_error_message = __(LOC_task_list_create_error)
|
2024-05-03 13:03:24 +00:00
|
|
|
} finally {
|
|
|
|
this.querySelectorAll('input[type=submit]').forEach(el => {
|
|
|
|
el.removeAttribute('disabled')
|
|
|
|
el.classList.remove('disabled')
|
|
|
|
})
|
2024-05-02 15:53:08 +00:00
|
|
|
}
|
2024-04-30 07:21:18 +00:00
|
|
|
}
|
2024-05-07 11:12:57 +00:00
|
|
|
|
2024-07-30 10:54:05 +00:00
|
|
|
isATaskEl (target) {
|
2024-05-07 11:12:57 +00:00
|
|
|
return target.nodeName?.toLowerCase() === 'livechat-converse-muc-task'
|
|
|
|
}
|
|
|
|
|
2024-05-09 12:42:07 +00:00
|
|
|
isATaskListEl (target) {
|
|
|
|
return target.nodeName?.toLowerCase() === 'livechat-converse-muc-task-list'
|
|
|
|
}
|
|
|
|
|
2024-07-30 10:54:05 +00:00
|
|
|
_dropDone (draggedEl, droppedOnEl, onTopHalf) {
|
|
|
|
super._dropDone(...arguments)
|
2024-05-09 12:42:07 +00:00
|
|
|
console.log('[livechat task drag&drop] Task dropped...')
|
2024-05-07 11:12:57 +00:00
|
|
|
|
2024-07-30 10:54:05 +00:00
|
|
|
const task = draggedEl.model
|
2024-05-07 11:12:57 +00:00
|
|
|
|
2024-05-09 12:42:07 +00:00
|
|
|
let newOrder, targetTasklist
|
2024-07-30 10:54:05 +00:00
|
|
|
if (this.isATaskListEl(droppedOnEl)) {
|
2024-05-09 12:42:07 +00:00
|
|
|
// We dropped on a task list, we must add as first entry.
|
|
|
|
newOrder = 0
|
|
|
|
|
2024-07-30 10:54:05 +00:00
|
|
|
targetTasklist = droppedOnEl.model
|
2024-05-09 12:42:07 +00:00
|
|
|
if (task.get('list') !== targetTasklist.get('id')) {
|
|
|
|
console.log('[livechat task drag&drop] Changing task list...')
|
|
|
|
task.set('list', targetTasklist.get('id'))
|
|
|
|
} else if (task.get('order') === newOrder) {
|
|
|
|
// Just to avoid doing some modifications for nothing...
|
|
|
|
console.log('[livechat task drag&drop] Task dropped on tasklist, but already first item, nothing to do')
|
|
|
|
return
|
|
|
|
}
|
2024-07-30 10:54:05 +00:00
|
|
|
} else if (this.isATaskEl(droppedOnEl)) {
|
2024-05-09 12:42:07 +00:00
|
|
|
// We dropped on a task, we must get its order (+1 if !onTopHalf)
|
2024-07-30 10:54:05 +00:00
|
|
|
const droppedOnTask = droppedOnEl.model
|
2024-05-09 12:42:07 +00:00
|
|
|
if (task === droppedOnTask) {
|
|
|
|
// But of course, if dropped on itself there is nothing to do.
|
|
|
|
console.log('[livechat task drag&drop] Task dropped on itself, nothing to do')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (task.get('list') !== droppedOnTask.get('list')) {
|
|
|
|
console.log('[livechat task drag&drop] Changing task list...')
|
|
|
|
task.set('list', droppedOnTask.get('list'))
|
|
|
|
}
|
|
|
|
|
|
|
|
newOrder = droppedOnTask.get('order') ?? 0
|
2024-07-30 10:54:05 +00:00
|
|
|
if (!onTopHalf) { newOrder = Math.max(0, newOrder + 1) }
|
2024-05-09 12:42:07 +00:00
|
|
|
|
|
|
|
if (typeof newOrder !== 'number' || isNaN(newOrder)) {
|
|
|
|
console.error(
|
|
|
|
'Dropped on a task that has not valid order. ' +
|
|
|
|
'Setting order to 0, that will refresh all tasks order, but the user will not have the expected result.'
|
|
|
|
)
|
|
|
|
newOrder = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
targetTasklist = this.model.get(droppedOnTask.get('list'))
|
|
|
|
} else {
|
|
|
|
console.error('[livechat task drag&drop] Dropped on something that is not valid, aborting')
|
2024-05-07 11:12:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-30 10:54:05 +00:00
|
|
|
this._saveOrders(targetTasklist.getTasks(), task, newOrder)
|
2024-05-07 11:12:57 +00:00
|
|
|
}
|
2024-04-30 07:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
api.elements.define('livechat-converse-muc-task-lists', MUCTaskListsView)
|