2024-07-29 23:24:19 +00:00
|
|
|
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { api } from '@converse/headless'
|
|
|
|
import tplMucNotes from '../templates/muc-notes'
|
|
|
|
import { __ } from 'i18n'
|
2024-07-30 10:54:05 +00:00
|
|
|
import { DraggablesCustomElement } from '../../../shared/components/draggables/index.js'
|
2024-07-29 23:24:19 +00:00
|
|
|
|
|
|
|
import '../styles/muc-notes.scss'
|
|
|
|
|
2024-07-30 10:54:05 +00:00
|
|
|
export default class MUCNotesView extends DraggablesCustomElement {
|
2024-07-29 23:24:19 +00:00
|
|
|
static get properties () {
|
|
|
|
return {
|
|
|
|
model: { type: Object, attribute: true },
|
|
|
|
create_note_error_message: { type: String, attribute: false },
|
2024-07-30 16:36:53 +00:00
|
|
|
create_note_opened: { type: Boolean, attribute: false },
|
|
|
|
create_note_for_occupant: { type: Object, attribute: false }
|
2024-07-29 23:24:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async initialize () {
|
|
|
|
this.create_note_error_message = ''
|
|
|
|
|
|
|
|
if (!this.model) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-30 10:54:05 +00:00
|
|
|
this.draggableTagName = 'livechat-converse-muc-note'
|
|
|
|
this.droppableTagNames = ['livechat-converse-muc-note']
|
|
|
|
this.droppableAlwaysBottomTagNames = []
|
|
|
|
|
2024-07-29 23:24:19 +00:00
|
|
|
// Adding or removing a new note: we must update.
|
|
|
|
this.listenTo(this.model, 'add', () => this.requestUpdate())
|
|
|
|
this.listenTo(this.model, 'remove', () => this.requestUpdate())
|
|
|
|
this.listenTo(this.model, 'sort', () => this.requestUpdate())
|
|
|
|
|
2024-07-30 10:54:05 +00:00
|
|
|
await super.initialize()
|
2024-07-29 23:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
return tplMucNotes(this, this.model)
|
|
|
|
}
|
|
|
|
|
2024-07-30 16:36:53 +00:00
|
|
|
async openCreateNoteForm (ev, occupant) {
|
2024-07-29 23:24:19 +00:00
|
|
|
ev?.preventDefault?.()
|
|
|
|
this.create_note_opened = true
|
2024-07-30 16:36:53 +00:00
|
|
|
this.create_note_for_occupant = occupant ?? undefined
|
2024-07-29 23:24:19 +00:00
|
|
|
await this.updateComplete
|
|
|
|
const textarea = this.querySelector('.notes-create-note textarea[name="description"]')
|
|
|
|
if (textarea) {
|
|
|
|
textarea.focus()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closeCreateNoteForm (ev) {
|
|
|
|
ev?.preventDefault?.()
|
|
|
|
this.create_note_opened = false
|
2024-07-30 16:36:53 +00:00
|
|
|
this.create_note_for_occupant = undefined
|
2024-07-29 23:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async submitCreateNote (ev) {
|
|
|
|
ev.preventDefault()
|
|
|
|
|
|
|
|
const description = ev.target.description.value
|
|
|
|
if (this.create_note_error_message) {
|
|
|
|
this.create_note_error_message = ''
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((description ?? '') === '') { return }
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.querySelectorAll('input[type=submit]').forEach(el => {
|
|
|
|
el.setAttribute('disabled', true)
|
|
|
|
el.classList.add('disabled')
|
|
|
|
})
|
|
|
|
|
|
|
|
await this.model.createNote({
|
2024-07-30 17:47:20 +00:00
|
|
|
description: description,
|
|
|
|
about_jid: ev.target.about_jid?.value || undefined,
|
|
|
|
about_nick: ev.target.about_nick?.value || undefined,
|
|
|
|
about_occupant_id: ev.target.about_occupant_id?.value || undefined
|
2024-07-29 23:24:19 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
this.closeCreateNoteForm()
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
this.create_note_error_message = __(LOC_moderator_notes_create_error)
|
|
|
|
} finally {
|
|
|
|
this.querySelectorAll('input[type=submit]').forEach(el => {
|
|
|
|
el.removeAttribute('disabled')
|
|
|
|
el.classList.remove('disabled')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-07-30 10:54:05 +00:00
|
|
|
|
|
|
|
_dropDone (draggedEl, droppedOnEl, onTopHalf) {
|
|
|
|
super._dropDone(...arguments)
|
|
|
|
console.log('[livechat note drag&drop] Note dropped...')
|
|
|
|
|
|
|
|
const note = draggedEl.model
|
|
|
|
if (!note) {
|
|
|
|
throw new Error('No model for the draggedEl')
|
|
|
|
}
|
|
|
|
const targetNote = droppedOnEl.model
|
|
|
|
if (!targetNote) {
|
|
|
|
throw new Error('No model for the droppedOnEl')
|
|
|
|
}
|
|
|
|
if (note === targetNote) {
|
|
|
|
console.log('[livechat note drag&drop] Note dropped on itself, nothing to do')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let newOrder = targetNote.get('order') ?? 0
|
2024-07-30 14:04:12 +00:00
|
|
|
if (onTopHalf) { newOrder = Math.max(0, newOrder + 1) } // reverse order!
|
2024-07-30 10:54:05 +00:00
|
|
|
|
2024-07-30 14:04:12 +00:00
|
|
|
// Warning: the order of the collection is reversed!
|
|
|
|
// _saveOrders needs it in ascending order!
|
|
|
|
this._saveOrders(Array.from(this.model).reverse(), note, newOrder)
|
2024-07-30 10:54:05 +00:00
|
|
|
}
|
2024-07-29 23:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
api.elements.define('livechat-converse-muc-notes', MUCNotesView)
|