1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-12-26 22:20:08 +00:00
Tims-Chat/files_wcf/js/Bastelstu.be/Chat/Ui/UserActionDropdownHandler.js

110 lines
2.7 KiB
JavaScript
Raw Normal View History

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.
*
2023-02-22 16:45:50 +00:00
* Change Date: 2027-02-22
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.
*/
2020-11-01 16:41:19 +00:00
define([
'WoltLabSuite/Core/Dom/Traverse',
'WoltLabSuite/Core/Dom/Util',
'WoltLabSuite/Core/Ui/Dropdown/Simple',
], function (DomTraverse, DomUtil, SimpleDropdown) {
'use strict'
2018-08-16 22:30:59 +00:00
2020-11-01 16:41:19 +00:00
const DEPENDENCIES = [
'ProfileStore',
'Template.UserListDropdownMenuItems',
'bottle',
]
2018-08-16 22:30:59 +00:00
class UserActionDropdownHandler {
constructor(profiles, dropdownTemplate, bottle) {
2020-11-01 16:41:19 +00:00
this.profiles = profiles
this.dropdownTemplate = dropdownTemplate
this.bottle = bottle
2018-08-16 22:30:59 +00:00
this.container = elById('main')
}
bootstrap() {
this.container.addEventListener('click', this.onClick.bind(this))
}
onClick(event) {
2020-11-01 16:41:19 +00:00
const userElement = event.target.classList.contains(
'jsUserActionDropdown'
)
? event.target
: DomTraverse.parentByClass(
event.target,
'jsUserActionDropdown',
this.container
)
2018-08-16 22:30:59 +00:00
if (!userElement) return
event.preventDefault()
event.stopPropagation()
const user = this.profiles.get(parseInt(userElement.dataset.userId, 10))
if (user == null) {
throw new Error('Unreachable')
}
// Note: We would usually use firstElementChild here, but this
// is not supported in Safari and Edge
2020-11-01 16:41:19 +00:00
const dropdown = DomUtil.createFragmentFromHtml(
this.dropdownTemplate.fetch({ user })
).querySelector('*')
2018-08-16 22:30:59 +00:00
2020-11-01 16:41:19 +00:00
Array.from(elBySelAll('[data-module]', dropdown)).forEach((element) => {
2018-08-16 22:30:59 +00:00
const moduleName = element.dataset.module
let userAction
2020-11-01 16:41:19 +00:00
if (
!this.bottle.container.UserAction ||
2021-09-17 13:14:39 +00:00
(userAction =
this.bottle.container.UserAction[
`${moduleName.replace(/\./g, '-')}`
]) == null
2020-11-01 16:41:19 +00:00
) {
this.bottle.factory(
`UserAction.${moduleName.replace(/\./g, '-')}`,
(_) => {
const UserAction = require(moduleName)
const deps = this.bottle.digest(UserAction.DEPENDENCIES || [])
2018-08-16 22:30:59 +00:00
2020-11-01 16:41:19 +00:00
return new UserAction(...deps)
}
)
2018-08-16 22:30:59 +00:00
2021-09-17 13:14:39 +00:00
userAction =
this.bottle.container.UserAction[
`${moduleName.replace(/\./g, '-')}`
]
2018-08-16 22:30:59 +00:00
}
2020-11-01 16:41:19 +00:00
element.addEventListener(WCF_CLICK_EVENT, (event) =>
userAction.onClick(user, event)
)
2018-08-16 22:30:59 +00:00
})
SimpleDropdown.initFragment(userElement, dropdown)
SimpleDropdown.registerCallback(userElement.id, (container, action) => {
if (action === 'close') {
SimpleDropdown.destroy(container)
}
})
SimpleDropdown.toggleDropdown(userElement.id)
}
}
UserActionDropdownHandler.DEPENDENCIES = DEPENDENCIES
return UserActionDropdownHandler
2020-11-01 16:41:19 +00:00
})