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-04-29 14:46:21 +00:00
|
|
|
import { Model } from '@converse/skeletor/src/model.js'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A chat room task list.
|
|
|
|
* @class
|
2024-07-15 15:46:45 +00:00
|
|
|
* @namespace _converse.exports.ChatRoomTaskList
|
2024-04-29 14:46:21 +00:00
|
|
|
* @memberof _converse
|
|
|
|
*/
|
|
|
|
class ChatRoomTaskList extends Model {
|
2024-04-30 15:11:10 +00:00
|
|
|
idAttribute = 'id'
|
2024-04-30 16:30:44 +00:00
|
|
|
|
|
|
|
getTasks () {
|
|
|
|
const taskListId = this.get('id')
|
|
|
|
return this.collection?.chatroom?.tasks?.filter({
|
|
|
|
list: taskListId
|
|
|
|
}) ?? []
|
|
|
|
}
|
2024-05-05 22:13:20 +00:00
|
|
|
|
2024-05-06 10:29:25 +00:00
|
|
|
async saveItem () {
|
|
|
|
console.log('Saving task list ' + this.get('id') + '...')
|
2024-05-06 15:26:20 +00:00
|
|
|
await this.collection.chatroom.taskManager.saveItem(this)
|
|
|
|
console.log('Task list ' + this.get('id') + ' saved.')
|
2024-05-06 10:29:25 +00:00
|
|
|
}
|
|
|
|
|
2024-05-05 22:13:20 +00:00
|
|
|
async deleteItem () {
|
2024-05-06 13:57:42 +00:00
|
|
|
const tasks = this.getTasks()
|
|
|
|
return this.collection.chatroom.taskManager.deleteItems([...tasks, this])
|
|
|
|
}
|
|
|
|
|
|
|
|
async createTask (data) {
|
|
|
|
// Cloning data to avoid side effects:
|
|
|
|
data = Object.assign({}, data)
|
|
|
|
|
|
|
|
const name = data?.name
|
|
|
|
if (!name) { throw new Error('Missing name') }
|
|
|
|
|
|
|
|
data.list = this.get('id')
|
|
|
|
if (!data.order) {
|
2024-07-30 14:04:12 +00:00
|
|
|
data.order = 1 + Math.max(
|
2024-05-06 15:26:20 +00:00
|
|
|
0,
|
|
|
|
...(this.getTasks().map(t => t.get('order') ?? 0).filter(o => !isNaN(o)))
|
|
|
|
)
|
2024-05-06 13:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log('Creating task ' + name + '...')
|
|
|
|
const chatroom = this.collection.chatroom
|
|
|
|
const tasksCollection = chatroom.tasks
|
|
|
|
await chatroom.taskManager.createItem(tasksCollection, data)
|
|
|
|
console.log('Task list ' + name + ' created.')
|
2024-05-05 22:13:20 +00:00
|
|
|
}
|
2024-04-29 14:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
ChatRoomTaskList
|
|
|
|
}
|