2024-07-29 16:58:02 +00:00
|
|
|
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { CustomElement } from 'shared/components/element.js'
|
|
|
|
import { api, _converse } from '@converse/headless'
|
|
|
|
import './styles/muc-app.scss'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for MUC App custom elements (task app, notes app, ...).
|
|
|
|
* This is an abstract class, should not be called directly.
|
|
|
|
*/
|
|
|
|
export class MUCApp extends CustomElement {
|
2024-08-01 15:14:07 +00:00
|
|
|
restoreSettingName = undefined // must be overloaded
|
|
|
|
sessionStorageRestoreKey = undefined // must be overloaded
|
2024-07-29 16:58:02 +00:00
|
|
|
|
|
|
|
static get properties () {
|
|
|
|
return {
|
|
|
|
model: { type: Object, attribute: true }, // mucModel
|
|
|
|
show: { type: Boolean, attribute: false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async initialize () {
|
|
|
|
this.classList.add('livechat-converse-muc-app')
|
2024-08-01 15:14:07 +00:00
|
|
|
this.show = this.restoreSettingName &&
|
|
|
|
api.settings.get(this.restoreSettingName) &&
|
|
|
|
this.sessionStorageRestoreKey &&
|
|
|
|
(window.sessionStorage?.getItem?.(this.sessionStorageRestoreKey) === '1')
|
2024-07-29 16:58:02 +00:00
|
|
|
|
|
|
|
// we listen for livechatSizeChanged event,
|
|
|
|
// and close all apps except the first if small or medium width.
|
|
|
|
// Note: this will also be triggered when we first open the page
|
|
|
|
this.listenTo(_converse, 'livechatSizeChanged', () => {
|
|
|
|
if (!this.show || !api.livechat_size?.width_is(['small', 'medium'])) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// are we the first opened app?
|
|
|
|
for (const el of document.querySelectorAll('.livechat-converse-muc-app')) {
|
|
|
|
if (el === this) { break }
|
|
|
|
if (!el.show) { continue }
|
|
|
|
console.debug('The livechat size is small or medium, there is already an opened app, so closing myself', this)
|
|
|
|
// ok, there is already an opened app.
|
|
|
|
this.toggleApp() // we know we are open
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
render () { // must be overloaded.
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
2024-07-29 21:26:29 +00:00
|
|
|
updated () {
|
|
|
|
if (this.innerText.trim() === '') {
|
|
|
|
this.classList.add('hidden') // we must do this, otherwise will have CSS side effects
|
|
|
|
} else {
|
|
|
|
this.classList.remove('hidden')
|
|
|
|
}
|
|
|
|
|
|
|
|
super.updated()
|
|
|
|
}
|
|
|
|
|
2024-07-29 16:58:02 +00:00
|
|
|
toggleApp () {
|
|
|
|
this.show = !this.show
|
2024-08-01 15:14:07 +00:00
|
|
|
if (this.sessionStorageRestoreKey) {
|
|
|
|
window.sessionStorage?.setItem?.(this.sessionStorageRestoreKey, this.show ? '1' : '')
|
2024-07-29 16:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
this.show &&
|
|
|
|
api.livechat_size?.width_is(['small', 'medium'])
|
|
|
|
) {
|
|
|
|
// When showing an App, if the screen width is small or medium, we hide the others.
|
|
|
|
this._closeOtherApps()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-30 16:36:53 +00:00
|
|
|
showApp () {
|
|
|
|
if (!this.show) { return this.toggleApp() }
|
|
|
|
}
|
|
|
|
|
|
|
|
hideApp () {
|
|
|
|
if (this.show) { return this.toggleApp() }
|
|
|
|
}
|
|
|
|
|
2024-07-29 16:58:02 +00:00
|
|
|
_closeOtherApps () {
|
|
|
|
document.querySelectorAll('.livechat-converse-muc-app').forEach((el) => {
|
|
|
|
if (el !== this && el.show) {
|
|
|
|
console.debug('Closing another app, because livechat width is small or medium', el)
|
|
|
|
el.toggleApp()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|