This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
messages.ts
220 lines (185 loc) · 6.94 KB
/
messages.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { CSSResultGroup, LitElement, PropertyDeclaration, PropertyValueMap, html } from 'lit';
import { customElement } from 'lit/decorators.js';
import { classMap } from 'lit/directives/class-map.js';
import { Participant } from '../../../common/types/participant.types';
import { StoreType } from '../../../common/types/stores.types';
import { Following } from '../../../services/stores/who-is-online/types';
import { WebComponentsBase } from '../../base';
import importStyle from '../../base/utils/importStyle';
import { messagesStyle } from '../css';
import { HorizontalSide, VerticalSide } from './types';
const WebComponentsBaseElement = WebComponentsBase(LitElement);
const styles: CSSResultGroup[] = [WebComponentsBaseElement.styles, messagesStyle];
@customElement('superviz-who-is-online-messages')
export class WhoIsOnlineMessages extends WebComponentsBaseElement {
static styles = styles;
declare everyoneFollowsMe: boolean;
declare isPrivate: boolean;
declare verticalSide: VerticalSide;
declare horizontalSide: HorizontalSide;
private following: Following | undefined;
private participantColor: string;
private animationFrame: number | undefined;
static properties = {
everyoneFollowsMe: { type: Boolean },
isPrivate: { type: Boolean },
verticalSide: { type: String },
horizontalSide: { type: String },
};
constructor() {
super();
const { localParticipant } = this.useStore(StoreType.GLOBAL);
localParticipant.subscribe((participant: Participant) => {
this.participantColor = participant.color;
});
const { following } = this.useStore(StoreType.WHO_IS_ONLINE);
following.subscribe();
}
protected firstUpdated(
_changedProperties: PropertyValueMap<any> | Map<PropertyKey, unknown>,
): void {
super.firstUpdated(_changedProperties);
importStyle.call(this, 'who-is-online');
}
protected updated(_changedProperties: PropertyValueMap<any> | Map<PropertyKey, unknown>): void {
super.updated(_changedProperties);
const _ = ['following', 'everyoneFollowsMe', 'isPrivate'];
if (_.some((property) => _changedProperties.has(property))) {
this.repositionMessages();
}
}
// Logic to reposition the messages if there is not enough freespace
/**
* @function repositionMessages
* @description Reposition the messages container based on which side there's more free space
* @returns {void}
*/
private repositionMessages = () => {
const { following, everyoneFollowsMe, isPrivate } = this;
if (following || everyoneFollowsMe || isPrivate) {
this.repositionInVerticalDirection();
this.repositionInHorizontalDirection();
this.animationFrame = window.requestAnimationFrame(this.repositionMessages);
return;
}
window.cancelAnimationFrame(this.animationFrame);
};
/**
* @function repositionInVerticalDirection
* @description Reposition the messages container based on the vertical side with more free space
* @returns {void}
*/
private repositionInVerticalDirection() {
const verticalMidpoint = window.innerHeight / 2;
const { top } = this.parentElement.getBoundingClientRect();
if (top < verticalMidpoint) {
this.verticalSide = VerticalSide.BOTTOM;
return;
}
this.verticalSide = VerticalSide.TOP;
}
/**
* @function repositionInHorizontalDirection
* @description Reposition the messages container based on the horizontal side with more free space
* @returns {void
* }
*/
private repositionInHorizontalDirection() {
const horizontalMidpoint = window.innerWidth / 2;
const { left } = this.parentElement.getBoundingClientRect();
if (left < horizontalMidpoint) {
this.horizontalSide = HorizontalSide.LEFT;
return;
}
this.horizontalSide = HorizontalSide.RIGHT;
}
// Callbacks
/**
* @function stopFollowing
* @description Emits an event to stop following a participant
* @returns {void}
*/
private stopFollowing() {
this.emitEvent('stop-following', {});
}
/**
* @function cancelPrivate
* @description Emits an event to cancel private mode
* @returns {void}
*/
private cancelPrivate() {
this.emitEvent('cancel-private', {});
}
/**
* @function stopEveryoneFollowsMe
* @description Emits an event to cancel the Everyone Follows Me mode (does not prevent participants from following the user individually)
* @returns {void}
*/
private stopEveryoneFollowsMe() {
this.emitEvent('stop-everyone-follows-me', {});
}
// Content to be rendered
// Regarding the classes of all "*Message()" methods:
// The classes are used to style the messages, and they should be replicated for consistency should
// new messages/interactions be added
// The exception is 1 class unique to each message, so the user can target this message in particular
private followingMessage() {
if (!this.following) return '';
const { name, color } = this.following;
return html`<div
class="who-is-online__following-message who-is-online__presence-control-message who-is-online__pcm"
style="border-color: ${color}"
>
<p class="who-is-online__presence-control-message__text who-is-online__pcm__text">
Following: ${name}
<span
class="who-is-online__presence-control-message__cancel-action-button who-is-online__pcm__cancel-action-button"
@click=${this.stopFollowing}
>Stop</span
>
</p>
</div>`;
}
private everyoneFollowsMeMessage() {
if (!this.everyoneFollowsMe) return '';
return html`<div
class="who-is-online__follow-me-message who-is-online__presence-control-message who-is-online__pcm"
style="border-color: ${this.participantColor}"
>
<p class="who-is-online__presence-control-message__text who-is-online__pcm__text">
Everyone is following you
<span
class="who-is-online__presence-control-message__cancel-action-button who-is-online__pcm__cancel-action-button"
@click=${this.stopEveryoneFollowsMe}
>Stop</span
>
</p>
</div>`;
}
private privateMessage() {
if (!this.isPrivate) return '';
return html`<div
class="who-is-online__private-mode-message who-is-online__presence-control-message who-is-online__pcm"
style="border-color: ${this.participantColor}"
>
<p class="who-is-online__presence-control-message__text who-is-online__pcm__text">
You are in Private Mode
<span
class="who-is-online__presence-control-message__cancel-action-button who-is-online__pcm__cancel-action-button"
@click=${this.cancelPrivate}
>Cancel</span
>
</p>
</div>`;
}
protected render() {
const classList = {
'who-is-online__controls-messages': true,
[this.verticalSide]: true,
[this.horizontalSide]: true,
};
return html` <div class=${classMap(classList)}>
${this.followingMessage()} ${this.everyoneFollowsMeMessage()} ${this.privateMessage()}
</div>`;
}
}