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

83 lines
2.0 KiB
JavaScript
Raw Normal View History

import { Collection } from '@converse/skeletor/src/collection.js'
import { ChatRoomTaskList } from './task-list'
2024-04-30 15:11:10 +00:00
import { initStorage } from '@converse/headless/utils/storage.js'
import { getUniqueId } from '@converse/headless/utils/core.js'
/**
* A list of {@link _converse.ChatRoomTaskList} instances, representing task lists associated to a MUC.
* @class
* @namespace _converse.ChatRoomTaskLists
* @memberOf _converse
*/
class ChatRoomTaskLists extends Collection {
model = ChatRoomTaskList
2024-04-30 15:11:10 +00:00
initialize (models, options) {
2024-04-30 16:30:44 +00:00
this.model = ChatRoomTaskList // don't know why, must do it again here
2024-04-30 15:11:10 +00:00
super.initialize(arguments)
this.chatroom = options.chatroom
const id = `converse-livechat-tasks-lists-${this.chatroom.get('jid')}`
initStorage(this, id, 'session')
this.on('change:name', () => this.sort())
this.fetchTasksLists().catch(console.error)
}
comparator (tasklist1, tasklist2) {
// Case insensitive on task list name.
const name1 = tasklist1.get('name').toLowerCase()
const name2 = tasklist2.get('name').toLowerCase()
return name1 < name2 ? -1 : name1 > name2 ? 1 : 0
}
create (attrs, options) {
2024-04-30 16:30:44 +00:00
if (attrs instanceof ChatRoomTaskList) {
return super.create(attrs, options)
}
2024-04-30 15:11:10 +00:00
attrs.id ??= getUniqueId()
return super.create(attrs, options)
}
/**
* Requires Task lists from the server.
*/
async fetchTasksLists () {
// TODO: remove these test lines, and subscribe to pubsub.
const taskListsData = [
{
id: 'task-list-1',
name: 'Task List 1'
},
{
2024-04-30 16:30:44 +00:00
id: 'task-list-2',
2024-04-30 15:11:10 +00:00
name: 'Task List 2'
}
]
for (const item of taskListsData) {
let id = item.id
const tasklist = id ? this.get(id) : undefined
if (tasklist) {
tasklist.save({
name: item.name
})
return
}
id ??= getUniqueId()
this.create({
id,
name: item.name
})
}
}
}
export {
ChatRoomTaskLists
}