-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add top banner #2381
Open
Myzel394
wants to merge
10
commits into
zadam:master
Choose a base branch
from
Myzel394:add-internet-lost-message
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add top banner #2381
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1d30421
created banner widget
Myzel394 92ae3b6
Merge branch 'master' into add-internet-lost-message
Myzel394 e20b5c8
fixed styling
Myzel394 e29d50f
created event handler
Myzel394 204c717
added better timer
Myzel394 7f6b717
Merge branch 'master' into add-internet-lost-message
Myzel394 6b4adec
improved customization
Myzel394 a3cd7e5
reverted some changes
Myzel394 94b10b4
fixes
Myzel394 4ad73e8
created new banner
Myzel394 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,29 @@ | ||
// https://stackoverflow.com/a/3969760 | ||
export default class Timer { | ||
timerId; | ||
start; | ||
remaining; | ||
callback; | ||
|
||
constructor(callback, delay) { | ||
this.remaining = delay; | ||
this.callback = callback; | ||
|
||
this.resume() | ||
} | ||
|
||
pause() { | ||
clearTimeout(this.timerId); | ||
this.remaining -= Date.now() - this.start; | ||
} | ||
|
||
resume() { | ||
this.start = Date.now(); | ||
clearTimeout(this.timerId); | ||
this.timerId = setTimeout(this.callback, this.remaining); | ||
} | ||
|
||
clear() { | ||
clearTimeout(this.timerId); | ||
} | ||
} |
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,158 @@ | ||
import BasicWidget from "./basic_widget.js"; | ||
import Timer from "../services/timer.js"; | ||
|
||
const TLP = ` | ||
<div id="banner-message" class="empty"> | ||
<style> | ||
@keyframes bannerMessageTimer { | ||
0% { | ||
transform: scaleX(1); | ||
} | ||
100% { | ||
transform: scaleX(0); | ||
} | ||
} | ||
|
||
@keyframes bannerMessageTimerIndefinite { | ||
0%, 100% { | ||
opacity: 0.4; | ||
} | ||
40% { | ||
opacity: 0; | ||
} | ||
} | ||
|
||
#banner-message { | ||
text-align: center; | ||
font-weight: 900; | ||
font-size: 1rem; | ||
width: 100%; | ||
color: var(--banner-color); | ||
background: var(--banner-background-color); | ||
|
||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
z-index: 6; | ||
} | ||
|
||
#banner-message > p { | ||
margin: 0; | ||
padding: 5px 20px; | ||
} | ||
|
||
#banner-message.empty > p { | ||
padding: 0; | ||
} | ||
|
||
#banner-message > .timer { | ||
position: absolute; | ||
left: 0; | ||
bottom: 0; | ||
width: 100%; | ||
height: 1px; | ||
background: #fff; | ||
opacity: .4; | ||
animation: bannerMessageTimer linear 5s forwards; | ||
transform-origin: left; | ||
will-change: transform; | ||
} | ||
|
||
#banner-message > .timer.indefinite { | ||
animation: bannerMessageTimerIndefinite linear 1s infinite; | ||
} | ||
</style> | ||
<p></p> | ||
<div class="timer"></div> | ||
</div> | ||
`; | ||
|
||
const AVAILABLE_TYPES = new Set([ | ||
"error", "info", "warning", "success", "plain" | ||
]); | ||
|
||
export default class BannerMessageWidget extends BasicWidget { | ||
durationTimer; | ||
|
||
constructor() { | ||
super(); | ||
} | ||
|
||
doRender() { | ||
this.$widget = $(TLP); | ||
this.$bannerParagraph = this.$widget.find("p"); | ||
this.$timer = this.$widget.find(".timer"); | ||
|
||
this.$widget.on("mouseenter", this.pauseTimer.bind(this)); | ||
this.$widget.on("mouseleave", this.resumeTimer.bind(this)); | ||
} | ||
|
||
hideBanner() { | ||
this.$bannerParagraph.text(""); | ||
this.$widget.removeClass(); | ||
this.$widget.addClass("empty"); | ||
|
||
// In case `hideBanner` is called before the actual end, clear timer to avoid hard bugs | ||
this.durationTimer?.clear(); | ||
this.durationTimer = undefined; | ||
} | ||
|
||
pauseTimer() { | ||
if (this.durationTimer) { | ||
this.$timer.css({ | ||
animationPlayState: "paused", | ||
}); | ||
this.durationTimer?.pause(); | ||
} | ||
} | ||
|
||
resumeTimer() { | ||
if (this.durationTimer) { | ||
this.$timer.css({ | ||
animationPlayState: "", | ||
}); | ||
this.durationTimer?.resume(); | ||
} | ||
} | ||
|
||
/** | ||
* Shows a top banner. | ||
* @param text - string: The text that should be displayed | ||
* @param type - string: Type of the banner ("error", "info", "warning", "success", "plain") | ||
* @param duration - number?: How long to show the banner. If `none` or `undefined`, | ||
* the banner will not automatically be hidden. | ||
*/ | ||
setBannerEvent({ | ||
text, | ||
type = "alert", | ||
duration, | ||
}) { | ||
if (!text) { | ||
this.hideBanner(); | ||
return; | ||
} | ||
|
||
const className = AVAILABLE_TYPES.has(type) ? type : "plain"; | ||
|
||
this.$bannerParagraph.text(text); | ||
this.$widget.removeClass(); | ||
this.$widget.addClass(className); | ||
this.$timer.removeClass("indefinite"); | ||
|
||
// Remove old timer to avoid hard bug | ||
this.durationTimer?.clear(); | ||
|
||
if (duration) { | ||
this.durationTimer = new Timer(this.hideBanner.bind(this), duration); | ||
this.$timer.css({ | ||
animationDuration: `${duration}ms` | ||
}) | ||
} else { | ||
this.$timer.addClass("indefinite"); | ||
} | ||
} | ||
|
||
hideBannerEvent() { | ||
this.hideBanner(); | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -68,4 +68,7 @@ html { | |
--link-color: blue; | ||
|
||
--mermaid-theme: default; | ||
|
||
--banner-background-color: #DA321B; | ||
--banner-color: var(--main-text-color); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it's needed to have this themable since it's for exceptional cases only and user shouldn't see it often. |
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you clarify what's going on here?
In my mind it's really simple - (non-pausable) timer, when it detects that the network is down it will show the banner, once it's up again, banner disappears. But here I see info, warning, success, plain, what is all that for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case we want to show other notifications, they should be pauseable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest I don't understand what "pausable notification" is, but in general YAGNI principle applies here.