1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-12-22 21:40:08 +00:00
Tims-Chat/files_wcf/js/Bastelstu.be/Chat/User.js

100 lines
2.1 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.
*
2022-09-17 14:23:02 +00:00
* Change Date: 2026-09-17
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/User',
'WoltLabSuite/Core/StringUtil',
'./Helper',
], function (CoreUser, StringUtil, Helper) {
'use strict'
2018-08-16 22:30:59 +00:00
const u = Symbol('user')
/**
* Represents a user.
*/
class User {
constructor(user) {
this[u] = Helper.deepFreeze(user)
2020-11-01 16:41:19 +00:00
Object.getOwnPropertyNames(this[u]).forEach((key) => {
2018-08-16 22:30:59 +00:00
if (this[key]) {
throw new Error('Attempting to override existing property')
}
2020-11-01 16:41:19 +00:00
Object.defineProperty(this, key, {
value: this[u][key],
enumerable: true,
})
2018-08-16 22:30:59 +00:00
})
}
get coloredUsername() {
// No color
if (this.color1 === null && this.color2 === null) return this.username
// Single color
2020-11-01 16:41:19 +00:00
if (this.color1 === this.color2)
return `<span style="color: ${Helper.intToRGBHex(
this.color1
)};">${StringUtil.escapeHTML(this.username)}</span>`
2018-08-16 22:30:59 +00:00
// Gradient
2020-11-01 16:41:19 +00:00
const r1 = (this.color1 >> 16) & 0xff
const r2 = (this.color2 >> 16) & 0xff
const g1 = (this.color1 >> 8) & 0xff
const g2 = (this.color2 >> 8) & 0xff
const b1 = this.color1 & 0xff
const b2 = this.color2 & 0xff
2018-08-16 22:30:59 +00:00
const steps = this.username.length - 1
const r = (r1 - r2) / steps
const g = (g1 - g2) / steps
const b = (b1 - b2) / steps
2020-11-01 16:41:19 +00:00
return this[u].username
.split('')
.map((letter, index) => {
const R = Math.round(r1 - index * r)
const G = Math.round(g1 - index * g)
const B = Math.round(b1 - index * b)
return `<span style="color: rgb(${R}, ${G}, ${B})">${StringUtil.escapeHTML(
letter
)}</span>`
})
.join('')
2018-08-16 22:30:59 +00:00
}
get self() {
return this.userID === CoreUser.userId
}
static getGuest(username) {
2020-11-01 16:41:19 +00:00
const payload = { username, userID: null, color1: null, color2: null }
2018-08-16 22:30:59 +00:00
return new User(payload)
}
wrap() {
return { user: this[u] }
}
toJSON() {
return this[u]
}
}
return User
2020-11-01 16:41:19 +00:00
})