1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-11-16 16:30:09 +00:00
Tims-Chat/files_wcf/js/Bastelstu.be/Chat/BoxRoomList.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

113 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-08-16 22:30:59 +00:00
/*
2024-01-13 20:03:36 +00:00
* Copyright (c) 2010-2024 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.
*
2024-06-15 19:56:58 +00:00
* Change Date: 2028-06-15
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',
'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',
], 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)
DomChangeListener.trigger()
2018-08-16 22:30:59 +00:00
}
}
_ajaxSetup() {
return { silent: true, ignoreError: true }
}
}
return BoxRoomList
})