forked from ILIAS-eLearning/ILIAS
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Badge/User: Improve toggling hidden badges in public profile
- Loading branch information
1 parent
b92a16b
commit b82015d
Showing
5 changed files
with
114 additions
and
82 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 was deleted.
Oops, something went wrong.
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,95 @@ | ||
class BadgeToggle { | ||
static HIDDEN_CLASS = 'ilNoDisplay'; | ||
|
||
static BADGE_CLASS = 'ilHiddenProfileBadge'; | ||
|
||
constructor(containerId, showButtonId, hideButtonId) { | ||
this.badgeContainer = document.getElementById(containerId); | ||
this.showButton = document.getElementById(showButtonId); | ||
this.hideButton = document.getElementById(hideButtonId); | ||
|
||
this.init(); | ||
} | ||
|
||
init() { | ||
this.showButton.addEventListener('click', (e) => this.handleClick(e, true)); | ||
this.hideButton.addEventListener('click', (e) => this.handleClick(e, false)); | ||
} | ||
|
||
handleClick(event, show) { | ||
event.preventDefault(); | ||
this.toggleButtonVisibility(show) | ||
.then(() => this.toggleBadgeVisibility(show)); | ||
} | ||
|
||
toggleBadgeVisibility(show) { | ||
return new Promise((resolve) => { | ||
const badges = this.badgeContainer.querySelectorAll(`.${BadgeToggle.BADGE_CLASS}`); | ||
const promises = Array.from(badges).map((badge) => this.animateBadge(badge, show)); | ||
Promise.all(promises).then(resolve); | ||
}); | ||
} | ||
|
||
animateBadge(badge, show) { | ||
return new Promise((resolve) => { | ||
const height = badge.scrollHeight; | ||
|
||
const stylesBeforeAnimation = show ? { | ||
display: 'inline-block', | ||
height: '0px', | ||
opacity: '0', | ||
overflow: 'hidden', | ||
} : { | ||
overflow: 'hidden', | ||
}; | ||
|
||
const stylesAfterAnimation = show ? { | ||
height: '', | ||
opacity: '', | ||
overflow: '', | ||
} : { | ||
display: 'none', | ||
height: '', | ||
opacity: '', | ||
overflow: '', | ||
}; | ||
|
||
const classAction = show ? 'remove' : 'add'; | ||
|
||
Object.assign(badge.style, stylesBeforeAnimation); | ||
|
||
this.animateHeightOpacity( | ||
badge, | ||
show ? 0 : height, | ||
show ? height : 0, | ||
show ? 0 : 1, | ||
show ? 1 : 0, | ||
).then(() => { | ||
Object.assign(badge.style, stylesAfterAnimation); | ||
badge.classList[classAction](BadgeToggle.HIDDEN_CLASS); | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
animateHeightOpacity(element, fromHeight, toHeight, fromOpacity, toOpacity) { | ||
return new Promise((resolve) => { | ||
const animation = element.animate( | ||
[ | ||
{ height: `${fromHeight}px`, opacity: fromOpacity }, | ||
{ height: `${toHeight}px`, opacity: toOpacity }, | ||
], | ||
{ duration: 200, easing: 'cubic-bezier(0.42, 0, 0.58, 1)' }, | ||
); | ||
animation.onfinish = resolve; | ||
}); | ||
} | ||
|
||
toggleButtonVisibility(show) { | ||
return new Promise((resolve) => { | ||
this.showButton.classList.toggle(BadgeToggle.HIDDEN_CLASS); | ||
this.hideButton.classList.toggle(BadgeToggle.HIDDEN_CLASS); | ||
resolve(); | ||
}); | ||
} | ||
} |
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