-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make watching focus active state dependent on user focus
- Loading branch information
Showing
2 changed files
with
78 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* Handles the dimming of chat messages from users not watching the same embedded stream | ||
*/ | ||
class ChatWatchingFocus { | ||
constructor(ui) { | ||
this.ui = ui; | ||
|
||
// constant vars | ||
this.watchingFocusClassName = 'watching-focus'; | ||
this.userFocusClassName = 'focus'; | ||
this.observer = new MutationObserver(this.mutationCallback); | ||
|
||
// mutable class vars | ||
this.watchingFocusActive = null; | ||
this.lastClassState = null; | ||
|
||
this.init(); | ||
} | ||
|
||
// Initialize variables & methods | ||
init() { | ||
this.watchingFocusActive = false; | ||
this.lastClassState = this.ui[0].classList.contains( | ||
this.userFocusClassName, | ||
); | ||
|
||
// Start mutation observer | ||
this.observe(this.ui[0]); | ||
|
||
// Watching focus button action | ||
this.ui.on('click touch', '#chat-watching-focus-btn', () => | ||
this.toggleWatchingFocus(), | ||
); | ||
} | ||
|
||
observe(element) { | ||
this.observer.observe(element, { attributes: true }); | ||
} | ||
|
||
mutationCallback = (mutationsList) => { | ||
for (const mutation of mutationsList) { | ||
if ( | ||
mutation.type === 'attributes' && | ||
mutation.attributeName === 'class' | ||
) { | ||
const currentClassState = mutation.target.classList.contains( | ||
this.userFocusClassName, | ||
); | ||
|
||
// If focus status changed and watching focus is active, do stuff | ||
if (this.lastClassState !== currentClassState) { | ||
this.lastClassState = currentClassState; | ||
|
||
// If 'focus' added to class list, disable 'watching-focus' | ||
// else if 'focus' is removed, toggle 'watching-focus' according to previous boolean value | ||
if (currentClassState) { | ||
this.ui.removeClass(this.watchingFocusClassName); | ||
} else { | ||
this.ui.toggleClass( | ||
this.watchingFocusClassName, | ||
this.watchingFocusActive, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
toggleWatchingFocus() { | ||
this.watchingFocusActive = !this.watchingFocusActive; | ||
this.ui.toggleClass(this.watchingFocusClassName, this.watchingFocusActive); | ||
this.ui.find('#chat-watching-focus-btn').toggleClass('active'); | ||
} | ||
} | ||
|
||
export default ChatWatchingFocus; |