peertube-plugin-livechat/conversejs/custom/plugins/tasks/task-list.js

53 lines
1.3 KiB
JavaScript

import { Model } from '@converse/skeletor/src/model.js'
/**
* A chat room task list.
* @class
* @namespace _converse.ChatRoomTaskList
* @memberof _converse
*/
class ChatRoomTaskList extends Model {
idAttribute = 'id'
getTasks () {
const taskListId = this.get('id')
return this.collection?.chatroom?.tasks?.filter({
list: taskListId
}) ?? []
}
async saveItem () {
console.log('Saving task list ' + this.get('id') + '...')
await this.collection.chatroom.taskManager.saveItem(this, { name })
console.log('Task list ' + this.get('id') + ' created.')
}
async deleteItem () {
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) {
data.order = 1 + Math.max(...this.getTasks().map(t => t.get('order') ?? 0))
}
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.')
}
}
export {
ChatRoomTaskList
}