2018-08-16 22:30:59 +00:00
|
|
|
/*
|
2021-02-04 22:04:35 +00:00
|
|
|
* Copyright (c) 2010-2021 Tim Düsterhus.
|
2018-08-16 22:30:59 +00:00
|
|
|
*
|
|
|
|
* Use of this software is governed by the Business Source License
|
|
|
|
* included in the LICENSE file.
|
|
|
|
*
|
2022-03-10 17:56:18 +00:00
|
|
|
* Change Date: 2026-03-10
|
2018-08-16 22:30:59 +00:00
|
|
|
*
|
|
|
|
* On the date above, in accordance with the Business Source
|
|
|
|
* License, use of this software will be governed by version 2
|
|
|
|
* or later of the General Public License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
define([
|
|
|
|
'./console',
|
|
|
|
'Bastelstu.be/_Push',
|
2021-02-05 21:15:07 +00:00
|
|
|
'WoltLabSuite/Core/Dom/Change/Listener',
|
2018-08-16 22:30:59 +00:00
|
|
|
'WoltLabSuite/Core/Dom/Util',
|
|
|
|
'WoltLabSuite/Core/Timer/Repeating',
|
|
|
|
'Bastelstu.be/PromiseWrap/Ajax',
|
2021-02-05 21:15:07 +00:00
|
|
|
], function (console, Push, DomChangeListener, DomUtil, RepeatingTimer, Ajax) {
|
2018-08-16 22:30:59 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
let timer = undefined
|
|
|
|
const mapping = new Map()
|
|
|
|
|
|
|
|
class BoxRoomList {
|
|
|
|
constructor(container) {
|
|
|
|
this.container = container
|
|
|
|
|
|
|
|
mapping.set(container, this)
|
|
|
|
|
|
|
|
if (timer == null) {
|
|
|
|
timer = new RepeatingTimer(
|
|
|
|
BoxRoomList.updateBoxes.bind(BoxRoomList),
|
|
|
|
60e3
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
Push.onConnect(timer.setDelta.bind(timer, 300e3)).catch((error) => {
|
|
|
|
console.debug(error)
|
|
|
|
})
|
|
|
|
Push.onDisconnect(timer.setDelta.bind(timer, 60e3)).catch((error) => {
|
|
|
|
console.debug(error)
|
|
|
|
})
|
|
|
|
Push.onMessage(
|
|
|
|
'be.bastelstu.chat.join',
|
|
|
|
BoxRoomList.updateBoxes.bind(BoxRoomList)
|
|
|
|
).catch((error) => {
|
|
|
|
console.debug(error)
|
|
|
|
})
|
|
|
|
Push.onMessage(
|
|
|
|
'be.bastelstu.chat.leave',
|
|
|
|
BoxRoomList.updateBoxes.bind(BoxRoomList)
|
|
|
|
).catch((error) => {
|
|
|
|
console.debug(error)
|
2020-11-01 16:41:19 +00:00
|
|
|
})
|
2018-08-16 22:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static updateBoxes() {
|
|
|
|
mapping.forEach((object) => {
|
|
|
|
object.update()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async update() {
|
|
|
|
const payload = {
|
|
|
|
className: 'chat\\data\\room\\RoomAction',
|
|
|
|
actionName: 'getBoxRoomList',
|
|
|
|
parameters: {},
|
|
|
|
}
|
|
|
|
|
|
|
|
payload.parameters.activeRoomID = this.container.dataset.activeRoomId
|
|
|
|
payload.parameters.boxID = this.container.dataset.boxId
|
|
|
|
payload.parameters.isSidebar = this.container.dataset.isSidebar
|
|
|
|
payload.parameters.skipEmptyRooms = this.container.dataset.skipEmptyRooms
|
|
|
|
|
|
|
|
this.replace(await Ajax.api(this, payload))
|
|
|
|
}
|
|
|
|
|
|
|
|
replace(data) {
|
|
|
|
if (data.returnValues.template == null)
|
|
|
|
throw new Error('template could not be found in returnValues')
|
|
|
|
|
|
|
|
const fragment = DomUtil.createFragmentFromHtml(
|
|
|
|
data.returnValues.template
|
|
|
|
)
|
|
|
|
const oldRoomList = this.container.querySelector('.chatBoxRoomList')
|
|
|
|
const newRoomList = fragment.querySelector('.chatBoxRoomList')
|
|
|
|
|
|
|
|
if (oldRoomList == null) {
|
|
|
|
throw new Error('.chatBoxRoomList could not be found in container')
|
|
|
|
}
|
|
|
|
if (newRoomList == null) {
|
|
|
|
throw new Error(
|
|
|
|
'.chatBoxRoomList could not be found in returned template'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldRoomList.dataset.hash !== newRoomList.dataset.hash) {
|
|
|
|
this.container.replaceChild(newRoomList, oldRoomList)
|
2021-02-05 21:15:07 +00:00
|
|
|
DomChangeListener.trigger()
|
2018-08-16 22:30:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_ajaxSetup() {
|
|
|
|
return { silent: true, ignoreError: true }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return BoxRoomList
|
|
|
|
})
|